def __init__(self): self.m_Position = Vector3D() self.m_Destination = Vector3D() self.m_Velocity = Vector3D() self.m_Heading = Vector3D() self.m_dViewRadius = 50.0 self.m_dAngle = 1.0 self.m_MinX = self.m_MinZ = self.m_MaxX = self.m_MaxZ = 0
def intersect(self, origin, vector): try: step = Vector3D(v=vector) step.normalize() inc = 0 sgn = 1.0 e = 0.0 if abs(step.x) > abs(step.z): if step.z < 0: sgn = -1 step.x /= abs(step.x) step.z /= abs(step.x) else: if step.x < 0: sgn = -1 inc = 1 step.x /= abs(step.z) step.z /= abs(step.z) error = Vector3D(x=0, y=0, z=0) point = Vector3D(v=origin) while True: if inc == 0: if e + abs(step.z) + 0.5 >= 1: point.z += sgn e -= 1 e += abs(step.z) point.x += step.x else: if e + abs(step.x) + 0.5 >= 1: point.x+=sgn e-= 1 e += abs(step.x) point.z += step.z if not self.m_Map.CanWalk(int(math.floor(point.x/8)), int(math.floor(point.z/8))): return error.length() if point.x < 0 or point.y < 0 or point.z < 0: break if point.x >= (self.m_Map.GetSizeX() * 8) or point.z >= (self.m_Map.GetSizeZ() * 8): break error.add(step) except: print("INTERSECT FAILED", origin, vector) return 0.0
def Shot(self, _idAgent): s = None minDistance = 1e10 #numero grande :-) a = None for agent in self.m_AgentList.values(): if agent.m_JID == _idAgent: a = agent break if a == None: return None #return -1 # agentes for agent in self.m_AgentList.values(): if agent.m_JID == _idAgent: continue if agent.m_iHealth <= 0: continue p = Vector3D(v=a.m_Locate.m_Position) p.sub(agent.m_Locate.m_Position) dv = p.dot(a.m_Locate.m_Heading) d2 = a.m_Locate.m_Heading.dot(a.m_Locate.m_Heading) sq = (dv * dv) - ((d2 * p.dot(p)) - 4) if sq >= 0: sq = math.sqrt(sq) dist1 = (-dv + sq) / d2 dist2 = (-dv - sq) / d2 if dist1 < dist2: distance = dist1 else: distance = dist2 if distance > 0 and distance < minDistance: minDistance = distance s = agent if s != None: v = Vector3D(v=s.m_Locate.m_Position) v.sub(a.m_Locate.m_Position) dDistanceTerrain = self.intersect(a.m_Locate.m_Position, a.m_Locate.m_Heading) #print "distanceTerrain: " + str(dDistanceTerrain) if dDistanceTerrain != 0.0 and dDistanceTerrain < minDistance: s = None return s
def CreateControlPoints(self): i_max_cp = 2 i_radius = 2 if self.m_eClass in [self.CLASS_MEDIC, self.CLASS_FIELDOPS]: i_max_cp = 3 i_radius = 10 elif self.m_eClass == self.CLASS_SOLDIER: i_max_cp = int(random.random() * 5) + 5 i_radius = 50 elif self.m_eClass in [self.CLASS_ENGINEER, self.CLASS_NONE]: pass self.m_ControlPoints = [] # Vector3D [iMaxCP] for i in range(0, i_max_cp - 1): control_point = Vector3D() while True: x = self.m_Map.GetTargetX() + ((i_radius / 2) - (random.random() * i_radius)) z = self.m_Map.GetTargetZ() + ((i_radius / 2) - (random.random() * i_radius)) if self.CheckStaticPosition(x, z): control_point.x = x control_point.z = z self.m_ControlPoints.append(control_point) break
def __init__(self): self.m_Position = Vector3D() self.m_eType = CPack.PACK_NONE self.m_eTeam = 0 self.m_bTaken = False self.m_Owner = 0 CDinObject.m_Index += 1 self.m_id = CDinObject.m_Index
def __init__(self): self.m_Position = Vector3D() self.m_id = 0 self.m_eTeam = 0 self.m_eType = 0 self.m_dDistance = 0.0 self.m_dAngle = 0.0 self.m_iHealth = 0
def __init__(self): self.m_Terrain = None self.m_AlliedBase = CBase() self.m_AxisBase = CBase() self.m_Target = Vector3D() self.m_iSizeX = 0 self.m_iSizeZ = 0
def test_dot(self): vector = Vector3D(x=2, y=3, z=0.5) reply = vector.dot(self.vector) self.assertEqual( reply, 24 )
def test_setters(self): mobile = CMobile() vector = Vector3D(x=1, y=2, z=3) mobile.setDestination(vector) self.assertEqual(mobile.m_Destination.x, vector.x) self.assertEqual(mobile.m_Destination.y, vector.y) self.assertEqual(mobile.m_Destination.z, vector.z)
def test_calculate_position(self): dt = 0.001 mobile = CMobile() mobile.m_Velocity = Vector3D(x=10, y=20, z=30) mobile.CalculatePosition(dt) self.assertEqual(mobile.m_Position.x, mobile.m_Velocity.x * dt) self.assertEqual(mobile.m_Position.y, 0) self.assertEqual(mobile.m_Position.z, mobile.m_Velocity.z * dt)
def start(self): CPack.start(self) self.m_bTaken = False self.m_eType = self.PACK_OBJPACK self.m_Origin = Vector3D() self.m_Origin.x = self.m_Position.x self.m_Origin.y = self.m_Position.y self.m_Origin.z = self.m_Position.z t = Template() t.set_metadata("performative", "inform") self.add_behaviour(self.PackLostResponderBehaviour(), t)
def test_cross(self): vector_x = Vector3D(x=1) vector_y = Vector3D(y=1) vector_z = vector_x.cross(vector_y) self.assertEqual( vector_z.x, 0 ) self.assertEqual( vector_z.y, 0 ) self.assertEqual( vector_z.z, 1 )
def __init__(self): self.m_jid = "" self.m_iType = 0 self.m_iPriority = 0 self.m_StampTime = 0 self.m_iData = 0 self.m_fData = 0.0 self.m_iPacksDelivered = 0 self.m_ObjPointer = None self.m_Position = Vector3D() self.m_bErasable = True CTask.m_Index = CTask.m_Index + 1 self.m_id = CTask.m_Index
def test_calculate_new_orientation(self): mobile = CMobile() x = 10 y = 20 z = 30 mobile.m_Destination = Vector3D(x=x, y=y, z=z) mobile.CalculateNewOrientation() leng = math.sqrt((x * x + y * y + z * z)) self.assertEqual(mobile.m_Heading.x, x / leng) self.assertEqual(mobile.m_Heading.y, y / leng) self.assertEqual(mobile.m_Heading.z, z / leng)
def __init__(self, name, passwd="secret", manager_jid="cmanager@localhost", x=0, z=0, team=0): CJGomasAgent.__init__(self, name, passwd, team) self.m_eType = self.PACK_NONE self.m_Manager = manager_jid self.m_Position = Vector3D() self.m_Position.x = (x * 8) self.m_Position.y = 0 self.m_Position.z = (z * 8) self.m_eTeam = team
def move(self, _dt): p = Vector3D() p.x = self.m_Movement.m_Position.x p.y = self.m_Movement.m_Position.y p.z = self.m_Movement.m_Position.z if self.m_Movement.CalculatePosition(_dt): if not self.CheckStaticPosition(): self.m_Movement.m_Position.x = p.x self.m_Movement.m_Position.y = p.y self.m_Movement.m_Position.z = p.z print(self.name + ": No puedo andar : (" + str(self.m_Movement.m_Position.x) + ", " + str(self.m_Movement.m_Position.z) + ")") return self.MV_CANNOT_GET_POSITION return self.MV_OK return self.MV_NOT_MOVED_BY_TIME
def test_sub(self): vector = Vector3D(x=1, y=3, z=3) vector.sub(self.vector) self.assertEqual( vector.x, -2 ) self.assertEqual( vector.y, -1 ) self.assertEqual( vector.z, -9 )
def test_add(self): vector = Vector3D(x=1, y=2, z=3) vector.add(self.vector) self.assertEqual( vector.x, 4 ) self.assertEqual( vector.y, 6 ) self.assertEqual( vector.z, 15 )
def test_normalize(self): vector = Vector3D(self.vector) leng = vector.length() vector.normalize() self.assertEqual( vector.x, self.vector.x/leng ) self.assertEqual( vector.y, self.vector.y / leng ) self.assertEqual( vector.z, self.vector.z / leng )
def GetObjectsInFieldOfView(self, _idAgent): ObjectsInSight = list() a = None for agent in self.m_AgentList.values(): if agent.m_JID == _idAgent: a = agent if a is None: return ObjectsInSight dotAngle = float(a.m_Locate.m_dAngle) # am I watching agents? for agent in self.m_AgentList.values(): if agent.m_JID == _idAgent: continue if agent.m_iHealth <= 0: # OJO, igual interesa ke veamos muertos :D continue v = Vector3D(v=agent.m_Locate.m_Position ) v.sub(a.m_Locate.m_Position) distance = v.length() # comprobamos la distancia # miramos la distancia a la pared mas cercano dDistanceTerrain = self.intersect(a.m_Locate.m_Position, v) # a.m_Locate.m_Heading) # comprobamos la distancia if distance < a.m_Locate.m_dViewRadius and distance < dDistanceTerrain: #testeamos el angulo angle = a.m_Locate.m_Heading.dot(v) try: angle /= a.m_Locate.m_Heading.length() * v.length() except: # Division BY ZEROOOOO!!!! pass if angle >= 0: if angle > 1: angle = 1 angle = math.acos(angle) if angle <= dotAngle: s = CSight() s.m_dDistance = distance s.m_id = agent.m_id s.m_Position = agent.m_Locate.m_Position s.m_eTeam = agent.m_eTeam s.m_eType = agent.m_eType s.m_dAngle = angle s.m_iHealth = agent.m_iHealth ObjectsInSight.append(s) # am I watching objects? if len(self.m_DinObjectList) > 0: for dinObject in self.m_DinObjectList.values(): v = Vector3D(v=dinObject.m_Position) v.sub(a.m_Locate.m_Position) distance = v.length() # comprobamos la distancia # miramos la distancia a la pared mas cercano dDistanceTerrain = self.intersect(a.m_Locate.m_Position, v) #a.m_Locate.m_Heading) if distance < a.m_Locate.m_dViewRadius and distance < dDistanceTerrain: angle = a.m_Locate.m_Heading.dot(v) angle /= (a.m_Locate.m_Heading.length() * v.length()) if angle >= 0: if angle > 1: angle = 1 angle = math.acos(angle) if angle <= dotAngle: s = CSight() s.m_dDistance = distance s.m_id = int(dinObject.m_id) s.m_Position = dinObject.m_Position s.m_eTeam = dinObject.m_eTeam s.m_eType = dinObject.m_eType s.m_dAngle = angle s.m_iHealth = -1 ObjectsInSight.append(s) return ObjectsInSight
class TestVector3D(unittest.TestCase): vector = Vector3D(x=3, y=4, z=12) def test_length(self): res = self.vector.__len__() self.assertEqual( res, 13 ) def test_add(self): vector = Vector3D(x=1, y=2, z=3) vector.add(self.vector) self.assertEqual( vector.x, 4 ) self.assertEqual( vector.y, 6 ) self.assertEqual( vector.z, 15 ) def test_sub(self): vector = Vector3D(x=1, y=3, z=3) vector.sub(self.vector) self.assertEqual( vector.x, -2 ) self.assertEqual( vector.y, -1 ) self.assertEqual( vector.z, -9 ) def test_dot(self): vector = Vector3D(x=2, y=3, z=0.5) reply = vector.dot(self.vector) self.assertEqual( reply, 24 ) def test_normalize(self): vector = Vector3D(self.vector) leng = vector.length() vector.normalize() self.assertEqual( vector.x, self.vector.x/leng ) self.assertEqual( vector.y, self.vector.y / leng ) self.assertEqual( vector.z, self.vector.z / leng ) def test_cross(self): vector_x = Vector3D(x=1) vector_y = Vector3D(y=1) vector_z = vector_x.cross(vector_y) self.assertEqual( vector_z.x, 0 ) self.assertEqual( vector_z.y, 0 ) self.assertEqual( vector_z.z, 1 ) def test_str(self): res = str(self.vector) self.assertEqual( res, '<3,4,12>' )
def __init__(self): self.m_Init = Vector3D() self.m_End = Vector3D()