From a370d2f5860a0f89ac44af857d1059885ad1d60f Mon Sep 17 00:00:00 2001 From: oroborus Date: Thu, 19 Jun 2008 19:28:04 +0000 Subject: - upravy v kode - prepis stirel pre space_t * - v kode su dva vyhladavacie algortmy, implicitne sa pouziva ten ktory funguje :) ten co nefunguje je zamakrovany makrom SUPPORT_ERROR a este ho musim doladit git-svn-id: http://opensvn.csie.org/tuxanci_ng@66 0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e --- src/Makefile | 2 +- src/arena.c | 24 +++--- src/gun.c | 3 +- src/hashTable.c | 221 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/item.c | 6 +- src/proto.c | 18 ++--- src/shot.c | 161 +++++++++++++++++++++-------------------- src/space.c | 75 +++++++++++++++---- src/tux.c | 8 +- 9 files changed, 390 insertions(+), 128 deletions(-) create mode 100644 src/hashTable.c (limited to 'src') diff --git a/src/Makefile b/src/Makefile index 1c7a1ab..d730826 100755 --- a/src/Makefile +++ b/src/Makefile @@ -9,7 +9,7 @@ CFLAGS = -g -O0 -std=c99 -I../include -Wall `sdl-config --cflags` BUILD_DIR = . LIBS = `sdl-config --libs` -lSDL_image -lSDL_ttf -lSDL_mixer -lSDL_net -FILES = interface.o space.o hashTable.o idManager.o storage.o font.o list.o modules.o\ +FILES = interface.o space.o idManager.o storage.o font.o list.o modules.o\ image.o layer.o director.o configFile.o tux.o main.o checkFront.o\ screen.o screen_world.o shot.o myTimer.o panel.o net_multiplayer.o \ buffer.o arena.o arenaFile.o textFile.o audio.o sound.o music.o gun.o \ diff --git a/src/arena.c b/src/arena.c index 1a8e803..75e94b4 100644 --- a/src/arena.c +++ b/src/arena.c @@ -44,12 +44,9 @@ arena_t* newArena(int w, int h) new->listTimer = newList(); - //new->listTux = newList(); - //new->listItem = newList(); new->spaceTux = newSpace(w, h, 320, 240, getStatusTux, setStatusTux); new->spaceItem = newSpace(w, h, 320, 240, getStatusItem, setStatusItem); - - new->listShot = newList(); + new->spaceShot = newSpace(w, h, 320, 240, getStatusShot, setStatusShot); return new; } @@ -62,12 +59,11 @@ int conflictSpace(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2) int isFreeSpace(arena_t *arena, int x, int y, int w, int h) { if( isConflictWithObjectFromSpace(arena->spaceTux, x, y, w, h) )return 0; - if( isConflictWithListShot(arena->listShot, x, y, w, h) )return 0; + if( isConflictWithObjectFromSpace(arena->spaceShot, x, y, w, h) )return 0; if( isConflictWithObjectFromSpace(arena->spaceItem, x, y, w, h) )return 0; + if( isConflictWithObjectFromSpace(arena->spaceShot, x, y, w, h) )return 0; + if( isConflictModule(x, y, w, h) )return 0; - //if( isConflictWithListWall(arena->listWall, x, y, w, h) )return 0; - //if( isConflictWithListTeleport(arena->listTeleport, x, y, w, h) )return 0; - //if( isConflictWithListPipe(arena->listPipe, x, y, w, h) )return 0; return 1; } @@ -162,13 +158,17 @@ void drawArena(arena_t *arena) getObjectFromSpace(arena->spaceItem, screen_x, screen_y, WINDOW_SIZE_X, WINDOW_SIZE_Y, listHelp); drawListItem(listHelp); - //printSpace(arena->spaceItem); + listDoEmpty(listHelp); + getObjectFromSpace(arena->spaceShot, screen_x, screen_y, WINDOW_SIZE_X, WINDOW_SIZE_Y, listHelp); + drawListShot(listHelp); + + //printSpace(arena->spaceShot); //drawListTux(arena->listTux); //drawListWall(arena->listWall); //drawListTeleport(arena->listTeleport); //drawListPipe(arena->listPipe); - drawListShot(arena->listShot); + //drawListShot(arena->listShot); //drawListItem(arena->listItem); drawModule(screen_x, screen_y, WINDOW_SIZE_X, WINDOW_SIZE_Y); @@ -200,7 +200,7 @@ void eventArena(arena_t *arena) for( i = 0 ; i < 8 ; i++) { - eventMoveListShot(arena->listShot); + eventMoveListShot(arena); checkShotIsInTuxScreen(arena); //eventConflictShotWithWall(arena->listWall, arena->listShot); //eventConflictShotWithTeleport(arena->listTeleport, arena->listShot); @@ -221,7 +221,7 @@ void destroyArena(arena_t *p) { destroySpaceWithObject(p->spaceTux, destroyTux); destroySpaceWithObject(p->spaceItem, destroyItem); - destroyListItem(p->listShot, destroyShot); + destroySpaceWithObject(p->spaceShot, destroyShot); destroyTimer(p->listTimer); free(p); } diff --git a/src/gun.c b/src/gun.c index bddeda1..dceec63 100644 --- a/src/gun.c +++ b/src/gun.c @@ -89,7 +89,8 @@ static void addShotTrivial(tux_t *tux, int x, int y, int px, int py, int gun) return; } */ - addList( getCurrentArena()->listShot, shot ); + //addList( getCurrentArena()->listShot, shot ); + addObjectToSpace(getCurrentArena()->spaceShot, shot); if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) { diff --git a/src/hashTable.c b/src/hashTable.c new file mode 100644 index 0000000..cc67225 --- /dev/null +++ b/src/hashTable.c @@ -0,0 +1,221 @@ + +#include +#include +#include + +#include "hashTable.h" + +hashtable_t* newHashTable() +{ + hashtable_t *new; + + new = malloc( sizeof(hashtable_t) ); + memset(new, 0, sizeof(hashtable_t) ); + + return new; +} + +void addHashTable(hashtable_t *p, char *key, void *item) +{ + hashtable_t *this; + hashtable_t *next; + int len; + int i; + + this = p; + next = p; + + len = strlen(key); + + for( i = 0 ; i < len ; i++ ) + { + //printf("addHashTable key[%d] = %c\n", i , key[i]); + + next = this->table[ (int)key[i] ]; + + if( next == NULL ) + { + next = newHashTable(); + this->table[ (int)key[i] ] = next; + } + + this = next; + } + + next->data = item; +} + +void addHashTableWithIndex(hashtable_t *p, int index, void *item) +{ + char str[16]; + sprintf(str, "%d", index); + addHashTable(p, str, item); +} + +void* getHashTable(hashtable_t *p, char *key) +{ + hashtable_t *this; + hashtable_t *next; + int len; + int i; + + this = p; + next = p; + + len = strlen(key); + + for( i = 0 ; i < len ; i++ ) + { + //printf("getHashTable key[%d] = %c\n", i , key[i]); + + next = this->table[ (int)key[i] ]; + + //printf("next = %p\n", next); + + if( next == NULL ) + { + return NULL; + } + + this = next; + } + + return next->data; +} + +void* getHashTableWithIndex(hashtable_t *p, int index) +{ + char str[16]; + sprintf(str, "%d", index); + return getHashTable(p, str); +} + +void delHashTable(hashtable_t *p, char *key) +{ + hashtable_t *this; + hashtable_t *next; + int len; + int i; + + this = p; + next = p; + + len = strlen(key); + + for( i = 0 ; i < len ; i++ ) + { + //printf("getHashTable key[%d] = %c\n", i , key[i]); + + next = this->table[ (int)key[i] ]; + + //printf("next = %p\n", next); + + if( next == NULL ) + { + return; + } + + this = next; + } + + next->data = NULL; + +} + +void delHashTableWithIndex(hashtable_t *p, int index) +{ + char str[16]; + sprintf(str, "%d", index); + delHashTable(p, str); +} + +void delHashTableWithMem(hashtable_t *p, char *key, void *f) +{ + void (*fce)(void *p); + + delHashTable(p, key); + + fce = f; + fce(key); +} + +void delHashTableWithMemWithIndex(hashtable_t *p, int index) +{ + char str[16]; + sprintf(str, "%d", index); + delHashTable(p, str); +} + +void destroyHashTable(hashtable_t *p) +{ + hashtable_t *this; + hashtable_t *next; + int i; + + this = p; + + for( i = 0 ; i < 256 ; i++ ) + { + next = this->table[i]; + + if( next != NULL ) + { + destroyHashTable(next); + } + } + + free(p); +} + +void destroyHashTableWithMem(hashtable_t *p, void *f) +{ + void (*fce)(void *p); + hashtable_t *this; + hashtable_t *next; + int i; + + this = p; + fce = f; + + for( i = 0 ; i < 256 ; i++ ) + { + next = this->table[i]; + + if( next != NULL ) + { + destroyHashTableWithMem(next, f); + } + } + + if( p->data != NULL ) + { + fce(p->data); + } + + free(p); +} + +int test_main(int argc, char **argv) +{ + hashtable_t *p; + int max = 10000; + int i; + + p = newHashTable(); + + for( i = 0 ; i < max ; i++ ) + { + addHashTableWithIndex(p, i, NULL); + } + + for( i = 0 ; i < max ; i++ ) + { + printf("%d -> %s\n", i, (char *)getHashTableWithIndex(p, i) ); + } + + //printHashTable(p); + + destroyHashTableWithMem(p, free); + + return 0; +} diff --git a/src/item.c b/src/item.c index 2ec126d..467117d 100644 --- a/src/item.c +++ b/src/item.c @@ -482,12 +482,12 @@ void eventConflictShotWithItem(arena_t *arena) return; } - for( i = 0 ; i < arena->listShot->count ; i++ ) + for( i = 0 ; i < arena->spaceShot->list->count ; i++ ) { bool_t isDelShot; isDelShot = FALSE; - thisShot = (shot_t *)arena->listShot->list[i]; + thisShot = (shot_t *)arena->spaceShot->list->list[i]; assert( thisShot != NULL ); listDoEmpty(listHelp); @@ -526,7 +526,7 @@ void eventConflictShotWithItem(arena_t *arena) proto_send_delshot_server(PROTO_SEND_ALL, NULL, thisShot); } - delListItem(arena->listShot, i, destroyShot); + delObjectFromSpaceWithObject(arena->spaceShot, thisItem, destroyShot); i--; } } diff --git a/src/proto.c b/src/proto.c index e14db9f..72e8f16 100644 --- a/src/proto.c +++ b/src/proto.c @@ -753,10 +753,9 @@ void proto_recv_shot_client(char *msg) proto_send_check_client(check_id); - if( ( shot = getShotID(getCurrentArena()->listShot, shot_id) ) != NULL ) + if( ( shot = getObjectFromSpaceWithID(getCurrentArena()->spaceShot, shot_id) ) != NULL ) { - delListItem(getCurrentArena()->listShot, - searchListItem(getCurrentArena()->listShot, shot), destroyShot); + delObjectFromSpaceWithObject(getCurrentArena()->spaceShot, shot, destroyShot); //return; } @@ -772,7 +771,7 @@ void proto_recv_shot_client(char *msg) transformOnlyLasser(shot); } - addList(getCurrentArena()->listShot, shot); + addObjectToSpace(getCurrentArena()->spaceShot, shot); } #endif @@ -807,18 +806,11 @@ void proto_recv_delshot_client(char *msg) proto_send_check_client(check_id); - shot = getShotID(getCurrentArena()->listShot, id); + shot = getObjectFromSpaceWithID(getCurrentArena()->spaceShot, id); if( shot != NULL ) { - int index; - - index = searchListItem(getCurrentArena()->listShot, shot); - - if( index >= 0 ) - { - delListItem(getCurrentArena()->listShot, index, destroyShot); - } + delObjectFromSpaceWithObject(getCurrentArena()->spaceShot, shot, destroyShot); } } diff --git a/src/shot.c b/src/shot.c index 65d2147..cb07bda 100644 --- a/src/shot.c +++ b/src/shot.c @@ -130,33 +130,35 @@ shot_t* newShot(int x,int y, int px, int py, int gun, int author_id) return new; } -shot_t* getShotID(list_t *listShot, int id) +void replaceShotID(shot_t *shot, int id) { - shot_t *thisShot; - int i; - - assert( listShot != NULL ); - - for( i = 0 ; i < listShot->count ; i++ ) - { - thisShot = (shot_t *)listShot->list[i]; - assert( thisShot != NULL ); + replaceID(shot->id, id); + shot->id = id; +} - if( thisShot->id == id ) - { - return thisShot; - } - } - return NULL; +void getStatusShot(void *p, int *id, int *x,int *y, int *w, int *h) +{ + shot_t *shot; + + shot = p; + *id = shot->id; + *x = shot->x; + *y = shot->y; + *w = shot->w; + *h = shot->h; } -void replaceShotID(shot_t *shot, int id) +void setStatusShot(void *p, int x, int y, int w, int h) { - replaceID(shot->id, id); - shot->id = id; -} + shot_t *shot; + shot = p; + shot->x = x; + shot->y = y; + shot->w = w; + shot->h = h; +} #ifndef PUBLIC_SERVER @@ -183,28 +185,6 @@ void drawListShot(list_t *listShot) #endif -int isConflictWithListShot(list_t *listShot, int x, int y, int w, int h) -{ - shot_t *thisShot; - int i; - - assert( listShot != NULL ); - - for( i = 0 ; i < listShot->count ; i++ ) - { - thisShot = (shot_t *)listShot->list[i]; - assert( thisShot != NULL ); - - if( conflictSpace(x, y, w, h, - thisShot->x, thisShot->y, thisShot->w, thisShot->h) ) - { - return 1; - } - } - - return 0; -} - static int getRandomCourse(int x, int y) { int ret; @@ -267,44 +247,27 @@ void transformOnlyLasser(shot_t *shot) } } -void eventMoveListShot(list_t *listShot) +void eventMoveListShot(arena_t *arena) { - arena_t *arena; shot_t *thisShot; + int new_x, new_y; int i; - assert( listShot != NULL ); - - arena = getCurrentArena(); - - for( i = 0 ; i < listShot->count ; i++ ) + for( i = 0 ; i < arena->spaceShot->list->count ; i++ ) { - thisShot = (shot_t *)listShot->list[i]; + thisShot = (shot_t *) arena->spaceShot->list->list[i]; assert( thisShot != NULL ); - thisShot->x += thisShot->px; - thisShot->y += thisShot->py; -/* - if( thisShot->gun == GUN_BOMBBALL && ( - isConflictWithListWall(getCurrentArena()->listWall, thisShot->x, thisShot->y, thisShot->w, thisShot->h) || - isConflictWithListPipe(getCurrentArena()->listPipe, thisShot->x, thisShot->y, thisShot->w, thisShot->h) ) ) - { - if( getNetTypeGame() != NET_GAME_TYPE_CLIENT ) - { - boundBombBall(thisShot); - } - else - { - delListItem(listShot, i, destroyShot); - i--; - continue; - } - } -*/ + new_x = thisShot->x + thisShot->px; + new_y = thisShot->y + thisShot->py; + + moveObjectInSpace(getCurrentArena()->spaceShot, thisShot, new_x, new_y); + if( thisShot->x+thisShot->w < 0 || thisShot->x > arena->w || thisShot->y+thisShot->h < 0 || thisShot->y > arena->h ) { - delListItem(listShot, i, destroyShot); + delObjectFromSpaceWithObject(arena->spaceShot, + thisShot, destroyShot); i--; continue; } @@ -316,7 +279,6 @@ static int myAbs(int n) return ( n > 0 ? n : -n ); } - static int getSppedShot(shot_t *shot) { return ( myAbs(shot->px) > myAbs(shot->py) ? myAbs(shot->px) : myAbs(shot->py) ); @@ -325,8 +287,6 @@ static int getSppedShot(shot_t *shot) void checkShotIsInTuxScreen(arena_t *arena) { - return; -/* int screen_x, screen_y; shot_t *thisShot; tux_t *thisTux; @@ -338,9 +298,10 @@ void checkShotIsInTuxScreen(arena_t *arena) return; } - for( i = 0 ; i < arena->listTux->count ; i++ ) + for( i = 0 ; i < arena->spaceTux->list->count ; i++ ) { - thisTux = (tux_t *)arena->listTux->list[i]; + thisTux = (tux_t *)arena->spaceTux->list->list[i]; + speed = 25; if( thisTux->control != TUX_CONTROL_NET ) { @@ -349,9 +310,53 @@ void checkShotIsInTuxScreen(arena_t *arena) getCenterScreen(&screen_x, &screen_y, thisTux->x, thisTux->y); - for( j = 0 ; j < arena->listShot->count ; j++ ) + listDoEmpty(listHelp); + getObjectFromSpace(arena->spaceShot, screen_x-speed, screen_y, speed, WINDOW_SIZE_Y, listHelp); + + for( j = 0 ; j count ; j++ ) + { + thisShot = (shot_t *)listHelp->list[j]; + client_t *thisClient; + thisClient = getClientFromTux(thisTux); + proto_send_shot_server(PROTO_SEND_ONE, thisClient, thisShot); + } + + listDoEmpty(listHelp); + getObjectFromSpace(arena->spaceShot, screen_x+WINDOW_SIZE_X, screen_y, speed, WINDOW_SIZE_Y, listHelp); + + for( j = 0 ; j count ; j++ ) + { + thisShot = (shot_t *)listHelp->list[j]; + client_t *thisClient; + thisClient = getClientFromTux(thisTux); + proto_send_shot_server(PROTO_SEND_ONE, thisClient, thisShot); + } + + listDoEmpty(listHelp); + getObjectFromSpace(arena->spaceShot, screen_x, screen_y-speed, WINDOW_SIZE_X, speed, listHelp); + + for( j = 0 ; j count ; j++ ) + { + thisShot = (shot_t *)listHelp->list[j]; + client_t *thisClient; + thisClient = getClientFromTux(thisTux); + proto_send_shot_server(PROTO_SEND_ONE, thisClient, thisShot); + } + + listDoEmpty(listHelp); + getObjectFromSpace(arena->spaceShot, screen_x, screen_y+WINDOW_SIZE_Y, WINDOW_SIZE_X, speed, listHelp); + + for( j = 0 ; j count ; j++ ) { - thisShot = (shot_t *)arena->listShot->list[j]; + thisShot = (shot_t *)listHelp->list[j]; + client_t *thisClient; + thisClient = getClientFromTux(thisTux); + proto_send_shot_server(PROTO_SEND_ONE, thisClient, thisShot); + } +#if 0 + for( j = 0 ; j count ; j++ ) + { + thisShot = (shot_t *)listHelp->list[j]; speed = getSppedShot(thisShot); @@ -387,8 +392,8 @@ void checkShotIsInTuxScreen(arena_t *arena) proto_send_shot_server(PROTO_SEND_ONE, thisClient, thisShot); } } +#endif } -*/ } /* diff --git a/src/space.c b/src/space.c index dfe38a7..c9688a8 100644 --- a/src/space.c +++ b/src/space.c @@ -9,7 +9,7 @@ #include "space.h" #define DEBUG_SPACE -#define ERROR_SUPPORT +//#define ERROR_SUPPORT #ifdef DEBUG_SPACE @@ -122,6 +122,49 @@ static void getSegment(space_t *p, int x, int y, int w, int h, #ifdef ERROR_SUPPORT +void printListID(space_t *space) +{ + int i; + + for( i = 0 ; i < space->list->count ; i++ ) + { + int id, x, y, w, h; + + space->getStatus(space->list->list[i], &id, &x, &y, &w, &h); + printf("%d\n", id); + } + + putchar('\n'); +} + +static void checkList(space_t *space) +{ + int id, x, y, w, h; + int thisId; + int i; + + if( space->list->count == 0 ) + { + printf("nothing\n"); + return; + } + + space->getStatus(space->list->list[0], &thisId, &x, &y, &w, &h); + + for( i = 1 ; i < space->list->count ; i++ ) + { + space->getStatus(space->list->list[i], &id, &x, &y, &w, &h); + + if( id <= thisId ) + { + printListID(space); + assert( ! "error" ); + } + + thisId = id; + } +} + static void addToList(space_t *space, void *p) { int id, point_id, inc_point_id; @@ -130,9 +173,15 @@ static void addToList(space_t *space, void *p) int min, max; int point; + len = space->list->count; space->getStatus(p, &id, &x, &y, &w, &h); +/* + printf("addToList (%d)\n", id); + printListID(space); + assert( id >= 0 ); +*/ if( len == 0 ) { @@ -175,12 +224,13 @@ static void addToList(space_t *space, void *p) space->getStatus(space->list->list[min], &id_min, &x, &y, &w, &h); space->getStatus(space->list->list[max], &id_max, &x, &y, &w, &h); */ - //printf("min = %d max = %d point = %d len = %d id = %d\n", min, max, point, len, id); + printf("min = %d max = %d point = %d len = %d id = %d\n", min, max, point, len, id); if( max < 0 ) { //printf("OK first\n"); insList(space->list, 0, p); + checkList(space); return; } @@ -188,6 +238,7 @@ static void addToList(space_t *space, void *p) { addList(space->list, p); //printf("OK height\n"); + checkList(space); return; } @@ -198,6 +249,7 @@ static void addToList(space_t *space, void *p) { //printf("OK\n"); insList(space->list, min, p); + checkList(space); return; } @@ -205,6 +257,7 @@ static void addToList(space_t *space, void *p) { //printf("OK\n"); insList(space->list, point+1, p); + checkList(space); return; } @@ -224,19 +277,6 @@ static void addToList(space_t *space, void *p) #endif -void printListID(space_t *space) -{ - int i; - - for( i = 0 ; i < space->list->count ; i++ ) - { - int id, x, y, w, h; - - space->getStatus(space->list->list[i], &id, &x, &y, &w, &h); - printf("%d\n", id); - } -} - void addObjectToSpace(space_t *p, void *item) { int segX, segY, segW, segH; @@ -301,6 +341,9 @@ void* getObjectFromSpaceWithID(space_t *space, int id) int min, max; int point; + printf("getObjectFromSpaceWithID %d\n", id); + printListID(space); + len = space->list->count; min = 0; @@ -559,7 +602,7 @@ void test_space() addObjectToSpace(space, newRecv(3, 0, 0, 1, 1) ); addObjectToSpace(space, newRecv(4, 0, 0, 1, 1) ); - printListID(space); + //printListID(space); /* recv = getObjectFromSpaceWithID(space, 6); if( recv != NULL )printf("id = %d\n", recv->id); diff --git a/src/tux.c b/src/tux.c index 39094c8..c2db1f2 100644 --- a/src/tux.c +++ b/src/tux.c @@ -560,9 +560,9 @@ void eventConflictTuxWithShot(arena_t *arena) tux_t *thisTux; int i, j; - for( i = 0 ; i < arena->listShot->count ; i++ ) + for( i = 0 ; i < arena->spaceShot->list->count ; i++ ) { - thisShot = (shot_t *)arena->listShot->list[i]; + thisShot = (shot_t *)arena->spaceShot->list->list[i]; assert( thisShot != NULL ); listDoEmpty(listHelp); @@ -592,7 +592,7 @@ void eventConflictTuxWithShot(arena_t *arena) if( getNetTypeGame() != NET_GAME_TYPE_CLIENT ) { bombBallExplosion(thisShot); - delListItem(arena->listShot, i, destroyShot); + delObjectFromSpaceWithObject(arena->spaceShot, thisShot, destroyShot); i--; } @@ -605,7 +605,7 @@ void eventConflictTuxWithShot(arena_t *arena) } } - delListItem(arena->listShot, i, destroyShot); + delObjectFromSpaceWithObject(arena->spaceShot, thisShot, destroyShot); i--; continue; -- cgit v1.2.3