sampleflockmember.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import rxactor
  2. # FlockMember not working at the moment. - Tuco
  3. import sys
  4. import clr
  5. clr.AddReferenceToFile("OpenSim.Region.RexScriptModule.dll")
  6. asm = clr.LoadAssemblyByName('OpenSim.Region.ScriptEngine.Common')
  7. Vector3 = asm.OpenSim.Region.ScriptEngine.Common.LSL_Types.Vector3
  8. import math
  9. import sampleflockinfo
  10. class FlockMember(rxactor.Actor):
  11. def __init__(self, vId):
  12. super(self.__class__,self).__init__(vId)
  13. self.MyFlock = None
  14. self.StartLoc = Vector3(0,0,0)
  15. self.bReady = False
  16. def GetScriptClassName():
  17. return "sampleflockmember.FlockMember"
  18. def EventCreated(self):
  19. super(self.__class__,self).EventCreated()
  20. self.bReady = True
  21. self.SetMesh("rabbit")
  22. self.Activate()
  23. def Activate(self):
  24. if(not self.bReady):
  25. return
  26. if(self.StartLoc == Vector3(0,0,0)):
  27. self.StartLoc = self.llGetPos()
  28. self.Physics = True
  29. self.PhysicsMode = 1
  30. self.Gravity = False
  31. self.Velocity = Vector3(0,0,0)
  32. self.Mass = 15.0
  33. self.SetTimer(0.1,True)
  34. def Deactivate(self):
  35. self.SetTimer(0,False)
  36. self.Velocity = Vector3(0,0,0)
  37. self.Gravity = True
  38. self.PhysicsMode = 0
  39. self.Physics = False
  40. self.llSetPos(self.StartLoc)
  41. self.MyFlock = None
  42. self.DestroyActor()
  43. def GetCenter(self):
  44. try:
  45. CurrentCenter = self.MyFlock.Center - self.llGetPos()
  46. CurrentCenter = CurrentCenter / (self.MyFlock.BoidsInFlock-1)
  47. return CurrentCenter
  48. except:
  49. print "FlockMember,GetCenter", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
  50. def GetAverageVelocity(self):
  51. try:
  52. CurrentAVGVel = self.MyFlock.AVGVel - self.Velocity
  53. CurrentAVGVel = CurrentAVGVel / (self.MyFlock.BoidsInFlock-1)
  54. return CurrentAVGVel
  55. except:
  56. print "FlockMember,GetAverageVelocity", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
  57. def AwayFromNeighbors(self):
  58. try:
  59. awayvec = Vector3(0,0,0)
  60. fullaway = Vector3(0,0,0)
  61. templist = self.GetRadiusActors(self.MyFlock.BoidMinDist)
  62. for i in templist:
  63. tempactor = self.MyWorld.AllActors[i]
  64. if((tempactor != self) and tempactor.__class__.__name__ == "FlockMember"):
  65. awayvec = self.llGetPos() - tempactor.llGetPos()
  66. fullaway = (fullaway + awayvec)
  67. return fullaway
  68. except:
  69. print "FlockMember,AwayFromNeighbors", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
  70. def LimitNewVelocity(self, vStartVel,vLimiter):
  71. try:
  72. limitvel = Vector3(vStartVel)
  73. if(math.fabs(vStartVel.x) > math.fabs(vStartVel.y)):
  74. a = math.fabs(vStartVel.x)
  75. else:
  76. a = math.fabs(vStartVel.y)
  77. if(math.fabs(vStartVel.z) > a):
  78. a = math.fabs(vStartVel.z)
  79. if(a < vLimiter):
  80. return limitvel
  81. else:
  82. limitvel.x = (vStartVel.x * vLimiter / a)
  83. limitvel.y = (vStartVel.y * vLimiter / a)
  84. limitvel.z = (vStartVel.z * vLimiter / a)
  85. return limitvel
  86. except:
  87. print "FlockMember,LimitNewVelocity", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
  88. def GetCurrentTarget(self):
  89. try:
  90. CurrentTarget = self.MyFlock.GetCurrentTarget()
  91. if(CurrentTarget != Vector3(0,0,0)):
  92. return (CurrentTarget-self.llGetPos())
  93. else:
  94. return Vector3(0,0,0)
  95. except:
  96. print "FlockMember,GetCurrentTarget", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
  97. def EventTimer(self):
  98. try:
  99. if(self.MyFlock == None):
  100. return
  101. center_bias = self.GetCenter() - self.llGetPos()
  102. center_bias.x *= 0.6 * self.MyFlock.ModifyCenterBias
  103. center_bias.y *= 0.6 * self.MyFlock.ModifyCenterBias
  104. center_bias.z *= 0.6 * self.MyFlock.ModifyCenterBias
  105. avgvel_bias = self.GetAverageVelocity() - self.Velocity
  106. avgvel_bias.x *= 0.3
  107. avgvel_bias.y *= 0.3
  108. avgvel_bias.z *= 0.3
  109. awayfrommates = self.AwayFromNeighbors()
  110. awayfrommates.x *= 0.7
  111. awayfrommates.y *= 0.7
  112. awayfrommates.z *= 0.7
  113. targetbias = self.GetCurrentTarget()
  114. targetbias.x *= 0.6
  115. targetbias.y *= 0.6
  116. targetbias.z *= 0.6
  117. combinedvel = (center_bias + avgvel_bias + awayfrommates + targetbias)
  118. combinedvel.x *= 40
  119. combinedvel.y *= 40
  120. combinedvel.z *= 40
  121. limitvel = self.LimitNewVelocity(combinedvel,self.MyFlock.BoidMaxSpeed)
  122. self.Velocity = limitvel
  123. except:
  124. print "FlockMember,EventTimer", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]