static mapNode *map_find_node(mapNode *node, const char *key) { // base case if(!node) return NULL; int result = strcmp(node->key, key); if(result == 0) return node; else if(result < 0) return map_find_node(node->left, key); else return map_find_node(node->right, key); }
void map_set(map *map, const char *key, void *element) { mapNode *node; if(map_contains_key(map, key)) { node = map_find_node(map->root, key); memcpy(node->value, element, map->elementSize); return; } node = malloc(sizeof(mapNode)); node->left = node->right = NULL; node->key = strdup(key); node->value = malloc(map->elementSize); memcpy(node->value, element, map->elementSize); map->logicalLength++; if(!map->root) { map->root = node; map->root->left = NULL; map->root->right = NULL; } else { map_insert_node(map->root, node); } }
void map_get(map *map, const char *key, void *element) { mapNode *node = map_find_node(map->root, key); assert(node != NULL); memcpy(element, node->value, map->elementSize); }
int map_find(map * m, void *key, void **value) { map_node *p = map_find_node(m, key); if (!p) return 0; *value = p->value; return 1; }
bool object_auto_walk_node_name_setup(obj_type *obj,char *start_node,char *end_node,int event_id) { int from_idx,to_idx; // get the nodes from_idx=map_find_node(&map,start_node); if (from_idx==-1) { JS_ReportError(js.cx,"Named node does not exist: %s",start_node); return(FALSE); } to_idx=map_find_node(&map,end_node); if (to_idx==-1) { JS_ReportError(js.cx,"Named node does not exist: %s",end_node); return(FALSE); } return(object_auto_walk_node_setup(obj,from_idx,to_idx,event_id)); }
bool camera_walk_to_node_setup(char *start_node,char *end_node,int msec,int event_id,bool open_doors,bool in_freeze) { int from_idx,to_idx,dist; float speed; obj_type *player_obj; // only works in static camera if (camera.mode!=cv_static) { JS_ReportError(js.cx,"Can only walk cameras in static mode"); return(FALSE); } // get the nodes from_idx=map_find_node(&map,start_node); if (from_idx==-1) { JS_ReportError(js.cx,"Named node does not exist: %s",start_node); return(FALSE); } to_idx=map_find_node(&map,end_node); if (to_idx==-1) { JS_ReportError(js.cx,"Named node does not exist: %s",end_node); return(FALSE); } // is end node in start node path? if (map_find_next_node_in_path(&map,from_idx,to_idx)==-1) { JS_ReportError(js.cx,"End node '%s' is not in the same path as the start node '%s'",end_node,start_node); return(FALSE); } // get total distance dist=map_node_to_node_distance(&map,from_idx,to_idx); if (dist==0) { JS_ReportError(js.cx,"Camera walk covers no distance"); return(FALSE); } speed=(float)dist/(float)(msec/10); // start at the start node and seek to the next node memmove(&camera_static_pnt,&map.nodes[from_idx].pnt,sizeof(d3pnt)); memmove(&camera_static_walk_ang,&map.nodes[from_idx].ang,sizeof(d3ang)); from_idx=map_find_next_node_in_path(&map,from_idx,to_idx); if (from_idx==-1) return(TRUE); // setup seeking camera.auto_walk.on=TRUE; camera.auto_walk.open_doors=open_doors; camera.auto_walk.in_freeze=in_freeze; camera.auto_walk.node_seek_idx=from_idx; camera.auto_walk.node_dest_idx=to_idx; camera.auto_walk.msec=msec; camera.auto_walk.speed=speed; camera.auto_walk.event_id=event_id; // player freeze if (in_freeze) { player_obj=object_find_uid(server.player_obj_uid); object_input_freeze(player_obj,TRUE); } return(TRUE); }
int map_delete(map * m, void *key) { map_node *p = map_find_node(m, key); return map_delete_node(p); }
bool map_contains_key(map *map, const char *key) { mapNode *node = map_find_node(map->root, key); return (node == NULL) ? FALSE : TRUE; }