adding getglobal and pop to luagate

This commit is contained in:
Bluub
2021-06-15 09:51:31 +02:00
parent a712e42596
commit 8dcc16b4c6
8 changed files with 38 additions and 117 deletions
+15 -19
View File
@@ -9,7 +9,7 @@ mapinfo.dist { }
------------
local debug = false
local debug = true
if not debug then
log = function() end
@@ -45,25 +45,21 @@ function CreateMyMap(infos, maxX, maxZ)
return map
end
function isInExplosionRange()
return true
end
function Update(mapinfo)
print(mapinfo.raw[0].x)
print(mapinfo.raw[0].y)
print(mapinfo.raw[0].z)
--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)
--print(mapinfo.raw[0].x)
--print(mapinfo.raw[0].y)
--print(mapinfo.raw[0].z)
local maxX = 0
local maxZ = 0
for i, info in ipairs(mapinfo.raw) do
if info.x > maxX then
maxX = info.x
end
if info.z > maxZ then
maxZ = info.z
end
end
local myMap = CreateMyMap(mapinfo.raw, maxX, maxZ)
return 1, 1, false, false;
--if (isInExplosionRange()) then
-- return 0, , false, true
+11 -1
View File
@@ -22,6 +22,11 @@ namespace LuaG
return _state;
}
void State::getGlobal(std::string str)
{
lua_getglobal(_state, str.c_str());
}
void State::dofile(std::string filepath)
{
luaL_dofile(_state, filepath.c_str());
@@ -59,7 +64,7 @@ namespace LuaG
return res;
}
bool State::callFunction(std::string , int nbParams, int nbReturns)
bool State::callFunction(int nbParams, int nbReturns)
{
lua_pcall(_state, nbParams, nbReturns, 0);
return true;
@@ -84,4 +89,9 @@ namespace LuaG
{
lua_newtable(_state);
}
void State::popLast(void)
{
lua_pop(_state, -1);
}
}
+7 -1
View File
@@ -28,6 +28,9 @@ namespace LuaG
//! @brief Get Lua state
lua_State *getState(void);
//! @brief Get global value on top of the stack
void getGlobal(std::string str);
//! @brief Execute a file in this state
void dofile(std::string filepath);
@@ -41,7 +44,7 @@ namespace LuaG
bool getReturnBool(void);
//! @brief call a lua function
bool callFunction(std::string funcName, int nbParams, int nbReturns);
bool callFunction(int nbParams, int nbReturns);
//! @brief setTable
void setTable(void);
@@ -54,5 +57,8 @@ namespace LuaG
//! @brief Creates a new table at the top of the stack
void newTable(void);
//! @brief Pop last value on the stack
void popLast(void);
};
}
-48
View File
@@ -1,48 +0,0 @@
Create a "Object" metatable
map function to member function and get the instance using lua_userdata
Constructor for Object instance
link to the Object metatable with __index
Pass a Map object to the lua Update
function Update(Map)
Or Map is global and set it before calling Update
Map->infos = {
{ x = 0, y = 0, z = 0, type = TYPE},
,
}
Map->player = {
x, y, z
}
Map->Ennemies = {
{x, y, z}
}
Map->Bombs = {
{ x, y, z, radius, timeLeft }
}
Algos ?
Minimax, ExpectiMax, Alpha beta pruning
Alpha beta, determine heuristic value of one node
-in bomb range
+closer to player
+ bomb breaks wall
6 moves by turn
[
1, 0, false, false
-1, 0, false, false
0, 1, false, false
0, -1, false, false
0, 0, false, true
0, 0, false, false
]
@@ -12,24 +12,9 @@
namespace BBM
{
IAControllableComponent::IAControllableComponent(WAL::Entity &entity, std::string scriptPath)
: Component(entity), _scriptPath(scriptPath), state(luaL_newstate()), _state()
: Component(entity), _scriptPath(scriptPath), _state()
{
luaL_openlibs(state);
_state.dofile(scriptPath);
/*
luabridge::getGlobalNamespace(state)
.beginNamespace ("luaBBM")
.beginClass<MapInfo>("MapInfo")
.addProperty("x", &MapInfo::x)
.addProperty("y", &MapInfo::y)
.addProperty("z", &MapInfo::z)
.addProperty("type", &MapInfo::type)
.endClass()
.endNamespace();
luaL_dofile(state, scriptPath.c_str());
*/
}
WAL::Component *IAControllableComponent::clone(WAL::Entity &entity) const
@@ -22,8 +22,6 @@ namespace BBM
//! @brief path to the lua script
const std::string _scriptPath;
public:
//! @brief Lua executing state
lua_State *state;
//! @brief LuaGate state
LuaG::State _state;
@@ -59,9 +59,9 @@ namespace BBM
void IAControllableSystem::pushInfoRaw(LuaG::State &state)
{
int index = 0;
state.push("raw");
state.newTable();
int index = 0;
for (auto &info : _map) {
state.push(index++);
state.newTable();
@@ -98,33 +98,16 @@ namespace BBM
UpdateMapInfos(entity);
lua_getglobal(ia._state.getState(), "Update");
ia._state.getGlobal("Update");
if (!lua_isfunction(ia._state.getState(), -1))
return;
pushInfo(ia._state, player);
ia._state.callFunction("Update", 1, 4);
ia._state.callFunction(1, 4);
controllable.bomb = ia._state.getReturnBool();
controllable.jump = ia._state.getReturnBool();
controllable.move.y = ia._state.getReturnNumber();
controllable.move.x = ia._state.getReturnNumber();
lua_pop(state, -1);
/*
luabridge::LuaRef updateFunc = luabridge::getGlobal(ia.state, "Update");
if (!updateFunc.isFunction())
return;
luabridge::LuaResult res = updateFunc(player, _map, _players);
if (res.hasFailed() || res.size() != 4)
return;
if (res[0].isNumber())
controllable.move.x = res[0];
if (res[1].isNumber())
controllable.move.y = res[1];
if (res[2].isBool())
controllable.jump = res[2];
if (res[3].isBool())
controllable.bomb = res[3];
*/
ia._state.popLast();
}
void IAControllableSystem::onSelfUpdate()
-9
View File
@@ -1,9 +0,0 @@
[ ] Metatable for MapInfo
[ ] Metatable for Vector of MapInfo
[ ] C++ helper function for ai
[ ] Closest Player
[ ] GetPath to Point
[ ] Closest Bonus
[ ] isInExplosionrRange
how do i do it ?