3 -- Category : uiR2EdCounter --
4 -- CreationFrom : uiR2EdCounterParameters
7 r2
.Features
.CounterFeature
= {}
9 local feature
= r2
.Features
.CounterFeature
11 feature
.Name
="Counter"
13 feature
.Description
="A Counter"
16 function feature
.registerForms()
17 r2
.Forms
.CounterFeatureForm
=
19 Caption
= "uiR2EdCounterParameters",
22 -- following field are tmp for property sheet building testing
23 {Name
="TriggerValue", Type
="Number", Category
="uiR2EDRollout_CounterFeature", Min
="0", Default
="0"},
24 {Name
="Value", Type
="Number", Category
="uiR2EDRollout_CounterFeature", Min
="1", Default
="1"}
30 feature
.Components
= {}
32 feature
.Components
.Counter
=
34 BaseClass
="LogicEntity",
36 Menu
="ui:interface:r2ed_feature_menu",
38 DisplayerUI
= "R2::CDisplayerLua",
39 DisplayerUIParams
= "defaultUIDisplayer",
40 DisplayerVisual
= "R2::CDisplayerVisualEntity",
41 -----------------------------------------------------------------------------------------------
44 ApplicableActions
= { "Activate", "Deactivate", "Increment", "Decrement", "Trigger"},
46 Events
= { "On Trigger", "On Activation", "On Deactivation"},
48 Conditions
= { "is active"},
58 {Name
="InstanceId", Type
="String", WidgetStyle
="StaticText"},
59 {Name
="Name", Type
="String", MaxNumChar
="32"},
60 {Name
="Components", Type
="Table"},
61 {Name
="Value", Type
="Number"},
62 {Name
="TriggerValue", Type
="Number", Min
="0", Default
="0"}
65 getParentTreeNode
= function(this
)
66 return this
:getFeatureParentTreeNode()
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
)
78 getSelectBarSons
= function(this
)
82 canHaveSelectBarSons
= function(this
)
86 onPostCreate
= function(this
)
87 --feature.createFeatureLocally(this)
90 getAvailableCommands
= function(this
, dest
)
91 r2
.Classes
.LogicEntity
.getAvailableCommands(this
, dest
) -- fill by ancestor
92 this
:getAvailableDisplayModeCommands(dest
)
95 translate
= function(this
, context
)
96 local initValue
= this
.Value
97 local trigValue
= this
.TriggerValue
99 r2
.Translator
.translateAiGroup(this
, context
)
101 local rtNpcGrp
= r2
.Translator
.getRtGroup(context
, this
.InstanceId
)
103 local action1
= r2
.Translator
.createAction("counter_init", rtNpcGrp
.Id
, initValue
, trigValue
)
104 local action2
= r2
.Translator
.createAction("generic_event_trigger", rtNpcGrp
.Id
, 0)
105 local action
= r2
.Translator
.createAction("multi_actions", {action1
, action2
})
107 r2
.Translator
.translateAiGroupInitialState(this
, context
, action
)
110 pretranslate
= function(this
, context
)
111 r2
.Translator
.createAiGroup(this
, context
)
115 local component
= feature
.Components
.Counter
118 function component
.getLogicActionTrigger(entity
, context
, action
, rtNpcGrp
)
119 local retAction
= r2
.Translator
.createAction("generic_event_trigger", rtNpcGrp
.Id
, 3)
121 return retAction
, retAction
124 component
.getLogicAction
= function(entity
, context
, action
)
126 assert( action
.Class
== "ActionStep")
127 local component
= r2
:getInstanceFromId(action
.Entity
)
129 local rtNpcGrp
= r2
.Translator
.getRtGroup(context
, component
.InstanceId
)
132 if (action
.Action
.Type
== "Activate") then
133 local action1
= r2
.Translator
.createAction("counter_enable", rtNpcGrp
.Id
, 0)
134 local action2
= r2
.Translator
.createAction("generic_event_trigger", rtNpcGrp
.Id
, 0)
135 local retAction
= r2
.Translator
.createAction("multi_actions", {action1
, action2
})
136 return retAction
, retAction
137 elseif (action
.Action
.Type
== "Deactivate") then
138 local action1
= r2
.Translator
.createAction("counter_disable", rtNpcGrp
.Id
, 0)
139 local action2
= r2
.Translator
.createAction("generic_event_trigger", rtNpcGrp
.Id
, 1)
140 local retAction
= r2
.Translator
.createAction("multi_actions", {action1
, action2
})
141 return retAction
, retAction
142 elseif (action
.Action
.Type
== "Increment") then
143 local action1
= r2
.Translator
.createAction("counter_inc", rtNpcGrp
.Id
, 0)
144 return action1
, action1
145 elseif (action
.Action
.Type
== "Decrement") then
146 local action1
= r2
.Translator
.createAction("counter_dec", rtNpcGrp
.Id
, 0)
147 return action1
, action1
148 elseif (action
.Action
.Type
== "Trigger") then
149 local retAction
= r2
.Translator
.createAction("generic_event_trigger", rtNpcGrp
.Id
, 3)
151 return retAction
, retAction
154 local firstAction
, lastAction
= nil, nil
155 return firstAction
, lastAction
158 component
.getLogicCondition
= function(this
, context
, condition
)
160 assert( condition
.Class
== "ConditionStep")
161 local component
= r2
:getInstanceFromId(condition
.Entity
)
163 local rtNpcGrp
= r2
.Translator
.getRtGroup(context
, component
.InstanceId
)
166 if condition
.Condition
.Type
== "is active" then
167 local action1
= r2
.Translator
.createAction("counter_is_enable", rtNpcGrp
.Id
, 0);
168 return action1
, action1
175 component
.getLogicEvent
= function(this
, context
, event
)
176 assert( event
.Class
== "LogicEntityAction")
178 local component
= this
-- r2:getInstanceFromId(event.Entity)
180 local rtNpcGrp
= r2
.Translator
.getRtGroup(context
, component
.InstanceId
)
183 local eventType
= tostring(event
.Event
.Type
)
185 local eventHandler
, lastCondition
= nil, nil
187 if eventType
== "On Trigger" then
188 return r2
.Translator
.getComponentGenericEvent(rtNpcGrp
, 3)
189 elseif eventType
== "On Activation" then
190 return r2
.Translator
.getComponentGenericEvent(rtNpcGrp
, 0)
191 elseif eventType
== "On Deactivation" then
192 return r2
.Translator
.getComponentGenericEvent(rtNpcGrp
, 1)
195 return eventHandler
, firstCondition
, lastCondition
201 component
.createComponent
= function(x
, y
, value
, tvalue
)
203 local comp
= r2
.newComponent("Counter")
206 comp
.Base
= "palette.entities.botobjects.milestone"
207 comp
.Name
= r2
:genInstanceName(i18n
.get("uiR2EDRollout_CounterFeature")):toUtf8()
211 comp
.Position
.z
= r2
:snapZToGround(x
, y
)
212 comp
.TriggerValue
= tvalue
215 comp
._Seed
= os
.time()
220 component
.create
= function()
221 r2
.requestNewAction(i18n
.get("uiR2EDCreateCounterFeatureAction"))
224 if not r2
:checkAiQuota() then return end
227 local function paramsOk(resultTable
)
229 local x
= tonumber( resultTable
["X"] )
230 local y
= tonumber( resultTable
["Y"] )
231 local tvalue
= tonumber( resultTable
["TriggerValue"] )
232 local value
= tonumber( resultTable
["Value"] )
234 if not x
or not y
or not tvalue
or not value
236 debugInfo("Can't create Component")
239 local component
= feature
.Components
.CounterFeature
.createComponent( x
, y
, value
, tvalue
)
240 r2
.requestInsertNode(r2
:getCurrentAct().InstanceId
, "Features", -1, "", component
)
241 --component.TriggerValue = tvalue
244 local function paramsCancel()
245 debugInfo("Cancel form for 'CounterFeature' creation")
247 local function posOk(x
, y
, z
)
248 debugInfo(string.format("Validate creation of 'CounterFeature' at pos (%d, %d, %d)", x
, y
, z
))
249 r2
:doForm("CounterFeatureForm", {X
=x
, Y
=y
}, paramsOk
, paramsCancel
)
251 local function posCancel()
252 debugInfo("Cancel choice 'CounterFeature' position")
254 r2
:choosePos("object_milestone.creature", posOk
, posCancel
, "createFeatureBanditCamp")
257 function component
:registerMenu(logicEntityMenu
)
258 local name
= i18n
.get("uiR2EDRollout_CounterFeature")
259 logicEntityMenu
:addLine(ucstring(name
), "lua", "", "CounterFeature")
262 function component
:getLogicTranslations()
263 local logicTranslations
= {
264 ["ApplicableActions"] = {
265 ["Activate"] = { menu
=i18n
.get( "uiR2AA0Activate" ):toUtf8(),
266 text
=i18n
.get( "uiR2AA1Activate" ):toUtf8()},
267 ["Desactivate"] = { menu
=i18n
.get( "uiR2AA0Deactivate" ):toUtf8(),
268 text
=i18n
.get( "uiR2AA1Deactivate" ):toUtf8()},
269 ["Increment"] = { menu
=i18n
.get( "uiR2AA0CounterIncrement" ):toUtf8(),
270 text
=i18n
.get( "uiR2AA1CounterIncrement" ):toUtf8()},
271 ["Decrement"] = { menu
=i18n
.get( "uiR2AA0CounterDecrement" ):toUtf8(),
272 text
=i18n
.get( "uiR2AA1CounterDecrement" ):toUtf8()},
275 ["activation"] = { menu
=i18n
.get( "uiR2Event0Activation" ):toUtf8(),
276 text
=i18n
.get( "uiR2Event1Activation" ):toUtf8()},
277 ["desactivation"] = { menu
=i18n
.get( "uiR2Event0Deactivation" ):toUtf8(),
278 text
=i18n
.get( "uiR2Event1Deactivation" ):toUtf8()},
279 ["trigger"] = { menu
=i18n
.get( "uiR2Event0Trigger" ):toUtf8(),
280 text
=i18n
.get( "uiR2Event1Trigger" ):toUtf8()},
283 ["is active"] = { menu
=i18n
.get( "uiR2Test0Active" ):toUtf8(),
284 text
=i18n
.get( "uiR2Test1Active" ):toUtf8()}
287 return logicTranslations
290 r2
.Features
["CounterFeature"] = feature