Skip to content

Commit

Permalink
[GameModifiers] Allow delayed effect execution (eg. delayed terrorist…
Browse files Browse the repository at this point in the history
…-sending). Idea: Ratz
  • Loading branch information
GWRon committed Nov 7, 2017
1 parent ebbdf69 commit 37931b8
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 31 deletions.
78 changes: 72 additions & 6 deletions source/game.modifier.base.bmx
Expand Up @@ -45,6 +45,11 @@ Type TGameModifierManager
End Method


Method ContainsModifier:int(modifier:TGameModifierBase)
return modifiers.Contains(modifier)
End Method


Method Remove:Int(modifier:TGameModifierBase)
return modifiers.Remove(modifier)
End Method
Expand Down Expand Up @@ -146,7 +151,8 @@ Type TGameModifierManager
local toRemove:TGameModifierBase[]
For local modifier:TGameModifierBase = EachIn modifiers
'execute run(), undo() and so on
modifier.Update()
'pass "null" as param so cached params are kept
modifier.Update(null)

if modifier.HasExpired() then toRemove :+ [modifier]
Next
Expand Down Expand Up @@ -193,6 +199,8 @@ End Type
'base effect class (eg. for newsevents, programmedata, adcontractbases)
Type TGameModifierBase
Field data:TData
'data passed during an update-call
Field passedParams:TData
Field conditions:TGameModifierCondition[]
'constant value of TVTGameModifierBase (CHANGETREND, TERRORISTATTACK, ...)
Field modifierTypes:int = 0
Expand All @@ -202,6 +210,10 @@ Type TGameModifierBase
Const FLAG_ACTIVATED:int = 2
Const FLAG_EXPIRED:int = 4
Const FLAG_EXPIRATION_DISABLED:int = 8
'a delayed modifier is automatically added to the manager when
'"run" and not in the the managers list
Const FLAG_DELAYED_EXECUTION:int = 16
Const FLAG_DELAY_MANAGED_BY_MANAGER:int = 32


Function CreateNewInstance:TGameModifierBase()
Expand Down Expand Up @@ -325,6 +337,30 @@ Type TGameModifierBase
End Method


Method SetDelayedExecutionTime:int(delayTime:Long)
if delayTime > 0
SetFlag(FLAG_DELAYED_EXECUTION, True)
GetData().AddNumber("delayExecutionUntilTime", delayTime)
else
SetFlag(FLAG_DELAYED_EXECUTION, False)
GetData().Remove("delayExecutionUntilTime")
endif
return True
End Method


Method HasDelayedExecution:int()
return HasFlag(FLAG_DELAYED_EXECUTION)
End Method


Method GetDelayedExecutionTime:Long()
'return "now" or
if not HasDelayedExecution() then return GetWorldTime().GetTimeGone()
return GetData().GetLong("delayExecutionUntilTime", 0)
End Method


Method SetData(data:TData)
local oldName:string = GetName()
self.data = data
Expand All @@ -340,12 +376,30 @@ Type TGameModifierBase
End Method


Method Update:int()
Method Update:int(params:TData)
if params then passedParams = params

'if delayed and delay is in the future, set item to be managed
'by the modifier manager (so it gets updated regularily until
'delay is gone)
if HasDelayedExecution() and GetDelayedExecutionTime() > GetWorldTime().GetTimeGone()
if not HasFlag(FLAG_DELAY_MANAGED_BY_MANAGER)
if not GetGameModifierManager().ContainsModifier(self)
GetGameModifierManager().Add(self)
endif
SetFlag(FLAG_DELAY_MANAGED_BY_MANAGER, True)
endif
return False
endif

'if HasDelayedExecution() then print "effect running now"


local conditionsOK:int = ConditionsFulfilled()
'run if not done yet and needed
if not HasFlag(FLAG_ACTIVATED)
if conditionsOK
Run(null)
Run(passedParams)
endif
endif

Expand All @@ -354,13 +408,15 @@ Type TGameModifierBase
if HasFlag(Flag_ACTIVATED)
if not HasFlag(FLAG_EXPIRATION_DISABLED) and (not conditions or not conditionsOK)
if not HasFlag(FLAG_PERMANENT)
Undo(null)
Undo(passedParams)
endif

if HasFlag(FLAG_EXPIRATION_DISABLED)
return True
else
SetFlag(FLAG_EXPIRED, True)
'reset params?
passedParams = null
return False
endif
endif
Expand Down Expand Up @@ -409,8 +465,7 @@ Type TGameModifierBase


Method UndoFunc:int(params:TData)
print "UndoFunc: " + ToString()

' print "UndoFunc: " + ToString()
return True
End Method

Expand Down Expand Up @@ -629,6 +684,17 @@ Type TGameModifierGroup
End Method


Method Update:int(trigger:string, params:TData)
local l:TList = GetList(trigger)
if not l then return 0

For local modifier:TGameModifierBase = eachin l
modifier.Update(params)
Next

return l.Count()
End Method


Method Run:int(trigger:string, params:TData)
local l:TList = GetList(trigger)
Expand Down
8 changes: 7 additions & 1 deletion source/game.newsagency.base.bmx
Expand Up @@ -461,9 +461,15 @@ Type TNewsAgency
else
effect.GetData().Add("room", GetRoomCollection().GetFirstByDetails("vrduban"))
endif
effect.GetData().AddString("customRunFuncKey", "TFigureTerrorist.SendFigureToRoom")
'mark as a special effect so AI can categorize it accordingly
effect.setModifierType(TVTGameModifierBase.TERRORIST_ATTACK)
'defined function to call when executing
effect.GetData().AddString("customRunFuncKey", "TFigureTerrorist.SendFigureToRoom")

'Variant 1: pass delay to the SendFigureToRoom-function (delay delivery schedule)
'effect.GetData().AddNumber("delayTime", 60 * RandRange(45,120))
'Variant 2: delay the execution of the effect
effect.SetDelayedExecutionTime(60 * RandRange(45,120))

NewsEvent.effects.AddEntry("happen", effect)
endif
Expand Down
16 changes: 8 additions & 8 deletions source/game.programme.adcontract.bmx
Expand Up @@ -1754,25 +1754,25 @@ price :* Max(1, minAudience/1000)
'finishBroadcast - after "onFinishBroadcasting"-call)
if base.GetTimesBroadcasted() = 0
If not base.hasBroadcastFlag(TVTBroadcastMaterialSourceFlag.BROADCAST_FIRST_TIME_DONE)
base.effects.Run("broadcastFirstTimeDone", effectParams)
base.effects.Update("broadcastFirstTimeDone", effectParams)
base.setBroadcastFlag(TVTBroadcastMaterialSourceFlag.BROADCAST_FIRST_TIME_DONE, True)
endif
endif

base.effects.Run("broadcastDone", effectParams)
base.effects.Update("broadcastDone", effectParams)

'send as infomercial
elseif broadcastType = TVTBroadcastMaterialType.PROGRAMME
'if nobody broadcasted till now (times are adjusted on
'finishBroadcast while this is called on beginBroadcast)
if base.GetTimesBroadcastedAsInfomercial() = 0
If not base.hasBroadcastFlag(TVTBroadcastMaterialSourceFlag.BROADCAST_FIRST_TIME_SPECIAL_DONE)
base.effects.Run("broadcastFirstTimeInfomercialDone", effectParams)
base.effects.Update("broadcastFirstTimeInfomercialDone", effectParams)
base.setBroadcastFlag(TVTBroadcastMaterialSourceFlag.BROADCAST_FIRST_TIME_SPECIAL_DONE, True)
endif
endif

base.effects.Run("broadcastInfomercialDone", effectParams)
base.effects.Update("broadcastInfomercialDone", effectParams)
endif
End Method

Expand All @@ -1790,24 +1790,24 @@ price :* Max(1, minAudience/1000)
'finishBroadcast while this is called on beginBroadcast)
if base.GetTimesBroadcasted() = 0
If not base.hasBroadcastFlag(TVTBroadcastMaterialSourceFlag.BROADCAST_FIRST_TIME)
base.effects.Run("broadcastFirstTime", effectParams)
base.effects.Update("broadcastFirstTime", effectParams)
base.setBroadcastFlag(TVTBroadcastMaterialSourceFlag.BROADCAST_FIRST_TIME, True)
endif
endif

base.effects.Run("broadcast", effectParams)
base.effects.Update("broadcast", effectParams)

elseif broadcastType = TVTBroadcastMaterialType.PROGRAMME
'if nobody broadcasted till now (times are adjusted on
'finishBroadcast while this is called on beginBroadcast)
if base.GetTimesBroadcastedAsInfomercial() = 0
If not base.hasBroadcastFlag(TVTBroadcastMaterialSourceFlag.BROADCAST_FIRST_TIME_SPECIAL)
base.effects.Run("broadcastFirstTimeInfomercial", effectParams)
base.effects.Update("broadcastFirstTimeInfomercial", effectParams)
base.setBroadcastFlag(TVTBroadcastMaterialSourceFlag.BROADCAST_FIRST_TIME_SPECIAL, True)
endif
endif

base.effects.Run("broadcastInfomercial", effectParams)
base.effects.Update("broadcastInfomercial", effectParams)
endif
End Method

Expand Down
10 changes: 5 additions & 5 deletions source/game.programme.newsevent.bmx
Expand Up @@ -621,7 +621,7 @@ Type TNewsEvent extends TBroadcastMaterialSource {_exposeToLua="selected"}

'trigger happenEffects
local effectParams:TData = new TData.Add("source", self)
effects.Run("happen", effectParams)
effects.Update("happen", effectParams)
endif
End Method

Expand All @@ -638,12 +638,12 @@ Type TNewsEvent extends TBroadcastMaterialSource {_exposeToLua="selected"}
'finishBroadcast while this is called on beginBroadcast)
if GetTimesBroadcasted() = 0
If not hasBroadcastFlag(TVTBroadcastMaterialSourceFlag.BROADCAST_FIRST_TIME)
effects.Run("broadcastFirstTime", effectParams)
effects.Update("broadcastFirstTime", effectParams)
setBroadcastFlag(TVTBroadcastMaterialSourceFlag.BROADCAST_FIRST_TIME, True)
endif
endif

effects.Run("broadcast", effectParams)
effects.Update("broadcast", effectParams)
End Method


Expand All @@ -656,12 +656,12 @@ Type TNewsEvent extends TBroadcastMaterialSource {_exposeToLua="selected"}
'finishBroadcast while this is called on beginBroadcast)
if GetTimesBroadcasted() = 0
If not hasBroadcastFlag(TVTBroadcastMaterialSourceFlag.BROADCAST_FIRST_TIME_DONE)
effects.Run("broadcastFirstTimeDone", effectParams)
effects.Update("broadcastFirstTimeDone", effectParams)
setBroadcastFlag(TVTBroadcastMaterialSourceFlag.BROADCAST_FIRST_TIME_DONE, True)
endif
endif

effects.Run("broadcastDone", effectParams)
effects.Update("broadcastDone", effectParams)
End Method


Expand Down
22 changes: 11 additions & 11 deletions source/game.programme.programmedata.bmx
Expand Up @@ -1816,7 +1816,7 @@ Type TProgrammeData extends TBroadcastMaterialSource {_exposeToLua}
Method onProductionStart:int(time:Long = 0)
'trigger effects/modifiers
local params:TData = new TData.Add("source", self)
effects.Run("productionStart", params)
effects.Update("productionStart", params)

return True
End Method
Expand Down Expand Up @@ -1910,25 +1910,25 @@ Type TProgrammeData extends TBroadcastMaterialSource {_exposeToLua}
'finishBroadcast - after "onFinishBroadcasting"-call)
if GetTimesBroadcasted() = 0
If not hasBroadcastFlag(TVTBroadcastMaterialSourceFlag.BROADCAST_FIRST_TIME_DONE)
effects.Run("broadcastFirstTimeDone", effectParams)
effects.Update("broadcastFirstTimeDone", effectParams)
setBroadcastFlag(TVTBroadcastMaterialSourceFlag.BROADCAST_FIRST_TIME_DONE, True)
endif
endif

effects.Run("broadcastDone", effectParams)
effects.Update("broadcastDone", effectParams)

'send as trailer
elseif broadcastType = TVTBroadcastMaterialType.ADVERTISEMENT
'if nobody broadcasted till now (times are adjusted on
'finishBroadcast while this is called on beginBroadcast)
if GetTimesTrailerAired() = 0
If not hasBroadcastFlag(TVTBroadcastMaterialSourceFlag.BROADCAST_FIRST_TIME_SPECIAL_DONE)
effects.Run("broadcastFirstTimeTrailerDone", effectParams)
effects.Update("broadcastFirstTimeTrailerDone", effectParams)
setBroadcastFlag(TVTBroadcastMaterialSourceFlag.BROADCAST_FIRST_TIME_SPECIAL_DONE, True)
endif
endif

effects.Run("broadcastTrailerDone", effectParams)
effects.Update("broadcastTrailerDone", effectParams)
endif
End Method

Expand All @@ -1952,11 +1952,11 @@ Type TProgrammeData extends TBroadcastMaterialSource {_exposeToLua}

'send as programme
if broadcastType = TVTBroadcastMaterialType.PROGRAMME
effects.Run("broadcastAborted", effectParams)
effects.Update("broadcastAborted", effectParams)

'send as trailer
elseif broadcastType = TVTBroadcastMaterialType.ADVERTISEMENT
effects.Run("broadcastTrailerAborted", effectParams)
effects.Update("broadcastTrailerAborted", effectParams)
endif
End Method

Expand Down Expand Up @@ -1986,25 +1986,25 @@ Type TProgrammeData extends TBroadcastMaterialSource {_exposeToLua}
'finishBroadcast - after "onFinishBroadcasting"-call)
if GetTimesBroadcasted() = 0
If not hasBroadcastFlag(TVTBroadcastMaterialSourceFlag.BROADCAST_FIRST_TIME)
effects.Run("broadcastFirstTime", effectParams)
effects.Update("broadcastFirstTime", effectParams)
setBroadcastFlag(TVTBroadcastMaterialSourceFlag.BROADCAST_FIRST_TIME, True)
endif
endif

effects.Run("broadcast", effectParams)
effects.Update("broadcast", effectParams)

'send as trailer
elseif broadcastType = TVTBroadcastMaterialType.ADVERTISEMENT
'if nobody broadcasted till now (times are adjusted on
'finishBroadcast while this is called on beginBroadcast)
if GetTimesTrailerAired() = 0
If not hasBroadcastFlag(TVTBroadcastMaterialSourceFlag.BROADCAST_FIRST_TIME_SPECIAL)
effects.Run("broadcastFirstTimeTrailer", effectParams)
effects.Update("broadcastFirstTimeTrailer", effectParams)
setBroadcastFlag(TVTBroadcastMaterialSourceFlag.BROADCAST_FIRST_TIME_SPECIAL, True)
endif
endif

effects.Run("broadcastTrailer", effectParams)
effects.Update("broadcastTrailer", effectParams)
endif
End Method

Expand Down

0 comments on commit 37931b8

Please sign in to comment.