int rts_build(th_obj *obj, int type, th_point point) { list_node *node; th_path *path; th_point source; th_point tmp_point; th_point extra_point; th_obj *obj_template; th_obj new_obj; int action; if(type >= NUM_OBJECTS) { printf("I can't build this object, Object not in list!\n"); return 0; } if(!obj) { printf("rts_goto error: object invalid!\n"); return 0; } if(obj->name_enum != VILLAGER_MILKMAID) { return 0; } action = BUILD; node = list_nodes; do{ if(node->obj.x == point.x && node->obj.y == point.y) { printf("A object is on the goal tile\n"); return 0; } node = node->next; }while(node); extra_point.x = obj->x; extra_point.y = obj->y; tmp_point = point; point = ai_alternative_tile(extra_point, point); if(tmp_point.x == -1 && tmp_point.y == -1) return 0; // Creating new object: Extract the object tempalte from object hash // modify this object and add it to the object list with CONSTRUCTION // state. printf("Searching for object template %s\n", object_names[type]); obj_template = hashtable_lookup(objects_hash, object_names[type]); if(!obj_template) return 0; printf("\nObject found, addint object to list.\n"); new_obj = *obj_template; //if(obj->player <= num_of_players) //{ if( player_vars[obj->player].wood - new_obj.cost[REC_WOOD] > 0 && player_vars[obj->player].food - new_obj.cost[REC_FOOD] > 0 && player_vars[obj->player].gold - new_obj.cost[REC_GOLD] > 0 && player_vars[obj->player].stone - new_obj.cost[REC_STONE] > 0 ) { player_vars[obj->player].wood = player_vars[obj->player].wood - new_obj.cost[REC_WOOD]; player_vars[obj->player].food = player_vars[obj->player].food - new_obj.cost[REC_FOOD]; player_vars[obj->player].gold = player_vars[obj->player].gold - new_obj.cost[REC_GOLD]; player_vars[obj->player].stone = player_vars[obj->player].stone - new_obj.cost[REC_STONE]; } else { printf("Not enoght resources! You need wood: %d food: %d gold: %d stone: %d\n", new_obj.cost[REC_WOOD], new_obj.cost[REC_FOOD], new_obj.cost[REC_GOLD], new_obj.cost[REC_STONE]); return 0; } //} new_obj.id = object_counter; new_obj.player = obj->player; new_obj.x = tmp_point.x; new_obj.y = tmp_point.y; ai_modify_state(obj->player, &new_obj, CONSTRUCTION); list_add(&list_nodes, new_obj); object_counter++; // Find the shortes path to a close tile, and chanche the object // state to build source.x = obj->x; source.y = obj->y; if(!(path = ai_shortes_path(obj->player,obj->type, source, point))) { printf("No shortes path found or a error ocurred!\n"); return 1; } node = list_nodes; do{ if(node->obj.x == tmp_point.x && node->obj.y == tmp_point.y) { obj_template = &(node->obj); break; } node = node->next; }while(node); obj->state.target_point = point; obj->state.target_obj = obj_template; obj->state.path = path; ai_modify_state(obj->player, obj, action); return 1; }
int main (int argc, char **argv) { apoint source, goal; aobj *astar; int i, x, y; char *output = "astar.bmp"; if (argc != 8) { init_error(); return 0; } source.x = atoi(argv[1]); source.y = atoi(argv[2]); goal.x = atoi(argv[3]); goal.y = atoi(argv[4]); debug = atoi(argv[7]); astar = malloc(sizeof(aobj)); if(astar == NULL) return 0; astar->path.size = 0; astar->path.path = NULL; astar->output = malloc(20*sizeof(char)); sprintf(astar->output, "%s", argv[6]); //printf("Archivo de salida: %s\n", argv[6]); if(!read_map(argv[5])) { printf("Error al cargar %s. Debe ser una imagen ", argv[5]); printf("BMP sin compresión de 24 bits.\n"); return 0; } //maps_init(map1); astar->cache = malloc(x_tildes * sizeof(anode *)); if(!(astar->cache)) return 0; for(i=0; i < x_tildes; i++) { astar->cache[i] = malloc(y_tildes * sizeof(anode)); if(!(astar->cache[i])) return 0; } for(x=0; x<x_tildes; x++) for(y=0; y<y_tildes; y++) astar->cache[x][y].pointer = NULL; astar->open_empty = 0; astar->closed_empty = 0; if(!ai_shortes_path(astar, 0, source, goal)) { printf("We couldn't find any path to the goal!\n"); } if( astar->path.size == 0) { printf("Error: Couldnt find a path!\n"); clean_gmaps(); return 0; } data_store(astar); ai_free_aobj(astar); clean_gmaps(); return 0; }
int rts_goto(th_obj *obj, th_point point) { list_node *node; th_path *path; th_point source; th_point tmp_point, extra_point; int action; if(!obj) { printf("rts_goto error: object invalid!\n"); return 0; } if(obj->type != UNIT) { return 0; } action = GOTO; node = list_nodes; do{ if(node->obj.x == point.x && node->obj.y == point.y) { tmp_point.x = -1; tmp_point.y = -1; printf("A object is on the goal tile\n"); extra_point.x = obj->x; extra_point.y = obj->y; tmp_point = point; point = ai_alternative_tile(extra_point, point); if(tmp_point.x == -1 && tmp_point.y == -1) return 0; printf("Finding a new action!\n"); if(node->obj.player != human_player) { if(node->obj.type == BUILDING || node->obj.type == UNIT) { obj->state.target_point = tmp_point; obj->state.target_obj = &(node->obj); action = ATTACK; } else if(node->obj.type == FOREST || node->obj.type == GOLD || node->obj.type == STONE ) { obj->state.target_point = tmp_point; obj->state.rec_point = tmp_point; obj->state.rec_point_flag = 1; obj->state.target_obj = &(node->obj); action = USE; } } else { if(node->obj.type == BUILDING && obj->name_enum == VILLAGER_MILKMAID) { if(node->obj.state.state == CONSTRUCTION) { obj->state.target_point = tmp_point; obj->state.target_obj = &(node->obj); obj->state.rec_point = tmp_point; obj->state.rec_point_flag = 1; action = BUILD; } else if(node->obj.name_enum == VILLAGE_CENTER && obj->state.carrying > 0) { obj->state.target_point = tmp_point; obj->state.target_obj = &(node->obj); action = STORE; } else if(node->obj.name_enum == FARM) { obj->state.target_point = tmp_point; obj->state.target_obj = &(node->obj); obj->state.rec_point = tmp_point; obj->state.rec_point_flag = 1; action = USE; } else { obj->state.target_point = tmp_point; obj->state.target_obj = &(node->obj); action = REPAIR; } } } break; } node = node->next; }while(node); printf("Change %s state: go from (%d,%d) to (%d,%d)\n", obj->rname, obj->x, obj->y, point.x, point.y); source.x = obj->x; source.y = obj->y; if(!(path = ai_shortes_path(obj->player,obj->type,source, point))) { printf("No shortes path found or a error ocurred!\n"); return 1; } obj->state.path = path; ai_modify_state(obj->player, obj, action); printf("Path found!\n"); return 1; }