random boy ai

This commit is contained in:
Bluub
2021-06-14 11:26:26 +02:00
parent 3631fe4203
commit a6140f6087
10 changed files with 178 additions and 42 deletions
+17 -3
View File
@@ -1,7 +1,7 @@
------------ JOHN AI
local debug = true
local debug = false
--local debug = false
if not debug then
log = function() end
@@ -37,6 +37,10 @@ function CreateMyMap(infos, maxX, maxZ)
return map
end
function isInExplosionRange()
return true
end
function Update(player, infos, players)
local maxX = 0
local maxZ = 0
@@ -48,11 +52,21 @@ function Update(player, infos, players)
maxZ = info.z
end
end
local myMap = CreateMyMap(infos, maxX, maxZ);
local myMap = CreateMyMap(infos, maxX, maxZ)
local x = math.random()
local y = math.random()
if (math.random() < 0.5) then
x = x * -1
end
if (math.random() < 0.5) then
y = y * -1
end
return x, y, false, true;
--if (isInExplosionRange()) then
-- return 0, , false, true
-- --play defensive RUN
--else
-- return 1, 1, false, false;
-- --play offensive
--end
return 1, 1, false, false;
end
+5
View File
@@ -0,0 +1,5 @@
math.randomseed(os.time())
function Update(player, infos, players)
return (math.random() < 0.5), (math.random() < 0.5), false, false;
end
+28
View File
@@ -0,0 +1,28 @@
#Definition of CMake version to use
CMAKE_MINIMUM_REQUIRED(VERSION 3.11)
set(CMAKE_CXX_STANDARD 20)
set(LIB_NAME "LuaGate")
project("${LIB_NAME}")
include_directories(${LIB_NAME} ./sources)
if (CMAKE_COMPILER_IS_GNUCXX)
set(GCC_COVERAGE_COMPILE_FLAGS "-Wall -Wextra -Werror -Wshadow")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")
endif ()
find_package(Lua REQUIRED)
include_directories(${LIB_NAME} ${LUA_INCLUDE_DIR})
set(HEADERS
sources/LuaGate.hpp
)
set(SRC
sources/LuaGate.cpp
)
add_library(${LIB_NAME} STATIC ${SRC} ${HEADERS})
target_compile_definitions(${LIB_NAME} INTERFACE INTERNAL=private PRIVATE INTERNAL=public)
target_link_libraries(${LIB_NAME} ${LUA_LIBRARIES})
+36
View File
@@ -31,4 +31,40 @@ namespace LuaG
{
luaL_dostring(_state, str.c_str());
}
float State::getReturnNumber(void)
{
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 State::getReturnBool(void)
{
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;
}
bool State::callFunction(std::string funcName, int nbParams, int nbReturns)
{
lua_getglobal(_state, funcName.c_str());
if (!lua_isfunction(_state, -1))
return false;
lua_pcall(_state, nbParams, nbReturns, 0);
return true;
}
}
+13 -5
View File
@@ -10,7 +10,7 @@ namespace LuaG
class State
{
private:
LuaState *_state;
lua_State *_state;
public:
//! @brief ctor
State();
@@ -19,19 +19,27 @@ namespace LuaG
~State();
//! @brief No copy constrructor
State &State(State &) = delete;
State(State &) = delete;
//! @brief No assign operator
State &operator() = delete;
State &operator=(State &) = delete;
//! @brief Get Lua state
LuaState *getState(void);
lua_State *getState(void);
//! @brief Execute a file in this state
void dofile(std::string filepath);
//! @brief Execute a string in this state
void dostring(std::string str);
//! @brief Get return Number
float getReturnNumber(void);
//! @brief Get return Number
bool getReturnBool(void);
//! @brief call a lua function
bool callFunction(std::string funcName, int nbParams, int nbReturns);
}
}
+48
View File
@@ -0,0 +1,48 @@
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
]
@@ -7,6 +7,7 @@
#include "Map/MapInfo.hpp"
#include "Component/IAControllable/IAControllableComponent.hpp"
//#include "System/IAControllable/IAControllableSystem.hpp"
namespace BBM
{
@@ -14,6 +15,7 @@ namespace BBM
: Component(entity), _scriptPath(scriptPath), state(luaL_newstate())
{
luaL_openlibs(state);
luabridge::getGlobalNamespace(state)
.beginNamespace ("luaBBM")
.beginClass<MapInfo>("MapInfo")
@@ -23,6 +25,7 @@ namespace BBM
.addProperty("type", &MapInfo::type)
.endClass()
.endNamespace();
luaL_dofile(state, scriptPath.c_str());
}
+1 -1
View File
@@ -531,7 +531,7 @@ namespace BBM
.addComponent<Drawable3DComponent, RAY3D::Model>("assets/player/player.iqm", true, std::make_pair(MAP_DIFFUSE, "assets/player/blue.png"))
.addComponent<ControllableComponent>()
.addComponent<AnimatorComponent>()
.addComponent<KeyboardComponent>()
//.addComponent<KeyboardComponent>()
.addComponent<IAControllableComponent>("./ai_scripts/john.lua")
.addComponent<ShaderComponentModel>("assets/shaders/glsl330/predator.fs")
.addComponent<TagComponent<Blowable>>()
@@ -2,6 +2,7 @@
// Created by Louis Auzuret on 06/07/21
//
#include "Component/Bomb/BasicBombComponent.hpp"
#include "Component/Tag/TagComponent.hpp"
#include "Component/Controllable/ControllableComponent.hpp"
#include "Component/IAControllable/IAControllableComponent.hpp"
@@ -11,35 +12,8 @@
namespace BBM
{
IAControllableSystem::IAControllableSystem(WAL::Wal &wal)
: System(wal), _wal(wal), _map(), _players(), _cached(false)
: System(wal), _wal(wal), _cached(false)
{ }
/*
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::ViewEntity<PositionComponent, ControllableComponent, IAControllableComponent> &entity)
{
@@ -60,8 +34,8 @@ namespace BBM
continue;
_players.push_back(MapInfo(pos.position, MapGenerator::NOTHING));
}
//for (auto &[other, pos, bomb] : _wal.getScene()->view<PositionComponent, BasicBombComponent>())
// _bombs.push_back(std::make_pair(pos.position, bomb.explosionRadius));
for (auto &[other, pos, bomb] : _wal.getScene()->view<PositionComponent, BasicBombComponent>())
_bombs.push_back(std::make_pair(pos.position, bomb.explosionRadius));
_cached = true;
}
@@ -74,6 +48,11 @@ namespace BBM
MapInfo player(pos.position, MapGenerator::NOTHING);
UpdateMapInfos(entity);
luabridge::getGlobalNamespace(ia.state)
.beginNamespace ("luaBBM")
.addFunction("isInExplosionRange", IAControllableSystem::isInExplosionRange)
.endNamespace();
luabridge::LuaRef updateFunc = luabridge::getGlobal(ia.state, "Update");
if (!updateFunc.isFunction())
return;
@@ -101,9 +80,22 @@ namespace BBM
bool IAControllableSystem::isInExplosionRange(float x, float y, float z)
{
return false;
//for (auto &bomb : bombs) {
//
Vector3f pos(x, y, z);
//pos = pos.round();
//for (auto &bomb : _bombs) {
// Vector3f bombPos = std::get<0>(bomb);
// int bombRadius = std::get<1>(bomb);
// for (int i = 1; i < bombRadius; i++) {
// if (pos == bombPos - Vector3f(i, 0, 0))
// return true;
// if (pos == bombPos - Vector3f(-i, 0, 0))
// return true;
// if (pos == bombPos - Vector3f(0, 0, i))
// return true;
// if (pos == bombPos - Vector3f(0, 0, -i))
// return true;
// }
//}
return true;
}
}
@@ -26,11 +26,13 @@ namespace BBM
//! @brief All players in the map
std::vector<MapInfo> _players;
//! @brief All bombs on the map
std::vector<std::pair<Vector3f, int>> _bombs;
//! @brief
void UpdateMapInfos(WAL::ViewEntity<PositionComponent, ControllableComponent, IAControllableComponent> &entity);
public:
//! @inherit
void onFixedUpdate(WAL::ViewEntity<PositionComponent, ControllableComponent, IAControllableComponent> &entity) override;