impactVisualizationController.py (6487B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\eveclientqatools\impactVisualizationController.py 2 from __future__ import print_function 3 import threading 4 import trinity 5 import geo2 6 import math 7 import random 8 9 class ImpactVisualizationController(object): 10 11 def __init__(self, shipId = None, impactVelocityScalar = 1000, impactObjectMass = 0.0, damageLocatorID = 0, randomize = False): 12 self.shipId = shipId 13 self.damageLocatorID = damageLocatorID 14 self.impactArrow = None 15 self.arrowModel = None 16 self.impactArrowBoundingRadius = 0.0 17 self.impactRotation = geo2.QuaternionRotationSetYawPitchRoll(0.0, math.pi, 0.0) 18 self.damageLocatorPos = (0.0, 0.0, 0.0) 19 self.arrowPositionUpdater = None 20 self.impactVelocity = None 21 self.randomize = randomize 22 self.arrowModel = trinity.Load('res:/Model/global/impactDirection.red') 23 self.impactObjectMass = impactObjectMass 24 self.impactVelocityScalar = impactVelocityScalar 25 self.SetupImpactArrow() 26 self.AddImpactArrowToScene() 27 self.damageLocatorsMaxValue = 0 28 self.OnSetShipId(self.shipId) 29 30 def Close(self): 31 print('attempting to remove the arrow ...') 32 self.RemoveImpactArrowFromScene() 33 self.arrowPositionUpdater.Stop() 34 35 def OnSetImpactVelocityScalar(self, value): 36 self.impactVelocityScalar = float(value) 37 self._OnSetDamageLocator(self.damageLocatorID) 38 39 def OnSetImpactMass(self, impactObjectMass): 40 self.impactObjectMass = float(impactObjectMass) 41 42 def SetupImpactArrow(self): 43 self.impactArrow = trinity.EveRootTransform() 44 self.impactArrow.name = 'DebugImpactArrow' 45 self.impactArrow.children.append(self.arrowModel) 46 self.arrowPositionUpdater = ArrowPositionUpdater(self.impactArrow) 47 self.OnSetImpactMass(self.impactObjectMass) 48 self.SetDamageLocator(self.damageLocatorID) 49 50 def AddImpactArrowToScene(self): 51 scene = sm.GetService('sceneManager').GetActiveScene() 52 scene.objects.append(self.impactArrow) 53 54 def OnSetShipId(self, shipId): 55 try: 56 shipId = long(shipId) 57 except ValueError: 58 print("Could not set shipId to '%s'" % shipId) 59 return False 60 61 if sm.GetService('michelle').GetBall(shipId) is None: 62 print("No ball with id '%d' found in ballpark" % shipId) 63 return False 64 print('Setting ship ID to %d' % shipId) 65 self.shipId = shipId 66 self.arrowPositionUpdater.SetBall(self.GetBall()) 67 damageLocators = self.GetDamageLocators() 68 self.damageLocatorsMaxValue = len(damageLocators) - 1 69 self._OnSetDamageLocator(self.damageLocatorID) 70 return True 71 72 def GetBall(self, ballID = None): 73 if ballID is None: 74 ballID = self.shipId 75 return sm.GetService('michelle').GetBall(ballID) 76 77 def GetDamageLocators(self): 78 return self.GetBall().model.locatorSets.FindByName('damage').locators 79 80 def SetDamageLocator(self, damageLocatorIDString): 81 self._OnSetDamageLocator(int(damageLocatorIDString)) 82 83 def _OnSetDamageLocator(self, damageLocatorID): 84 self.damageLocatorID = damageLocatorID 85 self.arrowPositionUpdater.SetDamageLocator(self.damageLocatorID) 86 _, rotation, __ = self.GetDamageLocators()[self.damageLocatorID] 87 self.impactVelocity = geo2.QuaternionTransformVector(rotation, (0.0, self.impactVelocityScalar, 0.0)) 88 self.impactVelocity = (-self.impactVelocity[0], -self.impactVelocity[1], -self.impactVelocity[2]) 89 self.arrowPositionUpdater.SetArrowDirection(self.impactVelocity) 90 91 def RemoveImpactArrowFromScene(self): 92 scene = sm.GetService('sceneManager').GetActiveScene() 93 objectsToRemove = [] 94 for o in scene.objects: 95 if o.name == 'DebugImpactArrow': 96 objectsToRemove.append(o) 97 98 for o in objectsToRemove: 99 scene.objects.remove(o) 100 101 def ApplyPhysicalImpact(self): 102 if self.randomize: 103 newDamageLocatorID = random.randint(0, len(self.GetDamageLocators()) - 1) 104 while newDamageLocatorID == self.damageLocatorID: 105 newDamageLocatorID = random.randint(0, len(self.GetDamageLocators()) - 1) 106 107 self.damageLocatorID = newDamageLocatorID 108 self._OnSetDamageLocator(self.damageLocatorID) 109 sm.GetService('michelle').GetBall(self.shipId).ApplyTorqueAtDamageLocator(self.damageLocatorID, self.impactVelocity, self.impactObjectMass) 110 111 112 class ArrowPositionUpdater(object): 113 114 def __init__(self, arrowObject): 115 self.arrowObject = arrowObject 116 self.stop = False 117 self.damageLocator = 0 118 self.ball = None 119 self.arrowScale = 1.0 120 self.arrowDirection = (0.0, 1.0, 0.0) 121 self.arrowModel = self.arrowObject.children[0] 122 arrowGeometry = self.arrowModel.mesh.geometry 123 self.arrowPoint = (0.0, arrowGeometry.GetBoundingBox(0)[1][1], 0.0) 124 self.arrowRotation = (0.0, 0.0, 0.0, 1.0) 125 self.thread = threading.Thread(target=self.Update) 126 self.thread.start() 127 128 def SetDamageLocator(self, damageLocatorIndex): 129 self.damageLocator = damageLocatorIndex 130 131 def SetBall(self, ball): 132 self.ball = ball 133 self.arrowScale = ball.model.GetBoundingSphereRadius() * 0.1 134 self.arrowModel.scaling = (self.arrowScale, self.arrowScale, self.arrowScale) 135 136 def SetArrowDirection(self, direction): 137 impactDir = geo2.Vec3Normalize(direction) 138 self.arrowRotation = geo2.QuaternionRotationArc((0, 1, 0), impactDir) 139 if impactDir == (0, -1, 0): 140 self.arrowRotation = geo2.QuaternionRotationSetYawPitchRoll(0.0, math.pi, 0.0) 141 142 def Stop(self): 143 self.stop = True 144 145 def Update(self): 146 print('starting update thread') 147 while not self.stop: 148 if self.ball: 149 arrowCenter = geo2.QuaternionTransformVector(self.arrowRotation, (self.arrowPoint[0] * self.arrowScale, self.arrowPoint[1] * self.arrowScale, self.arrowPoint[2] * self.arrowScale)) 150 dl = self.ball.GetModel().GetTransformedDamageLocator(self.damageLocator) 151 self.arrowObject.rotation = self.arrowRotation 152 self.arrowObject.translation = (dl[0] - arrowCenter[0], dl[1] - arrowCenter[1], dl[2] - arrowCenter[2]) 153 154 print('stopping update thread')