Exemplo n.º 1
0
static void push_movements(lua_State *L, const std::vector< std::pair < map_location, map_location > > & moves)
{
	lua_createtable(L, moves.size(), 0);

	int table_index = lua_gettop(L);

	std::vector< std::pair < map_location, map_location > >::const_iterator move = moves.begin();

	for (int i = 1; move != moves.end(); move++, i++)
	{
		lua_createtable(L, 2, 0); // Creating a table for a pair of map_location's

		lua_pushstring(L, "src");
		push_map_location(L, move->first);
		lua_rawset(L, -3);

		lua_pushstring(L, "dst");
		push_map_location(L, move->second);
		lua_rawset(L, -3);

		lua_rawseti(L, table_index, i); // setting  the pair as an element of the movements table
	}


}
Exemplo n.º 2
0
static void push_move_map(lua_State *L, const move_map& m)
{
	move_map::const_iterator it = m.begin();

	int index = 1;

	lua_createtable(L, 0, 0); // the main table

	do {
		map_location key = it->first;

		push_map_location(L, key);
		lua_createtable(L, 0, 0);

		while (key == it->first) {

			push_map_location(L, it->second);
			lua_rawseti(L, -2, index);

			++index;
			++it;

		}

		lua_settable(L, -3);

		index = 1;

	} while (it != m.end());
}
Exemplo n.º 3
0
/**
 * Expose map_location::vector_negation to lua
 */
int intf_vector_negation(lua_State* L)
{
	map_location l1 = pop_map_location(L);

	push_map_location(L, l1.vector_negation());
	return 2;
}
Exemplo n.º 4
0
static int cfun_ai_get_avoid(lua_State *L)
{
	std::set<map_location> locs;
	terrain_filter avoid = get_readonly_context(L).get_avoid();
	avoid.get_locations(locs);

	int sz = locs.size();
	lua_createtable(L, sz, 0); // create a table that we'll use as an array

	std::set<map_location>::iterator it = locs.begin();
	for (int i = 0; it != locs.end(); ++it, ++i)
	{
		lua_pushinteger(L, i + 1); // Index for the map location

		push_map_location(L, *it);

		// Deprecated
		//lua_createtable(L, 2, 0); // Table for a single map location

		//lua_pushstring(L, "x");
		//lua_pushinteger(L, it->x + 1);
		//lua_settable(L, -3);

		//lua_pushstring(L, "y");
		//lua_pushinteger(L, it->y + 1);
		//lua_settable(L, -3);

		lua_settable(L, -3);
	}

	return 1;
}
Exemplo n.º 5
0
static int cfun_ai_get_targets(lua_State *L)
{
	move_map enemy_dst_src = get_readonly_context(L).get_enemy_dstsrc();
	std::vector<target> targets = get_engine(L).get_ai_context()->find_targets(enemy_dst_src);
	int i = 1;

	lua_createtable(L, 0, 0);
	for (std::vector<target>::iterator it = targets.begin(); it != targets.end(); it++)
	{
		lua_pushinteger(L, i);

		//to factor out
		lua_createtable(L, 3, 0);


		lua_pushstring(L, "type");
		lua_pushnumber(L, it->type);
		lua_rawset(L, -3);

		lua_pushstring(L, "loc");
		push_map_location(L, it->loc);
		lua_rawset(L, -3);

		lua_pushstring(L, "value");
		lua_pushnumber(L, it->value);
		lua_rawset(L, -3);

		lua_rawset(L, -3);
		++i;
	}
	return 1;
}
Exemplo n.º 6
0
/**
 * Expose map_location::vector_sum to lua
 */
int intf_vector_sum(lua_State* L)
{
	map_location l2 = pop_map_location(L);
	map_location l1 = pop_map_location(L);

	push_map_location(L, l1.vector_sum_assign(l2));
	return 2;
}
Exemplo n.º 7
0
/**
 * Expose map_location::rotate_right_around_center to lua
 */
int intf_rotate_right_around_center(lua_State* L)
{
	int k = luaL_checkint(L, -1);
	lua_pop(L,1);
	map_location center = pop_map_location(L);
	map_location loc = pop_map_location(L);

	push_map_location(L, loc.rotate_right_around_center(center, k));
	return 2;
}
Exemplo n.º 8
0
/**
 * Expose map_location get_adjacent_tiles
 */
int intf_get_adjacent_tiles(lua_State* L)
{
	map_location l1 = pop_map_location(L);

	map_location locs[6];
	get_adjacent_tiles(l1, locs);

	for (int i = 0; i < 6; ++i) {
		push_map_location(L, locs[i]);
	}

	return 12;
}
Exemplo n.º 9
0
static void push_move_map(lua_State *L, const move_map& m)
{
	lua_createtable(L, 0, 0); // the main table

	if (m.empty())
	{
		return;
	}

	move_map::const_iterator it = m.begin();

	int index = 1;



	do
	{
		map_location key = it->first;

		//push_map_location(L, key); // deprecated

		// This should be factored out. The same function is defined in data/lua/location_set.lua
		// At this point, it is not clear, where this(hashing) function can be placed
		// Implemented it this way, to test the new version of the data structure
		// as requested from the users of LuaAI <Nephro>
		int hashed_index = (key.x + 1) * 16384 + (key.y + 1) + 2000;
		lua_pushinteger(L, hashed_index);

		lua_createtable(L, 0, 0);

		while (key == it->first) {

			push_map_location(L, it->second);
			lua_rawseti(L, -2, index);

			++index;
			++it;

		}

		lua_settable(L, -3);

		index = 1;

	} while (it != m.end());
}
Exemplo n.º 10
0
/**
 * Expose map_location::get_direction function to lua
 */
int intf_get_direction(lua_State* L)
{
	int nargs = lua_gettop(L);
	if (nargs != 3 and nargs != 4) {
		std::string msg("get_direction: must pass 3 or 4 args, found ");
		msg += nargs;
		luaL_error(L, msg.c_str());
		return 0;
	}

	int n = 1;
	if (nargs == 4) {
		n = luaL_checkint(L, -1);
		lua_pop(L,1);
	}

	map_location::DIRECTION d;
	if (lua_isnumber(L, -1)) {
		d = map_location::rotate_right(map_location::NORTH, luaL_checkint(L, -1)); //easiest way to correctly convert int to direction
		lua_pop(L,1);
	} else if (lua_isstring(L, -1)) {
		d = map_location::parse_direction(luaL_checkstring(L,-1));
		lua_pop(L,1);
	} else {
		std::string msg("get_direction: third argument should be a direction, either a string or an integer, instead found a ");
		msg += lua_typename(L, lua_type(L, -1));
		luaL_argerror(L, -1, msg.c_str());
		return 0;
	}

	map_location l = pop_map_location(L);
	map_location result = l.get_direction(d, n);

	push_map_location(L, result);
	return 2;
}
Exemplo n.º 11
0
/**
 * Expose map_location::ZERO to lua
 */
int intf_vector_zero(lua_State* L)
{
	push_map_location(L, map_location::ZERO());
	return 2;
}
Exemplo n.º 12
0
static void push_attack_analysis(lua_State *L, attack_analysis& aa)
{
	lua_newtable(L);

	// Pushing a pointer to the current object
	lua_pushstring(L, "att_ptr");
	lua_pushlightuserdata(L, &aa);
	lua_rawset(L, -3);

	// Registering callback function for the rating method
	lua_pushstring(L, "rating");
	lua_pushlightuserdata(L, &get_engine(L));
	lua_pushcclosure(L, &cfun_attack_rating, 1);
	lua_rawset(L, -3);

	lua_pushstring(L, "movements");
	push_movements(L, aa.movements);
	lua_rawset(L, -3);

	lua_pushstring(L, "target");
	push_map_location(L, aa.target);
	lua_rawset(L, -3);

	lua_pushstring(L, "target_value");
	lua_pushnumber(L, aa.target_value);
	lua_rawset(L, -3);

	lua_pushstring(L, "avg_losses");
	lua_pushnumber(L, aa.avg_losses);
	lua_rawset(L, -3);

	lua_pushstring(L, "chance_to_kill");
	lua_pushnumber(L, aa.chance_to_kill);
	lua_rawset(L, -3);

	lua_pushstring(L, "avg_damage_inflicted");
	lua_pushnumber(L, aa.avg_damage_inflicted);
	lua_rawset(L, -3);

	lua_pushstring(L, "target_starting_damage");
	lua_pushinteger(L, aa.target_starting_damage);
	lua_rawset(L, -3);

	lua_pushstring(L, "avg_damage_taken");
	lua_pushnumber(L, aa.avg_damage_taken);
	lua_rawset(L, -3);

	lua_pushstring(L, "resources_used");
	lua_pushnumber(L, aa.resources_used);
	lua_rawset(L, -3);

	lua_pushstring(L, "terrain_quality");
	lua_pushnumber(L, aa.alternative_terrain_quality);
	lua_rawset(L, -3);

	lua_pushstring(L, "alternative_terrain_quality");
	lua_pushnumber(L, aa.alternative_terrain_quality);
	lua_rawset(L, -3);

	lua_pushstring(L, "vulnerability");
	lua_pushnumber(L, aa.vulnerability);
	lua_rawset(L, -3);

	lua_pushstring(L, "support");
	lua_pushnumber(L, aa.support);
	lua_rawset(L, -3);

	lua_pushstring(L, "leader_threat");
	lua_pushboolean(L, aa.leader_threat);
	lua_rawset(L, -3);

	lua_pushstring(L, "uses_leader");
	lua_pushboolean(L, aa.uses_leader);
	lua_rawset(L, -3);

	lua_pushstring(L, "is_surrounded");
	lua_pushboolean(L, aa.is_surrounded);
	lua_rawset(L, -3);
}