7 r2
.Utils
.addReaction
= function(this
,event
,action
)
8 local reactions
= this
.Reactions
[event
]
12 this
.Reactions
[event
]=reactions
14 table.insert(reactions
,action
)
17 ------------------------------------------------------
18 --Create the states and groups to represent:
20 -- -The counters used for this mission
21 r2
.Utils
._obsolete_initCounterStates
= function(counter
,context
)
23 if counter
.Class
=="Counter"
26 local pGroup
= r2
.createPseudoGroup(context
)
27 context
.RtCounters
[counter
.InstanceId
]=pGroup
29 for k
,v
in pairs(counter
.Counters
)
31 Translator
.LogicEntityTranslator(v
,context
)
37 r2
.Utils
.createGroup
= function(x
,y
,n
,base
,name
,mode
)
44 local isGeneric
= false
46 if string.find(base
, "palette.entities.players.") ~= nil then
51 local npcGroup
= r2
.newComponent("NpcGrpFeature")
58 local pas
= (2 * math
.pi
)/n
59 local r
= (n
/(2*math
.pi
))+2
62 if ( isGeneric
== true) then
63 npc
= r2
.newComponent("NpcCustom")
65 npc
= r2
.newComponent("Npc")
68 npc
.Name
= name
.."_".."Npc"..i
70 npc
.Position
.x
= x
+ (r
-1) * math
.cos((i
-1)*pas
)
71 npc
.Position
.y
= y
+ (r
-1) * math
.sin((i
-1)*pas
)
72 npc
.Position
.z
= r2
:snapZToGround(npc
.Position
.x
, npc
.Position
.y
)
73 npc
.Angle
= (i
-1)*pas
+ math
.pi
74 table.insert(npcGroup
.Components
,npc
)
81 local npc
= r2
.newComponent("Npc")
82 npc
.Name
= name
.."_".."Npc"..i
84 npc
.Position
.x
= x
+ i
* pas
86 npc
.Position
.z
= r2
:snapZToGround(npc
.Position
.x
, npc
.Position
.y
)
88 table.insert(npcGroup
.Components
,npc
)
94 --region = region to fill
95 --x,y = region's center
97 --nbPoints = number of points to create the region
98 r2
.Utils
.createRegion
= function(region
,x
, y
, r
,nbPoints
)
100 local tmpPositions
= region
.Points
101 local pas
= (2 * math
.pi
)/nbPoints
105 local tmpVertex
= r2
.newComponent("RegionVertex")
106 local tmpPosition
= r2
.newComponent("Position")
108 sx
= x
+ r
* math
.cos(Angle
)
109 sy
= y
+ r
* math
.sin(Angle
)
110 sx
, sy
, sz
= r2
:snapPosToGround(sx
, sy
)
114 tmpVertex
.Position
= tmpPosition
115 table.insert(tmpPositions
, tmpVertex
)
117 nbPoints
= nbPoints
- 1
121 --region = region to fill
122 --x,y = region's center
124 --nbPoints = number of points to create the region
125 r2
.Utils
.createNonDeleteableRegion
= function(region
,x
, y
, r
,nbPoints
)
128 local tmpPositions
= region
.Points
129 local pas
= (2 * math
.pi
)/nbPoints
133 local tmpVertex
= r2
.newComponent("RegionVertex")
134 tmpVertex
.Deletable
= 0
135 local tmpPosition
= r2
.newComponent("Position")
137 sx
= x
+ r
* math
.cos(Angle
)
138 sy
= y
+ r
* math
.sin(Angle
)
139 sx
, sy
, sz
= r2
:snapPosToGround(sx
, sy
)
143 tmpVertex
.Position
= tmpPosition
144 table.insert(tmpPositions
, tmpVertex
)
146 nbPoints
= nbPoints
- 1
150 --region = trigger Zone
151 --x,y = region's center
153 --nbPoints = number of points to create the region
154 r2
.Utils
.createTriggerRegion
= function(region
,x
, y
, r
)
155 return r2
.Utils
.createNonDeleteableRegion(region
, x
, y
, r
, 4)
161 -- ex local road = createRoad("rout1", { {21570, -1363}, {21570, -1363}, {21570, -1363})
162 r2
.Utils
.createRoute
= function(name
, positions
)
163 local road
= r2
.newComponent("Road")
164 local function wp(x
, y
, z
)
165 local wayPoint
= r2
.newComponent("WayPoint")
166 local pos
= r2
.newComponent("Position")
170 wayPoint
.Position
= pos
174 -- road.Base = "palette.geom.road"
176 local tmpPositions
= road
.Points
-- depart a arrivée
178 for index
, points
in pairs(positions
)
180 table.insert(tmpPositions
, wp(points
[1], points
[2], 0))
187 --function to set an RtAiState with a npc's behavior
188 r2
.Utils
.setState
= function(context
,behavior
,rtAiState
)
189 local aiMovement
= behavior
.Type
190 rtAiState
.Name
= rtAiState
.Id
.. "|" .. aiMovement
191 rtAiState
.AiMovement
= aiMovement
192 if (aiMovement
== "wander" or aiMovement
== "follow_route" or aiMovement
== "patrol_route" or aiMovement
== "repeat_road") then
193 local id
= behavior
.ZoneId
194 local zone
= context
.Components
[id
]
196 local points
=zone
.Points
197 assert( points
~= nil)
198 local size
= table.getn(points
)
200 local k
,v
= next(points
, nil)
206 -- replacement for getworldPos
209 rtAiState
.Pts
[i
] = {}
210 -- ??? v.Position.x ??
211 rtAiState
.Pts
[i
].x
= r2
.getWorldPos(v
).x
212 rtAiState
.Pts
[i
].y
= r2
.getWorldPos(v
).y
213 rtAiState
.Pts
[i
].z
= r2
.getWorldPos(v
).z
215 k
,v
= next(points
, k
)
218 if (aiMovement
== "patrol_route") then
220 for i
= 1, size
-1 , 1
222 local first
= size
- i
223 local last
= size
+ i
225 rtAiState
.Pts
[last
] = {}
226 rtAiState
.Pts
[last
].x
= rtAiState
.Pts
[first
].x
227 rtAiState
.Pts
[last
].y
= rtAiState
.Pts
[first
].y
228 rtAiState
.Pts
[last
].z
= rtAiState
.Pts
[first
].z
231 if (aiMovement
== "patrol_route" or aiMovement
== "repeat_road") then
232 rtAiState
.AiMovement
= "follow_route"
233 local eventHandler
= Actions
.createEvent("destination_reached", rtAiState
.Id
,"")
234 assert( eventHandler
~= nil)
235 local eName
= rtAiState
.Id
.. ":destination_reached"
236 eventHandler
.Name
=eName
237 table.insert(context
.RtAct
.Events
,eventHandler
)
239 local action
= Actions
.createAction("begin_state", rtAiState
.Id
)
240 action
.Name
="begin state " .. rtAiState
.Id
241 table.insert(context
.RtAct
.Actions
,action
)
242 table.insert(eventHandler
.ActionsId
,action
.Id
)
248 r2
.Utils
.invertRoad
= function(road
)
249 local road2
= r2
.newComponent("Road")
250 local function wp(x
, y
, z
)
251 local wayPoint
= r2
.newComponent("WayPoint")
252 local pos
= r2
.newComponent("Position")
256 wayPoint
.Position
= pos
259 local max = table.getn(road
.Points
)
262 local point
= road
.Points
[max-i
+1].Position
263 table.insert(road2
.Points
,wp(point
.x
,point
.y
,point
.z
))
269 r2
.Utils
.createEntry
= function(who
,states
,event
,actions
)
270 local entry
= r2
.newComponent("EventHandlerEntry")
272 entry
.States
= states
274 entry
.Actions
= actions
278 r2
.Utils
.createPlace
= function(x
,y
,z
,r
)
279 local place
= r2
.newComponent("Place")
287 r2
.Utils
.searchEntry
= function(activity
,who
,event
,state
)
288 local max = table.getn(activity
.Entries
)
290 local entry
= activity
.Entries
[i
]
291 if entry
.Who
==who
and entry
.Event
== event
and (entry
.States
== state
or state
=="")
299 r2
.Utils
.groupActivities
= function(activity1
,activity2
,event1
,state1
,event2
,state2
)
301 entry
= r2
.Utils
.searchEntry(activity1
,activity1
.Entries
[1].Who
,event1
,state1
)
305 entry
= r2
.Utils
.createEntry(activity1
.Entries
[1].Who
,state1
,event1
,"begin_state\n"..state2
)
306 table.insert(activity1
.Entries
,entry
)
308 entry
.Actions
= entry
.Actions
.. "\nbegin_state\n"..state2
311 entry
= r2
.Utils
.searchEntry(activity2
,activity2
.Entries
[1].Who
,event2
,state2
)
314 entry
= r2
.Utils
.createEntry(activity2
.Entries
[1].Who
,state2
,event2
,"begin_state\n"..state1
)
315 table.insert(activity2
.Entries
,entry
)
317 entry
.Actions
= entry
.Actions
.. "\nbegin_state\n"..state
321 r2
.Utils
.evalCost
= function(feature
)
323 local components
= feature
.Components
324 --luaObject(components)
328 for key
,comp
in pairs(components
)
330 if key
~="Keys" and comp
.Class
== "Npc"
339 r2
.Utils
.createChatAction
= function(who
,says
,emote
,face
)
342 chatStep
= r2
.newComponent("ChatAction")
347 local entry
=r2
.registerText(says
)
348 chatStep
.Says
= entry
.InstanceId
353 if face
~="" and face
~=nil
355 chatStep
.Facing
= face
358 chatStep
.Emote
= emote
364 --replace each instanceId in the table by the new one
365 r2
.Utils
.replaceTab
= function(this
,ttable
)
366 for k
,v
in pairs(this
)
370 this
[k
]=ttable
[this
[k]]
374 --call the replace function for each object in the table
375 r2
.Utils
.callReplace
= function(this
,ttable
)
376 for k
,v
in pairs(this
)
386 r2
.Utils
.changeRepere
= function(position
,center
)
387 position
.x
=position
.x
- center
.x
388 position
.y
=position
.y
- center
.y
389 position
.z
= r2
:snapZToGround(position
.x
,position
.y
)
392 r2
.Utils
.changeZoneRepere
= function(zone
,center
)
393 for i
=1,table.getn(zone
.Points
)
395 r2
.Utils
.changeRepere(zone
.Points
[i
])
399 r2
.Utils
.changeRepereRt
= function(npc
,center
)
401 local position
= npc
.Position
402 x
= position
.x
- center
.x
403 y
= position
.y
- center
.y
404 r2
.requestSetNode(position
.InstanceId
,"x",x
)
405 r2
.requestSetNode(position
.InstanceId
,"y",y
)
406 r2
.requestSetNode(position
.InstanceId
,"z",r2
:snapZToGround(x
,y
))
409 r2
.Utils
.setNewGroupCenter
= function(group
,x
,y
)
410 local k
,v
= next(group
.Components
,nil)
411 local center
= r2
.newComponent("Position")
422 newCenter
.x
= -(center
.x
- v
.Position
.x
)
423 newCenter
.y
= -(center
.y
- v
.Position
.y
)
424 center
.z
= r2
:snapZToGround(center
.x
,center
.y
)
425 r2
.requestSetNode(v
.Position
.InstanceId
,"x",center
.x
)
426 r2
.requestSetNode(v
.Position
.InstanceId
,"y",center
.y
)
427 r2
.requestSetNode(v
.Position
.InstanceId
,"z",r2
:snapZToGround(center
.x
,center
.y
))
430 r2
.Utils
.changeRepereRt(v
,center
)
432 k
,v
= next(group
.Components
,k
)
438 r2
.Utils
.getRtGroup
= function(context
,instanceId
)
439 debugInfo("Call obsolete function: call r2.Translator.getRtGroup")
440 -- use r2.Translator.getRtGroup instead
441 return r2
.Translator
.getRtGroup(context
, instanceId
)
444 r2
.Utils
.concat
= function(text
,textSup
)
449 return text
.."\n"..textSup
454 -----------------------------------------------------------
455 --return a string like: "group1:Npc1" for use with actions
456 r2
.Utils
.getNpcParam
= function(npcId
, context
)
458 assert( type(npcId
) == "string")
459 local who
= r2
:getInstanceFromId(tostring(npcId
))
460 -- local group = who:getParentGroup()
461 local rtNpcGrp
= context
.RtGroups
[tostring(npcId
)]
464 debugInfo("Err: unable to know the npc's group name ("..npcId
..")")
468 return rtNpcGrp
.Name
..":"..tostring(who
.Name
)
473 -- Returns the RtNpcGrp Id for a given instanceId
475 r2
.Utils
.getRtIdFromInstanceId
= function(context
, instanceId
)
476 assert(instanceId
~= nil and type(instanceId
) == "string")
477 local instance
= r2
:getInstanceFromId(instanceId
)
479 return context
.RtGroups
[instanceId
].Id
484 function r2
.testVianney1()
485 r2
.requestStartAct(1)
488 function r2
.testVianney2()
489 r2
.requestStartAct(2)
492 function r2
.testVianney3()
496 function r2
:lowerTranslate(uiR2EdStr
)
497 return string.lower(i18n
.get(uiR2EdStr
):toUtf8())