2 r2
.Features
.Ambush
= {}
4 local feature
= r2
.Features
.Ambush
11 feature
.Components
= {}
13 local classAmbushVersion
= 1
15 feature
.Components
.Ambush
=
17 --PropertySheetHeader = r2.getDisplayButtonHeader("r2.events:openEditor()", "uiR2EdEditEventsButton"),
18 BaseClass
="LogicEntity",
21 Menu
="ui:interface:r2ed_feature_menu",
22 Version
=classAmbushVersion
,
24 DisplayerProperties
= "R2::CDisplayerLua",
25 DisplayerPropertiesParams
= "ambushDisplayer",
27 DisplayerUI
= "R2::CDisplayerLua",
28 DisplayerUIParams
= "defaultUIDisplayer",
29 DisplayerVisual
= "R2::CDisplayerVisualEntity",
30 -----------------------------------------------------------------------------------------------
32 ApplicableActions
= {"activate", "deactivate", "trigger"},
33 Events
= {"activation", "deactivation", "trigger"},
34 Conditions
= {"is active", "is inactive"},
38 -----------------------------------------------------------------------------------------------
41 {Name
="InstanceId", Type
="String", WidgetStyle
="StaticText", Visible
= false},
42 {Name
="Name", Type
="String", MaxNumChar
="32"},
43 {Name
="Active", Type
="Number", WidgetStyle
="Boolean", DefaultValue
="1"},
44 {Name
="MobNumber", Type
="Number", Category
="uiR2EDRollout_Mobs", WidgetStyle
="EnumDropDown", Enum
={"1", "2", "3", "4", "5"},
46 {Name
="Mob1Id", Type
="RefId", Category
="uiR2EDRollout_Mobs",PickFunction
="r2:canPickNpcOrGroup", SetRefIdFunction
="r2:setNpcOrGroupRefIdTarget",
47 Visible
= function(this
) return this
:displayRefId(1) end},
48 {Name
="Mob2Id", Type
="RefId", Category
="uiR2EDRollout_Mobs",PickFunction
="r2:canPickNpcOrGroup", SetRefIdFunction
="r2:setNpcOrGroupRefIdTarget",
49 Visible
= function(this
) return this
:displayRefId(2) end},
50 {Name
="Mob3Id", Type
="RefId", Category
="uiR2EDRollout_Mobs",PickFunction
="r2:canPickNpcOrGroup", SetRefIdFunction
="r2:setNpcOrGroupRefIdTarget",
51 Visible
= function(this
) return this
:displayRefId(3) end},
52 {Name
="Mob4Id", Type
="RefId", Category
="uiR2EDRollout_Mobs",PickFunction
="r2:canPickNpcOrGroup", SetRefIdFunction
="r2:setNpcOrGroupRefIdTarget",
53 Visible
= function(this
) return this
:displayRefId(4) end},
54 {Name
="Mob5Id", Type
="RefId", Category
="uiR2EDRollout_Mobs",PickFunction
="r2:canPickNpcOrGroup", SetRefIdFunction
="r2:setNpcOrGroupRefIdTarget",
55 Visible
= function(this
) return this
:displayRefId(5) end},
56 {Name
="TriggerOn", Type
="Number", WidgetStyle
="EnumDropDown",
57 Enum
={"Leaves the zone", "Enters the zone"},
59 {Name
="Components", Type
="Table"},
62 -----------------------------------------------------------------------------------------------
64 getParentTreeNode
= function(this
)
65 return this
:getFeatureParentTreeNode()
67 ---------------------------------------------------------------------------------------------------------
69 appendInstancesByType
= function(this
, destTable
, kind
)
70 assert(type(kind
) == "string")
71 --this:delegate():appendInstancesByType(destTable, kind)
72 r2
.Classes
.LogicEntity
.appendInstancesByType(this
, destTable
, kind
)
73 for k
, component
in specPairs(this
.Components
) do
74 component
:appendInstancesByType(destTable
, kind
)
77 ---------------------------------------------------------------------------------------------------------
79 getSelectBarSons
= function(this
)
82 ---------------------------------------------------------------------------------------------------------
84 canHaveSelectBarSons
= function(this
)
88 onPostCreate
= function(this
)
89 --this:createGhostComponents()
90 if this
.User
.DisplayProp
and this
.User
.DisplayProp
== 1 then
91 r2
:setSelectedInstanceId(this
.InstanceId
)
92 r2
:showProperties(this
)
93 this
.User
.DisplayProp
= nil
98 pretranslate
= function(this
, context
)
99 r2
.Translator
.createAiGroup(this
, context
)
102 translate
= function(this
, context
)
103 r2
.Translator
.translateAiGroup(this
, context
)
104 r2
.Translator
.translateFeatureActivation(this
, context
)
107 updateVersion
= function(this
, scenarioValue
, currentValue
)
114 local component
= feature
.Components
.Ambush
116 function component
:displayRefId(index
)
117 local nbMobs
= self
.MobNumber
+ 1
118 if index
<= nbMobs
then
123 ------------------------------------------------------------------------------------------------------------------
124 local ambushDisplayerTable
= clone(r2
:propertySheetDisplayer())
127 -- If the message is received by a client that didn't request the modification, we must make sure this client
128 -- doesn't modify the data because it has already been performed by the initial client.
130 local function checkPickedEntity(this
, instanceId
, attributeName
)
131 if instanceId
== "" then
134 local tmpInstance
= r2
:getInstanceFromId(instanceId
)
138 local attrName
= "Mob" ..i
.. "Id"
139 if attrName
~= attributeName
and this
[attrName
] == tmpInstance
.InstanceId
then
149 function ambushDisplayerTable
:onAttrModified(instance
, attributeName
)
151 if attributeName
== "MobNumber" then
152 local propertySheet
= r2
:getPropertySheet(instance
)
153 local nbMobs
= instance
.MobNumber
+ 1
157 local name
= "Mob"..tostring(i
).."Id"
158 local refId
= propertySheet
:find(name
)
159 local refIdName
= refId
:find("name")
160 refIdName
.hardtext
= "NONE"
161 r2
.requestSetNode(instance
.InstanceId
, name
, "")
165 propertySheet
.Env
.updatePropVisibility()
168 if string.find(attributeName
, "Id") == nil or attributeName
== "InstanceId" then return end
170 local propertySheet
= r2
:getPropertySheet(instance
)
171 local refId
= propertySheet
:find(attributeName
)
172 if refId
== nil then return end
173 local refIdName
= refId
:find("name")
175 local instanceId
= instance
[attributeName
]
176 if instanceId
== "" then
177 refIdName
.hardtext
= "NONE"
181 local inserted
= checkPickedEntity(instance
, instanceId
, attributeName
)
182 if inserted
== true then
183 local tmpInstance
= r2
:getInstanceFromId(instanceId
)
184 refIdName
.hardtext
= tmpInstance
.Name
186 r2
.requestSetNode(instance
.InstanceId
, attributeName
, "")
188 instance
.User
.onHrcMove
= false
191 function ambushDisplayerTable
:onSelect(instance
, isSelected
)
192 r2
:logicEntityPropertySheetDisplayer():onSelect(instance
, isSelected
)
195 function component
:onTargetInstancePreHrcMove(targetAttr
, targetIndexInArray
)
197 local targetId
= self
[targetAttr
]
198 local tmpInstance
= r2
:getInstanceFromId(targetId
)
199 tmpInstance
.User
.SelfModified
= true
204 local function reattributeIdOnHrcMove(ambush
, group
, targetAttr
)
205 local propertySheet
= r2
:getPropertySheet(ambush
)
206 local refId
= propertySheet
:find(targetAttr
)
207 local refIdName
= refId
:find("name")
209 r2
.requestSetNode(ambush
.InstanceId
, targetAttr
, group
.InstanceId
)
210 refIdName
.hardtext
= group
.Name
211 ambush
.User
.onHrcMove
= true
216 function component
:onTargetInstancePostHrcMove(targetAttr
, targetIndexInArray
)
218 local targetId
= self
[targetAttr
]
220 local tmpInstance
= r2
:getInstanceFromId(targetId
)
223 if tmpInstance
.User
.SelfModified
and tmpInstance
.User
.SelfModified
== true then
224 local group
= tmpInstance
.ParentInstance
225 if group
:isKindOf("NpcGrpFeature") then
226 reattributeIdOnHrcMove(self
, group
, targetAttr
)
228 tmpInstance
.User
.SelfModified
= false
234 function r2
:ambushDisplayer()
235 return ambushDisplayerTable
-- returned shared displayer to avoid wasting memory
237 --------------------------------------------------------------------------------------------------------------------
241 component
.getLogicAction
= function(entity
, context
, action
)
242 assert( action
.Class
== "ActionStep")
243 local component
= r2
:getInstanceFromId(action
.Entity
)
245 local rtNpcGrp
= r2
.Translator
.getRtGroup(context
, component
.InstanceId
)
248 if (action
.Action
.Type
== "trigger") then
250 local spawnActions
= {}
252 local attrName
= "Mob"..i
.."Id"
253 if component
[attrName
] ~= "" then
254 local rtMobGrp
= r2
.Translator
.getRtGroup(context
, component
[attrName
])
255 local actionSpawn
= r2
.Translator
.createAction("spawn", rtMobGrp
.Id
)
256 table.insert(spawnActions
, actionSpawn
)
261 if table.getn(spawnActions
) ~= 0 then
262 local actionTrigger
= r2
.Translator
.createAction("user_event_trigger", rtNpcGrp
.Id
, 6)
263 table.insert(spawnActions
, actionTrigger
)
264 local retAction
= r2
.Translator
.createAction("condition_if", r2
:getNamespace()..rtNpcGrp
.Id
..".Active == 1",
265 r2
.Translator
.createAction("multi_actions", spawnActions
)
267 return retAction
, retAction
271 return r2
.Translator
.getFeatureActivationLogicAction(rtNpcGrp
, action
)
274 component
.getLogicCondition
= function(this
, context
, condition
)
275 assert( condition
.Class
== "ConditionStep")
276 local component
= r2
:getInstanceFromId(condition
.Entity
)
278 local rtNpcGrp
= r2
.Translator
.getRtGroup(context
, component
.InstanceId
)
281 return r2
.Translator
.getFeatureActivationCondition(condition
, rtNpcGrp
)
284 component
.getLogicEvent
= function(this
, context
, event
)
285 assert( event
.Class
== "LogicEntityAction")
287 local component
= this
-- r2:getInstanceFromId(event.Entity)
289 local rtNpcGrp
= r2
.Translator
.getRtGroup(context
, component
.InstanceId
)
292 if tostring(event
.Event
.Type
) == "trigger" then
293 return r2
.Translator
.getComponentUserEvent(rtNpcGrp
, 6)
296 return r2
.Translator
.getFeatureActivationLogicEvent(rtNpcGrp
, event
)
299 component
.createGhostComponents
= function(this
, act
)
305 local propertyName
= "Mob"..id
.."Id"
306 if comp
[propertyName
] ~= nil and comp
[propertyName
] ~= "" then
307 local mob
= r2
:getInstanceFromId(comp
[propertyName
])
310 if mob
:isKindOf("NpcGrpFeature") then
311 local instanceId
= mob
.Components
[0].InstanceId
312 r2
.requestSetGhostNode(instanceId
, "AutoSpawn", 0)
314 r2
.requestSetGhostNode(mob
.InstanceId
, "AutoSpawn", 0)
323 local zoneTrigger
= r2
:getInstanceFromId(comp
._ZoneId
)
327 local type = "On Player Left"
328 if comp
.TriggerOn
== 1 then
329 type = "On Player Arrived"
332 local eventHandler
= r2
.newComponent("LogicEntityAction")
333 eventHandler
.Event
.Type
= type
334 eventHandler
.Event
.Value
= ""
335 eventHandler
.Name
= type
337 local action
= r2
.newComponent("ActionStep")
339 action
.Entity
= r2
.RefId(comp
.InstanceId
)
340 action
.Action
.Type
= "trigger"
341 action
.Action
.Value
= ""
343 table.insert(eventHandler
.Actions
, action
)
345 local behaviorId
= zoneTrigger
.Behavior
.InstanceId
347 r2
.requestInsertGhostNode(behaviorId
, "Actions", -1, "", eventHandler
)
352 local eventHandler
= r2
.newComponent("LogicEntityAction")
353 eventHandler
.Event
.Type
= "activation"
354 eventHandler
.Event
.Value
= ""
355 eventHandler
.Name
= "activation"
357 local action
= r2
.newComponent("ActionStep")
359 action
.Entity
= r2
.RefId(zoneTrigger
.InstanceId
)
360 action
.Action
.Type
= "activate"
361 action
.Action
.Value
= ""
363 table.insert(eventHandler
.Actions
, action
)
365 local behaviorId
= this
.Behavior
.InstanceId
367 r2
.requestInsertGhostNode(behaviorId
, "Actions", -1, "", eventHandler
)
370 local eventHandler
= r2
.newComponent("LogicEntityAction")
371 eventHandler
.Event
.Type
= "deactivation"
372 eventHandler
.Event
.Value
= ""
373 eventHandler
.Name
= "deactivation"
375 local action
= r2
.newComponent("ActionStep")
377 action
.Entity
= r2
.RefId(zoneTrigger
.InstanceId
)
378 action
.Action
.Type
= "deactivate"
379 action
.Action
.Value
= ""
381 table.insert(eventHandler
.Actions
, action
)
383 local behaviorId
= this
.Behavior
.InstanceId
385 r2
.requestInsertGhostNode(behaviorId
, "Actions", -1, "", eventHandler
)
393 component
.createComponent
= function(x
, y
)
395 local comp
= r2
.newComponent("Ambush")
397 assert(comp
.Position
)
399 comp
.Base
= r2
.Translator
.getDebugBase("palette.entities.botobjects.user_event")
400 comp
.Name
= r2
:genInstanceName(i18n
.get("uiR2EdAmbush")):toUtf8()
403 comp
.Position
.z
= r2
:snapZToGround(x
, y
)
405 local zoneTrigger
= r2
.Features
["ZoneTrigger"].Components
.ZoneTrigger
.createComponent(x
+ 3, y
+ 3)
406 zoneTrigger
.Name
= comp
.Name
.." "..i18n
.get("uiR2EDZoneTrigger"):toUtf8()--r2:genInstanceName(i18n.get("uiR2EdZoneTrigger")):toUtf8()
407 zoneTrigger
.InheritPos
= 0
408 zoneTrigger
.Deletable
= false
409 table.insert(comp
.Components
, zoneTrigger
)
410 comp
._ZoneId
= zoneTrigger
.InstanceId
415 component
.create
= function()
416 if not r2
:checkAiQuota() then return end
418 local function posOk(x
, y
, z
)
419 debugInfo("Validate creation of an Ambush.")
420 if r2
.mustDisplayInfo("Ambush") == 1 then
421 r2
.displayFeatureHelp("Ambush")
423 r2
.requestNewAction(i18n
.get("uiR2EDNewAmbushFeatureAction"))
424 local component
= feature
.Components
.Ambush
.createComponent( x
, y
)
425 r2
:setCookie(component
.InstanceId
, "DisplayProp", 1)
426 r2
.requestInsertNode(r2
:getCurrentAct().InstanceId
, "Features", -1, "", component
)
429 local function posCancel() end
431 local creature
= r2
.Translator
.getDebugCreature("object_component_user_event.creature")
432 r2
:choosePos(creature
, posOk
, posCancel
, "createFeatureAmbush")
435 -----------------------------------------
436 --- register the curent Feature to menu
438 function component
:getLogicTranslations()
440 local logicTranslations
= {
441 ["ApplicableActions"] = {
442 ["activate"] = { menu
=i18n
.get( "uiR2AA0Activate" ):toUtf8(),
443 text
=i18n
.get( "uiR2AA1Activate" ):toUtf8()},
444 ["deactivate"] = { menu
=i18n
.get( "uiR2AA0Deactivate" ):toUtf8(),
445 text
=i18n
.get( "uiR2AA1Deactivate" ):toUtf8()},
446 ["trigger"] = { menu
=i18n
.get( "uiR2AA0Trigger" ):toUtf8(),
447 text
=i18n
.get( "uiR2AA1Trigger" ):toUtf8()},
450 ["trigger"] = { menu
=i18n
.get( "uiR2Event0Trigger" ):toUtf8(),
451 text
=i18n
.get( "uiR2Event1Trigger" ):toUtf8()},
452 ["activation"] = { menu
=i18n
.get( "uiR2Event0Activation" ):toUtf8(),
453 text
=i18n
.get( "uiR2Event1Activation" ):toUtf8()},
454 ["deactivation"] = { menu
=i18n
.get( "uiR2Event0Deactivation" ):toUtf8(),
455 text
=i18n
.get( "uiR2Event1Deactivation" ):toUtf8()},
458 ["is active"] = { menu
=i18n
.get( "uiR2Test0Active" ):toUtf8(),
459 text
=i18n
.get( "uiR2Test1Active" ):toUtf8()},
460 ["is inactive"] = { menu
=i18n
.get( "uiR2Test0Inactive" ):toUtf8(),
461 text
=i18n
.get( "uiR2Test1Inactive" ):toUtf8()}
465 return logicTranslations
468 r2
.Features
["Ambush"] = feature