mirror of
https://github.com/zoriya/Bomberman.git
synced 2026-05-29 08:52:06 +00:00
update map infos function for all ia
This commit is contained in:
+51
-13
@@ -1,16 +1,54 @@
|
||||
function Update(player, infos, players)
|
||||
print(player.x);
|
||||
print(player.y);
|
||||
print(player.z);
|
||||
------------ JOHN AI
|
||||
|
||||
|
||||
local debug = true
|
||||
--local debug = false
|
||||
if not debug then
|
||||
log = function() end
|
||||
end
|
||||
|
||||
function PrintMap(map, maxX, maxZ)
|
||||
for i=0,maxX + 1 do
|
||||
local s = "| "
|
||||
for j=0,maxZ + 1 do
|
||||
s = s .. tostring(map[i][j]) .. " | ";
|
||||
end
|
||||
log(s)
|
||||
log(string.rep("-", (maxZ - 1) * 5 - 1))
|
||||
end
|
||||
end
|
||||
|
||||
function CreateMyMap(infos, maxX, maxZ)
|
||||
local map = {}
|
||||
for i=0,maxX + 1 do
|
||||
map[i] = {}
|
||||
for j=0,maxZ + 1 do
|
||||
map[i][j] = 0
|
||||
end
|
||||
end
|
||||
for i, info in ipairs(infos) do
|
||||
--print("x");
|
||||
--print (info.x);
|
||||
--print("y");
|
||||
--print (info.y);
|
||||
--print("z");
|
||||
--print (info.z);
|
||||
--print("type");
|
||||
--print (info.type);
|
||||
end
|
||||
map[info.x][info.z] = info.type
|
||||
end
|
||||
PrintMap(map, maxX, maxZ)
|
||||
return map
|
||||
end
|
||||
|
||||
function Update(player, infos, players)
|
||||
local maxX = 0
|
||||
local maxZ = 0
|
||||
for i, info in ipairs(infos) do
|
||||
if info.x > maxX then
|
||||
maxX = info.x
|
||||
end
|
||||
if info.z > maxZ then
|
||||
maxZ = info.z
|
||||
end
|
||||
end
|
||||
local myMap = CreateMyMap(infos, maxX, maxZ);
|
||||
if (isInExplosionRange()) then
|
||||
--play defensive RUN
|
||||
else
|
||||
--play offensive
|
||||
end
|
||||
return 1, 1, false, false;
|
||||
end
|
||||
@@ -2,12 +2,10 @@
|
||||
// Created by Louis Auzuret on 06/07/21
|
||||
//
|
||||
|
||||
#include "Map/MapInfo.hpp"
|
||||
#include "Component/Tag/TagComponent.hpp"
|
||||
#include "Component/Controllable/ControllableComponent.hpp"
|
||||
#include "Component/IAControllable/IAControllableComponent.hpp"
|
||||
#include "System/IAControllable/IAControllableSystem.hpp"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
namespace BBM
|
||||
@@ -15,6 +13,54 @@ namespace BBM
|
||||
IAControllableSystem::IAControllableSystem(WAL::Wal &wal)
|
||||
: System(wal), _wal(wal)
|
||||
{ }
|
||||
/*
|
||||
float IAControllableSystem::getReturnNumber(lua_State *state)
|
||||
{
|
||||
float res = 0;
|
||||
|
||||
if (lua_isnil(state, -1))
|
||||
return res;
|
||||
if (!lua_isnumber(state, -1))
|
||||
return res;
|
||||
res = lua_tonumber(state, -1);
|
||||
lua_pop(state, 1);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
bool IAControllableSystem::getReturnBool(lua_State *state)
|
||||
{
|
||||
bool res = false;
|
||||
|
||||
if (lua_isnil(state, -1))
|
||||
return res;
|
||||
if (!lua_isboolean(state, -1))
|
||||
return res;
|
||||
res = lua_toboolean(state, -1);
|
||||
lua_pop(state, 1);
|
||||
return res;
|
||||
}*/
|
||||
|
||||
void IAControllableSystem::UpdateMapInfos(WAL::Entity entity)
|
||||
{
|
||||
if (_cached)
|
||||
return;
|
||||
for (auto &[other, pos, _] : _wal.scene->view<PositionComponent, TagComponent<Breakable>>())
|
||||
_map.push_back(MapInfo(pos.position, MapGenerator::BREAKABLE));
|
||||
for (auto &[other, pos, _] : _wal.scene->view<PositionComponent, TagComponent<Unbreakable>>())
|
||||
_map.push_back(MapInfo(pos.position, MapGenerator::UNBREAKABLE));
|
||||
for (auto &[other, pos, _] : _wal.scene->view<PositionComponent, TagComponent<Bumper>>())
|
||||
_map.push_back(MapInfo(pos.position, MapGenerator::BUMPER));
|
||||
for (auto &[other, pos, _] : _wal.scene->view<PositionComponent, TagComponent<Hole>>())
|
||||
_map.push_back(MapInfo(pos.position, MapGenerator::HOLE));
|
||||
for (auto &[other, pos, _] : _wal.scene->view<PositionComponent, TagComponent<Player>>()) {
|
||||
if (entity.getUid() == other.getUid())
|
||||
continue;
|
||||
_players.push_back(MapInfo(pos.position, MapGenerator::NOTHING));
|
||||
}
|
||||
_cached = true;
|
||||
|
||||
}
|
||||
|
||||
void IAControllableSystem::onFixedUpdate(WAL::ViewEntity<PositionComponent, ControllableComponent, IAControllableComponent> &entity)
|
||||
{
|
||||
@@ -22,31 +68,12 @@ namespace BBM
|
||||
auto &controllable = entity.get<ControllableComponent>();
|
||||
auto &pos = entity.get<PositionComponent>();
|
||||
MapInfo player(pos.position, MapGenerator::NOTHING);
|
||||
std::vector<MapInfo> infos;
|
||||
std::vector<MapInfo> players;
|
||||
|
||||
for (auto &[other, pos, _] : _wal.scene->view<PositionComponent, TagComponent<Breakable>>())
|
||||
infos.push_back(MapInfo(pos.position, MapGenerator::BREAKABLE));
|
||||
|
||||
for (auto &[other, pos, _] : _wal.scene->view<PositionComponent, TagComponent<Unbreakable>>())
|
||||
infos.push_back(MapInfo(pos.position, MapGenerator::UNBREAKABLE));
|
||||
|
||||
for (auto &[other, pos, _] : _wal.scene->view<PositionComponent, TagComponent<Bumper>>())
|
||||
infos.push_back(MapInfo(pos.position, MapGenerator::BUMPER));
|
||||
|
||||
for (auto &[other, pos, _] : _wal.scene->view<PositionComponent, TagComponent<Hole>>())
|
||||
infos.push_back(MapInfo(pos.position, MapGenerator::HOLE));
|
||||
|
||||
for (auto &[other, pos, _] : _wal.scene->view<PositionComponent, TagComponent<Player>>()) {
|
||||
if (static_cast<WAL::Entity>(entity).getUid() == other.getUid())
|
||||
continue;
|
||||
players.push_back(MapInfo(pos.position, MapGenerator::NOTHING));
|
||||
}
|
||||
|
||||
UpdateMapInfos(static_cast<WAL::Entity>(entity));
|
||||
luabridge::LuaRef updateFunc = luabridge::getGlobal(ia.state, "Update");
|
||||
if (!updateFunc.isFunction())
|
||||
return;
|
||||
luabridge::LuaResult res = updateFunc(player, infos, players);
|
||||
luabridge::LuaResult res = updateFunc(player, _map, _players);
|
||||
|
||||
if (res.hasFailed() || res.size() != 4)
|
||||
return;
|
||||
@@ -59,4 +86,19 @@ namespace BBM
|
||||
if (res[3].isBool())
|
||||
controllable.bomb = res[3];
|
||||
}
|
||||
|
||||
void IAControllableSystem::onSelfUpdate()
|
||||
{
|
||||
_cached = false;
|
||||
_map.clear();
|
||||
_players.clear();
|
||||
}
|
||||
|
||||
bool IAControllableSystem::isInExplosionRange(float x, float y, float z)
|
||||
{
|
||||
return false;
|
||||
//for (auto &bomb : bombs) {
|
||||
//
|
||||
//}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include "Map/MapInfo.hpp"
|
||||
#include "System/System.hpp"
|
||||
|
||||
namespace BBM
|
||||
@@ -14,10 +16,25 @@ namespace BBM
|
||||
private:
|
||||
//! @brief Reference to wal to get Views
|
||||
WAL::Wal &_wal;
|
||||
|
||||
//! @brief Are the infos cached for current update
|
||||
bool _cached;
|
||||
|
||||
//! @brief All blocks in the map
|
||||
std::vector<MapInfo> _map;
|
||||
|
||||
//! @brief All players in the map
|
||||
std::vector<MapInfo> _players;
|
||||
|
||||
//! @brief
|
||||
void UpdateMapInfos(WAL::Entity entity);
|
||||
public:
|
||||
//! @inherit
|
||||
void onFixedUpdate(WAL::ViewEntity<PositionComponent, ControllableComponent, IAControllableComponent> &entity) override;
|
||||
|
||||
//! @inherit
|
||||
void onSelfUpdate() override;
|
||||
|
||||
//! @brief A default constructor
|
||||
IAControllableSystem(WAL::Wal &wal);
|
||||
//! @brief A keyboard system is copy constructable
|
||||
@@ -26,5 +43,7 @@ namespace BBM
|
||||
~IAControllableSystem() override = default;
|
||||
//! @brief A keyboard system is assignable.
|
||||
IAControllableSystem &operator=(const IAControllableSystem &) = default;
|
||||
|
||||
static bool isInExplosionRange(float x, float y, float z);
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user