mirror of
https://github.com/devZoGok/PlanetFleet.git
synced 2026-08-26 19:43:29 +00:00
simplified AI subsystem using sol usertypes
This commit is contained in:
@@ -1,20 +1,3 @@
|
||||
Player = {
|
||||
refineds = 0,
|
||||
wealth = 0,
|
||||
research = 0,
|
||||
faction = -1,
|
||||
difficulty = -1,
|
||||
team = -1,
|
||||
units = {}
|
||||
}
|
||||
|
||||
function Player:new(pl)
|
||||
player = pl or {}
|
||||
self.__index = self
|
||||
setmetatable(player, self)
|
||||
return player
|
||||
end
|
||||
|
||||
function Player:foo1()
|
||||
print('foo 1')
|
||||
return true
|
||||
@@ -35,12 +18,9 @@ function Player:foo4()
|
||||
return true
|
||||
end
|
||||
|
||||
function Player:update()
|
||||
executeBtNode(self.behaviour)
|
||||
end
|
||||
|
||||
Player.behaviour = {
|
||||
type = BTNodeType.SEQUENCE,
|
||||
children = {
|
||||
{type = BTNodeType.FUNCTION, func = Player.foo3}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@ namespace battleship{
|
||||
VEHICLE_DATA,
|
||||
STRUCTURE_DATA,
|
||||
RESOURCE_DATA,
|
||||
AI_AGENT,
|
||||
PLAYER
|
||||
};
|
||||
const static std::vector<std::string> scripts = std::vector<std::string>{
|
||||
|
||||
@@ -34,19 +34,19 @@ namespace battleship{
|
||||
SOL_LUA_VIEW.script("game.players = {}");
|
||||
|
||||
for(int i = 0; i < players.size(); i++)
|
||||
SOL_LUA_VIEW.script("game.players[" + to_string(i + 1) + "] = Player:new()");
|
||||
SOL_LUA_VIEW["game"]["players"][i + 1] = *players[i];
|
||||
|
||||
for(int i = 0; i < players.size(); i++)
|
||||
if(players[i]->isCpuPlayer())
|
||||
SOL_LUA_VIEW.script("executeBtNode(game.players[" + to_string(i + 1) + "].behaviour)");
|
||||
}
|
||||
|
||||
void Game::update(){
|
||||
resetLuaGameObjects();
|
||||
|
||||
for(int i = 0; i < players.size(); i++){
|
||||
for(int i = 0; i < players.size(); i++)
|
||||
players[i]->update();
|
||||
|
||||
if(players[i]->isCpuPlayer())
|
||||
generateView().script("game.players[" + to_string(i + 1) + "]:update()");
|
||||
}
|
||||
|
||||
for(Projectile *proj : projectiles)
|
||||
proj->update();
|
||||
|
||||
|
||||
+14
-4
@@ -2,7 +2,6 @@
|
||||
|
||||
#include <stateManager.h>
|
||||
#include <inputManager.h>
|
||||
#include <solUtil.h>
|
||||
|
||||
#include <assetManager.h>
|
||||
#include <root.h>
|
||||
@@ -10,6 +9,7 @@
|
||||
#include "gameManager.h"
|
||||
#include "guiAppState.h"
|
||||
#include "defConfigs.h"
|
||||
#include "player.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace vb01;
|
||||
@@ -25,10 +25,15 @@ namespace battleship{
|
||||
return gameManager;
|
||||
}
|
||||
|
||||
void GameManager::start(string gameDir) {
|
||||
path = gameDir + "Assets/";
|
||||
|
||||
void GameManager::registerPlayerMembers(){
|
||||
sol::state_view SOL_LUA_STATE = generateView();
|
||||
sol::usertype<Player> playerType = SOL_LUA_STATE.new_usertype<Player>("Player", sol::constructors<Player(int, int, int, bool, Vector3)>());
|
||||
}
|
||||
|
||||
void GameManager::initLua(string gameDir){
|
||||
sol::state_view SOL_LUA_STATE = generateView();
|
||||
|
||||
path = gameDir + "Assets/";
|
||||
SOL_LUA_STATE.script("PATH = \"" + path + "\";");
|
||||
|
||||
for(string f : configData::scripts)
|
||||
@@ -37,6 +42,11 @@ namespace battleship{
|
||||
sol::table resTable = SOL_LUA_STATE["graphics"]["resolution"];
|
||||
width = resTable["x"];
|
||||
height = resTable["y"];
|
||||
}
|
||||
|
||||
void GameManager::start(string gameDir) {
|
||||
registerPlayerMembers();
|
||||
initLua(gameDir);
|
||||
|
||||
Root *root = Root::getSingleton();
|
||||
root->start(width, height, path + "../external/vb01/", "Battleship");
|
||||
|
||||
@@ -29,6 +29,8 @@ namespace battleship{
|
||||
private:
|
||||
GameManager(){}
|
||||
~GameManager(){}
|
||||
void registerPlayerMembers();
|
||||
void initLua(std::string);
|
||||
|
||||
gameBase::StateManager *stateManager = nullptr;
|
||||
gameBase::InputManager *inputManager = nullptr;
|
||||
|
||||
+1
-20
@@ -11,30 +11,11 @@ namespace battleship{
|
||||
using namespace gameBase;
|
||||
using namespace std;
|
||||
|
||||
Player::Player(int diff, int fac, int t, bool cpuPl, Vector3 sp) : difficulty(diff), faction(fac), team(t), cpuPlayer(cpuPl), spawnPoint(sp) {
|
||||
sol::state_view SOL_LUA_VIEW = generateView();
|
||||
SOL_LUA_VIEW.script("numPlayers = #game.players");
|
||||
luaPlayerId = SOL_LUA_VIEW["numPlayers"];
|
||||
luaPlayerId++;
|
||||
}
|
||||
Player::Player(int diff, int fac, int t, bool cpuPl, Vector3 sp) : difficulty(diff), faction(fac), team(t), cpuPlayer(cpuPl), spawnPoint(sp) {}
|
||||
|
||||
Player::~Player() {}
|
||||
|
||||
void Player::syncLuaPlayer(){
|
||||
string playerStr = "game.players[" + to_string(luaPlayerId) + "].";
|
||||
|
||||
sol::state_view SOL_LUA_VIEW = generateView();
|
||||
SOL_LUA_VIEW.script(playerStr + "refineds = " + to_string(refineds));
|
||||
SOL_LUA_VIEW.script(playerStr + "research = " + to_string(research));
|
||||
SOL_LUA_VIEW.script(playerStr + "wealth = " + to_string(wealth));
|
||||
SOL_LUA_VIEW.script(playerStr + "faction = " + to_string(faction));
|
||||
SOL_LUA_VIEW.script(playerStr + "difficulty = " + to_string(difficulty));
|
||||
SOL_LUA_VIEW.script(playerStr + "team = " + to_string(team));
|
||||
}
|
||||
|
||||
void Player::update() {
|
||||
syncLuaPlayer();
|
||||
|
||||
for(Unit *u : units)
|
||||
u->update();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user