Skip to content

Commit

Permalink
* Pfadsuche leicht modifiziert, so dass beim Durchlaufen eines Porta…
Browse files Browse the repository at this point in the history
…ls Wegpunkte auf beiden Seiten des Portals angelegt werden. Dadurch sollten beim Ablaufen des Pfades keine Koordinaten mehr entstehen, welche eine Einheit ausserhalb des begehbaren Gebietes liegen. Dies korrigiert Bug #8.

 * world_size liefert nun als x2 und y2 Koordinaten, welche gerade noch innerhalb des Spielfelds liegen. Zuvor hat world_size Koordinaten geliefert, welche gerade ausserhalb liegen. Skripte, welche sich auf das alte Verhalten verlassen, koennen x2 und y2 jeweils um 1 inkrementieren.


git-svn-id: http://infon.googlecode.com/svn/branches/lua-5.1@72 8171fb75-e542-0410-96e4-03d5dd800671
  • Loading branch information
dividuum committed Oct 22, 2006
1 parent 9ac2dc9 commit fe75038
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 69 deletions.
4 changes: 2 additions & 2 deletions Makefile
Expand Up @@ -24,12 +24,12 @@ ifdef WINDOWS
MINGW = $(HOME)/progs/mingw32/ MINGW = $(HOME)/progs/mingw32/
SDLDIR = $(MINGW) SDLDIR = $(MINGW)
CC = /opt/xmingw/bin/i386-mingw32msvc-gcc CC = /opt/xmingw/bin/i386-mingw32msvc-gcc
CFLAGS = $(COMMON_CFLAGS) -I$(MINGW)/include CFLAGS += $(COMMON_CFLAGS) -I$(MINGW)/include
WINDRES = /opt/xmingw/bin/i386-mingw32msvc-windres WINDRES = /opt/xmingw/bin/i386-mingw32msvc-windres
LUAPLAT = mingw LUAPLAT = mingw
else else
SDLDIR = $(shell sdl-config --prefix) SDLDIR = $(shell sdl-config --prefix)
CFLAGS = $(COMMON_CFLAGS) CFLAGS += $(COMMON_CFLAGS)
LUAPLAT = debug #linux LUAPLAT = debug #linux
LDFLAGS += -ldl LDFLAGS += -ldl
endif endif
Expand Down
44 changes: 19 additions & 25 deletions creature.c
Expand Up @@ -83,6 +83,10 @@ int creature_num(const creature_t *creature) {
return creature - creatures; return creature - creatures;
} }


int creature_groundbased(const creature_t *creature) {
return creature->type != CREATURE_FLYER;
}

maptype_e creature_tile_type(const creature_t *creature) { maptype_e creature_tile_type(const creature_t *creature) {
return world_get_type(X_TO_TILEX(creature->x), Y_TO_TILEY(creature->y)); return world_get_type(X_TO_TILEX(creature->x), Y_TO_TILEY(creature->y));
} }
Expand Down Expand Up @@ -205,6 +209,9 @@ void creature_do_move_to_target(creature_t *creature, int delta) {


creature->x += dx * travelled / dist_to_waypoint; creature->x += dx * travelled / dist_to_waypoint;
creature->y += dy * travelled / dist_to_waypoint; creature->y += dy * travelled / dist_to_waypoint;

assert(!creature_groundbased(creature) || world_walkable(X_TO_TILEX(creature->x),
Y_TO_TILEY(creature->y)));
} }


// ------------- Food -> Health ------------- // ------------- Food -> Health -------------
Expand Down Expand Up @@ -774,36 +781,23 @@ void creature_moveall(int delta) {
int creature_set_path(creature_t *creature, int x, int y) { int creature_set_path(creature_t *creature, int x, int y) {
pathnode_t *newpath; pathnode_t *newpath;


switch (creature->type) { if (creature_groundbased(creature)) {
case CREATURE_SMALL: if (!world_walkable(X_TO_TILEX(x), Y_TO_TILEY(y)))
case CREATURE_BIG: return 0;
// Bodenbasierte Viecher
if (!world_walkable(X_TO_TILEX(x), Y_TO_TILEY(y))) newpath = world_findpath(creature->x, creature->y, x, y);
return 0;


newpath = world_findpath(creature->x, creature->y, x, y); if (!newpath)
break; return 0;
case CREATURE_FLYER: } else {
// Fliegendes Vieh // Fliegendes Vieh
if (!world_is_within_border(X_TO_TILEX(x), Y_TO_TILEY(y))) if (!world_is_within_border(X_TO_TILEX(x), Y_TO_TILEY(y)))
return 0; return 0;


newpath = malloc(sizeof(pathnode_t)); newpath = pathnode_new(x, y);
if (newpath) {
newpath->x = x;
newpath->y = y;
newpath->next = NULL;
}
break;
default:
assert(0);
} }


if (!newpath)
return 0;

path_delete(creature->path); path_delete(creature->path);

creature->path = newpath; creature->path = newpath;
return 1; return 1;
} }
Expand Down
2 changes: 1 addition & 1 deletion gui_creature.c
Expand Up @@ -238,7 +238,6 @@ void gui_creature_from_network(packet_t *packet) {
} else { } else {
if (CREATURE_USED(creature)) PROTOCOL_ERROR(); if (CREATURE_USED(creature)) PROTOCOL_ERROR();
memset(creature, 0, sizeof(gui_creature_t)); memset(creature, 0, sizeof(gui_creature_t));
creature->used = 1;
if (playerno >= MAXPLAYERS) PROTOCOL_ERROR(); if (playerno >= MAXPLAYERS) PROTOCOL_ERROR();
creature->player = playerno; creature->player = playerno;


Expand All @@ -248,6 +247,7 @@ void gui_creature_from_network(packet_t *packet) {


creature->last_x = x; creature->last_x = x;
creature->last_y = y; creature->last_y = y;
creature->used = 1;


// XXX: x, y checken? // XXX: x, y checken?
gui_creature_add_path(creature, gui_creature_add_path(creature,
Expand Down
5 changes: 2 additions & 3 deletions map.h
Expand Up @@ -46,9 +46,8 @@ typedef struct tile_s {
#define TILE_X1(x) ((x) * TILE_WIDTH) #define TILE_X1(x) ((x) * TILE_WIDTH)
#define TILE_Y1(y) ((y) * TILE_HEIGHT) #define TILE_Y1(y) ((y) * TILE_HEIGHT)


// XXX: Add +1? #define TILE_X2(x) (TILE_X1(x) + TILE_WIDTH - 1)
#define TILE_X2(x) (TILE_X1(x) + TILE_WIDTH) #define TILE_Y2(y) (TILE_Y1(y) + TILE_HEIGHT - 1)
#define TILE_Y2(y) (TILE_Y1(y) + TILE_HEIGHT)


#define TILE_XCENTER(x) (TILE_X1(x) + TILE_WIDTH / 2) #define TILE_XCENTER(x) (TILE_X1(x) + TILE_WIDTH / 2)
#define TILE_YCENTER(y) (TILE_Y1(y) + TILE_HEIGHT / 2) #define TILE_YCENTER(y) (TILE_Y1(y) + TILE_HEIGHT / 2)
Expand Down
63 changes: 26 additions & 37 deletions path.c
Expand Up @@ -37,8 +37,10 @@ inline int path_calc_cost(int x1, int y1, int x2, int y2) {


pathnode_t *pathnode_new(int x, int y) { pathnode_t *pathnode_new(int x, int y) {
pathnode_t *node = (pathnode_t*)malloc(sizeof(pathnode_t)); pathnode_t *node = (pathnode_t*)malloc(sizeof(pathnode_t));
node->x = x; assert(node);
node->y = y; node->x = x;
node->y = y;
node->next = NULL;
return node; return node;
} }


Expand Down Expand Up @@ -177,11 +179,8 @@ pathnode_t *finder_find(pathfinder_t *finder, map_t *map, int sx, int sy, int ex
const tile_t *etile = MAP_TILE(map, tex, tey); const tile_t *etile = MAP_TILE(map, tex, tey);


// Ziel und Quelle gleich // Ziel und Quelle gleich
if (sx == ex && sy == ey) { if (sx == ex && sy == ey)
pathnode_t *path = pathnode_new(ex, ey); return pathnode_new(ex, ey);
path->next = NULL;
return path;
}


// Gibt es ueberhaupt eine Verbindung? // Gibt es ueberhaupt eine Verbindung?
if (stile->region != etile->region) if (stile->region != etile->region)
Expand All @@ -193,21 +192,14 @@ pathnode_t *finder_find(pathfinder_t *finder, map_t *map, int sx, int sy, int ex
// Spezialfall: Nur ein Tile // Spezialfall: Nur ein Tile
if ((ABS(tsx - tex) == 1 && (tsy == tey)) || if ((ABS(tsx - tex) == 1 && (tsy == tey)) ||
((tsx == tex) && ABS(tsy - tey) == 1)) ((tsx == tex) && ABS(tsy - tey) == 1))
{ return pathnode_new(ex, ey);
pathnode_t *path = pathnode_new(ex, ey);
path->next = NULL;
return path;
}


const area_t *sarea = stile->area; const area_t *sarea = stile->area;
const area_t *earea = etile->area; const area_t *earea = etile->area;


// Ziel und Quelle im gleichen Area? // Ziel und Quelle im gleichen Area?
if (sarea == earea) { if (sarea == earea)
pathnode_t *path = pathnode_new(ex, ey); return pathnode_new(ex, ey);
path->next = NULL;
return path;
}


// Neue Path id zum eindeutigen erkennen, ob // Neue Path id zum eindeutigen erkennen, ob
// path_prev in portal_t aktuell oder veraltet ist holen. // path_prev in portal_t aktuell oder veraltet ist holen.
Expand Down Expand Up @@ -335,28 +327,25 @@ pathnode_t *finder_find(pathfinder_t *finder, map_t *map, int sx, int sy, int ex
pathnode_t *curpath = &pathstart; pathnode_t *curpath = &pathstart;
int lastx = sx, lasty = sy; int lastx = sx, lasty = sy;
do { do {
// Die cx und cy Koordinaten befinden sich jeweils an der linken bzw if (pathportal->dir == PORTAL_DIR_HORIZONTAL) {
// oberen Kante eines Tiles. Kommt ein Pfad daher von Rechts oder von curpath->next = pathnode_new(pathportal->cx,
// Unten zu einem solchen Portal, so wuerde der hier erzeugte Knoten noch im pathportal->cy + (lasty > pathportal->cy));
// aktuell durchlaufenen Area liegen. Wird ein solcher Weg abgelaufen und curpath = curpath->next;
// direkt nach dem Knoten folgt eine ~90 Grad Kurve, so kann es sein, dass curpath->next = pathnode_new(pathportal->cx,
// die x bzw y Koordinate fuer eine kurze Zeit nicht veraendert wird, pathportal->cy + (lasty <= pathportal->cy));
// d.h. das Area, obwohl schon auf dem Weg zum naechsten Knoten, nicht } else {
// verlassen wird. Wird in diesem Moment eine erneute Suche zum gleichen curpath->next = pathnode_new(pathportal->cx + (lastx > pathportal->cx),
// Ziel begonnen, wird der zuvor bereits ueberquerte Knoten erneut pathportal->cy);
// als Pfadknoten gefunden und es kommt zu einer haesslichen Endlosschleife. curpath = curpath->next;
// Daher wird von x bzw y der Wert 1 subtrahiert, falls das Portal von curpath->next = pathnode_new(pathportal->cx + (lastx <= pathportal->cx),
// Rechts bzw von unten angesteuert wird. pathportal->cy);
curpath->next = pathnode_new(pathportal->cx - (pathportal->cx <= lastx), }
pathportal->cy - (pathportal->cy <= lasty)); curpath = curpath->next;
lastx = pathportal->cx; lastx = curpath->x;
lasty = pathportal->cy; lasty = curpath->y;
curpath = curpath->next; pathportal = pathportal->path_prev;
pathportal = pathportal->path_prev;
} while (pathportal); } while (pathportal);
curpath->next = pathnode_new(ex, ey); curpath->next = pathnode_new(ex, ey);
curpath->next->next = NULL;

return pathstart.next; return pathstart.next;
} }


Expand Down
3 changes: 2 additions & 1 deletion path.h
Expand Up @@ -50,11 +50,12 @@ struct open_s {
struct pathnode_s { struct pathnode_s {
int x; int x;
int y; int y;
int dist;
pathnode_t *next; pathnode_t *next;
}; };


pathnode_t *finder_find(pathfinder_t *finder, map_t *map, int sx, int sy, int ex, int ey); pathnode_t *finder_find(pathfinder_t *finder, map_t *map, int sx, int sy, int ex, int ey);
pathnode_t *pathnode_new(int x, int y);

void path_delete(pathnode_t *path); void path_delete(pathnode_t *path);


void finder_init (pathfinder_t *finder); void finder_init (pathfinder_t *finder);
Expand Down

0 comments on commit fe75038

Please sign in to comment.