From d1394e9f2a5b6804d2b77893d9f2d105f64868cd Mon Sep 17 00:00:00 2001 From: devZoGok Date: Fri, 9 Feb 2024 20:21:38 +0200 Subject: [PATCH] AI builds a taskforce bugfixes and optimizations --- Assets/Scripts/Core/player.lua | 35 +++++++++++++++++++ Assets/Scripts/GameObjects/Units/unitData.lua | 3 ++ extractor.cpp | 5 ++- factory.cpp | 2 +- mapListbox.cpp | 34 ++++++++++++++++++ player.cpp | 2 +- resourceRover.cpp | 10 +++--- 7 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 mapListbox.cpp diff --git a/Assets/Scripts/Core/player.lua b/Assets/Scripts/Core/player.lua index f8fdb2c..591757c 100644 --- a/Assets/Scripts/Core/player.lua +++ b/Assets/Scripts/Core/player.lua @@ -11,7 +11,11 @@ Player.finishedNavalFactory = false Player.startedExtractor = false Player.finishedExtractor = false Player.startedHarvesting = false +Player.startedBuildingTaskforce = false +Player.finishedBuildingTaskforce = false +--TODO use enum-like values instead of literals for order types +--TODO simplify building construction function Player:buildFort() if self.startedFort then return true @@ -159,6 +163,36 @@ function Player:startHarvesting() return true end +function Player:buildTaskforce() + landFactory = self:getUnitsByClass(UnitClass.LAND_FACTORY, 1)[1]:toFactory() + + numWarMechs = 10 + numTanks = 5 + numArtillery = 5 + + if not self.startedBuildingTaskforce then + self.startedBuildingTaskforce = true + + for i = 0, numWarMechs do + landFactory:appendToQueue(UnitId.WAR_MECH) + end + + for i = 0, numTanks do + landFactory:appendToQueue(UnitId.TANK) + end + + for i = 0, numArtillery do + landFactory:appendToQueue(UnitId.ARTILLERY) + end + end + + currWarMechs = self:getUnitsByClass(UnitClass.WAR_MECH, -1) + currTanks = self:getUnitsByClass(UnitClass.TANK, -1) + currArtillery = self:getUnitsByClass(UnitClass.ARTILLERY, -1) + + return #currWarMechs == numWarMechs and #currTanks == numTanks and #currArtillery == numArtillery +end + Player.behaviour = { type = BTNodeType.SEQUENCE, children = { @@ -169,5 +203,6 @@ Player.behaviour = { {type = BTNodeType.FUNCTION, func = 'buildLandFactory'}, {type = BTNodeType.FUNCTION, func = 'buildHarvester'}, {type = BTNodeType.FUNCTION, func = 'startHarvesting'}, + {type = BTNodeType.FUNCTION, func = 'buildTaskforce'}, } } diff --git a/Assets/Scripts/GameObjects/Units/unitData.lua b/Assets/Scripts/GameObjects/Units/unitData.lua index da3696f..1ab03c5 100644 --- a/Assets/Scripts/GameObjects/Units/unitData.lua +++ b/Assets/Scripts/GameObjects/Units/unitData.lua @@ -196,6 +196,8 @@ units = { unitClass = UnitClass.RESOURCE_ROVER, unitType = UnitType.HOVER, armor = {ArmorType.CAST}, + capacity = 3000, + loadRate = 1, isVehicle = true, health = 500, buildTime = 1000, @@ -516,6 +518,7 @@ units = { isVehicle = false, health = 500, buildTime = 1000, + drawRate = 1, cost = 500, size = {x = 4, y = 1.15, z = 4}, hitboxOffset = {x = 0, y = 0, z = 0}, diff --git a/extractor.cpp b/extractor.cpp index ad6ebec..170991d 100644 --- a/extractor.cpp +++ b/extractor.cpp @@ -11,7 +11,7 @@ namespace battleship{ using namespace vb01; using namespace gameBase; - Extractor::Extractor(Player *player, int id, Vector3 pos, Quaternion rot, int buildStatus, ResourceDeposit *rd) : Structure(player, id, pos, rot, buildStatus), drawRate(10){ + Extractor::Extractor(Player *player, int id, Vector3 pos, Quaternion rot, int buildStatus, ResourceDeposit *rd) : Structure(player, id, pos, rot, buildStatus){ if(!rd){ vector deposits; @@ -27,6 +27,9 @@ namespace battleship{ } } + sol::table unitTable = generateView()["units"][id + 1]; + drawRate = unitTable["drawRate"]; + Vector2 size = Vector2(lenHpBar, 10); ammountBackground = Unit::createBar(Vector2::VEC_ZERO, size, Vector4(0, 0, 0, 1)); ammountForeground = Unit::createBar(Vector2::VEC_ZERO, size, Vector4(0, 0, 1, 1)); diff --git a/factory.cpp b/factory.cpp index 4d1ff5e..4897d91 100644 --- a/factory.cpp +++ b/factory.cpp @@ -35,7 +35,7 @@ namespace battleship{ bool mainPlayerSelecting = (activeState && find(selectingPlayers.begin(), selectingPlayers.end(), mainPlayer) != selectingPlayers.end()); Unit::displayUnitStats(buildStatusForeground, buildStatusBackground, trainingStatus, 100, mainPlayer == player && mainPlayerSelecting, Vector2(0, -10)); - sol::table targTable = generateView()["units"][unitQueue[0]]; + sol::table targTable = generateView()["units"][unitQueue[0] + 1]; int costRate = (int)targTable["cost"] / 100, trainRate = (int)targTable["buildTime"] / 100; if(player->getRefineds() >= costRate && getTime() - lastTrainTime > trainRate){ diff --git a/mapListbox.cpp b/mapListbox.cpp new file mode 100644 index 0000000..f187d3f --- /dev/null +++ b/mapListbox.cpp @@ -0,0 +1,34 @@ +#include "mapListbox.h" +#include "map.h" +#include "concreteGuiManager.h" +#include "gameManager.h" + +#include +#include +#include +#include +#include +#include + +namespace battleship{ + using namespace vb01; + using namespace vb01Gui; + using namespace std; + + MapListbox::MapListbox(Vector2 pos, Vector2 size, std::vector &lines, int maxDisplay, string fontPath, bool closeable) : Listbox(pos, size, lines, maxDisplay, fontPath, closeable){} + + void MapListbox::onClose(){ + ConcreteGuiManager *guiManager = ConcreteGuiManager::getSingleton(); + + Listbox *mapListbox = guiManager->getListboxes()[0]; + int selectedMap = mapListbox->getSelectedOption(); + string mapName = wstringToString(mapListbox->getContents()[selectedMap]); + string mapPath = GameManager::getSingleton()->getPath() + "Models/Maps/" + mapName + "/" + mapName + ".lua"; + int numSpawnPoints = Map::getNumSpawnPointsByMap(mapPath); + + guiManager->readLuaScreenScript("singlePlayerMenu.lua"); + mapListbox = guiManager->getListboxes()[0]; + vector buttons = guiManager->getButtons(); + guiManager->readLuaScreenScript("playerSelection.lua", buttons, vector{mapListbox}, vector{}, vector{}, vector{}, vector{}, vector{}, numSpawnPoints); + } +} diff --git a/player.cpp b/player.cpp index 57dfca1..9b0a223 100755 --- a/player.cpp +++ b/player.cpp @@ -11,7 +11,7 @@ namespace battleship{ using namespace gameBase; using namespace std; - Player::Player(int diff, int fac, int t, Vector3 col, bool cpuPl, Vector3 sp, string n) : difficulty(diff), faction(fac), team(t), color(col), cpuPlayer(cpuPl), spawnPoint(sp), name(n), refineds(10000) {} + Player::Player(int diff, int fac, int t, Vector3 col, bool cpuPl, Vector3 sp, string n) : difficulty(diff), faction(fac), team(t), color(col), cpuPlayer(cpuPl), spawnPoint(sp), name(n), refineds(30000) {} Player::~Player() {} diff --git a/resourceRover.cpp b/resourceRover.cpp index 8be0b70..9c33d70 100644 --- a/resourceRover.cpp +++ b/resourceRover.cpp @@ -13,11 +13,11 @@ namespace battleship{ using namespace vb01; using namespace gameBase; - ResourceRover::ResourceRover(Player *player, int id, Vector3 pos, Quaternion rot) : - Vehicle(player, id, pos, rot), - capacity(300), - loadRate(10) - { + ResourceRover::ResourceRover(Player *player, int id, Vector3 pos, Quaternion rot) : Vehicle(player, id, pos, rot) { + sol::table unitTable = generateView()["units"][id + 1]; + capacity = unitTable["capacity"]; + loadRate = unitTable["loadRate"]; + Vector2 size = Vector2(lenHpBar, 10); loadBackground = Unit::createBar(Vector2::VEC_ZERO, size, Vector4(0, 0, 0, 1)); loadForeground = Unit::createBar(Vector2::VEC_ZERO, size, Vector4(1, 1, 0, 1));