This repository has been archived by the owner on Dec 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
o_main_motion_tracker.lua
465 lines (387 loc) · 17.6 KB
/
o_main_motion_tracker.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
require('State')
require('o_WayPointEvents')
require('o_onEvent_Motion_Tracker')
require('o_mis')
require("o_Movement")
function CreateStates(self)
Timers = {}
Timers[1] = "blank"
self:SetVar("Timers",Timers)
storeHomePoint(self)
self:SetTetherPoint { tetherPt = self:GetPosition().pos,radius = 0 }
CombatState(self)
-- //////////////////////////////////////////////////////////////////////////////////
-- if Way Points
if self:GetVar("Set.WayPointSet") ~= nil or self:GetVar("attached_path") ~= nil or self:GetVar("groupID") ~= nil then
UseWayPoints(self)
end
-- if PetCalss
if self:GetVar("Set.Pet_Active") then
SetPetClass(self)
end
-- if Frozen
if self:GetVar("Set.MovementType") == "Frozen" then
AggroState(self)
end
-- //////////////////////////////////////////////////////////////////////////////////
-- if Guard
if self:GetVar("Set.MovementType") == "Guard" then
AggroState(self)
end
-- //////////////////////////////////////////////////////////////////////////////////
-- if Wander
if self:GetVar("Set.MovementType") == "Wander" then
MeanderState(self)
end
-- //////////////////////////////////////////////////////////////////////////////////
-- if follow
if self:GetVar("Set.FollowActive") then
FollowState(self)
end
-- //////////////////////////////////////////////////////////////////////////////////
-- if Helper
if self:GetVar("Set.Helper") then
SetHelper(self)
end
customState = State.create()
customState.onEnter = function(self)
if (onTemplateCustomStateEnter) and (onTemplateCustomStateEnter(self, msg) == true ) then
return
end
end
customState.onArrived = function(self, msg)
if (onTemplateCustomStateArrived) and (onTemplateCustomStateArrived(self, msg) == true ) then
return
end
end
-- //////////////////////////////////////////////////////////////////////////////////
-- Idle
Idle = State.create()
Idle.onEnter = function(self)
if self:GetVar("Set.Pet_Active") and not self:GetVar("PetLoaded") then
self:SetVar("PetLoaded", true)
setState("PetClass",self)
end
if self:GetVar("RebuildStart") then
storeHomePoint(self)
self:SetVar("inpursuit",true)
GAMEOBJ:GetTimer():AddTimerWithCancel( self:GetVar("Set.Emote_EventDelay") , "rebuildBreak", self )
end
end
AiDisable = State.create()
AiDisable.onEnter = function(self)
self:SetVar("myTarget", nil)
storeHomePoint(self)
self:SetTetherPoint { tetherPt = self:GetPosition().pos,radius = 0 }
CancelTemplateTimers(self)
self:SetVar("AiDisabled", true)
self:StopPathing()
end
AiEnable = State.create()
AiEnable.onEnter = function(self)
self:SetVar("AiDisabled", false)
SetResetState(self)
end
end
-- //////////////////////////////////////////////////////////////////////////////////
-- Death
Dead = State.create()
Dead.onEnter = function(self)
self:SetVar("CurrentState", "Dead")
if self:GetVar("I_Have_A_Parent") then
local WpSetName = self:GetVar("attached_path")
local myParent = getParent(self)
local sSplit = split(WpSetName,"_")
local xSplit = {}
local xSplit= {alpa=sSplit[1] , num=sSplit[2] }
myParent:SetVar( self:GetVar("SpawnedVar") , "NotSpawned")
setState("DeadChild", myParent)
end
if self:GetVar("Im_A_Child") then
myParent = getParent(self)
for i = 1, getParent(self):GetVar("MaxPet") do
local myidString = self:GetID()
local myfinalID = "|" .. myidString
local Pet = getPetID(myParent,i):GetID()
if Pet == self:GetID() then
myParent:SetVar("StoredPet."..i, "none")
myParent:SetVar("Child_Timer."..i, myfinalID )
GAMEOBJ:GetTimer():AddTimerWithCancel( myParent:GetVar("Set.Pet_Respawn") , myfinalID, myParent )
end
end
end
getVarables(self)
CancelTemplateTimers(self)
end
-- //////////////////////////////////////////////////////////////////////////////////
-- Tether
tether = State.create()
tether.onEnter = function(self)
self:SetVar("aggrotarget",2)
self:SetVar("myTarget", nil)
if self:GetVar("Set.tetherSpeed") ~= nil then
local Tspeed = self:GetVar("Set.tetherSpeed")
else
local Tspeed = 1
end
self:SetVar("collideswitch", 1)
local myPos = getHomePoint(self)
self:GoTo { speed = self:GetVar("Set.tetherSpeed"), target = { x = myPos.x, z = myPos.z, y = myPos.y }}
end
tether.onArrived = function(self,msg)
self:SetVar("CurrentState", "tether")
self:SetVar("aggrotarget",0)
self:SetHealth{ health = self:GetMaxHealth{}.health }
getVarables(self)
SetResetState(self)
end
tether.onCollisionPhantom = function(self,msg)
local target = msg.objectID
local faction = target:BelongsToFaction{factionID = 1}
local playerVelx = target:GetLinearVelocity().linVelocity.x
local playerVely = target:GetLinearVelocity().linVelocity.y
local playerVelz = target:GetLinearVelocity().linVelocity.z
if faction and faction.bIsInFaction and playerVelx ~= 0 and playerVely ~= 0 and playerVelz ~= 0 and self:GetVar("collideswitch") == 1 then
self:NotifyClientObject{name = "Gotcha"}
self:SetVar("collideswitch", 0)
local myPos = self:GetPosition().pos
local myTarget = msg.objId
CancelTemplateTimers(self)
storeObjectByName(self, "AttackingTarget", target)
storeHomePoint(self)
self:SetTetherPoint { tetherPt = myPos, radius = self:GetVar("tetherRadius") }
self:SetVar("aggrotarget",1)
self:SetVar("pauseswitch", 1)
setState("aggro", self)
GAMEOBJ:GetTimer():CancelTimer( "CheckingforMovement", self )
GAMEOBJ:GetTimer():AddTimerWithCancel( 0.5 , "CheckingforMovement", self )
end
end
GoHome = State.create()
GoHome.onEnter = function(self)
self:SetVar("CurrentState", "GoHome")
local hpos = getHomePoint(self)
self:GoTo { speed = 1, target = { x =hpos.x ,z =hpos.z ,y =hpos.y } }
end
GoHome.onArrived = function(self)
self:SetRotation(self:GetVar("rot"))
SetResetState(self)
end
-- //////////////////////////////////////////////////////////////////////////////////
-- Aggro
function dist(a ,b)
local dx = math.abs(b.x - a.x)
local dy = math.abs(b.y - a.y)
local dz = math.abs(b.z - a.z)
local d = math.sqrt((dx ^ 2) + (dy ^ 2) + (dz ^ 2))
return d
end
function Distance(point1, point2)
local distance = { }
distance.x = (point2.x - point1.x)
distance.y = (point2.y - point1.y)
return distance
end
--[[
Aggro::
Right away, turn on the heartbeat timer and follow the target. The FollowTarget method will continually follow, face the target, and keep a good attacking distance.
When the heartbeat comes back, decide what to do. For now, we just say that we are ready to attack.
If you are a given distance from the target and you are facing him, attack. If you cannot attack, wait until onArrived and then attack.
--]]
function AggroState(self)
aggro = State.create()
-----------------------------------------------------------------------------
-- Aggro onEnter
-----------------------------------------------------------------------------
aggro.onEnter = function(self)
self:SetVar("CurrentState", "aggro")
local INITIAL_ATTACK_TIME = 1
-- Start the heartbeat
self:SetVar("readyToAttack", false)
GAMEOBJ:GetTimer():AddTimerWithCancel( INITIAL_ATTACK_TIME , "AttackHeartBeat", self )
GAMEOBJ:GetTimer():CancelTimer( "MeanderPause", self )
local myPos = getHomePoint(self)
local target = getObjectByName(self, "AttackingTarget")
if target:IsDead().bDead then
self:SetVar("Aggroed", false)
ProximityPuls(self)
else
if self:GetVar("Set.MovementType") ~= "Frozen" then
self:SetTetherPoint { tetherPt = myPos,radius = self:GetVar("Set.tetherRadius") }
-- print("Setting Tether Point ")
if self:GetVar("Set.AggroDist") ~= nil and self:GetVar("Set.AggroSpeed") ~= nil then
self:FollowTarget { targetID = target, radius = self:GetVar("Set.AggroDist"),speed = self:GetVar("Set.AggroSpeed") * 2 , keepFollowing = true }
self:SetVar("Aggroed", true)
else
self:FollowTarget { targetID = target,radius = 3, speed = 1, keepFollowing = true }
end
end -- end if frozen
end -- end else is not dead
end -- end function
aggro.onTimerDone = function(self, msg)
if msg.name == "AttackHeartBeat" then
self:SetVar("readyToAttack", true)
DoAttack(self);
local castTime = self:GetVar("SkillTime")
if (castTime < 1) then
print("castTime", castTime)
end
GAMEOBJ:GetTimer():AddTimerWithCancel( castTime , "AttackHeartBeat", self )
elseif msg.name == "CheckingforMovement" then
if self:GetVar("pauseswitch") == 1 then
GAMEOBJ:GetTimer():AddTimerWithCancel( 0.5 , "CheckingforMovement", self )
local target = getObjectByName(self, "AttackingTarget")
local playerVelx = target:GetLinearVelocity().linVelocity.x
local playerVely = target:GetLinearVelocity().linVelocity.y
local playerVelz = target:GetLinearVelocity().linVelocity.z
if playerVelx == 0 and playerVely == 0 and playerVelz == 0 then
self:NotifyClientObject{name = "Lostya"}
self:PlayAnimation{ animationID = "looking" }
--self:SetVar("AttackingTarget", "NoTarget" )
--self:SetVar("Aggroed", false)
self:FollowTarget { targetID = target, radius = self:GetVar("Set.AggroDist"),speed = 0 , keepFollowing = false }
GAMEOBJ:GetTimer():AddTimerWithCancel( 3.0 , "MotionTrackPause", self )
self:SetVar("pauseswitch", 0)
self:SetRotation(self:GetVar("rot"))
GAMEOBJ:GetTimer():CancelTimer("CheckingforMovement", self)
GAMEOBJ:GetTimer():CancelTimer("AttackHeartBeat", self)
GAMEOBJ:GetTimer():AddTimerWithCancel( 0.5 , "CheckingforMovementAfterPause", self )
else
end
end
elseif msg.name == "CheckingforMovementAfterPause" then
local target = getObjectByName(self, "AttackingTarget")
local playerVelx = target:GetLinearVelocity().linVelocity.x
local playerVely = target:GetLinearVelocity().linVelocity.y
local playerVelz = target:GetLinearVelocity().linVelocity.z
if playerVelx == 0 and playerVelz == 0 then
self:PlayAnimation{ animationID = "looking" }
--self:FollowTarget { targetID = target, radius = self:GetVar("Set.AggroDist"),speed = 0 , keepFollowing = false }
GAMEOBJ:GetTimer():CancelTimer("CheckingforMovementAfterPause", self)
GAMEOBJ:GetTimer():AddTimerWithCancel( 0.5 , "CheckingforMovementAfterPause", self )
else
self:SetVar("pauseswitch", 1)
--ProximityPuls(self)
GAMEOBJ:GetTimer():CancelTimer( "MotionTrackPause", self )
self:FollowTarget { targetID = target, radius = self:GetVar("Set.AggroDist"),speed = self:GetVar("Set.AggroSpeed") * 2 , keepFollowing = true }
GAMEOBJ:GetTimer():AddTimerWithCancel( 0.5 , "CheckingforMovement", self )
GAMEOBJ:GetTimer():CancelTimer("CheckingforMovementAfterPause", self)
self:NotifyClientObject{name = "Gotcha"}
end
elseif msg.name == "MotionTrackPause" then
if self:GetVar("pauseswitch") == 0 then
self:SetVar("collideswitch", 1)
self:SetVar("pauseswitch", 1)
self:SetVar("Aggroed", true)
setState("tether", self)
GAMEOBJ:GetTimer():CancelTimer("MeanderPause", self)
GAMEOBJ:GetTimer():AddTimerWithCancel( 1.0 , "MeanderPause", self )
GAMEOBJ:GetTimer():CancelTimer( "CheckingforMovement", self )
GAMEOBJ:GetTimer():CancelTimer( "CheckingforMovementAfterPause", self )
end
end
end
aggro.onArrived = function(self, msg)
DoAttack(self)
end
aggro.onExit = function(self)
GAMEOBJ:GetTimer():CancelTimer("AttackHeartBeat", self)
end
-- This will be called if FollowTarget fails. That would only fail because the target has died or logged out. In any case, the target was lost
aggro.onCancelled = function(self, msg)
self:SetVar("Aggroed", false)
--setState("tether",self)
ProximityPuls(self)
end
function DoAttack(self)
if (self:GetVar("readyToAttack") == false) then
-- print("Not Ready to Attack")
return
end
if CanAttackTarget(self) then
-- print("Can Attack")
self:SetVar("readyToAttack", false)
if self:GetVar("Set.Aggression") == "PassiveAggres" and self:GetVar("AggroOnce") == 0 then
self:SetVar("AggroOnce",1)
end
if self:GetVar("MaxTableSkill") == nil then
for i = 1, table.maxn (self:GetSkills().skills) do
self:SetVar("MaxTableSkill", i )
end
end
-- set current skill to max entry in table (default behavior)
local attackSkillID = self:GetSkills().skills[self:GetVar("MaxTableSkill")]
-- look for an attack override skill and use it
local bOverrideSkill = self:GetVar("Set.OverRideAttackSkill")
local overrideSkillID = self:GetVar("Set.AttackSkill")
if (bOverrideSkill and overrideSkillID and bOverrideSkill == true) then
attackSkillID = overrideSkillID
end
-- use an optional target if needed
local bUseOptTarget = self:GetVar("Set.UseOptionalTargetOnAttack")
if (bUseOptTarget and bUseOptTarget == true) then
self:CastSkill{skillID = attackSkillID, optionalTargetID = getObjectByName(self, "AttackingTarget") }
else
self:CastSkill{skillID = attackSkillID }
end
end
end
end -- end create aggro state
-----------------------------------------------------------------------------
-- Combat state - native code handling it
-----------------------------------------------------------------------------
function CombatState(self)
combat = State.create()
combat.onEnter = function(self)
GAMEOBJ:GetTimer():CancelTimer( "MeanderPause", self )
self:SetVar("CurrentState", "combat")
end -- function combat.onEnter
end
-----------------------------------------------------------------------------
-- CanAttackTarget
-----------------------------------------------------------------------------
function CanAttackTarget(self)
local myTarget = getObjectByName(self, "AttackingTarget")
--local range = self:GetVar("Set.AggroDist")
--local myPos = Vector.new(self:GetPosition().pos)
--local hisPos = Vector.new(myTarget:GetPosition().pos)
--local dist = hisPos - myPos
if not myTarget:Exists() or myTarget:IsDead().bDead or not self:IsEnemy{ targetID = myTarget }.enemy then
self:SetVar("AttackingTarget", "NoTarget" )
self:SetVar("Aggroed", false)
ProximityPuls(self)
return false
end
-- If we are close enough and if the object is in the FOV, then attack
--if dist:sqrLength() - range <= range * range then --and (self:IsObjectInFOV { target = myTarget, radius = 5 , minRange = 0, maxRange = 100 }.result) then
return true
--end
--self:SetVar("Aggroed", false)
--return false
end -- end CanAttackTarget
-- Start timer funciton
function SetStoreTimmer(intTime, Name, self)
-- Start Timer
if (intTime) and (Name) and (self) then
GAMEOBJ:GetTimer():AddTimerWithCancel( intTime, Name, self )
end
-- Store Timer
for i = 1, table.maxn(self:GetVar("Timers")) + 1 do
if self:GetVar("Timers."..i) == Name then
break
end
if self:GetVar("Timers."..i) == nil then
self:SetVar("Timers."..i, Name)
end
end
end
function CancelTemplateTimers(self)
for i = 2,table.maxn(self:GetVar("Timers")) do
GAMEOBJ:GetTimer():CancelTimer( self:GetVar("Timers."..i), self )
end
end
onDie = function(self,msg)
if (onTemplateDie) and (onTemplateDie(self, msg) == true ) then
return
end
end