From 7af32f143eb8615163e1bc78a417434e18da0e71 Mon Sep 17 00:00:00 2001 From: oroborus Date: Fri, 20 Jun 2008 20:16:44 +0000 Subject: - oprava chyb s pamatov - vyhladavanie objetku podla jeho ID ma konecne casovu narocnost log(2,n) git-svn-id: http://opensvn.csie.org/tuxanci_ng@67 0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e --- src/arena.c | 28 +++++--- src/idManager.c | 4 +- src/item.c | 4 +- src/shot.c | 160 ------------------------------------------- src/space.c | 87 ++++++------------------ src/tux.c | 206 +------------------------------------------------------- 6 files changed, 44 insertions(+), 445 deletions(-) (limited to 'src') diff --git a/src/arena.c b/src/arena.c index 75e94b4..d9df24f 100644 --- a/src/arena.c +++ b/src/arena.c @@ -32,6 +32,8 @@ arena_t* getCurrentArena() arena_t* newArena(int w, int h) { arena_t *new; + int zone_w, zone_h; + new = malloc( sizeof(arena_t) ); #ifndef PUBLIC_SERVER @@ -43,10 +45,21 @@ arena_t* newArena(int w, int h) new->h = h; new->listTimer = newList(); - - new->spaceTux = newSpace(w, h, 320, 240, getStatusTux, setStatusTux); - new->spaceItem = newSpace(w, h, 320, 240, getStatusItem, setStatusItem); - new->spaceShot = newSpace(w, h, 320, 240, getStatusShot, setStatusShot); + + if( w > 800 || h > 600) + { + zone_w = 320; + zone_h = 240; + } + else + { + zone_w = 80; + zone_h = 60; + } + + new->spaceTux = newSpace(w, h, zone_w, zone_h, getStatusTux, setStatusTux); + new->spaceItem = newSpace(w, h, zone_w, zone_h, getStatusItem, setStatusItem); + new->spaceShot = newSpace(w, h, zone_w, zone_h, getStatusShot, setStatusShot); return new; } @@ -164,13 +177,6 @@ void drawArena(arena_t *arena) //printSpace(arena->spaceShot); - //drawListTux(arena->listTux); - //drawListWall(arena->listWall); - //drawListTeleport(arena->listTeleport); - //drawListPipe(arena->listPipe); - //drawListShot(arena->listShot); - //drawListItem(arena->listItem); - drawModule(screen_x, screen_y, WINDOW_SIZE_X, WINDOW_SIZE_Y); /* my_time_t t; diff --git a/src/idManager.c b/src/idManager.c index 3162771..ea7e679 100644 --- a/src/idManager.c +++ b/src/idManager.c @@ -45,13 +45,15 @@ int getNewID() assert( listID != NULL ); do{ + ret = random() % MAX_ID; +/* ret = ++lastID; if( lastID > MAX_ID ) { lastID = 0; } - +*/ }while( isRegisterID(ret) != -1 ); addList(listID, newInt(ret) ); diff --git a/src/item.c b/src/item.c index 467117d..171ac76 100644 --- a/src/item.c +++ b/src/item.c @@ -610,7 +610,7 @@ void eventGiveTuxItem(tux_t *tux, item_t *item, space_t *spaceItem) case GUN_MINE : case GUN_BOMBBALL : tuxGiveGun(tux, item); - delObjectFromSpace(spaceItem, item); + delObjectFromSpaceWithObject(spaceItem, item, destroyItem); break; case ITEM_MINE : @@ -632,7 +632,7 @@ void eventGiveTuxItem(tux_t *tux, item_t *item, space_t *spaceItem) case BONUS_4X : case BONUS_HIDDEN : tuxGiveBonus(tux, item); - delObjectFromSpace(spaceItem, item); + delObjectFromSpaceWithObject(spaceItem, item, destroyItem); break; } } diff --git a/src/shot.c b/src/shot.c index cb07bda..50e6e9a 100644 --- a/src/shot.c +++ b/src/shot.c @@ -353,169 +353,9 @@ void checkShotIsInTuxScreen(arena_t *arena) 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); - - if( conflictSpace(thisShot->x, thisShot->y, thisShot->w, thisShot->h, - screen_x-speed, screen_y, speed, WINDOW_SIZE_Y) ) - { - client_t *thisClient; - thisClient = getClientFromTux(thisTux); - proto_send_shot_server(PROTO_SEND_ONE, thisClient, thisShot); - } - - if( conflictSpace(thisShot->x, thisShot->y, thisShot->w, thisShot->h, - screen_x+WINDOW_SIZE_X, screen_y, speed, WINDOW_SIZE_Y) ) - { - client_t *thisClient; - thisClient = getClientFromTux(thisTux); - proto_send_shot_server(PROTO_SEND_ONE, thisClient, thisShot); - } - - if( conflictSpace(thisShot->x, thisShot->y, thisShot->w, thisShot->h, - screen_x, screen_y-speed, WINDOW_SIZE_X, speed) ) - { - client_t *thisClient; - thisClient = getClientFromTux(thisTux); - proto_send_shot_server(PROTO_SEND_ONE, thisClient, thisShot); - } - - if( conflictSpace(thisShot->x, thisShot->y, thisShot->w, thisShot->h, - screen_x, screen_y+WINDOW_SIZE_Y, WINDOW_SIZE_X, speed) ) - { - client_t *thisClient; - thisClient = getClientFromTux(thisTux); - proto_send_shot_server(PROTO_SEND_ONE, thisClient, thisShot); - } - } -#endif } } -/* -static int getSppedShot(shot_t *shot) -{ - return ( myAbs(shot->px) > myAbs(shot->py) ? myAbs(shot->px) : myAbs(shot->py) ); -} - -void transformOnlyLasser(shot_t *shot) -{ - switch( shot->position ) - { - case TUX_RIGHT : - case TUX_LEFT : - shot->w = GUN_LASSER_HORIZONTAL; - shot->h = GUN_SHOT_VERTICAL; -#ifndef PUBLIC_SERVER - shot->img = g_shot_lasserX; -#endif - break; - case TUX_UP : - case TUX_DOWN : - shot->w = GUN_SHOT_VERTICAL; - shot->h = GUN_LASSER_HORIZONTAL; -#ifndef PUBLIC_SERVER - shot->img = g_shot_lasserY; -#endif - break; - } -} - -static void transformShot(shot_t *shot, int position) -{ - int speed; - - speed = getSppedShot(shot); - - switch( position ) - { - case TUX_UP : - shot->px = 0; - shot->py = -speed; - break; - - case TUX_LEFT : - shot->px = -speed; - shot->py = 0; - break; - - case TUX_RIGHT : - shot->px = speed; - shot->py = 0; - break; - - case TUX_DOWN : - shot->px = 0; - shot->py = +speed; - break; - } - - shot->position = position; - shot->isCanKillAuthor = TRUE; - - if( shot->gun == GUN_LASSER ) - { - transformOnlyLasser(shot); - } -} - -void moveShot(shot_t *shot, int position, int src_x, int src_y, - int dist_x, int dist_y, int dist_w, int dist_h) -{ - int offset = 0; - - switch( shot->position ) - { - case TUX_UP : - case TUX_DOWN : - offset = shot->x - src_x; - break; - - case TUX_RIGHT : - case TUX_LEFT : - offset = shot->y - src_y; - break; - } - - transformShot(shot, position); - - switch( shot->position ) - { - case TUX_UP : - shot->x = dist_x + offset; - shot->y = dist_y; - break; - - case TUX_LEFT : - shot->x = dist_x; - shot->y = dist_y + offset; - break; - - case TUX_RIGHT : - shot->x = dist_x + dist_w; - shot->y = dist_y + offset; - break; - - case TUX_DOWN : - shot->x = dist_x + offset; - shot->y = dist_y + dist_h; - break; - } - - shot->x += shot->px; - shot->y += shot->py; - - if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) - { - proto_send_shot_server(PROTO_SEND_ALL, NULL, shot); - } - -} -*/ void destroyShot(shot_t *p) { assert( p != NULL ); diff --git a/src/space.c b/src/space.c index c9688a8..96ac81b 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 @@ -165,7 +165,7 @@ static void checkList(space_t *space) } } -static void addToList(space_t *space, void *p) +static void addToListOnTheBaseIndex(space_t *space, void *p) { int id, point_id, inc_point_id; int x, y, w, h; @@ -178,11 +178,10 @@ static void addToList(space_t *space, void *p) space->getStatus(p, &id, &x, &y, &w, &h); /* - printf("addToList (%d)\n", id); + printf("addToListOnTheBaseIndex (%d)\n", id); printListID(space); assert( id >= 0 ); */ - if( len == 0 ) { //printf("OK first\n"); @@ -224,13 +223,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); + //checkList(space); return; } @@ -238,13 +237,13 @@ static void addToList(space_t *space, void *p) { addList(space->list, p); //printf("OK height\n"); - checkList(space); + //checkList(space); 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"); @@ -252,12 +251,12 @@ static void addToList(space_t *space, void *p) checkList(space); return; } - +*/ if( point_id < id && id < inc_point_id ) { //printf("OK\n"); insList(space->list, point+1, p); - checkList(space); + //checkList(space); return; } @@ -295,7 +294,7 @@ void addObjectToSpace(space_t *p, void *item) } #ifdef ERROR_SUPPORT - addToList(p, item); + addToListOnTheBaseIndex(p, item); #endif #ifndef ERROR_SUPPORT @@ -341,9 +340,10 @@ 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; @@ -592,72 +592,23 @@ void destroySpaceWithObject(space_t *p, void *f) #ifdef DEBUG_SPACE void test_space() -{ +{/* space_t *space; recv_t *recv; space = newSpace(5000, 2500, 320, 240, getStatus, setStatus); + addObjectToSpace(space, newRecv(5, 0, 0, 1, 1) ); + addObjectToSpace(space, newRecv(7, 0, 0, 1, 1) ); + addObjectToSpace(space, newRecv(8, 0, 0, 1, 1) ); + addObjectToSpace(space, newRecv(9, 0, 0, 1, 1) ); 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); + recv = getObjectFromSpaceWithID(space, 6); if( recv != NULL )printf("id = %d\n", recv->id); */ - -#if 0 - for( i = 1 ; i < 16 ; i++ ) - for( j = 1 ; j < 16 ; j++ ) - { - recv_t *this; - - this = newRecv(j*160, i*120, 32, 32); - //addToSpace(space, newRecv(j*550, i*550, 1, 1) ); - addObjectToSpace(space, this ); - delObjectFromSpaceWithMem(space, this, destroyRecv); - } -#endif -/* - list = newList(); - getObjectFromSpace(space, 640, 480, 800 , 600, list); - - printf("list->count = %d\n", list->count); - - for( i = 0 ; i < list->count ; i++ ) - { - recv_t *recv; - recv = list->list[i]; - - printf("%3d %3d %3d %3d\n", recv->x, recv->y, recv->w, recv->h); - } -*/ -/* - destroyList(list); - - printSpace(space); - - destroySpace(space); -*/ +// printListID(space); } #endif diff --git a/src/tux.c b/src/tux.c index c2db1f2..710b253 100644 --- a/src/tux.c +++ b/src/tux.c @@ -223,29 +223,6 @@ void drawListTux(list_t *listTux) #endif -/* -tux_t* getTuxID(list_t *listTux, int id) -{ - tux_t *thisTux; - int i; - - assert( listTux != NULL ); - - for( i = 0 ; i < listTux->count ; i++ ) - { - thisTux = (tux_t *)listTux->list[i]; - assert( thisTux != NULL ); - - if( thisTux->id == id ) - { - return thisTux; - } - } - - return NULL; -} -*/ - void replaceTuxID(tux_t *tux, int id) { replaceID(tux->id, id); @@ -267,68 +244,6 @@ int isTuxSeesTux(tux_t *tux, tux_t *thisTux) WINDOW_SIZE_X*1.25, WINDOW_SIZE_Y*1.25, thisTux_x, thisTux_y, thisTux_w, thisTux_h); } -/* -tux_t* isConflictWithListTux(list_t *listTux, int x, int y, int w, int h) -{ - tux_t *thisTux; - int tx, ty, tw, th; - int i; - - assert( listTux != NULL ); - - for( i = 0 ; i < listTux->count ; i++ ) - { - thisTux = (tux_t *)listTux->list[i]; - assert( thisTux != NULL ); - - getTuxProportion(thisTux, &tx, &ty, &tw, &th); - - if( conflictSpace(x, y, w, h, tx, ty, tw, th) ) - { - return thisTux; - } - } - - return NULL; -} -*/ - -/* -int isConflictTuxWithListTux(tux_t *tux, list_t *listTux) -{ - tux_t *thisTux; - int tux_x, tux_y, tux_w, tux_h; - int thisTux_x, thisTux_y, thisTux_w, thisTux_h; - int i; - - assert( tux != NULL ); - assert( listTux != NULL ); - - getTuxProportion(tux, &tux_x, &tux_y, &tux_w, &tux_h); - - for( i = 0 ; i < listTux->count ; i++ ) - { - thisTux = (tux_t *)listTux->list[i]; - assert( thisTux != NULL ); - - if( thisTux == tux ) - { - continue; - } - - getTuxProportion(thisTux, &thisTux_x, &thisTux_y, &thisTux_w, &thisTux_h); - - if( thisTux->bonus != BONUS_GHOST && conflictSpace(tux_x, tux_y, tux_w, tux_h, - thisTux_x, thisTux_y, thisTux_w, thisTux_h) ) - { - return 1; - } - } - - return 0; -} -*/ - static void timer_spawnTux(void *p) { tux_t *tux; @@ -485,8 +400,11 @@ static void bombBallExplosion(shot_t *shot) y = ( shot->y + shot->h/2 ) - ITEM_BIG_EXPLOSION_HEIGHT/2; item = newItem(x, y, ITEM_BIG_EXPLOSION, shot->author_id ); + addObjectToSpace(getCurrentArena()->spaceItem, item ); + delObjectFromSpaceWithObject(getCurrentArena()->spaceShot, shot, destroyShot); + if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) { proto_send_delshot_server(PROTO_SEND_ALL, NULL, shot); @@ -494,66 +412,6 @@ static void bombBallExplosion(shot_t *shot) } } -/* -void eventConflictTuxWithShot(list_t *listTux, list_t *listShot) -{ - shot_t *thisShot; - tux_t *thisTux; - - int i; - - assert( listTux != NULL ); - assert( listShot != NULL ); - - for( i = 0 ; i < listShot->count ; i++ ) - { - thisShot = (shot_t *)listShot->list[i]; - assert( thisShot != NULL ); - - if( ( thisTux = isConflictWithListTux(listTux, thisShot->x, thisShot->y, - thisShot->w, thisShot->h) ) != NULL ) - { - if( thisTux->status == TUX_STATUS_ALIVE ) - { - if( thisShot->author_id == thisTux->id && - thisShot->isCanKillAuthor == FALSE ) - { - continue; - } - - if( thisTux->bonus == BONUS_TELEPORT ) - { - tuxTeleport(thisTux); - continue; - } - - if( thisShot->gun == GUN_BOMBBALL ) - { - if( getNetTypeGame() != NET_GAME_TYPE_CLIENT ) - { - bombBallExplosion(thisShot); - delListItem(listShot, i, destroyShot); - i--; - } - - continue; - } - - if( getNetTypeGame() != NET_GAME_TYPE_CLIENT ) - { - eventTuxIsDeadWIthShot(thisTux, thisShot); - } - } - - delListItem(listShot, i, destroyShot); - i--; - - continue; - } - } -} -*/ - void eventConflictTuxWithShot(arena_t *arena) { shot_t *thisShot; @@ -592,7 +450,6 @@ void eventConflictTuxWithShot(arena_t *arena) if( getNetTypeGame() != NET_GAME_TYPE_CLIENT ) { bombBallExplosion(thisShot); - delObjectFromSpaceWithObject(arena->spaceShot, thisShot, destroyShot); i--; } @@ -613,33 +470,6 @@ void eventConflictTuxWithShot(arena_t *arena) } } -/* -void eventConflictTuxWithTeleport(list_t *listTux, list_t *listTeleport) -{ - teleport_t *thisTeleport; - tux_t *thisTux; - - int i; - - assert( listTux != NULL ); - assert( listTeleport != NULL ); - - for( i = 0 ; i < listTeleport->count ; i++ ) - { - thisTeleport = (teleport_t *)listTeleport->list[i]; - assert( thisTeleport != NULL ); - - if( ( thisTux = isConflictWithListTux(listTux, thisTeleport->x, thisTeleport->y, - thisTeleport->w, thisTeleport->h) ) != NULL ) - { - if( getNetTypeGame() != NET_GAME_TYPE_CLIENT ) - { - eventTeleportTux(listTeleport, thisTeleport, thisTux); - } - } - } -} -*/ void moveTux(tux_t *tux, int n) { @@ -649,25 +479,6 @@ void moveTux(tux_t *tux, int n) int w, h; arena_t *arena; -/* - printf("%d %d\n", tux->x, tux->y); -*/ -/* - static my_time_t lastTime = 0; - my_time_t currentTime; - - if( lastTime == 0 ) - { - lastTime = getMyTime(); - } - - currentTime = getMyTime(); - - printf("%d\n", currentTime - lastTime ); - - lastTime = getMyTime(); -*/ - assert( tux != NULL ); arena = getCurrentArena(); @@ -713,17 +524,6 @@ void moveTux(tux_t *tux, int n) new_x += px * TUX_STEP; new_y += py * TUX_STEP; } -/* - setTuxProportion(tux, new_x, new_y); - - if( tux->bonus != BONUS_GHOST && ( - isConflictWithObjectFromSpace(arena->spaceTux, x, y, w, h) || - isConflictModule(x, y, w, h) ) ) - { - setTuxProportion(tux, zal_x, zal_y); - return; - } -*/ if( tux->bonus != BONUS_GHOST && ( isConflictWithObjectFromSpaceBut(arena->spaceTux, new_x, new_y, w, h, tux) || -- cgit v1.2.3