start of find safe space

This commit is contained in:
Bluub
2021-06-19 12:01:19 +02:00
parent 96255c5c89
commit 8eba1e88c5
5 changed files with 50 additions and 67 deletions
+16 -64
View File
@@ -107,25 +107,8 @@ end
function getPathToSafeSpace(player)
local minXesc = (player.x - 3 < 0) and 0 or (player.x - 3);
local MaxXesc = (player.x + 3 > 16) and 16 or (player.x + 3);
local minYesc = (player.y - 3 < 0) and 0 or (player.y - 3);
local MaxYesc = (player.y + 3 > 16) and 16 or (player.y + 3);
local minDist = 100000
local res = {}
for i=minXesc,MaxXesc do
for j=minYesc, MaxYesc do
if getBlockType(i, j) == 0 and getDangerLevel(i, j) == 0 then
local safe = {x = i, y = j}
local currDist = dist(player, safe)
if currDist < minDist then
minDist, res = currDist, safe
end
end
end
end
print("res")
local res = getClosestSafeSpace()
print("run to")
print(res.x)
print(res.y)
local path = getPath(player.x, player.y, res.x, res.y)
@@ -149,61 +132,30 @@ function Update()
return (LastTarget.x - player.x), (LastTarget.y - player.y), false, false
end
end
print("player")
print(player.x)
print(player.y)
local player = getPlayerRound();
local dangerMap = getDanger()
PrintMap(dangerMap, 16, 16)
--PrintMap(dangerMap, 16, 16)
if getDangerLevelPlayer() then
print("b")
print("player")
print(player.x)
print(player.y)
print("INDANGER")
local path = getPathToSafeSpace(player)
print("w")
for i,c in ipairs(path) do
print(i)
print(c.x)
print(c.y)
end
if #path >= 2 then
print("path found")
for i, c in ipairs(path) do
print(i)
print(c.x)
print(c.y)
end
LastTarget = {x = path[2].x, y = path[2].y}
return path[2].x - player.x, path[2].y - player.y, false, false
end
print("nopath found")
return 0, 0, false, false
end
---- sjould send Map Danger and MaxX MaxY
--MaxX = 0
--MaxY = 0
--for i, info in ipairs(mapinfo.raw) do
-- if info.x > MaxX then
-- MaxX = info.
-- end
-- if info.y > MaxY then
-- MaxY = info.y
-- end
--end
--Map = CreateMyMap(mapinfo.raw, MaxX, MaxY)
--Danger = CreateDangerMap(mapinfo.danger)
--PrintMap(Map, MaxX, MaxY)
--log("Current player pos")
--log(mapinfo.player.x)
--log(mapinfo.player.y)
--log("Rounded player pos")
--local roundedPlayerPos = {x = math.floor(mapinfo.player.x+0.5), y = math.floor(mapinfo.player.y+0.5)}
--log(roundedPlayerPos.x)
--log(roundedPlayerPos.y)
--log("Last target")
--if LastTarget ~= nil then
-- log(LastTarget.x)
-- log(LastTarget.y)
-- if math.abs(LastTarget.x - mapinfo.player.x) <= 0.1 and math.abs(LastTarget.x - mapinfo.player.x) <= 0.1 then
-- LastTarget = nil
-- else
-- return (LastTarget.x - mapinfo.player.x), (LastTarget.y - mapinfo.player.y), false, false
-- end
--else
-- log("No last target")
--end
print("SAFE")
--if (isInExplosionRange(roundedPlayerPos.x, roundedPlayerPos.y)) then
-- log("IN DANGER")
-- local pathToSafeSpace = getPathToSafeSpace(roundedPlayerPos)
+24
View File
@@ -69,6 +69,17 @@ namespace BBM
_danger[neighbor.y][neighbor.x] != 1)
neighbors.push_back(neighbor);
}
if (neighbors.size())
return neighbors;
for (auto &dir : _dirs) {
Vector2f neighbor(node.x + dir.x, node.y + dir.y);
if (neighbor.y < 0 || neighbor.x < 0)
continue;
if (neighbor.y >= 17 || neighbor.x >= 17)
continue;
if (_map[neighbor.y][neighbor.x] == 0)
neighbors.push_back(neighbor);
}
return neighbors;
}
@@ -126,6 +137,11 @@ namespace BBM
return path;
}
Vector2f LuaMap::findSafeSpace(void) const
{
return _roundedPlayer;
}
int LuaMap::getMap(lua_State *L)
{
LuaG::State state(L);
@@ -224,6 +240,14 @@ namespace BBM
{
LuaG::State state(L);
const LuaMap *map = (const LuaMap *) lua_topointer(L, lua_upvalueindex(1));
Vector2f closest = map->findSafeSpace();
lua_newtable(L);
lua_pushstring(L, "x");
lua_pushinteger(L, closest.x);
lua_settable(L, -3);
lua_pushstring(L, "y");
lua_pushinteger(L, closest.y);
lua_settable(L, -3);
return 1;
}
+3
View File
@@ -30,6 +30,9 @@ namespace BBM
//! @brief A star pathfinding between two points
std::vector<Vector2f> pathfind(Vector2f, Vector2f) const;
//! @brief find a safe space for current player
Vector2f findSafeSpace(void) const;
//! @brief push table of table of the map
static int getMap(lua_State *L);
+3 -3
View File
@@ -240,9 +240,9 @@ namespace BBM
.addComponent<TagComponent<Blowable>>()
.addComponent<TagComponent<Breakable>>()
.addComponent<HealthComponent>(1, &MapGenerator::wallDestroyed)
.addComponent<CollisionComponent>(
WAL::Callback<WAL::Entity &, const WAL::Entity &, CollisionComponent::CollidedAxis>(),
&MapGenerator::wallCollided, 0.25, .75)
//.addComponent<CollisionComponent>(
// WAL::Callback<WAL::Entity &, const WAL::Entity &, CollisionComponent::CollidedAxis>(),
// &MapGenerator::wallCollided, 0.25, .75)
.addComponent<Drawable3DComponent, RAY3D::Model>(breakableObj, false, std::make_pair(MAP_DIFFUSE, breakablePng));
}
@@ -103,6 +103,10 @@ namespace BBM
lua_pushlightuserdata(state.getState(), &_luamap);
lua_pushcclosure(state.getState(), LuaMap::getBlockType, 1);
lua_setglobal(state.getState(), "getBlockType");
lua_pushlightuserdata(state.getState(), &_luamap);
lua_pushcclosure(state.getState(), LuaMap::getClosestSafeSpace, 1);
lua_setglobal(state.getState(), "getClosestSafeSpace");
}
void IAControllableSystem::onFixedUpdate(WAL::ViewEntity<PositionComponent, ControllableComponent, IAControllableComponent, BombHolderComponent> &entity)