Page 1 of 1
How to randomize events?
Posted: Thu Aug 13, 2015 11:49 pm
by Robotron
I'd like to include events that show up only with a certain probability. How can this be done?
Let's take the "christmas truce"-event for example. What lines of code have be added to make it appear only with a 50% chance on turn 14?
Code: Select all
-- Christmas truce
function ChristmasTruce()
if GetEvent("ChristmasTruce") == 0 then
if game.turn == 14 then
SetEvent("ChristmasTruce", game.turn)
end
end
end
Any ideas?
Re: How to randomize events?
Posted: Fri Aug 14, 2015 1:26 am
by DanielHerr
You will need to use the math.random function. I haven't tried it inside the game, but this should work:
Code: Select all
function ChristmasTruce()
if GetEvent("ChristmasTruce") == 0 then
if game.turn == 14 then
if math.random(0,1) == 1 then
SetEvent("ChristmasTruce", game.turn)
end
end
end
end
Here is more info on lua math:
http://lua-users.org/wiki/MathLibraryTutorial
Re: How to randomize events?
Posted: Sun Aug 16, 2015 12:48 am
by Robotron
Yeah, that worked like a charm, thanks a bunch.
My next problem: I want to set up an event that drops german morale by ten points if Paris isn't conquered by turn 13 (called the "Moltke"-event, because Chief of Staff General Moltke got sacked after being unable to conquer Paris in time).
This event should be triggered if the Paris-Hex (x82,y30) does NOT belong to germany at the start of turn #13 (the first turn of winter in 1914).
However, this code which I thought should be doing the trick only leads to a crash to desktop...I wonder why?
Code: Select all
function Moltke(hex)
if GetEvent("Moltke") == 0 then
if hex.x == 82 and hex.y == 30 and hex.faction.alliance.id ~= 2 then
if game.turn == 13 then
SetEvent("Moltke", game.turn)
local germany = game:GetFactionById(2)
ChangeFactionMorale(germany, -10)
end
end
end
end
Re: How to randomize events?
Posted: Sun Aug 16, 2015 1:03 am
by DanielHerr
I haven't added an event, so I'm not sure, but I would guess that function is not being passed a hex. I think there is a function that gets the hex by the coordinates that you can use instead. You also might want to look in the log file inside Documents>Games>CtGW
Re: How to randomize events?
Posted: Sun Aug 16, 2015 12:10 pm
by Robotron
Thanks again, DanielHerr, using "local hex = GetHex(82, 30)" it worked indeed. Brilliant!
Code: Select all
--Moltke
function Moltke(hex)
if GetEvent("Moltke") == 0 then
if game.turn >= 13 then
local hex = GetHex(82, 30)
if hex.alliance.id == 1 then
SetEvent("Moltke", game.turn)
local germany = game:GetFactionById(2)
ChangeFactionMorale(germany, -10)
end
end
end
end
