/******************************************** return the type of the tile on map layer at x,y Returned string MUST BE FREED ********************************************/ char * map_get_tile_type(const char * map, int layer, int x, int y) { char * map_type = nullptr; int width; int height; char layer_name[SMALL_BUF]; if (entry_read_int(MAP_TABLE, map, &width, MAP_KEY_WIDTH, nullptr) == RET_NOK) { return nullptr; } if (entry_read_int(MAP_TABLE, map, &height, MAP_KEY_HEIGHT, nullptr) == RET_NOK) { return nullptr; } if (x < 0 || y < 0 || x >= width || y >= height) { return nullptr; } sprintf(layer_name, "%s%d", MAP_KEY_LAYER, layer); entry_read_list_index(MAP_TABLE, map, &map_type, (width * y) + x, layer_name, MAP_KEY_TYPE, nullptr); if (map_type != nullptr) { return map_type; } return map_type; }
/********************************** Show tiles types **********************************/ static void compose_type(context_t * ctx,int layer_index) { int x = 0; int y = 0; item_t * item; char * type; static TTF_Font * font = NULL; int w; int h; char layer_name[SMALL_BUF]; if( option->show_tile_type == false) { return; } sprintf(layer_name,"%s%d",MAP_KEY_LAYER,layer_index); if( entry_exist(MAP_TABLE, ctx->map, layer_name,MAP_KEY_TYPE,NULL) == false ) { return; } font = font_get(ctx,ITEM_FONT, ITEM_FONT_SIZE); for( x=0; x<default_layer->map_w; x++) { for( y=0; y<default_layer->map_h; y++) { if(entry_read_list_index(MAP_TABLE,ctx->map,&type,x + y * default_layer->map_w,layer_name,MAP_KEY_TYPE,NULL) == RET_NOK ) { continue; } if( type[0] == 0 ) { continue; } item = item_list_add(&item_list); item_set_string(item,type); item_set_font(item,font); sdl_get_string_size(item->font,item->string,&w,&h); item_set_anim_shape(item,map_t2p_x(x,y,default_layer),map_t2p_y(x,y,default_layer),w,h); } } }
/*********************************** Write a new tile type into a map file return RET_NOK if fails ***********************************/ ret_code_t map_set_tile_type(const char * map, int layer, const char * type, int x, int y, int network_broadcast) { char * previous_type = nullptr; int width = -1; int index; char layer_name[SMALL_BUF]; if (map == nullptr || type == nullptr) { return RET_NOK; } if (x < 0 || y < 0) { return RET_NOK; } sprintf(layer_name, "%s%d", MAP_KEY_LAYER, layer); // Manage concurrent access to map files SDL_LockMutex(map_mutex); // read size of map grid if (entry_read_int(MAP_TABLE, map, &width, MAP_KEY_WIDTH, nullptr) == RET_NOK) { SDL_UnlockMutex(map_mutex); return RET_NOK; } index = width * y + x; // read previous map type if (entry_read_list_index(MAP_TABLE, map, &previous_type, index, layer_name, MAP_KEY_TYPE, nullptr) == RET_OK) { /* Do not change the type if it already the requested type Avoid calling useless context_broadcast_file */ if (strcmp(previous_type, type) == 0) { free(previous_type); SDL_UnlockMutex(map_mutex); return RET_OK; } free(previous_type); } if (entry_write_list_index(MAP_TABLE, map, type, index, layer_name, MAP_KEY_TYPE, nullptr) == RET_OK) { if (network_broadcast) { context_broadcast_map(map); } } SDL_UnlockMutex(map_mutex); return RET_OK; }