diff options
| author | oroborus <oroborus@0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e> | 2008-06-30 20:25:53 +0000 |
|---|---|---|
| committer | oroborus <oroborus@0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e> | 2008-06-30 20:25:53 +0000 |
| commit | 62cdf2b04aa8d0162da8e024d9901ba5743bf5e3 (patch) | |
| tree | 0fee4d7fe539986956cafc8c729d3b68d37d3d7b /src/base | |
| parent | 55233c4949d7bed70345f6fea44315df151d9ba5 (diff) | |
- hrac moze sucastne chodit aj strielat
- na indexovanie poloziek v list_t sa pouizva index.c ( postaveny na list_t )
- oprava chyby, porgram sa zrutil ak nebolo inicializovane audio
- zexxov patch
git-svn-id: http://opensvn.csie.org/tuxanci_ng@80 0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e
Diffstat (limited to 'src/base')
| -rw-r--r-- | src/base/Makefile | 5 | ||||
| -rw-r--r-- | src/base/index.c | 249 | ||||
| -rw-r--r-- | src/base/index.h | 15 | ||||
| -rw-r--r-- | src/base/protect.c | 3 | ||||
| -rw-r--r-- | src/base/space.c | 292 | ||||
| -rw-r--r-- | src/base/space.h | 5 |
6 files changed, 286 insertions, 283 deletions
diff --git a/src/base/Makefile b/src/base/Makefile index 6de422d..d9998a6 100644 --- a/src/base/Makefile +++ b/src/base/Makefile @@ -1,5 +1,5 @@ -all: arena.o arenaFile.o director.o gun.o homeDirector.o checkFront.o idManager.o item.o list.o main.o modules.o myTimer.o net_multiplayer.o protect.o proto.o server.o shot.o space.o storage.o textFile.o tux.o +all: arena.o arenaFile.o director.o gun.o homeDirector.o checkFront.o idManager.o item.o index.o list.o main.o modules.o myTimer.o net_multiplayer.o protect.o proto.o server.o shot.o space.o storage.o textFile.o tux.o arena.o: arena.c arena.h $(CC) $(CFLAGS) -o arena.o -c arena.c @@ -25,6 +25,9 @@ idManager.o: idManager.c idManager.h item.o: item.c item.h $(CC) $(CFLAGS) -o item.o -c item.c +index.o: index.c index.h + $(CC) $(CFLAGS) -o index.o -c index.c + list.o: list.c list.h $(CC) $(CFLAGS) -o list.o -c list.c diff --git a/src/base/index.c b/src/base/index.c new file mode 100644 index 0000000..d3f75a5 --- /dev/null +++ b/src/base/index.c @@ -0,0 +1,249 @@ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <assert.h> + +#include "main.h" +#include "list.h" +#include "index.h" + +static int* index_newInt(int x) +{ + int *new; + new = malloc( sizeof(int) ); + *new = x; + return new; +} + +static void printIndex(list_t *list) +{ + int i; + + printf("printIndex\n"); + + for( i = 0 ; i < list->count ; i++ ) + { + int thisIndex; + + thisIndex = *(int *)list->list[i]; + printf("%d\n", thisIndex); + } +} + +static void checkIndex(list_t *list) +{ + int index, thisIndex; + int i; + + if( list->count == 0 ) + { + printf("nothing\n"); + return; + } + + thisIndex = *(int *)list->list[0]; + + for( i = 1 ; i < list->count ; i++ ) + { + index = *(int *)list->list[i]; + + if( index <= thisIndex ) + { + printIndex(list); + assert( ! "error" ); + } + + thisIndex = index; + } +} + +list_t* newIndex() +{ + return newList(); +} + +int addToIndex(list_t *list, int index) +{ + int point_index, inc_point_index; + int len; + int min, max; + int point; + + len = list->count; + + //printf("addToIndex (%p, %d)\n", list, index); + //printIndex(list); + + if( len == 0 ) + { + //printf("OK first\n"); + addList(list, index_newInt(index)); + checkIndex(list); + return 0; + } + + if( len == 1 ) + { + //space->getStatus(space->list->list[0], &point_id, &x, &y, &w, &h); + point_index = *(int *)list->list[0]; + + if( index > point_index ) + { + addList(list, index_newInt(index)); + //checkIndex(list); + return 1; + } + + if( index < point_index ) + { + insList(list, 0, index_newInt(index)); + //checkIndex(list); + return 0; + } + + //printf("OK\n") + return -1; + } + + 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(list, 0, index_newInt(index)); + //checkIndex(list); + return 0; + } + + if( point+1 >= len /*|| min >= len*/ ) + { + addList(list, index_newInt(index) ); + //printf("OK height\n"); + //checkIndex(list); + return len; + } + +// 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); + + point_index = *(int *)list->list[point]; + inc_point_index = *(int *)list->list[point+1]; +/* + if( min == max ) + { + //printf("OK\n"); + insList(space->list, min, p); + checkList(space); + return; + } +*/ + if( point_index < index && index < inc_point_index ) + { + //printf("OK\n"); + insList(list, point+1, index_newInt(index) ); + //checkIndex(list); + return point+1; + } + + if( index > inc_point_index ) + { + min = point + 1; + continue; + } + + if( index < point_index ) + { + max = point - 1; + continue; + } + } +} + +int getFormIndex(list_t *list, int index) +{ + int point_index; + int len; + int min, max; + int point; + + + //printf("getFormIndex %d\n", index); + //printIndex(list); + + len =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 -1; + } + + //space->getStatus(space->list->list[point], &point_id, &x, &y, &w, &h); + + point_index = *(int *)list->list[point]; + + if( min == max ) + { + //space->getStatus(space->list->list[min], &point_id, &x, &y, &w, &h); + point_index = *(int *)list->list[point]; + } + + if( point_index == index ) + { + return point; + } + + if( index > point_index ) + { + min = point + 1; + continue; + } + + if( index < point_index ) + { + max = point - 1; + continue; + } + } +} + +void delFromIndex(list_t *list, int index) +{ + int offset; + + offset = getFormIndex(list, index); + assert( offset != -1 ); + + delListItem(list, offset, free); +} + +void destroyIndex(list_t *list) +{ + assert( list != NULL); + + destroyListItem(list, free); +} diff --git a/src/base/index.h b/src/base/index.h new file mode 100644 index 0000000..3799aa3 --- /dev/null +++ b/src/base/index.h @@ -0,0 +1,15 @@ + +#ifndef INDEX_H + +#define INDEX_H + +#include "main.h" +#include "list.h" + +extern list_t* newIndex(); +extern int addToIndex(list_t *list, int index); +extern int getFormIndex(list_t *list, int index); +extern void delFromIndex(list_t *list, int index); +extern void destroyIndex(list_t *list); + +#endif diff --git a/src/base/protect.c b/src/base/protect.c index 074873a..7da70bb 100644 --- a/src/base/protect.c +++ b/src/base/protect.c @@ -42,11 +42,12 @@ void refreshLastMove(protect_t *p) p->avarage = 0; p->count = 0; +#if 0 if( index < PROTECT_SPEED_INTERVAL_TIMEOUT ) { p->isDown = TRUE; } - +#endif //printf("speed index = %d\n", index); } } diff --git a/src/base/space.c b/src/base/space.c index aeae784..b32ae33 100644 --- a/src/base/space.c +++ b/src/base/space.c @@ -7,68 +7,7 @@ #include "main.h" #include "list.h" #include "space.h" - -#define DEBUG_SPACE -#define ERROR_SUPPORT - -#ifdef DEBUG_SPACE - - -typedef struct recv_struct -{ - int id; - int x, y; - int w, h; -} recv_t; - -recv_t* newRecv(int id, int x , int y, int w, int h) -{ - recv_t *new; - - new = malloc( sizeof(recv_t) ); - memset(new, 0, sizeof(recv_t)); - - new->id = id; - new->x = x; - new->y = y; - new->w = w; - new->h = h; - - return new; -} - -void getStatus(void *p, int *id, int *x, int *y, int *w, int *h) -{ - recv_t *recv; - - recv = p; - - *id = recv->id; - *x = recv->x; - *y = recv->y; - *w = recv->w; - *h = recv->h; -} - -void setStatus(void *p, int x, int y, int w, int h) -{ - recv_t *recv; - - recv = p; - - recv->x = x; - recv->y = y; - recv->w = w; - recv->h = h; - recv->x = x; -} - -void destroyRecv(recv_t *p) -{ - free(p); -} - -#endif +#include "index.h" static int my_conflictSpace(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2) { @@ -92,6 +31,7 @@ space_t *newSpace(int w, int h, int segW, int segH, new->getStatus = getStatus; new->setStatus = setStatus; new->list = newList(); + new->listIndex = newIndex(); new->area = malloc( new->w * sizeof(list_t **) ); @@ -120,167 +60,12 @@ static void getSegment(space_t *p, int x, int y, int w, int h, *segH = ( (y+h) / p->segH + 1 ) - *segY; } -#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 addToListOnTheBaseIndex(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); -/* - printf("addToListOnTheBaseIndex (%d)\n", id); - printListID(space); - assert( id >= 0 ); -*/ - 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); - //checkList(space); - return; - } - - if( point+1 >= len /*|| min >= len*/ ) - { - addList(space->list, p); - //printf("OK height\n"); - //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"); - insList(space->list, min, p); - checkList(space); - return; - } -*/ - if( point_id < id && id < inc_point_id ) - { - //printf("OK\n"); - insList(space->list, point+1, p); - //checkList(space); - return; - } - - if( id > inc_point_id ) - { - min = point + 1; - continue; - } - - if( id < point_id ) - { - max = point - 1; - continue; - } - } -} - -#endif - void addObjectToSpace(space_t *p, void *item) { int segX, segY, segW, segH; int id, x, y, w, h; int i, j; + int offset; p->getStatus(item, &id, &x, &y, &w, &h); getSegment(p, x, y, w, h, &segX, &segY, &segW, &segH); @@ -298,13 +83,8 @@ void addObjectToSpace(space_t *p, void *item) } } -#ifdef ERROR_SUPPORT - addToListOnTheBaseIndex(p, item); -#endif - -#ifndef ERROR_SUPPORT - addList(p->list, item); -#endif + offset = addToIndex(p->listIndex, id); + insList(p->list, offset, item); } void getObjectFromSpace(space_t *p, int x, int y, int w, int h, list_t *list) @@ -341,65 +121,11 @@ void getObjectFromSpace(space_t *p, int x, int y, int w, int h, list_t *list) } } -int searchObjectInSpace(space_t *space, int id) -{ - int point_id; - int x, y, w, h; - int len; - int min, max; - int point; - -/* - printf("getObjectFromSpaceWithID %d\n", id); - printListID(space); -*/ - 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 -1; - } - - 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 point; - } - - if( id > point_id ) - { - min = point + 1; - continue; - } - - if( id < point_id ) - { - max = point - 1; - continue; - } - } -} - void* getObjectFromSpaceWithID(space_t *space, int id) { int index; - index = searchObjectInSpace(space, id); + index = getFormIndex(space->listIndex, id); return ( index != -1 ? space->list->list[index] : NULL ); } @@ -505,9 +231,12 @@ void delObjectFromSpace(space_t *p, void *item) } } - index = searchObjectInSpace(p, id); + index = getFormIndex(p->listIndex, id); assert( index != -1 ); + delList(p->list, index); + + delFromIndex(p->listIndex, id); } void delObjectFromSpaceWithObject(space_t *p, void *item, void *f) @@ -568,6 +297,7 @@ void destroySpace(space_t *p) int j, i; destroyList(p->list); + destroyIndex(p->listIndex); for( i = 0 ; i < p->h ; i++ ) { diff --git a/src/base/space.h b/src/base/space.h index de31768..be21411 100644 --- a/src/base/space.h +++ b/src/base/space.h @@ -10,10 +10,15 @@ typedef struct space_struct { int w; int h; + int segW; int segH; + list_t ***area; + list_t *list; + list_t *listIndex; + void (*getStatus)(void *p, int *id, int *x, int *y, int *w, int *h); void (*setStatus)(void *p, int x, int y, int w, int h); } space_t; |