improvements to reform taskforces upon destruction

This commit is contained in:
devZoGok
2026-03-15 15:21:11 +02:00
parent 6c55a255c0
commit a61f3eebdf
5 changed files with 46 additions and 31 deletions
+14 -29
View File
@@ -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
if #self.taskForces[tfId].units > 0 and self.taskForces[tfId].arrived then
return BTNodeResult.SUCCESS
end
elseif #self.taskForces[tfId].units == 0 then
table.remove(self.taskForces, tfId)
return BTNodeResult.FAILURE
end
-1
View File
@@ -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,
+16
View File
@@ -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<Player*> cpuPlayers = Game::getSingleton()->getCpuPlayers();
for(int i = 0; i < cpuPlayers.size(); i++)
if(cpuPlayers[i] == this)
return i;
return -1;
}
}
+1
View File
@@ -43,6 +43,7 @@ namespace battleship{
std::vector<Unit*> getFriendlyUnits(bool = true);
std::vector<Unit*> getHostileUnits();
bool isObjectVisible(GameObject*, std::vector<Unit*>);
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;}
+14
View File
@@ -306,6 +306,20 @@ namespace battleship{
if(!currOrderStarted) startCurrentOrder();
if(!orders[0].targets.empty() && orders[0].targets[0].unit){
vector<Unit*> units;
for(Player *pl : Game::getSingleton()->getPlayers(true)){
vector<Unit*> 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]);