More Lua Help

RenaissanceFan

Warlord
Joined
Mar 15, 2011
Messages
125
Hello again. I'm trying to write a script that does the following, line by line, assigned to the RunCombatSim event:

1) Checks if the attacking unit was a galleon;
2) Checks if the defending unit's domain was sea;
3) Checks if the damage was enough to destroy the unit;
4) Random 70% chance for getting 25:c5gold:;
5) Awards the gold.

Could you take a look and see if it would work? Also, what to script so it prints "Your Galleon has earned 25 (gold icon) from the (enemy ship unit type).? The parts I need help are in italic.

Code:
function GalleonAwardGold(iAttackingPlayer, iAttackingUnit, iAttackingUnitDamage, iAttackingUnitFinalDamage, iAttackingUnitMaxHitPoints, iDefendingPlayer, iDefendingUnit, iDefendingUnitDamage, iDefendingUnitFinalDamage, iDefendingUnitMaxHitPoints, bContinuation)

	if (iAttackingUnit:GetUnitType() == GameInfoTypes["UNIT_GALLEON"]) then
		if (iDefendingUnit:GetDomainType() == GameInfoTypes["DOMAIN_SEA"]) then
			if (iDefendingUnitFinalDamage >= iDefendingUnitMaxHitPoints) then
				if (Game.Rand(100, "Galleon Award Chance") < 70) then
					local iGold = Game.Rand(25, "Galleon Award Value")
					iAttackingPlayer.ChangeGold(iGold)
				end
			end
		end
	end
end
Events.RunCombatSim.Add( GalleonAwardGold );

Thanks in advance.
 
Sorry, that just won't work, for several reasons. I tried spelling out most of these in the previous thread, but here goes.

1> iAttackingUnit and iDefendingUnit are indices, not structures. They're the temporary ID numbers for the individual units, and will usually be a 5-digit number. Also, unit numbers are only unique within a single player, so each civilization can have its own unit number; you MUST check only within that player's unit list.

You either need to use the Players:GetUnitByID() function, or do the usual double-loop method (loop over players, loop over units that player owns, check each ID). Either works, and which is better depends on what else you're doing.

2> Likewise, the iAttackingPlayer and iDefendingPlayer are the players' ID numbers. You can't treat them like structures, although at least that's easier, because it's just Players[iAttackingPlayer]:Whatever().

3> You have to check to see if the Attacking Unit or Defending Unit were cities. (That is, if it's a unit attacking a city or a city being bombarded.) The iAttackingUnit or iDefendingUnit will be -1 if it's a city. You MUST check for this before trying to extract a Unit structure, or it can crash.

4> The way you wrote that, the Galleon would only get the bonus if it's the attacker. You should duplicate the logic to see if it happens while defending as well.
 
4> The way you wrote that, the Galleon would only get the bonus if it's the attacker. You should duplicate the logic to see if it happens while defending as well.

Well, I didn't put that because ship attacks are one-sided. I mean, when defending, ships don't shoot back. Also, take a look at the changes:

Code:
function GalleonAwardGold(iAttackingPlayer, iAttackingUnit, iAttackingUnitDamage, iAttackingUnitFinalDamage, iAttackingUnitMaxHitPoints, iDefendingPlayer, iDefendingUnit, iDefendingUnitDamage, iDefendingUnitFinalDamage, iDefendingUnitMaxHitPoints, bContinuation)

	if (iAttackingUnit == -1 or iDefendingUnit == -1) then 
		break
	end

	local iAttUnit = Players[iAttackingPlayer]:GetUnitByID(iAttackingUnit)
	local iDefUnit = Players[iDefendingPlayer]:GetUnitByID(iDefendingUnit)

	if (iAttUnit:GetUnitType() == GameInfoTypes["UNIT_GALLEON"]) then
		if (iDefUnit:GetDomainType() == GameInfoTypes["DOMAIN_SEA"]) then
			if (iDefendingUnitFinalDamage >= iDefendingUnitMaxHitPoints) then
				if (Game.Rand(100, "Galleon Award Chance") < 70) then
					local iGold = 25
					Players[iAttackingPlayer]:ChangeGold(iGold)
					Print("Your Galleon has earned 25 gold.")
				end
			end
		end
	end
end
Events.RunCombatSim.Add( GalleonAwardGold );
 
Well, I didn't put that because ship attacks are one-sided. I mean, when defending, ships don't shoot back.

Not normally, no, unless a mod has turned on the Supporting Fire logic in GlobalDefines, or added a ranged counterattack system like previous games had, or you have a naval unit that doesn't have PROMOTION_ONLY_DEFENSIVE (which disables the Melee attack that all units would otherwise have). But yeah, for now you're probably safe.

As for the code, it looks fine (other than the fact that I don't like using a Break command), although the Print statement will only show up in FireTuner. If you want some kind of in-game notification, you have to tie it to the existing notification system and/or the Custom Notifications that people have modded in.

So try it and see if it works.
 
Not normally, no, unless a mod has turned on the Supporting Fire logic in GlobalDefines, or added a ranged counterattack system like previous games had, or you have a naval unit that doesn't have PROMOTION_ONLY_DEFENSIVE (which disables the Melee attack that all units would otherwise have). But yeah, for now you're probably safe.

As for the code, it looks fine (other than the fact that I don't like using a Break command), although the Print statement will only show up in FireTuner. If you want some kind of in-game notification, you have to tie it to the existing notification system and/or the Custom Notifications that people have modded in.

So try it and see if it works.

Thanks. As for the notification, how do you write it so that a message appears in the top of the screen, like when you declare war ("You have declared war on player)? The message is: "Your Galleon has earned 25:c5gold: from the enemy ship). It needs to see what kind of ship it was.

And what command should the script use instead of 'break'?
 
Back
Top Bottom