Exemplo n.º 1
0
static void execute_aggro(context_t * agressor, context_t * target, char * script, int aggro_dist)
{
	int dist;
	const char * param[] = { NULL,NULL,NULL };

	dist = context_distance(agressor,target);

	if(dist <= aggro_dist) {
		param[1] = "1";
	} else {
		param[1] = "0";
	}

	param[0] = target->id;
	param[2] = NULL;
	action_execute_script(agressor,script,param);

}
Exemplo n.º 2
0
/******************************************************
 return 0 if new position OK or if position has not changed.
 return -1 if the position was not set (because tile not allowed or out of bound)
 ******************************************************/
int character_set_pos(context_t * ctx, const char * map, int x, int y)
{
	char ** event_id;
	char * script;
	char ** param = nullptr;
	int i;
	bool change_map = false;
	int width = x + 1;
	int height = y + 1;
	int warpx = 0;
	int warpy = 0;
	int ctx_layer = 0;
	char layer_name[SMALL_BUF];
	char buf[SMALL_BUF];
	char * coord[3];
	int ret_value;
	int layer;

	if (ctx == nullptr)
	{
		return -1;
	}

	// Do nothing if no move
	if (!strcmp(ctx->map, map) && ctx->pos_tx == x && ctx->pos_ty == y)
	{
		return 0;
	}

	ctx_layer = 0;
	entry_read_int(CHARACTER_TABLE, ctx->id, &ctx_layer, CHARACTER_LAYER,
			nullptr);
	sprintf(layer_name, "%s%d", MAP_KEY_LAYER, ctx_layer);

	entry_read_int(MAP_TABLE, map, &width, MAP_KEY_WIDTH, nullptr);
	entry_read_int(MAP_TABLE, map, &height, MAP_KEY_HEIGHT, nullptr);
	entry_read_int(MAP_TABLE, map, &warpx, MAP_KEY_WARP_X, nullptr);
	entry_read_int(MAP_TABLE, map, &warpy, MAP_KEY_WARP_Y, nullptr);

	// Offscreen script
	entry_read_string(MAP_TABLE, map, &script, MAP_OFFSCREEN, nullptr);
	if (script != nullptr && (x < 0 || y < 0 || x >= width || y >= height))
	{
		snprintf(buf, SMALL_BUF, "%d", x);
		coord[0] = strdup(buf);
		snprintf(buf, SMALL_BUF, "%d", y);
		coord[1] = strdup(buf);
		coord[2] = nullptr;

		ret_value = action_execute_script(ctx, script, (const char **) coord);

		free(coord[0]);
		free(coord[1]);
		free(script);

		return ret_value;
	}
	if (script)
	{
		free(script);
	}

	// Coordinates warping
	if (x < 0)
	{
		if (warpy == 0)
		{
			return -1;
		}
		x = width - 1;
	}
	if (y < 0)
	{
		if (warpy == 0)
		{
			return -1;
		}
		y = height - 1;
	}
	if (x >= width)
	{
		if (warpx == 0)
		{
			return -1;
		}
		x = 0;
	}
	if (y >= height)
	{
		if (warpy == 0)
		{
			return -1;
		}
		y = 0;
	}

	// Check if this character is allowed to go to the target tile
	layer = ctx_layer;
	while (layer >= 0)
	{
		ret_value = map_check_tile(ctx, ctx->id, map, layer, x, y);
		/* not allowed */
		if (ret_value == 0)
		{
			return -1;
		}
		/* allowed */
		if (ret_value == 1)
		{
			break;
		}
		layer--;
	}

	if (layer < 0)
	{
		return -1;
	}

	if (strcmp(ctx->map, map))
	{
		change_map = true;
	}

	/* If this character is a platform, move all characters on it */
	platform_move(ctx, map, x, y, change_map);

	do_set_pos(ctx, map, x, y, change_map);

	event_id = map_get_event(map, ctx_layer, x, y);

	if (event_id)
	{
		i = 0;
		while (event_id[i])
		{
			script = nullptr;
			if (entry_read_string(MAP_TABLE, map, &script, layer_name,
			MAP_ENTRY_EVENT_LIST, event_id[i], MAP_EVENT_SCRIPT,
					nullptr) == RET_OK)
			{
				entry_read_list(MAP_TABLE, map, &param, layer_name,
				MAP_ENTRY_EVENT_LIST, event_id[i], MAP_EVENT_PARAM, nullptr);
			}
			else if (entry_read_string(MAP_TABLE, map, &script,
			MAP_ENTRY_EVENT_LIST, event_id[i], MAP_EVENT_SCRIPT,
					nullptr) == RET_OK)
			{
				entry_read_list(MAP_TABLE, map, &param, MAP_ENTRY_EVENT_LIST,
						event_id[i], MAP_EVENT_PARAM, nullptr);
			}

			if (script == nullptr)
			{
				i++;
				continue;
			}

			action_execute_script(ctx, script, (const char **) param);

			free(script);
			deep_free(param);
			param = nullptr;

			i++;
		}
		deep_free(event_id);
	}

	character_update_aggro(ctx);
	return 0;
}
Exemplo n.º 3
0
/***********************************
 check if id is allowed to go on a tile
 return 1 if the context is allowed to go to the tile at coord x,y
 return 0 if the context is NOT allowed to go to the tile at coord x,y
 return RET_NOK on error or no data found
 *************************************/
ret_code_t map_check_tile(context_t * ctx, char * id, const char * map,
		int layer, int x, int y)
{
	char * script;
	char sx[64];
	char sy[64];
	char * param[5];
	int res;
	char * tile_type;
	char ** allowed_tile;
	int i = 0;
	int width = 0;
	int height = 0;

	if (entry_read_int(MAP_TABLE, map, &width, MAP_KEY_WIDTH,
			nullptr) == RET_NOK)
	{
		return RET_NOK;
	}
	if (entry_read_int(MAP_TABLE, map, &height, MAP_KEY_HEIGHT,
			nullptr) == RET_NOK)
	{
		return RET_NOK;
	}

	if (x < 0 || y < 0 || x >= width || y >= height)
	{
		return 0;
	}

	// If there is an allowed_tile_script, run it
	if (entry_read_string(CHARACTER_TABLE, id, &script,
			CHARACTER_KEY_ALLOWED_TILE_SCRIPT, nullptr) == RET_OK)
	{
		param[0] = id;
		param[1] = (char *) map;
		sprintf(sx, "%d", x);
		sprintf(sy, "%d", y);
		param[2] = sx;
		param[3] = sy;
		param[4] = nullptr;
		res = action_execute_script(ctx, script, (const char**) param);
		free(script);
		return res;
	}

	// Read tile at given index on this map
	entry_read_int(CHARACTER_TABLE, id, &layer, CHARACTER_LAYER, nullptr);
	tile_type = get_tile_type_through_layer(map, layer, x, y);

	// Allow tile if no type defined
	if (tile_type == nullptr)
	{
		return 1;
	}

	// Allow tile if its type is empty (i.e. "")
	if (tile_type[0] == 0)
	{
		free(tile_type);
		return 1;
	}

	// If there is allowed_tile list, check it
	if (entry_read_list(CHARACTER_TABLE, id, &allowed_tile,
			CHARACTER_KEY_ALLOWED_TILE, nullptr) == RET_OK)
	{
		i = 0;
		while (allowed_tile[i] != nullptr)
		{
			if (strcmp(allowed_tile[i], tile_type) == 0)
			{
				deep_free(allowed_tile);
				free(tile_type);
				return 1;
			}
			i++;
		}

		deep_free(allowed_tile);
		free(tile_type);
		return 0;
	}

	free(tile_type);
	// Allow all tiles by default
	return 1;
}
Exemplo n.º 4
0
/***************************************************************************
Add the specified value to the specified attribute
Check on max and min are done and call to on_* scripts are done if set
ctx is the context of the source of the attribute change request
id is the id of the target of the change
return -1 if fails
***************************************************************************/
int attribute_change(context_t * context, const char * table, const char * id, const char * attribute, int value)
{
	int current;
	int old;
	int min;
	int max;
	char buf[SMALL_BUF];
	bool do_min_action = false;
	bool do_down_action = false;
	bool do_max_action = false;
	bool do_up_action = false;
	char * action;
	char * min_action = NULL;
	char * down_action = NULL;
	char * max_action = NULL;
	char * up_action = NULL;

	SDL_LockMutex(attribute_mutex);

	if(entry_read_int(table,id,&current,ATTRIBUTE_GROUP,attribute, ATTRIBUTE_CURRENT, NULL) == RET_NOK) {
		SDL_UnlockMutex(attribute_mutex);
		return -1;
	}

	if(entry_read_int(table,id,&min,ATTRIBUTE_GROUP,attribute, ATTRIBUTE_MIN, NULL) == RET_NOK) {
		min = -1;
	}

	if(entry_read_int(table,id,&max,ATTRIBUTE_GROUP,attribute, ATTRIBUTE_MAX, NULL) == RET_NOK) {
		max = -1;
	}

	old = current;
	current = current + value;
	if( min != -1 ) {
		if(current <= min) {
			do_min_action = true;
			current = min;
		}
	}

	if( max != -1 ) {
		if(current >= max) {
			do_max_action = true;
			current = max;
		}
	}

	if(entry_write_int(table,id,current,ATTRIBUTE_GROUP,attribute, ATTRIBUTE_CURRENT, NULL) == RET_NOK ) {
		SDL_UnlockMutex(attribute_mutex);
		return -1;
	}
	if(entry_write_int(table,id,old,ATTRIBUTE_GROUP,attribute, ATTRIBUTE_PREVIOUS, NULL) == RET_NOK ) {
		SDL_UnlockMutex(attribute_mutex);
		return -1;
	}

	// Check automatic actions
	if( value < 0 ) {
		if( do_min_action == true ) {
			if(entry_read_string(table,id,&action,ATTRIBUTE_GROUP,attribute, ATTRIBUTE_ON_MIN, NULL) == RET_NOK ) {
				do_min_action = false;
			} else {
				min_action = action;
			}
		}

		if(entry_read_string(table,id,&action,ATTRIBUTE_GROUP,attribute, ATTRIBUTE_ON_DOWN, NULL) == RET_OK ) {
			do_down_action = true;
			down_action = action;
		}
	}

	if( value > 0 ) {
		if( do_max_action == true ) {
			if(entry_read_string(table,id,&action,ATTRIBUTE_GROUP,attribute, ATTRIBUTE_ON_MAX, NULL) == RET_NOK ) {
				do_max_action = false;
			} else {
				max_action = action;
			}
		}

		if(entry_read_string(table,id,&action,ATTRIBUTE_GROUP,attribute, ATTRIBUTE_ON_UP, NULL) == RET_OK ) {
			do_up_action = true;
			up_action = action;
		}
	}

	SDL_UnlockMutex(attribute_mutex);

	// do automatic actions
	if( do_down_action == true && down_action != NULL ) {
		action_execute_script(context,down_action,NULL);
	}
	if( down_action ) {
		free(down_action);
	}

	if( do_min_action == true && min_action != NULL ) {
		action_execute_script(context,min_action,NULL);
	}
	if( min_action ) {
		free(min_action);
	}

	if( do_up_action == true && up_action != NULL ) {
		action_execute_script(context,up_action,NULL);
	}
	if( min_action ) {
		free(up_action);
	}

	if( do_max_action == true && max_action != NULL ) {
		action_execute_script(context,max_action,NULL);
		free(max_action);
	}
	if( max_action ) {
		free(max_action);
	}

	sprintf(buf,"%s.%s.%s",ATTRIBUTE_GROUP,attribute,ATTRIBUTE_CURRENT);
	network_broadcast_entry_int(table,id,buf,current,true);
	sprintf(buf,"%s.%s.%s",ATTRIBUTE_GROUP,attribute,ATTRIBUTE_PREVIOUS);
	network_broadcast_entry_int(table,id,buf,old,true);
	return 0;
}