Example #1
0
/**********************************
Draw the "list" keyword of a layer
**********************************/
static void compose_map_scenery(context_t * ctx, int layer_index)
{
	int i = 0;
	int x = 0;
	int y = 0;
	char * image_name = NULL;
	anim_t * anim;
	item_t * item;
	char ** scenery_list = NULL;
	char layer_name[SMALL_BUF];

	sprintf(layer_name,"%s%d",MAP_KEY_LAYER,layer_index);
	if(entry_get_group_list(MAP_TABLE, ctx->map, &scenery_list,layer_name,MAP_KEY_SCENERY,NULL) == RET_NOK ) {
		return;
	}

	while(scenery_list[i] != NULL ) {
		if(entry_read_int(MAP_TABLE, ctx->map, &x,layer_name,MAP_KEY_SCENERY,scenery_list[i],MAP_KEY_SCENERY_X,NULL) == RET_NOK ) {
			i++;
			continue;
		}
		if(entry_read_int(MAP_TABLE, ctx->map, &y,layer_name,MAP_KEY_SCENERY,scenery_list[i],MAP_KEY_SCENERY_Y,NULL) == RET_NOK ) {
			i++;
			continue;
		}
		if(entry_read_string(MAP_TABLE, ctx->map, &image_name,layer_name,MAP_KEY_SCENERY,scenery_list[i],MAP_KEY_SCENERY_IMAGE,NULL) == RET_NOK ) {
			i++;
			continue;
		}

		anim = imageDB_get_anim(ctx,image_name);

		item = item_list_add(&item_list);
		item_set_pos(item, x, y);
		item_set_anim(item,anim,0);
		//item_set_anim(item, x*ctx->tile_width, y*ctx->tile_height, anim,0);

		i++;
	}

	deep_free(scenery_list);
}
Example #2
0
/**********************************************
 Delete an event on map at given coordinate
 return RET_NOK if fails
 **********************************************/
ret_code_t map_delete_event(const char * map, int layer, const char * script,
		int x, int y)
{
	char ** eventlist;
	int i = 0;
	int mapx;
	int mapy;
	char * map_script = nullptr;
	const char * id = nullptr;
	char layer_name[SMALL_BUF];

	if (x < 0 || y < 0)
	{
		return RET_NOK;
	}

	sprintf(layer_name, "%s%d", MAP_KEY_LAYER, layer);

	/* Manage concurrent acces to map files */
	SDL_LockMutex(map_mutex);
	/* Search events on the specified tile */
	if (entry_get_group_list(MAP_TABLE, map, &eventlist, layer_name,
			MAP_ENTRY_EVENT_LIST, nullptr) == RET_NOK)
	{
		SDL_UnlockMutex(map_mutex);
		return RET_NOK;
	}

	while (eventlist[i] != nullptr)
	{
		if (entry_read_int(MAP_TABLE, map, &mapx, layer_name,
				MAP_ENTRY_EVENT_LIST, eventlist[i], MAP_EVENT_POS_X,
				nullptr) == RET_NOK)
		{
			i++;
			continue;
		}
		if (entry_read_int(MAP_TABLE, map, &mapy, layer_name,
				MAP_ENTRY_EVENT_LIST, eventlist[i], MAP_EVENT_POS_Y,
				nullptr) == RET_NOK)
		{
			i++;
			continue;
		}
		if (entry_read_string(MAP_TABLE, map, &map_script, layer_name,
				MAP_ENTRY_EVENT_LIST, eventlist[i], MAP_EVENT_SCRIPT,
				nullptr) == RET_NOK)
		{
			i++;
			continue;
		}
		if (x == mapx && y == mapy && !strcmp(map_script, script))
		{
			id = eventlist[i];
			free(map_script);
			break;
		}
		free(map_script);
		i++;
	}

	if (id == nullptr)
	{
		SDL_UnlockMutex(map_mutex);
		return RET_NOK;
	}

	/* remove the event from the events list of the map */
	if (entry_remove_group(MAP_TABLE, map, id, layer_name, MAP_ENTRY_EVENT_LIST,
			nullptr) == RET_NOK)
	{
		SDL_UnlockMutex(map_mutex);
		return RET_NOK;
	}

	deep_free(eventlist);

	SDL_UnlockMutex(map_mutex);

	/* Send network notifications */
	context_broadcast_map(map);

	return RET_OK;
}
Example #3
0
/**************************************
 delete an item on context's map
 **************************************/
char * map_delete_item(const char * map, int layer, int x, int y)
{
	char ** itemlist;
	int i = 0;
	int mapx;
	int mapy;
	const char * id = nullptr;
	char * saved_item = nullptr;
	char layer_name[SMALL_BUF];

	if (x < 0 || y < 0)
	{
		return nullptr;
	}

	sprintf(layer_name, "%s%d", MAP_KEY_LAYER, layer);
	/* Manage concurrent acces to map files */
	SDL_LockMutex(map_mutex);
	/* Search the items on the specified tile */
	if (entry_get_group_list(MAP_TABLE, map, &itemlist, layer_name,
			MAP_ENTRY_ITEM_LIST, nullptr) == RET_NOK)
	{
		SDL_UnlockMutex(map_mutex);
		return nullptr;
	}

	while (itemlist[i] != nullptr)
	{
		if (entry_read_int(MAP_TABLE, map, &mapx, layer_name,
				MAP_ENTRY_ITEM_LIST, itemlist[i], MAP_ITEM_POS_X,
				nullptr) == RET_NOK)
		{
			SDL_UnlockMutex(map_mutex);
			deep_free(itemlist);
			return nullptr;
		}

		if (entry_read_int(MAP_TABLE, map, &mapy, layer_name,
				MAP_ENTRY_ITEM_LIST, itemlist[i], MAP_ITEM_POS_Y,
				nullptr) == RET_NOK)
		{
			SDL_UnlockMutex(map_mutex);
			deep_free(itemlist);
			return nullptr;
		}

		if (x == mapx && y == mapy)
		{
			id = itemlist[i];
			saved_item = strdup(itemlist[i]);
			break;
		}

		i++;
	}

	if (id == nullptr)
	{
		deep_free(itemlist);
		if (saved_item)
		{
			free(saved_item);
		}
		SDL_UnlockMutex(map_mutex);
		return nullptr;
	}

	/* remove the item from the item list of the map */
	if (entry_remove_group(MAP_TABLE, map, id, layer_name, MAP_ENTRY_ITEM_LIST,
			nullptr) == RET_NOK)
	{
		deep_free(itemlist);
		free(saved_item);
		SDL_UnlockMutex(map_mutex);
		return nullptr;
	}

	deep_free(itemlist);

	SDL_UnlockMutex(map_mutex);

	/* Send network notifications */
	context_broadcast_map(map);

	return saved_item;
}
Example #4
0
/**********************************
Compose item on map
**********************************/
static void compose_item(context_t * ctx,int layer_index)
{
	char * sprite_name = NULL;
	int sprite_align = ALIGN_CENTER;
	int sprite_offset_y = 0;
	anim_t * anim;
	item_t * item;
	int x;
	int y;
	int temp_x;
	int temp_y;
	char ** item_id;
	int i;
	static TTF_Font * font = NULL;
	char * mytemplate;
	int quantity;
	char buf[SMALL_BUF];
	char layer_name[SMALL_BUF];

	sprintf(layer_name,"%s%d",MAP_KEY_LAYER,layer_index);

	if(entry_get_group_list(MAP_TABLE,ctx->map,&item_id,layer_name,MAP_ENTRY_ITEM_LIST,NULL) == RET_NOK ) {
		return;
	}

	font = font_get(ctx,ITEM_FONT, ITEM_FONT_SIZE);

	i=0;
	while( item_id[i] != NULL ) {
		sprite_align = ALIGN_CENTER;

		if(entry_read_int(MAP_TABLE,ctx->map,&x,layer_name,MAP_ENTRY_ITEM_LIST,item_id[i],MAP_ITEM_POS_X,NULL) == RET_NOK ) {
			i++;
			continue;
		}

		if(entry_read_int(MAP_TABLE,ctx->map,&y,layer_name,MAP_ENTRY_ITEM_LIST,item_id[i],MAP_ITEM_POS_Y,NULL) == RET_NOK ) {
			i++;
			continue;
		}

		mytemplate = item_is_resource(item_id[i]);

		if ( mytemplate == NULL ) {
			if(entry_read_string(ITEM_TABLE,item_id[i],&sprite_name,ITEM_SPRITE,NULL) == RET_NOK ) {
				i++;
				continue;
			}
			entry_read_int(ITEM_TABLE,item_id[i],&sprite_align,ITEM_ALIGN,NULL);
			entry_read_int(ITEM_TABLE,item_id[i],&sprite_offset_y,ITEM_OFFSET_Y,NULL);
		} else {
			if(entry_read_string(ITEM_TEMPLATE_TABLE,mytemplate,&sprite_name,ITEM_SPRITE,NULL) == RET_NOK ) {
				free(mytemplate);
				i++;
				continue;
			}
			entry_read_int(ITEM_TEMPLATE_TABLE,mytemplate,&sprite_align,ITEM_ALIGN,NULL);
			entry_read_int(ITEM_TEMPLATE_TABLE,mytemplate,&sprite_offset_y,ITEM_OFFSET_Y,NULL);
			free(mytemplate);
		}

		item = item_list_add(&item_list);

		anim = imageDB_get_anim(ctx,sprite_name);
		free(sprite_name);

		temp_x = map_t2p_x(x,y,default_layer);
		temp_y = map_t2p_y(x,y,default_layer);
		x = temp_x;
		y = temp_y;
		/* Align on tile */
		if( sprite_align == ALIGN_CENTER ) {
			x -= ((anim->w*default_layer->map_zoom)-default_layer->tile_width)/2;
			y -= ((anim->h*default_layer->map_zoom)-default_layer->tile_height)/2;
		}
		if( sprite_align == ALIGN_LOWER ) {
			x -= ((anim->w*default_layer->map_zoom)-default_layer->tile_width)/2;
			y -= (anim->h*default_layer->map_zoom)-default_layer->tile_height;
		}

		y += sprite_offset_y;

		item_set_pos(item,x,y);
		item_set_anim(item,anim,0);
		item_set_zoom_x(item, default_layer->map_zoom );
		item_set_zoom_y(item, default_layer->map_zoom );
		if(font) {
			quantity = resource_get_quantity(item_id[i]);
			sprintf(buf,"%d",quantity);
			item_set_string(item,buf);
			item_set_font(item,font);
		}

		i++;
	}

	deep_free(item_id);
}