From a61f3eebdf200ef06134fa1ff522c88fc9b9b555 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Thu, 5 Mar 2026 20:10:58 +0200 Subject: [PATCH] improvements to reform taskforces upon destruction --- Assets/Scripts/Core/player.lua | 45 ++++++++++-------------------- Assets/Scripts/Core/playerInit.lua | 1 - player.cpp | 16 +++++++++++ player.h | 1 + unit.cpp | 14 ++++++++++ 5 files changed, 46 insertions(+), 31 deletions(-) diff --git a/Assets/Scripts/Core/player.lua b/Assets/Scripts/Core/player.lua index 56babb3..c944d28 100644 --- a/Assets/Scripts/Core/player.lua +++ b/Assets/Scripts/Core/player.lua @@ -237,12 +237,12 @@ function Player:getBuildableUnitSlotId(buildingUnit, buildableUnitId) end function Player:updateTaskForces(arguments) - units = self:getUnits() + numUnits = self:getNumUnits() for i = 1, #self.taskForces do for j = #self.taskForces[i].units, 1, -1 do - for k = 1, #units do - if self.taskForces[i].units[j] == units[k] then goto continue end + for k = 1, numUnits do + if self.taskForces[i].units[j] == self:getUnit(k - 1) then goto continue end end table.remove(self.taskForces[i].units, j) @@ -339,25 +339,6 @@ function Player:buildExtractors(arguments) return BTNodeResult.RUNNING end -function Player:buildHarvester(arguments) - if #self:getUnitsByClass(UnitClass.RESOURCE_ROVER, 1) > 0 then - return BTNodeResult.SUCCESS - end - - factory = self:getUnitsByClass(UnitClass.LAND_FACTORY, 1)[1] - - if not factory then return BTNodeResult.FAILURE end - - factory = factory:toFactory() - roverSlotId = self.unitClassMappings[UnitClass.RESOURCE_ROVER + 1][self:getFaction() + 1] - - if #factory:getQueue() == 0 then - factory:appendToQueue(self:getBuildableUnitSlotId(factory, roverSlotId)) - end - - return BTNodeResult.RUNNING -end - function Player:startHarvesting() harvesters = self:getUnitsByClass(UnitClass.RESOURCE_ROVER, -1) harvester = nil @@ -365,7 +346,7 @@ function Player:startHarvesting() for i = 1, #harvesters do order = (harvesters[i]:getNumOrders() > 0 and harvesters[i]:getOrder(0) or nil) - if i == #harvesters and order.type == OrderType.SUPPLY then + if i == #harvesters and order and order.type == OrderType.SUPPLY then return BTNodeResult.SUCCESS elseif not order or (order and order.type ~= OrderType.SUPPLY) then harvester = harvesters[i] @@ -383,10 +364,13 @@ function Player:startHarvesting() end minDistId = 1 + hPos = harvester:getPos() + for i = 1, #extractors do - if extractors[i]:getPos():getDistanceFrom(harvester:getPos()) < extractors[minDistId]:getPos():getDistanceFrom(harvester:getPos()) then - minDistId = i - end + currDist = extractors[i]:getPos():getDistanceFrom(hPos) + minDist = extractors[minDistId]:getPos():getDistanceFrom(hPos) + + if currDist < minDist then minDistId = i end end self:deselectUnits() @@ -612,15 +596,16 @@ end function Player:occupySpawnPoint(arguments) tfId = arguments.taskForceId + if not self.taskForces[tfId] then return BTNodeResult.FAILURE end + if not self.spawnPointData[self.taskForces[tfId].spawnPointId + 1].reachable and not self.taskForces[tfId].landed then return BTNodeResult.FAILURE end - if #self.taskForces[tfId].units > 0 then - if self.taskForces[tfId].arrived then - return BTNodeResult.SUCCESS - end + if #self.taskForces[tfId].units > 0 and self.taskForces[tfId].arrived then + return BTNodeResult.SUCCESS elseif #self.taskForces[tfId].units == 0 then + table.remove(self.taskForces, tfId) return BTNodeResult.FAILURE end diff --git a/Assets/Scripts/Core/playerInit.lua b/Assets/Scripts/Core/playerInit.lua index a6fce0c..2d67779 100644 --- a/Assets/Scripts/Core/playerInit.lua +++ b/Assets/Scripts/Core/playerInit.lua @@ -5,7 +5,6 @@ for i = 1, #game.cpuPlayers do player.behaviour = { type = BTNodeType.SEQUENCE, children = { - --{type = BTNodeType.FUNCTION, func = player.updateTaskForces}, {type = BTNodeType.FUNCTION, func = player.buildLandFactory}, { type = BTNodeType.FUNCTION, diff --git a/player.cpp b/player.cpp index 72efd66..16558b2 100755 --- a/player.cpp +++ b/player.cpp @@ -154,6 +154,12 @@ namespace battleship{ delete units[id]; units.erase(units.begin() + id); + + if(cpuPlayer){ + sol::state_view SOL_LUA_VIEW = generateView(); + int id = getCpuPlayerId() + 1; + SOL_LUA_VIEW.script("game.cpuPlayers[" + to_string(id) + "]:updateTaskForces()"); + } } void Player::removeResourceDeposit(int id){ @@ -339,4 +345,14 @@ namespace battleship{ return false; } + + int Player::getCpuPlayerId(){ + vector cpuPlayers = Game::getSingleton()->getCpuPlayers(); + + for(int i = 0; i < cpuPlayers.size(); i++) + if(cpuPlayers[i] == this) + return i; + + return -1; + } } diff --git a/player.h b/player.h index 3fc2f55..13afc78 100755 --- a/player.h +++ b/player.h @@ -43,6 +43,7 @@ namespace battleship{ std::vector getFriendlyUnits(bool = true); std::vector getHostileUnits(); bool isObjectVisible(GameObject*, std::vector); + int getCpuPlayerId(); inline int getResource(ResourceType rt){return resources[(int)rt];} inline void updateResource(ResourceType rt, int amount, bool add){resources[(int)rt] = (add ? resources[(int)rt] + amount : amount);} inline Trader* getTrader(){return trader;} diff --git a/unit.cpp b/unit.cpp index 02a550f..70e4537 100755 --- a/unit.cpp +++ b/unit.cpp @@ -306,6 +306,20 @@ namespace battleship{ if(!currOrderStarted) startCurrentOrder(); + if(!orders[0].targets.empty() && orders[0].targets[0].unit){ + vector units; + + for(Player *pl : Game::getSingleton()->getPlayers(true)){ + vector us = pl->getUnits(); + units.insert(units.end(), us.begin(), us.end()); + } + + if(find(units.begin(), units.end(), orders[0].targets[0].unit) == units.end()){ + removeOrder(0); + return; + } + } + switch (orders[0].type) { case Order::TYPE::ATTACK: attack(orders[0]);