From fadf9365dba24b680db5cb8750c53e264ff1ec27 Mon Sep 17 00:00:00 2001 From: oroborus Date: Tue, 17 Jun 2008 19:15:10 +0000 Subject: - drobne upravy v kode git-svn-id: http://opensvn.csie.org/tuxanci_ng@65 0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e --- src/Makefile | 5 +- src/arena.c | 4 +- src/game.c | 2 + src/item.c | 4 +- src/main.c | 1 + src/proto.c | 11 ++- src/server.c | 3 +- src/space.c | 259 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----- src/tux.c | 4 +- 9 files changed, 261 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/Makefile b/src/Makefile index 57e51d8..1c7a1ab 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 idManager.o storage.o font.o list.o modules.o\ +FILES = interface.o space.o hashTable.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 \ @@ -24,6 +24,9 @@ tuxanci-ng: $(FILES) main.o: main.c $(CC) $(CFLAGS) -o $(BUILD_DIR)/main.o -c main.c +hashTable.o: hashTable.c ../include/hashTable.h + $(CC) $(CFLAGS) -o $(BUILD_DIR)/hashTable.o -c hashTable.c + space.o: space.c ../include/space.h $(CC) $(CFLAGS) -o $(BUILD_DIR)/space.o -c space.c diff --git a/src/arena.c b/src/arena.c index 4b97f46..1a8e803 100644 --- a/src/arena.c +++ b/src/arena.c @@ -219,8 +219,8 @@ void eventArena(arena_t *arena) void destroyArena(arena_t *p) { - destroySpace(p->spaceTux, destroyTux); - destroySpace(p->spaceItem, destroyItem); + destroySpaceWithObject(p->spaceTux, destroyTux); + destroySpaceWithObject(p->spaceItem, destroyItem); destroyListItem(p->listShot, destroyShot); destroyTimer(p->listTimer); free(p); diff --git a/src/game.c b/src/game.c index 4226c7f..0c4903f 100644 --- a/src/game.c +++ b/src/game.c @@ -97,6 +97,8 @@ void quitGame() quitScreenCredits(); quitScreenTable(); + destroyList(listHelp); + printf("quit..\n"); exit(0); diff --git a/src/item.c b/src/item.c index fdd0ad3..2ec126d 100644 --- a/src/item.c +++ b/src/item.c @@ -328,7 +328,7 @@ void eventListItem(space_t *spaceItem) case ITEM_BIG_EXPLOSION : if( thisItem->frame == ITEM_EXPLOSION_MAX_FRAME ) { - delObjectFromSpaceWithMem(spaceItem, thisItem, destroyItem); + delObjectFromSpaceWithObject(spaceItem, thisItem, destroyItem); i--; } break; @@ -398,7 +398,7 @@ void mineExplosion(space_t *spaceItem, item_t *item) addObjectToSpace(spaceItem, item_explosion ); } - delObjectFromSpaceWithMem(spaceItem, item, destroyItem); + delObjectFromSpaceWithObject(spaceItem, item, destroyItem); } /* diff --git a/src/main.c b/src/main.c index 4f9774c..f2482f3 100644 --- a/src/main.c +++ b/src/main.c @@ -112,6 +112,7 @@ int main(int argc, char *argv[]) test_space(); exit(0); */ + #ifndef PUBLIC_SERVER startGame(); #endif diff --git a/src/proto.c b/src/proto.c index cffde8d..e14db9f 100644 --- a/src/proto.c +++ b/src/proto.c @@ -179,6 +179,8 @@ void proto_recv_hello_server(client_t *client, char *msg) client->tux = newTux(); client->tux->control = TUX_CONTROL_NET; + client->tux->client = client; + addObjectToSpace(getCurrentArena()->spaceTux, client->tux); if( strlen(name) > STR_NAME_SIZE-1 ) @@ -451,6 +453,11 @@ void proto_recv_newtux_client(char *msg) tux = getObjectFromSpaceWithID(getCurrentArena()->spaceTux, id); + if( tux != NULL ) + { + assert( tux->id == id ); + } + if( tux == NULL ) { char term_msg[STR_SIZE]; @@ -458,11 +465,11 @@ void proto_recv_newtux_client(char *msg) appendTextInTerm(term_msg); tux = newTux(); + replaceTuxID(tux, id); tux->control = TUX_CONTROL_NET; addObjectToSpace(getCurrentArena()->spaceTux, tux); } - replaceTuxID(tux, id); moveObjectInSpace(getCurrentArena()->spaceTux, tux, x, y); tux->status = status; tux->position = position; @@ -590,7 +597,7 @@ void proto_recv_deltux_client(char *msg) appendTextInTerm(term_msg); //index = searchListItem(getCurrentArena()->listTux, tux); - delObjectFromSpaceWithMem(getCurrentArena()->spaceTux, tux, destroyTux); + delObjectFromSpaceWithObject(getCurrentArena()->spaceTux, tux, destroyTux); } } diff --git a/src/server.c b/src/server.c index 9fd4b0f..3b06a3f 100644 --- a/src/server.c +++ b/src/server.c @@ -106,7 +106,6 @@ static void eventPeriodicSyncClient(void *p_nothink) thisTux = getControlTux(TUX_CONTROL_KEYBOARD_RIGHT); if( isTuxSeesTux(thisClientSend->tux, thisTux) ) proto_send_newtux_server(PROTO_SEND_ONE, thisClientSend, thisTux); - #endif for( j = 0 ; j < listClient->count; j++) @@ -286,7 +285,7 @@ void destroyClient(client_t *p) index = searchListItem(getCurrentArena()->listTux, p->tux); delListItem(getCurrentArena()->listTux, index, destroyTux); */ - delObjectFromSpaceWithMem(getCurrentArena()->spaceTux, p->tux, destroyTux); + delObjectFromSpaceWithObject(getCurrentArena()->spaceTux, p->tux, destroyTux); } free(p); diff --git a/src/space.c b/src/space.c index 32f7daf..dfe38a7 100644 --- a/src/space.c +++ b/src/space.c @@ -8,8 +8,12 @@ #include "list.h" #include "space.h" +#define DEBUG_SPACE +#define ERROR_SUPPORT + #ifdef DEBUG_SPACE + typedef struct recv_struct { int id; @@ -17,16 +21,14 @@ typedef struct recv_struct int w, h; } recv_t; -recv_t* newRecv(int x , int y, int w, int h) +recv_t* newRecv(int id, int x , int y, int w, int h) { - static int last_id = 0; - recv_t *new; new = malloc( sizeof(recv_t) ); memset(new, 0, sizeof(recv_t)); - new->id = ++last_id; + new->id = id; new->x = x; new->y = y; new->w = w; @@ -118,6 +120,123 @@ static void getSegment(space_t *p, int x, int y, int w, int h, *segH = ( (y+h) / p->segH + 1 ) - *segY; } +#ifdef ERROR_SUPPORT + +static void addToList(space_t *space, void *p) +{ + int id, point_id, inc_point_id; + int x, y, w, h; + int len; + int min, max; + int point; + + len = space->list->count; + + space->getStatus(p, &id, &x, &y, &w, &h); + + if( len == 0 ) + { + //printf("OK first\n"); + addList(space->list, p); + return; + } + + if( len == 1 ) + { + space->getStatus(space->list->list[0], &point_id, &x, &y, &w, &h); + + if( id > point_id ) + { + addList(space->list, p); + } + + if( id < point_id ) + { + insList(space->list, 0, p); + } + + //printf("OK\n"); + + return; + } + + min = 0; + max = len-1; +/* + if( max < 0 ) + { + max = 0; + } +*/ + for(;;) + { + point = min + ( max - min ) / 2; +/* + 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); + + if( max < 0 ) + { + //printf("OK first\n"); + insList(space->list, 0, p); + return; + } + + if( point+1 >= len /*|| min >= len*/ ) + { + addList(space->list, p); + //printf("OK height\n"); + return; + } + + space->getStatus(space->list->list[point], &point_id, &x, &y, &w, &h); + space->getStatus(space->list->list[point+1], &inc_point_id, &x, &y, &w, &h); + + if( min == max ) + { + //printf("OK\n"); + insList(space->list, min, p); + return; + } + + if( point_id < id && id < inc_point_id ) + { + //printf("OK\n"); + insList(space->list, point+1, p); + return; + } + + if( id > inc_point_id ) + { + min = point + 1; + continue; + } + + if( id < point_id ) + { + max = point - 1; + continue; + } + } +} + +#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; @@ -135,7 +254,13 @@ void addObjectToSpace(space_t *p, void *item) } } +#ifdef ERROR_SUPPORT + addToList(p, item); +#endif + +#ifndef ERROR_SUPPORT addList(p->list, item); +#endif } void getObjectFromSpace(space_t *p, int x, int y, int w, int h, list_t *list) @@ -167,18 +292,69 @@ void getObjectFromSpace(space_t *p, int x, int y, int w, int h, list_t *list) } } -void* getObjectFromSpaceWithID(space_t *p, int id) +void* getObjectFromSpaceWithID(space_t *space, int id) { +#ifdef ERROR_SUPPORT + int point_id; + int x, y, w, h; + int len; + int min, max; + int point; + + len = space->list->count; + + min = 0; + max = len-1; + + for(;;) + { + point = min + ( max - min ) / 2; + + //printf("min = %d max = %d point = %d len = %d id = %d\n", min, max, point, len, id); + + if( max < 0 || point >= len || max < min ) + { + return NULL; + } + + space->getStatus(space->list->list[point], &point_id, &x, &y, &w, &h); + + if( min == max ) + { + space->getStatus(space->list->list[min], &point_id, &x, &y, &w, &h); + } + + if( point_id == id ) + { + return space->list->list[point]; + } + + if( id > point_id ) + { + min = point + 1; + continue; + } + + if( id < point_id ) + { + max = point - 1; + continue; + } + } + +#endif + +#ifndef ERROR_SUPPORT int this_id, x, y, w, h; void *this; int i; - for( i = 0 ; i < p->list->count ; i++ ) + for( i = 0 ; i < space->list->count ; i++ ) { - this = p->list->list[i]; + this = space->list->list[i]; assert( this != NULL ); - p->getStatus(this, &this_id, &x, &y, &w, &h); + space->getStatus(this, &this_id, &x, &y, &w, &h); if( this_id == id ) { @@ -187,6 +363,7 @@ void* getObjectFromSpaceWithID(space_t *p, int id) } return NULL; +#endif } int isConflictWithObjectFromSpace(space_t *p, int x, int y, int w, int h) @@ -280,7 +457,7 @@ void delObjectFromSpace(space_t *p, void *item) delList(p->list, index); } -void delObjectFromSpaceWithMem(space_t *p, void *item, void *f) +void delObjectFromSpaceWithObject(space_t *p, void *item, void *f) { void (*fce)(void *param); @@ -333,16 +510,11 @@ void printSpace(space_t *p) } } -void destroySpace(space_t *p, void *f) +void destroySpace(space_t *p) { - void (*fce)(void *param); int j, i; - fce = f; - - //getObjectFromSpace(p, 0, 0, p->w * p->segW - 1 , p->h * p->segH - 1, list); - - destroyListItem(p->list, fce); + destroyList(p->list); for( i = 0 ; i < p->h ; i++ ) { @@ -361,16 +533,56 @@ void destroySpace(space_t *p, void *f) free(p); } +void destroySpaceWithObject(space_t *p, void *f) +{ + void (*fce)(void *param); + int i; + + fce = f; + + for( i = 0 ; i < p->list->count ; i++ ) + fce(p->list->list[i]); + + destroySpace(p); +} + #ifdef DEBUG_SPACE void test_space() { space_t *space; - list_t *list; - int i, j; - + recv_t *recv; + space = newSpace(5000, 2500, 320, 240, getStatus, setStatus); + addObjectToSpace(space, newRecv(6, 0, 0, 1, 1) ); + addObjectToSpace(space, newRecv(3, 0, 0, 1, 1) ); + addObjectToSpace(space, newRecv(4, 0, 0, 1, 1) ); + + printListID(space); +/* + recv = getObjectFromSpaceWithID(space, 6); + if( recv != NULL )printf("id = %d\n", recv->id); +*/ + recv = getObjectFromSpaceWithID(space, 3); + + if( recv != NULL ) + { + printf("id = %d\n", recv->id); + } + else + { + printf("recv = %p\n", recv); + } + +/* + recv = getObjectFromSpaceWithID(space, 4); + if( recv != NULL )printf("id = %d\n", recv->id); + recv = getObjectFromSpaceWithID(space, 1); + if( recv != NULL )printf("id = %d\n", recv->id); +*/ + +#if 0 for( i = 1 ; i < 16 ; i++ ) for( j = 1 ; j < 16 ; j++ ) { @@ -381,7 +593,8 @@ void test_space() addObjectToSpace(space, this ); delObjectFromSpaceWithMem(space, this, destroyRecv); } - +#endif +/* list = newList(); getObjectFromSpace(space, 640, 480, 800 , 600, list); @@ -394,12 +607,14 @@ void test_space() printf("%3d %3d %3d %3d\n", recv->x, recv->y, recv->w, recv->h); } - +*/ +/* destroyList(list); printSpace(space); - destroySpace(space, destroyRecv); + destroySpace(space); +*/ } #endif diff --git a/src/tux.c b/src/tux.c index 8dad6c4..39094c8 100644 --- a/src/tux.c +++ b/src/tux.c @@ -84,7 +84,7 @@ tux_t* newTux() new->position = TUX_DOWN; new->gun = GUN_SIMPLE; new->shot[ new->gun ] = GUN_MAX_SHOT; - + sprintf(new->name, "no_name_id_%d", new->id); new->score = 0; new->frame = 0; @@ -95,6 +95,8 @@ tux_t* newTux() new->isCanShot = TRUE; new->isCanSwitchGun = TRUE; + new->client = NULL; + return new; } -- cgit v1.2.3