Quantcast
Channel: Homeworld Mods and Modding - The Official Gearbox Software Forums
Viewing all articles
Browse latest Browse all 663

For loop assistance with auto-repairing

$
0
0

@Nathanius wrote:

Hi guys,

I just can’t wrap my head around for loops, here’s what I’ve done for auto-repair in STC but I am unable to get the thing to loop through the proximity group and pick the worst off ship :confused:

Can someone please tell me where my for loop went wrong?


function AutoRepair(CustomGroup, playerIndex, shipID)

   if SOB_tShips[shipID].iRepairShip == 1 then

   	print(">> REPAIR >> | repair ship "..shipID.." of player "..playerIndex.." recognised")

   	if SOB_tShips[shipID].iRepairingShipID == 0 then

   		local nearbyShips = "nearbyShips"
   		SobGroup_Create(nearbyShips)
   		SobGroup_Clear(nearbyShips)

   		Player_FillProximitySobGroup(nearbyShips, playerIndex, CustomGroup, 6000)

   		local nearbyShipCount = 0
   		local mostDamagedShipID = 0
   		local mostDamagePercentage = 1

   		if SobGroup_Count(nearbyShips) >= 0 then

   			nearbyShipCount = SobGroup_Count(nearbyShips)

   		end

   		print(">> REPAIR >> | There is no ship currently assigned to repairship "..shipID.." of player "..playerIndex.." with "..nearbyShipCount.." ships nearby")

   		for i = 1, nearbyShipCount do

   			if SobGroup_HealthPercentage(nearbyShips[i]) < mostDamagePercentage then

   				mostDamagePercentage = SobGroup_HealthPercentage(nearbyShips[i])
   				mostDamagedShipID = GetShipId(nearbyShips[i])

   			end

   		end

   		print(">> REPAIR >> | the most damaged ship near repairship "..shipID.." of player "..playerIndex.." is ship "..mostDamagedShipID)

   		SOB_tShips[shipID].iRepairingShipID = mostDamagedShipID

   		SobGroup_RepairSobGroup(CustomGroup, SOB_tShips[mostDamagedShipID].sob)

   	else

   		if SobGroup_HealthPercentage(SOB_tShips[iRepairingShipID].sob) == 1 then

   			print(">> REPAIR >> | ship "..SOB_tShips[shipID].iRepairingShipID.." is now fully repaired")

   			SOB_tShips[shipID].iRepairingShipID = 0

   		end

   	end

   end

end

I’m hoping to get this working before Thursday night AEST so I can include it in this month’s upload :slight_smile:

Posts: 12

Participants: 3

Read full topic


Viewing all articles
Browse latest Browse all 663

Trending Articles