int TCOD_namegen_get_nb_sets_wrapper() { TCOD_list_t l=TCOD_namegen_get_sets(); int nb = TCOD_list_size(l); TCOD_list_delete(l); return nb; }
int TCOD_dijkstra_size(TCOD_dijkstra_t p) { dijkstra_t * data = (dijkstra_t*)p; TCOD_IFNOT(data != NULL) return 0; return TCOD_list_size(data->path); }
int TCOD_path_size(TCOD_path_t p) { TCOD_path_data_t *path=(TCOD_path_data_t *)p; TCOD_IFNOT(p != NULL) return 0; return TCOD_list_size(path->path); }
/* this is the slow part, when we change the heuristic of a cell already in the heap */ static void heap_reorder(TCOD_path_data_t *path, uint32 offset) { uintptr *array=(uintptr *)TCOD_list_begin(path->heap); uintptr *end=(uintptr *)TCOD_list_end(path->heap); uintptr *cur=array; uintptr off_idx=0; float value; int idx=0; int heap_size=TCOD_list_size(path->heap); /* find the node corresponding to offset ... SLOW !! */ while (cur != end) { if (*cur == offset ) break; cur++;idx++; } if ( cur == end ) return; off_idx=array[idx]; value=path->heur[off_idx]; if ( idx > 0 ) { int parent=(idx-1)/2; /* compare to its parent */ uintptr off_parent=array[parent]; float parent_value=path->heur[off_parent]; if (value < parent_value) { /* smaller. bubble it up */ while ( idx > 0 && value < parent_value ) { /* swap with parent */ array[parent]=off_idx; array[idx] = off_parent; idx=parent; if ( idx > 0 ) { parent=(idx-1)/2; off_parent=array[parent]; parent_value=path->heur[off_parent]; } } return; } } /* compare to its sons */ while ( idx*2+1 < heap_size ) { int child=idx*2+1; uintptr off_child=array[child]; int toSwap=idx; int child2; float swapValue=value; if ( path->heur[off_child] < value ) { /* swap with son1 ? */ toSwap=child; swapValue=path->heur[off_child]; } child2 = child+1; if ( child2 < heap_size ) { uintptr off_child2=array[child2]; if ( path->heur[off_child2] < swapValue) { /* swap with son2 */ toSwap=child2; } } if ( toSwap != idx ) { /* bigger. bubble it down */ uintptr tmp = array[toSwap]; array[toSwap]=array[idx]; array[idx] = tmp; idx=toSwap; } else return; } }
/* generate a name using a given generation rule */ char * TCOD_namegen_generate_custom (char * name, char * rule, bool allocate) { namegen_t * data; size_t buflen = 1024; char * buf ; size_t rule_len ; if (namegen_generator_check(name)) data = namegen_generator_get(name); else { fprintf(stderr,"The name \"%s\" has not been found.\n",name); namegen_get_sets_on_error(); return NULL; } buf = malloc(buflen); rule_len = strlen(rule); /* let the show begin! */ do { char * it = rule; memset(buf,'\0',buflen); while (it <= rule + rule_len) { /* make sure the buffer is large enough */ if (strlen(buf) >= buflen) { char * tmp ; while (strlen(buf) >= buflen) buflen *= 2; tmp = malloc(buflen); strcpy(tmp,buf); free(buf); buf = tmp; } /* append a normal character */ if ((*it >= 'a' && *it <= 'z') || (*it >= 'A' && *it <= 'Z') || *it == '\'' || *it == '-') strncat(buf,it,1); /* special character */ else if (*it == '/') { it++; strncat(buf,it,1); } /* underscore is converted to space */ else if (*it == '_') strcat(buf," "); /* interpret a wildcard */ else if (*it == '$') { int chance = 100; it++; /* food for the randomiser */ if (*it >= '0' && *it <= '9') { chance = 0; while (*it >= '0' && *it <= '9') { chance *= 10; chance += (int)(*it) - (int)('0'); it++; } } /* ok, so the chance of wildcard occurrence is calculated, now evaluate it */ if (chance >= TCOD_random_get_int(data->random,0,100)) { TCOD_list_t lst; switch (*it) { case 'P': lst = data->syllables_pre; break; case 's': lst = data->syllables_start; break; case 'm': lst = data->syllables_middle; break; case 'e': lst = data->syllables_end; break; case 'p': lst = data->syllables_post; break; case 'v': lst = data->vocals; break; case 'c': lst = data->consonants; break; case '?': lst = (TCOD_random_get_int(data->random,0,1) == 0 ? data->vocals : data->consonants); break; default: fprintf(stderr,"Wrong rules syntax encountered!\n"); exit(1); break; } if (TCOD_list_size(lst) == 0) fprintf(stderr,"No data found in the requested string (wildcard *%c). Check your name generation rule %s.\n",*it,rule); else strcat(buf,(char*)TCOD_list_get(lst,TCOD_random_get_int(data->random,0,TCOD_list_size(lst)-1))); } } it++; } } while (!namegen_word_is_ok(data,buf)); /* prune the spare spaces out */ namegen_word_prune_spaces(buf); /* return the name accordingly */ if (allocate == true) return buf; else { /* take care of ensuring the recipient is sized properly */ if (namegen_name == NULL) { namegen_name_size = 64; namegen_name = malloc (namegen_name_size); } while (strlen(buf) > namegen_name_size - 1) { namegen_name_size *= 2; free(namegen_name); namegen_name = malloc(namegen_name_size); } strcpy(namegen_name,buf); free(buf); return namegen_name; } }
void TCOD_map_compute_fov_diamond_raycasting(TCOD_map_t map, int player_x, int player_y, int max_radius, bool light_walls) { map_t *m = (map_t *)map; TCOD_list_t perim=TCOD_list_allocate(m->nbcells); cell_t *c; ray_data_t **r; int nbcells; int r2=max_radius*max_radius; perimidx=0; raymap=(ray_data_t **)calloc(sizeof(ray_data_t*),m->nbcells); raymap2=(ray_data_t *)calloc(sizeof(ray_data_t),m->nbcells); origx=player_x; origy=player_y; expandPerimeterFrom(m,perim,new_ray(m,0,0)); while ( perimidx < TCOD_list_size(perim) ) { ray_data_t *ray=(ray_data_t *)TCOD_list_get(perim,perimidx); int distance = 0; if ( r2 > 0 ) distance = ((ray->xloc * ray->xloc) + (ray->yloc * ray->yloc)); perimidx++; if ( distance <= r2) { merge_input(m, ray); if ( !ray->ignore ) expandPerimeterFrom(m,perim,ray); } else ray->ignore=true; } // set fov data c=m->cells; r=raymap; nbcells=m->nbcells; while ( nbcells!= 0 ) { if ( *r == NULL || (*r)->ignore || ((*r)->xerr > 0 && (*r)->xerr <= (*r)->xob ) || ((*r)->yerr > 0 && (*r)->yerr <= (*r)->yob ) ) { c->fov=0; } else { c->fov=1; } c++; r++; nbcells--; } m->cells[origx+origy*m->width].fov=1; // light walls if ( light_walls ) { int xmin=0, ymin=0, xmax=m->width, ymax=m->height; if ( max_radius > 0 ) { xmin=MAX(0,player_x-max_radius); ymin=MAX(0,player_y-max_radius); xmax=MIN(m->width,player_x+max_radius+1); ymax=MIN(m->height,player_y+max_radius+1); } TCOD_map_postproc(m,xmin,ymin,player_x,player_y,-1,-1); TCOD_map_postproc(m,player_x,ymin,xmax-1,player_y,1,-1); TCOD_map_postproc(m,xmin,player_y,player_x,ymax-1,-1,1); TCOD_map_postproc(m,player_x,player_y,xmax-1,ymax-1,1,1); } free(raymap); free(raymap2); TCOD_list_delete(perim); }