bool TCOD_path_compute(TCOD_path_t p, int ox,int oy, int dx, int dy) { TCOD_path_data_t *path=(TCOD_path_data_t *)p; TCOD_IFNOT(p != NULL) return false; path->ox=ox; path->oy=oy; path->dx=dx; path->dy=dy; TCOD_list_clear(path->path); TCOD_list_clear(path->heap); if ( ox == dx && oy == dy ) return true; /* trivial case */ /* check that origin and destination are inside the map */ TCOD_IFNOT((unsigned)ox < (unsigned)path->w && (unsigned)oy < (unsigned)path->h) return false; TCOD_IFNOT((unsigned)dx < (unsigned)path->w && (unsigned)dy < (unsigned)path->h) return false; /* initialize djikstra grids */ memset(path->grid,0,sizeof(float)*path->w*path->h); memset(path->prev,NONE,sizeof(dir_t)*path->w*path->h); path->heur[ ox + oy * path->w ] = 1.0f; /* anything != 0 */ TCOD_path_push_cell(path,ox,oy); /* put the origin cell as a bootstrap */ /* fill the djikstra grid until we reach dx,dy */ TCOD_path_set_cells(path); if ( path->grid[dx + dy * path->w] == 0 ) return false; /* no path found */ /* there is a path. retrieve it */ do { /* walk from destination to origin, using the 'prev' array */ int step=path->prev[ dx + dy * path->w ]; TCOD_list_push(path->path,(void *)(uintptr)step); dx -= dirx[step]; dy -= diry[step]; } while ( dx != ox || dy != oy ); return true; }
/* delete a Dijkstra object */ void TCOD_dijkstra_delete (TCOD_dijkstra_t dijkstra) { dijkstra_t * data = (dijkstra_t*)dijkstra; TCOD_IFNOT(data != NULL) return; if ( data->distances ) free(data->distances); if ( data->nodes ) free(data->nodes); if ( data->path ) TCOD_list_clear(data->path); free(data); }
/* delete all the generators */ void TCOD_namegen_destroy (void) { /* delete all generators */ namegen_t ** it; for (it = (namegen_t**)TCOD_list_begin(namegen_generators_list); it < (namegen_t**)TCOD_list_end(namegen_generators_list); it++) namegen_generator_delete(*it); /* clear the generators list */ TCOD_list_clear(namegen_generators_list); /* get rid of the parsed files list */ TCOD_list_clear_and_delete(parsed_files); }
void sensor_sense(Sensor s) { Map m = s->map; mapVec pos=s->borig, sz=s->bsz; memset(s->vistiles, 0, sz.x*sz.y*sz.z*sizeof(perception)); map_get_visible_tiles(m, s->vistiles, s->volume, pos, sz); Stimulus vistiles = stimulus_init_tile_vis_change(stimulus_new(), s->vistiles, pos, sz); // TCOD_console_print_left(NULL, 0, 20, "p {%f, %f, %f}, s {%f, %f, %f}", pos.x, pos.y, pos.z, sz.x, sz.y, sz.z); TCOD_list_push(s->stimuli, vistiles); TCOD_list_t oldObjList = s->visObjects; //one ago s->visObjects = s->oldVisObjects; //two ago TCOD_list_clear(s->visObjects); s->oldVisObjects = oldObjList; map_get_visible_objects(m, s->visObjects, s->vistiles, pos, sz); //remove the old objects Object o; mapVec pt; int index; Stimulus visobj; for(int i = 0; i < TCOD_list_size(s->oldVisObjects); i++) { o = TCOD_list_get(s->oldVisObjects, i); if(!TCOD_list_contains(s->visObjects, o)) { //not visible anymore //this isn't quite right, really, using percept_none here visobj = stimulus_init_obj_vis_change(stimulus_new(), o, percept_none, object_context(o)); TCOD_list_push(s->stimuli, visobj); } } //add the new objects for(int i = 0; i < TCOD_list_size(s->visObjects); i++) { o = TCOD_list_get(s->visObjects, i); if(!TCOD_list_contains(s->oldVisObjects, o)) { //not visible before pt = object_position(o); index = tile_index(pt.x, pt.y, pt.z, map_size(m), pos, sz); visobj = stimulus_init_obj_vis_change(stimulus_new(), o, s->vistiles[index], object_context(o)); TCOD_list_push(s->stimuli, visobj); } } }
/* create a path */ bool TCOD_dijkstra_path_set (TCOD_dijkstra_t dijkstra, int x, int y) { dijkstra_t * data = (dijkstra_t*)dijkstra; int px = x, py = y; static int dx[9] = { -1, 0, 1, 0, -1, 1, 1, -1, 0 }; static int dy[9] = { 0, -1, 0, 1, -1, -1, 1, 1, 0 }; unsigned int distances[9]; int lowest_index = 666; TCOD_IFNOT(data != NULL) return false; TCOD_IFNOT((unsigned)x < (unsigned)data->width && (unsigned)y < (unsigned)data->height) return false; // check that destination is reachable if ( dijkstra_get_int_distance(data,x,y) == 0xFFFFFFFF ) return false; TCOD_list_clear(data->path); do { unsigned int lowest = 0xFFFFFFFF; int i; TCOD_list_push(data->path,(const void*)(uintptr)((py * data->width) + px)); for(i=0;i<8;i++) { int cx = px + dx[i]; int cy = py + dy[i]; if ((unsigned)cx < (unsigned)data->width && (unsigned)cy < (unsigned)data->height) distances[i] = dijkstra_get_int_distance(data,cx,cy); else distances[i] = 0xFFFFFFFF; } distances[8] = dijkstra_get_int_distance(data,px,py); for(i=0;i<9;i++) if (distances[i] < lowest) { lowest = distances[i]; lowest_index = i; } px += dx[lowest_index]; py += dy[lowest_index]; } while (lowest_index != 8); // remove the last step TCOD_list_pop(data->path); return true; }