mirror of
https://github.com/zoriya/Bomberman.git
synced 2026-06-01 09:45:42 +00:00
cleanup on some files
This commit is contained in:
+28
-187
@@ -41,72 +41,6 @@ function PrintMap(map, MaxX, maxZ)
|
||||
end
|
||||
end
|
||||
|
||||
function CreateMyMap(infos, MaxX, MaxY)
|
||||
local map = {}
|
||||
for i=0,MaxX + 1 do
|
||||
map[i] = {}
|
||||
for j=0,MaxY + 1 do
|
||||
map[i][j] = 0
|
||||
end
|
||||
end
|
||||
for i, info in ipairs(infos) do
|
||||
map[info.x][info.y] = math.floor(info.type)
|
||||
end
|
||||
--PrintMap(map, MaxX, MaxY)
|
||||
return map
|
||||
end
|
||||
|
||||
function CreateDangerMap(dangers)
|
||||
local danger = {}
|
||||
for i=0,MaxX + 1 do
|
||||
danger[i] = {}
|
||||
for j=0,MaxY + 1 do
|
||||
danger[i][j] = 0
|
||||
end
|
||||
end
|
||||
for i, zone in ipairs(dangers) do
|
||||
if danger[math.floor(zone.x)] == nil then
|
||||
danger[math.floor(zone.x)] = {}
|
||||
end
|
||||
danger[math.floor(zone.x)][math.floor(zone.y)] = math.floor(zone.level)
|
||||
end
|
||||
PrintMap(danger, MaxX, MaxY)
|
||||
return danger
|
||||
end
|
||||
|
||||
function isInExplosionRange(x, y)
|
||||
if Danger[x][y] > 0 then
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
---- Pathfinding
|
||||
|
||||
function setAdd(set, toAdd)
|
||||
table.insert(set, toAdd)
|
||||
end
|
||||
|
||||
function setRemove(set, toRemove)
|
||||
for i, node in ipairs(set) do
|
||||
if node == toRemove then
|
||||
set[i] = set[#set]
|
||||
set[#set] = nil
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function not_in(set, node)
|
||||
for _,value in pairs(set) do
|
||||
if value.x == node.x and value.y == node.y then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
|
||||
function getNeighborsDefend(node)
|
||||
local neighbors = {}
|
||||
for _, dir in ipairs(Dirs) do
|
||||
@@ -136,130 +70,10 @@ function getNeighborsDefend(node)
|
||||
return neighbors
|
||||
end
|
||||
|
||||
function getLowestFromSet(set, f_score)
|
||||
local lowest = 100000
|
||||
local best = nil
|
||||
for _,node in ipairs(set) do
|
||||
local score = f_score[node]
|
||||
if score < lowest then
|
||||
lowest = score
|
||||
best = node
|
||||
end
|
||||
end
|
||||
return best
|
||||
end
|
||||
|
||||
function fill_path(path, came_from, node)
|
||||
if came_from[node.x][node.y] ~= nil then
|
||||
table.insert(path, 1, came_from[node.x][node.y])
|
||||
return fill_path(path, came_from, came_from[node.x][node.y])
|
||||
else
|
||||
return path
|
||||
end
|
||||
end
|
||||
|
||||
--A star search
|
||||
function pathfind(root, target, getNeighborFunc)
|
||||
if getNeighborFunc == nil then
|
||||
getNeighborFunc = getNeighborsDefend
|
||||
end
|
||||
local closed = {}
|
||||
local open = { root }
|
||||
local came_from = {}
|
||||
|
||||
local g_score = {}
|
||||
local f_score = {}
|
||||
|
||||
g_score[root] = 0
|
||||
f_score[root] = dist(root, target)
|
||||
|
||||
|
||||
for i=0,MaxX + 1 do
|
||||
came_from[i] = {}
|
||||
for j=0,MaxY + 1 do
|
||||
came_from[i][j] = nil
|
||||
end
|
||||
end
|
||||
while #open > 0 do
|
||||
log("openset size")
|
||||
log(#open)
|
||||
local curr = getLowestFromSet(open, f_score) --get lowest node of openset
|
||||
log("current node")
|
||||
log(curr.x)
|
||||
log(curr.y)
|
||||
if curr.x == target.x and curr.y == target.y then
|
||||
log("came from")
|
||||
local path = fill_path({}, came_from, target) -- fill the path with came from
|
||||
table.insert(path, target)
|
||||
log("yee")
|
||||
return path
|
||||
end
|
||||
setRemove(open, curr) -- remove curr from open
|
||||
setAdd(closed, curr)-- add node to closed
|
||||
log("closed set")
|
||||
for i, c in ipairs(closed) do
|
||||
log("member")
|
||||
log(c.x)
|
||||
log(c.y)
|
||||
end
|
||||
local neighbors = getNeighborFunc(curr)
|
||||
log("current neightbors") -- get neighbors of current
|
||||
log("openset size")
|
||||
log(#open)
|
||||
for _, neighbor in ipairs(neighbors) do
|
||||
log("i")
|
||||
if not_in(closed, neighbor) then -- neighbor not in closed set
|
||||
log("j")
|
||||
local try_g_score = g_score[curr] + 1
|
||||
log("g score")
|
||||
log(g_score[curr])
|
||||
log("g score neig")
|
||||
log(g_score[neighbor])
|
||||
local g_score_neigh = 10000
|
||||
if g_score[neighbor] ~= nil then
|
||||
g_score_neigh = g_score[neighbor]
|
||||
end
|
||||
if not_in(open, neighbor) or try_g_score < g_score_neigh then
|
||||
came_from[neighbor.x][neighbor.y] = {x = curr.x, y = curr.y}
|
||||
g_score[neighbor] = try_g_score
|
||||
f_score[neighbor] = g_score[neighbor] + dist(neighbor, target)
|
||||
if not_in(open, neighbor) then
|
||||
setAdd(open, neighbor)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return {}
|
||||
end
|
||||
|
||||
function dist(nodeA, nodeB)
|
||||
return math.sqrt(math.pow(nodeB.x - nodeA.x, 2) + math.pow(nodeB.y - nodeA.y, 2))
|
||||
end
|
||||
|
||||
function getPathToSafeSpace(player)
|
||||
local minXesc = (player.x - 3 < 0) and 0 or (player.x - 3);
|
||||
local MaxXesc = (player.x + 3 > MaxX) and MaxX or (player.x + 3);
|
||||
local minYesc = (player.y - 3 < 0) and 0 or (player.y - 3);
|
||||
local MaxYesc = (player.y + 3 > MaxY) and MaxY or (player.y + 3);
|
||||
|
||||
local minDist = 100000
|
||||
local res = {}
|
||||
for i=minXesc,MaxXesc do
|
||||
for j=minYesc, MaxYesc do
|
||||
if Map[i][j] == 0 and Danger[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
|
||||
local path = pathfind(player, res)
|
||||
return path
|
||||
end
|
||||
|
||||
function getNeighborAttack(node)
|
||||
log("atta")
|
||||
local neighbors = {}
|
||||
@@ -292,11 +106,38 @@ end
|
||||
|
||||
|
||||
|
||||
function getPathToSafeSpace(player)
|
||||
local minXesc = (player.x - 3 < 0) and 0 or (player.x - 3);
|
||||
local MaxXesc = (player.x + 3 > MaxX) and MaxX or (player.x + 3);
|
||||
local minYesc = (player.y - 3 < 0) and 0 or (player.y - 3);
|
||||
local MaxYesc = (player.y + 3 > MaxY) and MaxY or (player.y + 3);
|
||||
|
||||
local minDist = 100000
|
||||
local res = {}
|
||||
for i=minXesc,MaxXesc do
|
||||
for j=minYesc, MaxYesc do
|
||||
if Map[i][j] == 0 and Danger[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
|
||||
local path = pathfind(player, res)
|
||||
return path
|
||||
end
|
||||
|
||||
------ Update
|
||||
function Update(mapinfo)
|
||||
log("NEW FRAME")
|
||||
x = getDanger()
|
||||
getPath(0, 0, 16, 16);
|
||||
p = getPath(0, 0, 16, 16);
|
||||
for i, c in ipairs(p) do
|
||||
print(c.x)
|
||||
print(c.y)
|
||||
end
|
||||
---- sjould send Map Danger and MaxX MaxY
|
||||
--MaxX = 0
|
||||
--MaxY = 0
|
||||
|
||||
@@ -11,22 +11,9 @@
|
||||
|
||||
namespace BBM
|
||||
{
|
||||
auto a = [](lua_State *L) -> int
|
||||
{
|
||||
const int *pThis = (const int*) lua_topointer(L, lua_upvalueindex(1));
|
||||
//const float x = lua_tonumber(state, -1);
|
||||
std::cout << *pThis;
|
||||
return 0;
|
||||
};
|
||||
IAControllableComponent::IAControllableComponent(WAL::Entity &entity, std::string scriptPath)
|
||||
: Component(entity), _scriptPath(scriptPath), _state(), registered(false)
|
||||
{
|
||||
static int x = 1;
|
||||
lua_pushlightuserdata(_state.getState(), &x);
|
||||
//lua_pushnumber(state, x);
|
||||
lua_pushcclosure(_state.getState(), a, 1);
|
||||
lua_setglobal(_state.getState(), "a");
|
||||
x++;
|
||||
if (std::filesystem::exists(scriptPath))
|
||||
_state.dofile(scriptPath);
|
||||
|
||||
|
||||
+73
-1
@@ -101,7 +101,6 @@ namespace BBM
|
||||
closed.push_back(current);
|
||||
auto neighbors = getNeighbors(current);
|
||||
for (auto &neighbor : neighbors) {
|
||||
std::cout << neighbor << std::endl;
|
||||
if (std::find(closed.begin(), closed.end(), neighbor) != closed.end())
|
||||
continue;
|
||||
int tryGSCore = gScore[current] + 1;
|
||||
@@ -119,4 +118,77 @@ namespace BBM
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
int LuaMap::getMap(lua_State *L)
|
||||
{
|
||||
LuaG::State state(L);
|
||||
int index = 1;
|
||||
const LuaMap *map = (const LuaMap *) lua_topointer(L, lua_upvalueindex(1));
|
||||
lua_newtable(L);
|
||||
for (int i = 0; i < 17; i++) {
|
||||
lua_pushinteger(L, index++);
|
||||
lua_newtable(L);
|
||||
int indexrow = 1;
|
||||
for (int j = 0; j < 17; j++) {
|
||||
lua_pushinteger(L, indexrow++);
|
||||
lua_pushinteger(L, map->_map[i][j]);
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int LuaMap::getDanger(lua_State *L)
|
||||
{
|
||||
int index = 1;
|
||||
const LuaMap *map = (const LuaMap *) lua_topointer(L, lua_upvalueindex(1));
|
||||
lua_newtable(L);
|
||||
for (int i = 0; i < 17; i++) {
|
||||
lua_pushinteger(L, index++);
|
||||
lua_newtable(L);
|
||||
int indexrow = 1;
|
||||
for (int j = 0; j < 17; j++) {
|
||||
lua_pushinteger(L, indexrow++);
|
||||
lua_pushinteger(L, map->_danger[i][j]);
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int LuaMap::getPath(lua_State *L)
|
||||
{
|
||||
LuaG::State state(L);
|
||||
auto y2 = lua_tonumber(L, -1);
|
||||
auto x2 = lua_tonumber(L, -2);
|
||||
auto y1 = lua_tonumber(L, -3);
|
||||
auto x1 = lua_tonumber(L, -4);
|
||||
const LuaMap *map = (const LuaMap *) lua_topointer(L, lua_upvalueindex(1));
|
||||
Vector2f fst(x1, y1);
|
||||
Vector2f snd(x2, y2);
|
||||
auto path = map->pathfind(fst, snd);
|
||||
int index = 1;
|
||||
lua_newtable(L);
|
||||
for (auto &r : path) {
|
||||
lua_pushinteger(L, index++);
|
||||
lua_newtable(L);
|
||||
lua_pushstring(L, "x");
|
||||
lua_pushnumber(L, r.x);
|
||||
lua_settable(L, -3);
|
||||
lua_pushstring(L, "y");
|
||||
lua_pushnumber(L, r.y);
|
||||
lua_settable(L, -3);
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int LuaMap::getClosestSafeSpace(lua_State *L)
|
||||
{
|
||||
LuaG::State state(L);
|
||||
const LuaMap *map = (const LuaMap *) lua_topointer(L, lua_upvalueindex(1));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
+6
-58
@@ -28,69 +28,17 @@ namespace BBM
|
||||
std::vector<Vector2f> pathfind(Vector2f, Vector2f) const;
|
||||
|
||||
//! @brief push table of table of the map
|
||||
static int getMap(lua_State *L)
|
||||
{
|
||||
LuaG::State state(L);
|
||||
int index = 1;
|
||||
const LuaMap *map = (const LuaMap *) lua_topointer(L, lua_upvalueindex(1));
|
||||
|
||||
lua_newtable(L);
|
||||
for (int i = 0; i < 17; i++) {
|
||||
lua_pushinteger(L, index++);
|
||||
lua_newtable(L);
|
||||
int indexrow = 1;
|
||||
for (int j = 0; j < 17; j++) {
|
||||
lua_pushinteger(L, indexrow++);
|
||||
lua_pushinteger(L, map->_map[i][j]);
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
static int getMap(lua_State *L);
|
||||
|
||||
//! @brief push table of table of the danger map
|
||||
static int getDanger(lua_State *L)
|
||||
{
|
||||
int index = 1;
|
||||
const LuaMap *map = (const LuaMap *) lua_topointer(L, lua_upvalueindex(1));
|
||||
|
||||
lua_newtable(L);
|
||||
for (int i = 0; i < 17; i++) {
|
||||
lua_pushinteger(L, index++);
|
||||
lua_newtable(L);
|
||||
int indexrow = 1;
|
||||
for (int j = 0; j < 17; j++) {
|
||||
lua_pushinteger(L, indexrow++);
|
||||
lua_pushinteger(L, map->_danger[i][j]);
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
lua_settable(L, -3);
|
||||
}return 1;
|
||||
}
|
||||
static int getDanger(lua_State *L);
|
||||
|
||||
//! @brief get array of nodes, path from a to b
|
||||
static int getPath(lua_State *L)
|
||||
{
|
||||
LuaG::State state(L);
|
||||
auto y2 = lua_tonumber(L, -1);
|
||||
auto x2 = lua_tonumber(L, -2);
|
||||
auto y1 = lua_tonumber(L, -3);
|
||||
auto x1 = lua_tonumber(L, -4);
|
||||
const LuaMap *map = (const LuaMap *) lua_topointer(L, lua_upvalueindex(1));
|
||||
Vector2f fst(x1, y1);
|
||||
Vector2f snd(x2, y2);
|
||||
auto path = map->pathfind(fst, snd);
|
||||
int index = 0;
|
||||
std::cout << "start of path " << std::endl;
|
||||
for (auto &r : path) {
|
||||
std::cout << "index is " << index++ << " : " << r << std::endl << std::flush;
|
||||
}
|
||||
static int getPath(lua_State *L);
|
||||
|
||||
//! @brief get closest safe space of player
|
||||
static int getClosestSafeSpace(lua_State *L);
|
||||
|
||||
std::cout << "end of path " << std::endl;
|
||||
//push newtable {{x = X, y = Y}, ...}
|
||||
return 1;
|
||||
}
|
||||
std::vector<std::vector<int>> _map;
|
||||
std::vector<std::vector<int>> _danger;
|
||||
private:
|
||||
|
||||
@@ -70,31 +70,6 @@ namespace BBM
|
||||
}
|
||||
}
|
||||
|
||||
void IAControllableSystem::pushInfoEnemies(LuaG::State &state)
|
||||
{
|
||||
int index = 1;
|
||||
state.push("enemies");
|
||||
state.newTable();
|
||||
for (auto &player : _players) {
|
||||
state.push(index++);
|
||||
state.newTable();
|
||||
state.push("x");
|
||||
state.push(player.x);
|
||||
state.setTable();
|
||||
state.push("y");
|
||||
state.push(player.z);
|
||||
state.setTable();
|
||||
state.setTable();
|
||||
}
|
||||
state.setTable();
|
||||
}
|
||||
|
||||
void IAControllableSystem::pushInfo(LuaG::State &state, MapInfo &player, BombHolderComponent &bombHolder)
|
||||
{
|
||||
state.newTable();
|
||||
pushInfoEnemies(state);
|
||||
}
|
||||
|
||||
void IAControllableSystem::registerFunc(LuaG::State &state)
|
||||
{
|
||||
lua_pushlightuserdata(state.getState(), &_luamap);
|
||||
@@ -118,13 +93,14 @@ namespace BBM
|
||||
auto &bombHolder = entity.get<BombHolderComponent>();
|
||||
MapInfo player(pos.position, MapGenerator::NOTHING);
|
||||
|
||||
if (!ia.registered)
|
||||
if (!ia.registered) {
|
||||
this->registerFunc(ia._state);
|
||||
ia.registered = true;
|
||||
}
|
||||
UpdateMapInfos(entity);
|
||||
ia._state.getGlobal("Update");
|
||||
if (!lua_isfunction(ia._state.getState(), -1))
|
||||
return;
|
||||
pushInfo(ia._state, player, bombHolder);
|
||||
ia._state.callFunction(1, 4);
|
||||
controllable.bomb = ia._state.getReturnBool();
|
||||
controllable.select = ia._state.getReturnBool();
|
||||
|
||||
@@ -36,12 +36,7 @@ namespace BBM
|
||||
|
||||
//! @brief update the raw info of the map
|
||||
void UpdateMapInfos(WAL::ViewEntity<PositionComponent, ControllableComponent, IAControllableComponent, BombHolderComponent> &entity);
|
||||
|
||||
//! @brief push info ennemies
|
||||
void pushInfoEnemies(LuaG::State &state);
|
||||
|
||||
//! @brief push all the infos to the lua stack
|
||||
void pushInfo(LuaG::State &state, MapInfo &player, BombHolderComponent &bombHolder);
|
||||
|
||||
public:
|
||||
|
||||
//! @inherit
|
||||
|
||||
Reference in New Issue
Block a user