cecell

USSEP critter.psc

Aug 30th, 2021
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 41.85 KB | None | 0 0
  1. scriptName Critter extends ObjectReference
  2. {THIS SCRIPT HAS BUGFIXES BY STEVE40 and additional fixes by USKP}
  3.  
  4. ; Imported for RandomFloat()
  5. import Utility
  6. import debug
  7.  
  8. ;----------------------------------------------
  9. ; Properties to be set for this Critter
  10. ;----------------------------------------------
  11.  
  12. ; Randomness added to location and orientation at a plant
  13. float Property fPositionVarianceX = 20.0 auto
  14. {When picking a destination reference, how much variance in he X coordinate the critter to travel to.}
  15. float Property fPositionVarianceY = 20.0 auto
  16. {When picking a destination reference, how much variance in he Y coordinate the critter to travel to.}
  17. float Property fPositionVarianceZMin = 50.0 auto
  18. {When picking a destination reference, how much variance in he Z coordinate the critter to travel to, lower bound}
  19. float Property fPositionVarianceZMax = 100.0 auto
  20. {When picking a destination reference, how much variance in he Z coordinate the critter to travel to, upper bound}
  21. float Property fAngleVarianceZ = 90.0 auto
  22. {When picking a destination reference, how much variance from the ref's Z angle the critter can end at}
  23. float Property fAngleVarianceX = 20.0 auto
  24. {When picking a destination reference, how much variance from the ref's X angle the critter can end at}
  25.  
  26. ; Path Curviness
  27. float Property fPathCurveMean = 100.0 auto
  28. {When moving, how "curvy" the path can be, mean (see associated Variance)}
  29. float Property fPathCurveVariance = 200.0 auto
  30. {When moving, how "curvy" the path can be, variance (see associated Mean)}
  31.  
  32. ; For bell-shaped paths, where along the path to place waypoints
  33. float Property fBellShapedWaypointPercent = 0.2 auto
  34. {When moving on a bell-shaped path, how far along the path to place the bell waypoint (so that the critter doesn't go straight up, but up and forward)}
  35.  
  36. ; Animation events to be sent to the graph, and associated delays
  37. string Property PathStartGraphEvent = "" auto
  38. {Animation event to send when starting a path}
  39. float Property fPathStartAnimDelay = 1.0 auto
  40. {Duration of the path start animation (delay used before the critter actually moves)}
  41. string Property PathEndGraphEvent = "" auto
  42. {Animation event to send when ending a path}
  43. float Property fPathEndAnimDelay = 1.0 auto
  44. {Duration of the path end animation (delay used before the critter returns path complete)}
  45.  
  46. ; properties relevant to collection items
  47. ingredient property lootable auto
  48. {ingredient gathered from this critter}
  49. formlist property nonIngredientLootable auto
  50. {Optional: If our loot item is not an ingredient, use this instead.}
  51. formlist property fakeCorpse auto
  52. {Optional: If we want to leave a fake "Corpse" behind, point to it here.}
  53. bool property bPushOnDeath = FALSE auto
  54. {apply a small push on death to add english to ingredients? Default: False}
  55. explosion property deathPush auto
  56. {a small explosion force to propel wings on death}
  57. int property lootableCount = 1 auto
  58. {How many lootables do I drop on Death? Default: 1}
  59. bool property bIncrementsWingPluckStat = FALSE auto
  60. {do I count towards the wings plucked misc stat? will be false for majority}
  61.  
  62. ; properties relevant to landing behavior
  63. Static property LandingMarkerForm auto
  64. {What landing marker to use to compute offsets from landing position}
  65. float property fLandingSpeedRatio = 0.33 auto
  66. {The speed percentage to use when landing, Default = 0.33 (or 33% of flight speed)}
  67. string property ApproachNodeName = "ApproachSmall01" auto
  68. {The name of the approach node in the landing marker, Default=ApproachSmall01}
  69.  
  70.  
  71. ;----------------------------------------------
  72. ; Hidden Properties for AI Sim
  73. ;----------------------------------------------
  74. bool property reserved auto hidden
  75. {should this object be invalidated for searches?}
  76. objectReference property hunter auto hidden
  77. {if being hunted, by whom?}
  78. bool bKilled = false
  79. ; if been killed once, don't do Die() a second time
  80.  
  81. ;----------------------------------------------
  82. ; Properties set by the spawner
  83. ;----------------------------------------------
  84.  
  85. ; The distance from the spawner this critter can be
  86. float Property fLeashLength auto hidden
  87. ; The distance from the player this critter can be
  88. float Property fMaxPlayerDistance auto hidden
  89. ; The Height above the spawner that critters be move to (when applicable: dragonfly)
  90. float Property fHeight auto hidden
  91. ; The Depth below the spawner that critters be move to (when applicable: fish)
  92. float Property fDepth auto hidden
  93. ; The spawner that owns this critter
  94. CritterSpawn Property Spawner auto hidden
  95.  
  96. ;----------------------------------------------
  97. ; Debugging
  98. ;----------------------------------------------
  99. bool Property bCritterDebug = false auto hidden
  100.  
  101. ;----------------------------------------------
  102. ; USKP Properties
  103. ;----------------------------------------------
  104. Actor Property PlayerRef auto hidden
  105. ;{set by SetSpawnerProperties, cleared by DisableAndDelete}
  106. ObjectReference property Follower = none auto hidden
  107. ;{if being followed, by whom?}
  108. float Property fSpawnerX auto hidden
  109. ;{set by SetSpawnerProperties}
  110. float Property fSpawnerY auto hidden
  111. ;{set by SetSpawnerProperties}
  112. float Property fSpawnerZ auto hidden
  113. ;{set by SetSpawnerProperties}
  114.  
  115. bool property bCalculating = false auto hidden
  116. ; don't do DisableAndDelete() during calculations
  117.  
  118. bool bDeleting = false
  119. ; don't do DisableAndDelete() a second time
  120.  
  121. ;----------------------------------------------
  122. ; Events
  123. ;----------------------------------------------
  124.  
  125. FUNCTION Die()
  126. if bKilled == false
  127. ;! bKilled = true; now in disableAndDelete [USKP 2.0.1]
  128. disableAndDelete(false)
  129. int i = 0
  130. while i < lootableCount
  131. i += 1
  132. objectReference createdLootable
  133. if fakeCorpse ; do we have the optional fakeCorpse parameter?
  134. ; we have to jump through a hoop here to get the more open-ended functionality of using a formlist
  135. int total = fakeCorpse.getSize() ; it would unusual if this was > 1, but just in case...
  136. int current = 0
  137. while current < total
  138. createdLootable = placeAtMe(fakeCorpse.getAt(current), 1)
  139. current += 1
  140. endWhile
  141. else
  142. createdLootable = placeatMe(lootable, 1)
  143. endif
  144. createdLootable.moveTo(createdLootable, randomFloat(self.getWidth(), 2*self.getWidth()), randomFloat(self.getWidth(), 2*self.getWidth()), -1*(randomFloat(self.getHeight(), 2*self.getHeight())), false)
  145. endWhile
  146. if bPushOnDeath == TRUE
  147. placeatMe(deathPush)
  148. endif
  149. endif
  150. endFUNCTIOn
  151.  
  152. EVENT onActivate(objectReference actronaut)
  153. if (bKilled == false)
  154. ;! bKilled = true; now in disableAndDelete [USKP 2.0.1]
  155. disableAndDelete(false) ; Nuke the critter as early as possible so the player knows something happened
  156. ;Player receives loot for direct activation? Probably need a bool here for other critters in future
  157. if nonIngredientLootable ; do I have the optional non-ingredient lootable parameter set?
  158. ;! actronaut.additem(nonIngredientLootable, lootableCount)
  159. ; [USKP 1.2] fix list
  160. Int iIndex = NonIngredientLootable.GetSize()
  161. While iIndex > 0
  162. iIndex -= 1
  163. Actronaut.AddItem(NonIngredientLootable.GetAt(iIndex), lootableCount)
  164. EndWhile
  165. ; ^^ FROM USKP
  166. else
  167. actronaut.addItem(lootable, lootableCount)
  168. endif
  169. if bIncrementsWingPluckStat == TRUE
  170. game.IncrementStat("Wings Plucked", lootableCount)
  171. endif
  172. endIf
  173. endEVENT
  174.  
  175. EVENT onHit(ObjectReference akAggressor, Form akWeapon, Projectile akProjectile, bool abPowerAttack, bool abSneakAttack, bool abBashAttack, bool abHitBlocked)
  176. ; debug.trace(self + " got hit by " + akAggressor)
  177. Die()
  178. endEVENT
  179.  
  180. Event OnMagicEffectApply(ObjectReference akCaster, MagicEffect akEffect)
  181. ; debug.trace(self + " got hit by magic of " + akCaster)
  182. Die()
  183. EndEvent
  184.  
  185. ; This event is called once the Critter is ready to kick off its processing
  186. Event OnStart()
  187. ; moved from KickOffOnStart OnUpdate [USKP 2.0.1]
  188. Enable()
  189. endEvent
  190.  
  191. ; Triggered when the critter is about to reach its destination
  192. Event OnCritterGoalAlmostReached()
  193. endEvent
  194.  
  195. ; Triggered when the critter reaches its destination
  196. Event OnCritterGoalReached()
  197. endEvent
  198.  
  199. ; Triggered if critter failed reaching the destination
  200. Event OnCritterGoalFailed()
  201. ; kick the OnUpdate in hopes it will clean up. [USKP 2.0.1]
  202. ; less than OnCellAttach RegisterForSingleUpdate(0.70711)
  203. RegisterForSingleUpdate(0.15); [USKP 2.0.3]
  204. endEvent
  205.  
  206. ; Indicates whether the critter has had everything initialized
  207. ;bool b3DInitialized = false
  208. bool bDefaultPropertiesInitialized = false
  209. bool bSpawnerVariablesInitialized = false
  210.  
  211. ; Prevent getting event for 3D loaded after move to
  212. bool bfirstOnStart = true
  213.  
  214. ; These are used to store location/orientation
  215. ObjectReference landingMarker = none
  216. ObjectReference dummyMarker = none
  217.  
  218. ;/ Catch default, copied from critterMoth and FireFly [USKP 2.0.1]
  219. /;
  220. Event OnUpdate()
  221. ;~ Trace(self + "OnUpdate() in the default state, killing itself", 2)
  222. DisableAndDelete(true)
  223. endEvent
  224.  
  225. ;/ Backup for deletions that happen during calculations [USKP 2.0.3]
  226. /;
  227. Event OnUpdateGameTime()
  228. ;~ Trace(self + "OnUpdateGameTime() killing itself", 2)
  229. DisableAndDelete(false)
  230. endEvent
  231.  
  232. ;/ Match OnCellDetach() for all critters, to clean up old critters [USKP 2.0.1]
  233. /;
  234. EVENT OnCellAttach()
  235. ;~ Trace(self + "OnCellAttach() had failed to kill self OnCellDetach", 2)
  236. ; kick the OnUpdate in hopes it will clean up.
  237. RegisterForSingleUpdate(0.70711); [USKP 2.0.3]
  238. if isDeleted()
  239. ;~ Trace(self + "OnCellAttach() isDeleted", 2)
  240. elseIf isDisabled()
  241. ;~ Trace(self + "OnCellAttach() isDisabled", 2)
  242. else
  243. ; possibly trigger shorter OnUpdate time in Critter OnCritterGoalFailed()
  244. StopTranslation()
  245. endIf
  246. endEVENT
  247.  
  248. ;/ Safety measure - when my cell is detached, for whatever reason, kill me.
  249. ; duplicate code from various critters moved here. [USKP 2.0.3]
  250. /;
  251. EVENT OnCellDetach()
  252. ;~ Trace(self + "OnCellDetach() Killing self", 2)
  253. ;! DisableAndDelete()
  254. ; Parentcell usually removed by the time this event triggers,
  255. ; so StopTranslation() doesn't work. Schedule cleanup after a
  256. ; longer time than any possible TranslateTo. [USKP 2.0.3]
  257. RegisterForSingleUpdateGameTime(0.31831); 1/pi hours
  258. endEVENT
  259.  
  260. ; Called as soon as the critter is loaded
  261. ;Event OnLoad()
  262. ; ; ; Debug.TraceConditional("Critter " + self + " OnLoad() called!", bCritterDebug)
  263. ; if (bfirstOnStart)
  264. ; ; We know the 3D is good
  265. ; b3DInitialized = true
  266. ; ; If everything else is also good, start doing stuff
  267. ; CheckStateAndStart()
  268. ; bfirstOnStart = false
  269. ; endIf
  270. ;endEvent
  271.  
  272. ; Called when properties for this object have been initialized
  273. Event OnInit()
  274. ; We know default properties are good
  275. bDefaultPropertiesInitialized = true
  276. ; If everything else is also good, start doing stuff
  277. CheckStateAndStart()
  278. endEvent
  279.  
  280. ; Called by the spawner when it has set the initial variables
  281. float fradiusPropVal
  282. float fmaxPlayerDistPropVal
  283. float fheightPropVal
  284. float fdepthPropVal
  285. CritterSpawn spawnerPropVal
  286. Function SetInitialSpawnerProperties(float afRadius, float afHeight, float afDepth, float afMaxPlayerDistance, CritterSpawn arSpawner)
  287. ; Set initial variables to be set once default properties have been set
  288. fradiusPropVal = afRadius
  289. fheightPropVal = afHeight
  290. fdepthPropVal = afDepth
  291. fmaxPlayerDistPropVal = afMaxPlayerDistance
  292. spawnerPropVal = arSpawner
  293. ; We know spawner-set variables are good now
  294. bSpawnerVariablesInitialized = true
  295. ; If everything else is also good, start doing stuff
  296. CheckStateAndStart()
  297. endFunction
  298.  
  299. ; Stores initial spawner properties temporarily until object is ready to have them overwritten
  300. Function SetSpawnerProperties()
  301. fLeashLength = fradiusPropVal
  302. fHeight = fheightPropVal
  303. fDepth = fDepthPropVal
  304. fMaxPlayerDistance = fmaxPlayerDistPropVal
  305. Spawner = spawnerPropVal
  306. ; [USKP 2.0.1]
  307. fSpawnerX = Spawner.X
  308. fSpawnerY = Spawner.Y
  309. fSpawnerZ = Spawner.Z
  310. PlayerRef = Game.GetPlayer()
  311. endFunction
  312.  
  313. ; Checks that the critter is all initialized, and if so, kick off the behavior
  314. Function CheckStateAndStart()
  315. ; ; Debug.TraceConditional("Critter " + self + "bDefaultPropertiesInitialized=" + bDefaultPropertiesInitialized + ", bSpawnerVariablesInitialized=" + bSpawnerVariablesInitialized, bCritterDebug)
  316.  
  317. if (bDefaultPropertiesInitialized && bSpawnerVariablesInitialized)
  318.  
  319. ; Set actual properties from the spawner
  320. SetSpawnerProperties()
  321.  
  322. ; Do the next part asynchronously, to allow the spawner to keep creating more critters
  323. GotoState("KickOffOnStart")
  324. RegisterForSingleUpdate(0.0)
  325. endIf
  326. endFunction
  327.  
  328. state KickOffOnStart
  329. ;!Function OnUpdate()
  330. Event OnUpdate() ; BUGFIX BY STEVE40
  331. GotoState("")
  332. ; ; Debug.TraceConditional("Critter " + self + " is creating landing markers", bCritterDebug)
  333.  
  334. ; Create our landing and dummy markers
  335. landingMarker = PlaceAtMe(LandingMarkerForm)
  336. dummyMarker = PlaceAtMe(LandingMarkerForm)
  337. ; delay all 3D checks until immediately before usage [USKP 2.0.1]
  338.  
  339. ; We're all set to go, start doing stuff
  340. ; ; Debug.TraceConditional("Critter " + self + " is triggering OnStart", bCritterDebug)
  341. ;~ Debug.Trace("Critter " + self + " is triggering OnStart")
  342.  
  343. ; STEVE40+USKP consolidate duplicated code for most critters
  344. if landingMarker && dummyMarker && CheckCellAttached(self) ;/&& Game.GetPlayer().GetDistance(self) <= fMaxPlayerDistance/;
  345. OnStart()
  346. else
  347. ;~ Debug.Trace(self + " Unable to place any markers.")
  348. ; Kill this critter
  349. DisableAndDelete()
  350. endif
  351.  
  352. ; possible conflicting Enable after OnStart DisableAndDelete,
  353. ; moved to stub OnStart [USKP 2.0.1]
  354. ;! Enable()
  355. ;!endFunction
  356. endEvent ; BUGFIX BY STEVE40
  357. endState
  358.  
  359. ;/ clear Follower stub [USKP 2.0.1]
  360. /;
  361. Function FollowClear()
  362. endFunction
  363.  
  364. ;/ set Follower [USKP 2.0.1]
  365. /;
  366. Bool Function FollowSet(ObjectReference Following)
  367. if Follower == None && !IsDisabled()
  368. Follower = Following
  369. return true
  370. endIf
  371. return false
  372. endFunction
  373.  
  374. ;/ clear Leader and Follower and Target stub [USKP 2.0.1]
  375. /;
  376. Function TargetClear()
  377. endFunction
  378.  
  379. ; Called to remove the critter
  380. ; This should be called when a critter is too far from the player to be seen for instance
  381. Function DisableAndDelete(bool abFadeOut = true)
  382. bKilled = true ; BUGFIX BY STEVE40+USKP
  383.  
  384. if bCalculating && abFadeOut
  385. ; interlock [UKSP 2.0.3]
  386. ;~ TraceStack(self + " bCalculating && abFadeOut true")
  387. RegisterForSingleUpdateGameTime(0.367879); 1/e hours
  388. return
  389. endif
  390.  
  391. if bDeleting
  392. ; interlock [UKSP 2.0.1]
  393. ;~ TraceStack(self + " bDeleting true")
  394. return
  395. endif
  396. bDeleting = true
  397.  
  398. ; Nota Bene: The following notDisabled checks for failure to
  399. ; clear temporarily persistent variables, and failure to delete
  400. ; prior to USKP 1.3.3 [USKP 2.0.1]
  401. bool notDisabled = !isDisabled()
  402.  
  403. ; Disable the critter - don't wait if we aren't fading
  404. if abFadeOut
  405. Disable(true)
  406. else
  407. DisableNoWait()
  408. endIf
  409.  
  410. ; vanilla code doesn't have this variable. Use it to detect
  411. ; earlier partially disabled/deleted critters, and also as the
  412. ; StopNowWeReallyMeanIt flag! The advantage over isDisabled() is
  413. ; non-interruptible idempotent run-time testing.
  414. PlayerRef = None
  415.  
  416. ; Stop Any movement
  417. CurrentMovementState = "Idle"
  418.  
  419. if (GetParentCell())
  420. StopTranslation()
  421. endIf
  422.  
  423. ; give self some time to process/register updates [USKP 2.0.4]
  424. ; may occur during harvesting, or after StopTranslation()
  425. Float delay = 0.016667; 1 frame
  426. while bCalculating && delay < 1.1
  427. Wait(delay)
  428. delay += delay
  429. endWhile
  430.  
  431. ; give others plenty of time to clear leaders and followers [USKP 2.0.3]
  432. ; 3 times maximum above for looped chain of 3 followers.
  433. ; total not unreasonable for delayed display of harvest.
  434. TargetClear()
  435. while Follower && delay < 3.3
  436. Wait(delay)
  437. delay += delay
  438. endWhile
  439. if Follower
  440. Critter FollowedBy = Follower as Critter
  441. if FollowedBy
  442. FollowedBy.FollowClear()
  443. endIf
  444. endIf
  445. Follower = none
  446.  
  447. ; Notify spawner
  448. if Spawner && notDisabled
  449. Spawner.OnCritterDied()
  450. endIf
  451. ; USKP fix persistent reference, don't repeatedly decrement
  452. Spawner = none
  453.  
  454. ; Delete dummy marker
  455. if landingMarker && notDisabled
  456. landingMarker.Delete()
  457. endIf
  458. ; STEVE40+USKP fix persistent reference to deleted object
  459. landingMarker = none
  460.  
  461. if dummyMarker && notDisabled
  462. dummyMarker.Delete()
  463. endIf
  464. ; STEVE40+USKP fix persistent reference to deleted object
  465. dummyMarker = none
  466.  
  467. ; StopTranslation must be called before UnregisterForUpdate,
  468. ; because OnCritterGoalFailed() can now RegisterForSingleUpdate()
  469. ; Unregister for any kind of update
  470. UnregisterForUpdate()
  471. UnregisterForUpdateGameTime()
  472.  
  473. ; And delete ourselves
  474. ;~ Debug.Trace("Critter " + self + " will kill itself.")
  475. ; Delete must be called, is not dependent on parent cell [USKP 1.3.3]
  476. ;! if getParentCell()
  477. Delete()
  478. ;! endif
  479. endFunction
  480.  
  481. ; Variable used to store the desired state after translation
  482. string CurrentTargetState = ""
  483.  
  484. ; Target Node name
  485. string CurrentTargetNode = ""
  486.  
  487. ; Internal state used when moving
  488. string CurrentMovementState = "Idle"
  489.  
  490. ;/ PlaceLandingMarker
  491. ; Returns true when DisableAndDelete [USKP 2.0.1]
  492. /;
  493. Bool Function PlaceLandingMarker(ObjectReference arTarget, string asTargetNode)
  494. ; ; Debug.TraceConditional("Critter " + self + " placing landing marker " + landingMarker, bCritterDebug)
  495. if !(PlayerRef && CheckFor3D( landingMarker ) && CheckFor3D( arTarget ))
  496. DisableAndDelete(false)
  497. return true
  498. endIf
  499.  
  500. if (asTargetNode != "")
  501. landingMarker.MoveToNode(arTarget, asTargetNode)
  502. else
  503. ; Use random offset from ref pivot point
  504. landingMarker.SetPosition(arTarget.X + RandomFloat(-fPositionVarianceX, fPositionVarianceX), arTarget.Y + RandomFloat(-fPositionVarianceY, fPositionVarianceY), arTarget.Z + RandomFloat(fPositionVarianceZMin, fPositionVarianceZMax))
  505. landingMarker.SetAngle(arTarget.GetAngleX() + RandomFloat(-fAngleVarianceX, fAngleVarianceX), arTarget.GetAngleY(), arTarget.GetAngleZ() + RandomFloat(-fAngleVarianceZ, fAngleVarianceZ))
  506. endIf
  507.  
  508. ; MoveTo might have temporarily disabled 3D
  509. ; delay all 3D checks until immediately before usage [USKP 2.0.1]
  510. return false
  511. endFunction
  512.  
  513. ;/ PlaceDummyMarker
  514. ; Returns true when DisableAndDelete [USKP 2.0.1]
  515. /;
  516. Bool Function PlaceDummyMarker(ObjectReference arTarget, string asTargetNode)
  517. ; ; Debug.TraceConditional("Critter " + self + " placing Dummy marker " + dummyMarker, bCritterDebug)
  518. if !(PlayerRef && CheckFor3D( dummyMarker ) && CheckFor3D( arTarget ))
  519. DisableAndDelete(false)
  520. return true
  521. endIf
  522.  
  523. dummyMarker.MoveToNode(arTarget, asTargetNode)
  524.  
  525. ; MoveTo might have temporarily disabled 3D
  526. ; delay all 3D checks until immediately before usage [USKP 2.0.1]
  527. return false
  528. endFunction
  529.  
  530. ; Variables used during spline paths
  531. float fStraightLineTargetX
  532. float fStraightLineTargetY
  533. float fStraightLineTargetZ
  534. float fStraightLineTargetAngleX
  535. float fStraightLineTargetAngleY
  536. float fStraightLineTargetAngleZ
  537. float fStraightLineSpeed
  538. float fStraightLineMaxRotationSpeed
  539.  
  540. ; Travel to the given reference at a given speed
  541. ; This function will call the OnCritterGoalReached() event upon completion
  542. Function SplineTranslateToRefAtSpeed(ObjectReference arTarget, float afSpeed, float afMaxRotationSpeed)
  543. if CheckViability()
  544. return
  545. endIf
  546.  
  547. ; Make sure we're keyframed so we can be moved around
  548. SetMotionType(Motion_Keyframed, false)
  549.  
  550. ; If applicable, play animation
  551. DoPathStartStuff()
  552.  
  553. ; We're about to kick off a standard spline stranslation, so switch to that state
  554. ; ; Debug.TraceConditional("Critter " + self + " going to state SplineTranslation", bCritterDebug)
  555. CurrentMovementState = "SplineTranslation"
  556.  
  557. ; Compute target location
  558. float ftargetX
  559. float ftargetY
  560. float ftargetZ
  561.  
  562. ; Use a dummy marker to get the node location
  563. if PlaceLandingMarker(arTarget, CurrentTargetNode)
  564. ; extra safeguard here - STEVE40
  565. return
  566. endif
  567.  
  568. ; Place a dummy marker to store the position / orientation
  569. if PlaceDummyMarker(landingMarker, ApproachNodeName)
  570. ; extra safeguard here - STEVE40
  571. return
  572. endif
  573.  
  574. if !(PlayerRef && CheckFor3D( dummyMarker ))
  575. DisableAndDelete(false)
  576. return
  577. endIf
  578.  
  579. ; Use the X,Y and Z of the dummy marker
  580. fStraightLineTargetX = dummyMarker.X
  581. fStraightLineTargetY = dummyMarker.Y
  582. fStraightLineTargetZ = dummyMarker.Z
  583. fStraightLineTargetAngleX = dummyMarker.GetAngleX()
  584. fStraightLineTargetAngleY = dummyMarker.GetAngleY()
  585. fStraightLineTargetAngleZ = dummyMarker.GetAngleZ()
  586.  
  587. float fdeltaX = fStraightLineTargetX - X
  588. float fdeltaY = fStraightLineTargetY - Y
  589. float fdeltaZ = fStraightLineTargetZ - Z
  590. ftargetX = X + fdeltaX * 0.9
  591. ftargetY = Y + fdeltaY * 0.9
  592. ftargetZ = Z + fdeltaZ * 0.9
  593.  
  594. ; Clear target node for next time
  595. CurrentTargetNode = ""
  596.  
  597. fStraightLineSpeed = afSpeed
  598. fStraightLineMaxRotationSpeed = afMaxRotationSpeed
  599.  
  600. ; Kick off the the translation
  601. if CheckViability()
  602. return
  603. endIf
  604. SplineTranslateTo(ftargetX, ftargetY, ftargetZ, fStraightLineTargetAngleX, fStraightLineTargetAngleY, fStraightLineTargetAngleZ, RandomFloat(fPathCurveMean - fPathCurveVariance, fPathCurveMean + fPathCurveVariance), afSpeed, afMaxRotationSpeed)
  605. bCalculating = False; [USKP 2.0.4]
  606. endFunction
  607.  
  608. ; Travel to the given reference's named node
  609. ; This function will call the OnCritterGoalReached() event upon completion
  610. Function SplineTranslateToRefNodeAtSpeed(ObjectReference arTarget, string arNode, float afSpeed, float afMaxRotationSpeed)
  611. ; Set target node name
  612. CurrentTargetNode = arNode
  613.  
  614. ; Call base version
  615. SplineTranslateToRefAtSpeed(arTarget, afSpeed, afMaxRotationSpeed)
  616. endFunction
  617.  
  618. ; Travel to the given reference and then switch to the given state
  619. ; This function will call the OnCritterGoalReached() event upon completion
  620. Function SplineTranslateToRefAtSpeedAndGotoState(ObjectReference arTarget, float afSpeed, float afMaxRotationSpeed, string arTargetState)
  621. ; Set target state for the OnTranslationComplete event
  622. CurrentTargetState = arTargetState
  623.  
  624. ; Call base version
  625. SplineTranslateToRefAtSpeed(arTarget, afSpeed, afMaxRotationSpeed)
  626. endFunction
  627.  
  628. ; Travel to the given reference's named node and then switch to the given state
  629. ; This function will call the OnCritterGoalReached() event upon completion
  630. Function SplineTranslateToRefNodeAtSpeedAndGotoState(ObjectReference arTarget, string arNode, float afSpeed, float afMaxRotationSpeed, string arTargetState)
  631. ; Set target state for the OnTranslationComplete event
  632. CurrentTargetState = arTargetState
  633.  
  634. ; Set target node name
  635. CurrentTargetNode = arNode
  636.  
  637. ; Call base version
  638. SplineTranslateToRefAtSpeed(arTarget, afSpeed, afMaxRotationSpeed)
  639. endFunction
  640.  
  641.  
  642. ; Travel to the given reference at a given speed
  643. ; This function will call the OnCritterGoalReached() event upon completion
  644. Function TranslateToRefAtSpeed(ObjectReference arTarget, float afSpeed, float afMaxRotationSpeed)
  645. if CheckViability()
  646. return
  647. endIf
  648.  
  649. ; Make sure we're keyframed so we can be moved around
  650. SetMotionType(Motion_Keyframed, false)
  651.  
  652. ; If applicable, play animation
  653. DoPathStartStuff()
  654.  
  655. ; We're about to kick off a standard spline stranslation, so switch to that state
  656. ; ; Debug.TraceConditional("Critter " + self + " going to state SplineTranslation", bCritterDebug)
  657. CurrentMovementState = "Translation"
  658.  
  659. ; Compute target location
  660. float ftargetX
  661. float ftargetY
  662. float ftargetZ
  663.  
  664. ; Use a dummy marker to get the node location
  665. if PlaceLandingMarker(arTarget, CurrentTargetNode)
  666. ; extra safeguard here - STEVE40
  667. return
  668. endif
  669.  
  670. ; Place a dummy marker to store the position / orientation
  671. if PlaceDummyMarker(landingMarker, ApproachNodeName)
  672. ; extra safeguard here - STEVE40
  673. return
  674. endif
  675.  
  676. if !(PlayerRef && CheckFor3D( dummyMarker ))
  677. DisableAndDelete(false)
  678. return
  679. endIf
  680.  
  681. ; Use the X,Y and Z of the dummy marker
  682. fStraightLineTargetX = dummyMarker.X
  683. fStraightLineTargetY = dummyMarker.Y
  684. fStraightLineTargetZ = dummyMarker.Z
  685. fStraightLineTargetAngleX = dummyMarker.GetAngleX()
  686. fStraightLineTargetAngleY = dummyMarker.GetAngleY()
  687. fStraightLineTargetAngleZ = dummyMarker.GetAngleZ()
  688.  
  689. float fdeltaX = fStraightLineTargetX - X
  690. float fdeltaY = fStraightLineTargetY - Y
  691. float fdeltaZ = fStraightLineTargetZ - Z
  692. ftargetX = X + fdeltaX * 0.9
  693. ftargetY = Y + fdeltaY * 0.9
  694. ftargetZ = Z + fdeltaZ * 0.9
  695.  
  696. ; Clear target node for next time
  697. CurrentTargetNode = ""
  698.  
  699. fStraightLineSpeed = afSpeed
  700.  
  701. ; Kick off the the translation
  702. if CheckViability()
  703. return
  704. endIf
  705. TranslateTo(ftargetX, ftargetY, ftargetZ, fStraightLineTargetAngleX, fStraightLineTargetAngleY, fStraightLineTargetAngleZ, afSpeed, afMaxRotationSpeed)
  706. bCalculating = False; [USKP 2.0.4]
  707. endFunction
  708.  
  709. ; Travel to the given reference's named node
  710. ; This function will call the OnCritterGoalReached() event upon completion
  711. Function TranslateToRefNodeAtSpeed(ObjectReference arTarget, string arNode, float afSpeed, float afMaxRotationSpeed)
  712. ; Set target node name
  713. CurrentTargetNode = arNode
  714.  
  715. ; Call base version
  716. TranslateToRefAtSpeed(arTarget, afSpeed, afMaxRotationSpeed)
  717. endFunction
  718.  
  719. ; Travel to the given reference and then switch to the given state
  720. ; This function will call the OnCritterGoalReached() event upon completion
  721. Function TranslateToRefAtSpeedAndGotoState(ObjectReference arTarget, float afSpeed, float afMaxRotationSpeed, string arTargetState)
  722. ; Set target state for the OnTranslationComplete event
  723. CurrentTargetState = arTargetState
  724.  
  725. ; Call base version
  726. TranslateToRefAtSpeed(arTarget, afSpeed, afMaxRotationSpeed)
  727. endFunction
  728.  
  729. ; Travel to the given reference's named node and then switch to the given state
  730. ; This function will call the OnCritterGoalReached() event upon completion
  731. Function TranslateToRefNodeAtSpeedAndGotoState(ObjectReference arTarget, string arNode, float afSpeed, float afMaxRotationSpeed, string arTargetState)
  732. ; Set target state for the OnTranslationComplete event
  733. CurrentTargetState = arTargetState
  734.  
  735. ; Set target node name
  736. CurrentTargetNode = arNode
  737.  
  738. ; Call base version
  739. TranslateToRefAtSpeed(arTarget, afSpeed, afMaxRotationSpeed)
  740. endFunction
  741.  
  742.  
  743. ; Variables used during bell-shaped paths
  744. ObjectReference BellShapeTarget = none
  745. float fBellShapeSpeed
  746. float fBellShapeMaxRotationSpeed
  747. float fBellShapeStartX
  748. float fBellShapeStartY
  749. float fBellShapeStartZ
  750. float fBellShapeStartLandingPointX
  751. float fBellShapeStartLandingPointY
  752. float fBellShapeStartLandingPointZ
  753. float fBellShapeTargetPointX
  754. float fBellShapeTargetPointY
  755. float fBellShapeTargetPointZ
  756. float fBellShapeTargetAngleX
  757. float fBellShapeTargetAngleY
  758. float fBellShapeTargetAngleZ
  759. float fBellShapeDeltaX
  760. float fBellShapeDeltaY
  761. float fBellShapeDeltaZ
  762. float fBellShapeHeight
  763.  
  764. ; Travel to the given reference in a bell-shaped path
  765. ; This function will call the OnCritterGoalReached() event upon completion
  766. Function BellShapeTranslateToRefAtSpeed(ObjectReference arTarget, float afBellHeight, float afSpeed, float afMaxRotationSpeed)
  767. if CheckViability()
  768. return
  769. endIf
  770.  
  771. ; Make sure we're keyframed so we can be moved around
  772. SetMotionType(Motion_Keyframed, false)
  773.  
  774. ; If applicable, play animation
  775. DoPathStartStuff()
  776.  
  777. ; We're about to kick off a bell-shaped stranslation, so switch to the first state
  778. ; ; Debug.TraceConditional("Critter " + self + " going to state BellShapeGoingUp", bCritterDebug)
  779. CurrentMovementState = "BellShapeGoingUp"
  780.  
  781. fBellShapeStartX = self.X
  782. fBellShapeStartY = self.Y
  783. fBellShapeStartZ = self.Z
  784.  
  785. ; Use a dummy marker to get the node location
  786. if PlaceLandingMarker(arTarget, CurrentTargetNode)
  787. ; extra safeguard here - STEVE40
  788. return
  789. endif
  790.  
  791. ; Place a dummy marker to store the position / orientation
  792. if PlaceDummyMarker(landingMarker, ApproachNodeName)
  793. ; extra safeguard here - STEVE40
  794. return
  795. endif
  796.  
  797. if !(PlayerRef && CheckFor3D( dummyMarker ))
  798. DisableAndDelete(false)
  799. return
  800. endIf
  801.  
  802. ; Use the X,Y and Z of the dummy marker
  803. fBellShapeStartLandingPointX = dummyMarker.X
  804. fBellShapeStartLandingPointY = dummyMarker.Y
  805. fBellShapeStartLandingPointZ = dummyMarker.Z
  806. fBellShapeTargetPointX = landingMarker.X
  807. fBellShapeTargetPointY = landingMarker.Y
  808. fBellShapeTargetPointZ = landingMarker.Z
  809. fBellShapeTargetAngleX = landingMarker.GetAngleX()
  810. fBellShapeTargetAngleY = landingMarker.GetAngleY()
  811. fBellShapeTargetAngleZ = landingMarker.GetAngleZ()
  812.  
  813. ; Clear target node for next time
  814. CurrentTargetNode = ""
  815.  
  816. ; Compute location for the "UP" portion of the path
  817. fBellShapeDeltaX = fBellShapeTargetPointX - fBellShapeStartX
  818. fBellShapeDeltaY = fBellShapeTargetPointY - fBellShapeStartY
  819. fBellShapeDeltaZ = fBellShapeTargetPointZ - fBellShapeStartZ
  820.  
  821. ; Remember the bell height and the target
  822. fBellShapeHeight = afBellHeight
  823. BellShapeTarget = arTarget
  824. fBellShapeSpeed = afSpeed
  825. fBellShapeMaxRotationSpeed = afMaxRotationSpeed
  826.  
  827. ; Compute point 1/5th of the way along (1/5 is an example, the percentage is defined by a property)
  828. float fFirstWaypointX = fBellShapeStartX + fBellShapeDeltaX * fBellShapedWaypointPercent
  829. float fFirstWaypointY = fBellShapeStartY + fBellShapeDeltaY * fBellShapedWaypointPercent
  830. float fFirstWaypointZ = fBellShapeStartZ + fBellShapeDeltaZ * fBellShapedWaypointPercent + fBellShapeHeight
  831.  
  832. ; Kick off the the translation
  833. if CheckViability()
  834. return
  835. endIf
  836. SplineTranslateTo(fFirstWaypointX, fFirstWaypointY, fFirstWaypointZ, GetAngleX(), GetAngleY(), GetAngleZ(), fPathCurveMean, fBellShapeSpeed, afMaxRotationSpeed)
  837. bCalculating = False; [USKP 2.0.4]
  838. endFunction
  839.  
  840. ; Travel to the given reference's named node in a bell-shaped path
  841. ; This function will call the OnCritterGoalReached() event upon completion
  842. Function BellShapeTranslateToRefNodeAtSpeed(ObjectReference arTarget, string arNode, float afBellHeight, float afSpeed, float afMaxRotationSpeed)
  843. ; Set target node name
  844. CurrentTargetNode = arNode
  845.  
  846. ; Call base version
  847. BellShapeTranslateToRefAtSpeed(arTarget, afBellHeight, afSpeed, afMaxRotationSpeed)
  848. endFunction
  849.  
  850. ; Travel to the given reference in a bell-shaped path and then switch to the given state
  851. ; This function will call the OnCritterGoalReached() event upon completion
  852. Function BellShapeTranslateToRefAtSpeedAndGotoState(ObjectReference arTarget, float afBellHeight, float afSpeed, float afMaxRotationSpeed, string arTargetState)
  853. ; Set target state for the OnTranslationComplete event
  854. CurrentTargetState = arTargetState
  855.  
  856. ; Call base version
  857. BellShapeTranslateToRefAtSpeed(arTarget, afBellHeight, afSpeed, afMaxRotationSpeed)
  858. endFunction
  859.  
  860. ; Travel to the given reference's named node in a bell-shaped path and then switch to the given state
  861. ; This function will call the OnCritterGoalReached() event upon completion
  862. Function BellShapeTranslateToRefNodeAtSpeedAndGotoState(ObjectReference arTarget, string arNode, float afBellHeight, float afSpeed, float afMaxRotationSpeed, string arTargetState)
  863. ; Set target state for the OnTranslationComplete event
  864. CurrentTargetState = arTargetState
  865.  
  866. ; Set target node name
  867. CurrentTargetNode = arNode
  868.  
  869. ; Call base version
  870. BellShapeTranslateToRefAtSpeed(arTarget, afBellHeight, afSpeed, afMaxRotationSpeed)
  871. endFunction
  872.  
  873. Function WarpToRefAndGotoState(ObjectReference arTarget, string asState)
  874. if PlaceLandingMarker(arTarget, "")
  875. ; extra safeguard here - STEVE40
  876. return
  877. endif
  878. MoveTo(landingMarker);
  879.  
  880. ; Switch state
  881. ; ; Debug.TraceConditional("Critter " + self + " Warping to ref (and switching to " + CurrentTargetState + " )", bCritterDebug)
  882. GotoState(asState)
  883. ;! CurrentMovementState == "Idle"
  884. CurrentMovementState = "Idle" ; BUGFIX BY STEVE40
  885. endFunction
  886.  
  887. Function WarpToRefNodeAndGotoState(ObjectReference arTarget, string asNode, string asState)
  888. if PlaceLandingMarker(arTarget, asNode)
  889. ; extra safeguard here - STEVE40
  890. return
  891. endif
  892. MoveTo(landingMarker);
  893.  
  894. ; Switch state
  895. ; ; Debug.TraceConditional("Critter " + self + " Warping to ref (and switching to " + CurrentTargetState + " )", bCritterDebug)
  896. GotoState(asState)
  897. ;! CurrentMovementState == "Idle"
  898. CurrentMovementState = "Idle" ; BUGFIX BY STEVE40
  899. endFunction
  900.  
  901. ;----------------------------------------------
  902. ; Internal bell-shaped path management
  903. ;----------------------------------------------
  904.  
  905. ; Handle translation complete event
  906. Event OnTranslationComplete()
  907. ; prevent DisableAndDelete during calculations [USKP 2.0.3]
  908. bCalculating = True
  909.  
  910. if (CurrentMovementState == "BellShapeGoingUp")
  911. ; Switch state to the next segment
  912. ; ; Debug.TraceConditional("Critter " + self + " going to state BellShapeGoingAcross", bCritterDebug)
  913. CurrentMovementState = "BellShapeGoingAcross"
  914.  
  915. ; Move to the 2nd waypoint
  916. float fsecondWaypointPercent = 1.0 - fBellShapedWaypointPercent
  917. float fSecondWaypointX = fBellShapeStartX + fBellShapeDeltaX * fsecondWaypointPercent
  918. float fSecondWaypointY = fBellShapeStartY + fBellShapeDeltaY * fsecondWaypointPercent
  919. float fSecondWaypointZ = fBellShapeStartZ + fBellShapeDeltaZ * fsecondWaypointPercent + fBellShapeHeight
  920.  
  921. ; Kick off the the translation
  922. if CheckViability()
  923. return
  924. endIf
  925. SplineTranslateTo(fSecondWaypointX, fSecondWaypointY, fSecondWaypointZ, GetAngleX(), GetAngleY(), GetAngleZ(), fPathCurveMean, fBellShapeSpeed, fBellShapeMaxRotationSpeed)
  926. elseif (CurrentMovementState == "BellShapeGoingAcross")
  927. ; Switch state to the last segment
  928. ; ; Debug.TraceConditional("Critter " + self + " going to state BellShapeGoingDown", bCritterDebug)
  929. CurrentMovementState = "BellShapeGoingDown"
  930.  
  931. ; Move to the goal
  932. if CheckViability()
  933. return
  934. endIf
  935. SplineTranslateTo(fBellShapeStartLandingPointX, fBellShapeStartLandingPointY, fBellShapeStartLandingPointZ, fBellShapeTargetAngleX, fBellShapeTargetAngleY, fBellShapeTargetAngleZ, fPathCurveMean, fBellShapeSpeed, fBellShapeMaxRotationSpeed)
  936. elseif (CurrentMovementState == "BellShapeGoingDown")
  937.  
  938. ; Wait for the end event
  939. ; ; Debug.TraceConditional("Critter " + self + " going to state BellShapeLanding", bCritterDebug)
  940.  
  941. ; Play landing animation if applicable
  942. DoPathEndStuff()
  943.  
  944. CurrentMovementState = "BellShapeLanding"
  945.  
  946. ; Move to the destination
  947. if CheckViability()
  948. return
  949. endIf
  950. TranslateTo(fBellShapeTargetPointX, fBellShapeTargetPointY, fBellShapeTargetPointZ, fBellShapeTargetAngleX, fBellShapeTargetAngleY, fBellShapeTargetAngleZ, fBellShapeSpeed * fLandingSpeedRatio, fBellShapeMaxRotationSpeed)
  951. elseif (CurrentMovementState == "BellShapeLanding")
  952.  
  953. ; Switch state
  954. ; ; Debug.TraceConditional("Critter " + self + " going to state Idle (and switching to " + CurrentTargetState + " )", bCritterDebug)
  955. if (CurrentTargetState != "")
  956. ; ; Debug.TraceConditional("Critter " + self + " going to state " + CurrentTargetState, bCritterDebug)
  957. GotoState(CurrentTargetState)
  958. endIf
  959. ;! CurrentMovementState == "Idle"
  960. CurrentMovementState = "Idle" ; BUGFIX BY STEVE40
  961.  
  962. ; Clear all global variables that we shouldn't use anymore, just for safety's sake
  963. BellShapeTarget = none
  964. CurrentTargetState = ""
  965.  
  966. ; Trigger event for derived script to handle
  967. OnCritterGoalReached()
  968. elseif (CurrentMovementState == "SplineTranslation")
  969. ; Play landing animation if applicable
  970. DoPathEndStuff()
  971.  
  972. CurrentMovementState = "StraightLineLanding"
  973.  
  974. ; Move to the destination
  975. if CheckViability()
  976. return
  977. endIf
  978. SplineTranslateTo(fStraightLineTargetX, fStraightLineTargetY, fStraightLineTargetZ, fStraightLineTargetAngleX, fStraightLineTargetAngleY, fStraightLineTargetAngleZ, RandomFloat(fPathCurveMean - fPathCurveVariance, fPathCurveMean + fPathCurveVariance), fStraightLineSpeed * fLandingSpeedRatio, fStraightLineMaxRotationSpeed)
  979. elseif (CurrentMovementState == "StraightLineLanding")
  980. ; Switch state
  981. if (CurrentTargetState != "")
  982. ; ; Debug.TraceConditional("Critter " + self + " going to state " + CurrentTargetState, bCritterDebug)
  983. GotoState(CurrentTargetState)
  984. endIf
  985. ;! CurrentMovementState == "Idle"
  986. CurrentMovementState = "Idle" ; BUGFIX BY STEVE40
  987. CurrentTargetState = ""
  988.  
  989. ; Trigger event for derived script to handle
  990. OnCritterGoalReached()
  991. elseif (CurrentMovementState == "Translation")
  992. ; Play landing animation if applicable
  993. DoPathEndStuff()
  994.  
  995. ; Switch state
  996. if (CurrentTargetState != "")
  997. ; ; Debug.TraceConditional("Critter " + self + " going to state " + CurrentTargetState, bCritterDebug)
  998. GotoState(CurrentTargetState)
  999. endIf
  1000. ;! CurrentMovementState == "Idle"
  1001. CurrentMovementState = "Idle" ; BUGFIX BY STEVE40
  1002. CurrentTargetState = ""
  1003.  
  1004. ; Trigger event for derived script to handle
  1005. OnCritterGoalReached()
  1006. else
  1007. ; Don't know this state, just trigger event for derived script to handle
  1008. OnCritterGoalReached()
  1009. endif
  1010. bCalculating = False; [USKP 2.0.4]
  1011. endEvent
  1012.  
  1013.  
  1014. Event OnTranslationAlmostComplete()
  1015. if ((CurrentMovementState != "BellShapeGoingUp") && (CurrentMovementState != "BellShapeGoingAcross") && (CurrentMovementState != "BellShapeGoingDown") && (CurrentMovementState != "SplineTranslation"))
  1016. ; Trigger custom event
  1017. OnCritterGoalAlmostReached()
  1018. endif
  1019. endEvent
  1020.  
  1021.  
  1022. ; Regardless of state, handle the translation failed event
  1023. Event OnTranslationFailed()
  1024. ; Trigger event
  1025. ; Debug.Trace("Critter " + self + " Translation Failed", 1)
  1026. OnCritterGoalFailed()
  1027. endEvent
  1028.  
  1029. ; Debugging
  1030. Function PrintInitialProperties()
  1031. ; Debug.Trace("Critter " + self + " initial properties")
  1032. ; Debug.Trace("\tfRadius = " + fLeashLength)
  1033. ; Debug.Trace("\tfMaxPlayerDistance = " + fMaxPlayerDistance)
  1034. ; Debug.Trace("\tSpawner = " + Spawner)
  1035. endFunction
  1036.  
  1037.  
  1038. Function DoPathStartStuff()
  1039. ; Transition to the flight state
  1040. ;! SetAnimationVariableFloat("fTakeOff", 1.0); moved to Moth [USKP 2.0.1]
  1041. endFunction
  1042.  
  1043. Function DoPathEndStuff()
  1044. ; Transition to the hover/landed state
  1045. ;! SetAnimationVariableFloat("fTakeOff", 0.0); moved to Moth [USKP 2.0.1]
  1046. endFunction
  1047.  
  1048. ;----------------------------------------
  1049. ; Fly to a random point within the leash radius
  1050. ;----------------------------------------------
  1051. FUNCTION flyAroundSpawner(float fminTravel, float fMaxTravel, float fSpeed, float afMaxRotationSpeed, bool bflyBelowSpawner = false)
  1052. {this function chooses and flies to a new, nearby point}
  1053. ; if flybelowSpawner is
  1054. if CheckViability()
  1055. return
  1056. endIf
  1057. float fMinHeight = fSpawnerZ
  1058. float fMaxheight = fMinHeight + (0.5*fLeashLength)
  1059. float oldX = self.x
  1060. float oldY = self.y
  1061. float oldZ = self.z
  1062. float newX = (self.x + randomFloat(fminTravel, fMaxTravel))
  1063. float newY = (self.Y + randomFloat(fminTravel, fMaxTravel))
  1064. float newZ = (self.Z + randomFloat(fminTravel, fMaxTravel))
  1065. doPathStartStuff()
  1066. ; some safety checks to keep him from flying far from home
  1067. if newX > fSpawnerX
  1068. if newX > fSpawnerX + fLeashLength
  1069. newX = fSpawnerX + fLeashLength
  1070. endif
  1071. else
  1072. if newX < fSpawnerX - fLeashLength
  1073. newX = fSpawnerX - fLeashLength
  1074. endif
  1075. endif
  1076.  
  1077. if newY > fSpawnerY
  1078. if newY > fSpawnerY + fLeashLength
  1079. newY = fSpawnerY + fLeashLength
  1080. endif
  1081. else
  1082. if newY < fSpawnerY - fLeashLength
  1083. newY = fSpawnerY - fLeashLength
  1084. endif
  1085. endif
  1086.  
  1087. if bFlyBelowSpawner == true
  1088. if newZ < fMinHeight
  1089. newZ = fMinHeight
  1090. endif
  1091. if newZ > fMaxHeight
  1092. newZ = fMaxHeight
  1093. endif
  1094. endif
  1095. ; now fly to that spot!
  1096. if CheckViability()
  1097. return
  1098. endIf
  1099. ;gotoMarker.setPosition(newX, newY, newZ)
  1100. TranslateTo(newX, newY, newZ, self.getAngleX(), self.getAngleY(), self.getAngleZ(), fSpeed, afMaxRotationSpeed)
  1101. ; traceConditional(self + " flying to point at: " + newX as int + ", " + newY as int + ", " + newZ as int, bCritterDebug)
  1102. bCalculating = False; [USKP 2.0.4]
  1103. endFunction
  1104.  
  1105. ;/-----------------------------------------
  1106. Checks for this reference still in attached cell.
  1107.  
  1108. Returns false when GetParentCell is "None" or when not attached.
  1109. /;
  1110. Bool Function CheckCellAttached(ObjectReference AnyItemRef)
  1111.  
  1112. Cell parentCell = AnyItemRef.GetParentCell()
  1113.  
  1114. If parentCell == None
  1115. ;~ Trace(Self + "CheckCellAttached() " + AnyItemRef + " GetParentCell == None")
  1116. return False
  1117. EndIf
  1118.  
  1119. Return parentCell.IsAttached()
  1120.  
  1121. EndFunction
  1122.  
  1123. ;/-----------------------------------------
  1124. Waits for a reference's 3D to load, with exponential backoff.
  1125.  
  1126. Returns false when not loaded or when the reference is "None".
  1127. /;
  1128. Bool Function CheckFor3D(ObjectReference AnyItemRef)
  1129.  
  1130. Float delay = 0.016667; one frame
  1131.  
  1132. While AnyItemRef && !AnyItemRef.Is3DLoaded()
  1133. if delay < 8.5; 9 times
  1134. Wait(delay)
  1135. delay += delay
  1136. else
  1137. ;~ TraceStack(Self + "CheckFor3D() " + AnyItemRef + " 3D failed, delay = " + ((delay - 0.016667) as float))
  1138. Return False
  1139. endif
  1140. EndWhile
  1141.  
  1142. If AnyItemRef
  1143. ;~ Trace(Self + "CheckFor3D() " + AnyItemRef + " 3D loaded, delay = " + ((delay - 0.016667) as float))
  1144. Return True
  1145. EndIf
  1146.  
  1147. ;~ Trace(Self + "CheckFor3D() " + AnyItemRef + " == None, delay = " + ((delay - 0.016667) as float))
  1148. Return False
  1149.  
  1150. EndFunction
  1151.  
  1152. ;/-----------------------------------------
  1153. Checks for this reference still viable.
  1154. End bCalculating.
  1155.  
  1156. Returns true when DisableAndDelete [USKP 2.0.4]
  1157.  
  1158. Credit STEVE40 for initial code.
  1159. /;
  1160. Bool Function CheckViability()
  1161.  
  1162. If PlayerRef && !bKilled && CheckCellAttached(self) && CheckFor3D(self)
  1163. return False
  1164. EndIf
  1165.  
  1166. ; allow DisableAndDelete [USKP 2.0.3]
  1167. bCalculating = False
  1168.  
  1169. ;~ Trace(Self + "CheckViability() DisableAndDelete.")
  1170. DisableAndDelete(PlayerRef && !bKilled)
  1171. Return True
  1172.  
  1173. EndFunction
  1174.  
  1175. ;/-----------------------------------------
  1176. Checks for this reference both viable and within distance.
  1177. Begin/Enforce bCalculating.
  1178.  
  1179. Returns false when DisableAndDelete or already calculating.
  1180. /;
  1181. Bool Function CheckViableDistance()
  1182.  
  1183. ; prevent DisableAndDelete during calculations [USKP 2.0.3]
  1184. Bool wasCalculating = bCalculating
  1185. bCalculating = True
  1186.  
  1187. ; repeat PlayerRef test as OnCellAttach can occur during rapid cell boundary crossings
  1188. If PlayerRef && !bKilled && CheckCellAttached(self) && CheckFor3D(self) && PlayerRef && PlayerRef.GetDistance(self) <= fMaxPlayerDistance
  1189. ; prevent repeat calculations [USKP 2.0.4]
  1190. If wasCalculating
  1191. return False
  1192. EndIf
  1193.  
  1194. UnregisterForUpdateGameTime()
  1195. return True
  1196. EndIf
  1197.  
  1198. ; allow DisableAndDelete [USKP 2.0.3]
  1199. bCalculating = wasCalculating
  1200.  
  1201. ;~ Trace(Self + "CheckViable() DisableAndDelete.")
  1202. DisableAndDelete(PlayerRef && !bKilled)
  1203. Return False
  1204.  
  1205. EndFunction
  1206.  
Advertisement
Add Comment
Please, Sign In to add comment