Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
traffic.py
Go to the documentation of this file.
1 import VS
2 import sys
3 import vsrandom
4 from Vector import *
5 import car_lib
6 maxspeed=45
7 
8 class TrafficAI(VS.PythonAI):
9  def restoreCruisingSpeed(self,speed):
10  vel = Scale (self.GetParent().GetOrientation()[2],speed)
11  self.MatchLinearVelocity(0,vel,0,1)
12  self.AddReplaceLastOrder(1)
13  self.GetParent().setSpeed(speed)
14  def init(self,un):
15  self.shipiter=0;
16  self.speed = vsrandom.uniform (maxspeed/3,maxspeed);
17  print 'self.speed'
18  print self.speed
19  self.stopping=0
20  self.begin=1
21  self.restoreCruisingSpeed(self.speed)
22  def Execute(self):
23  if (self.begin):
24  self.begin=0
25  self.restoreCruisingSpeed(self.speed)
26  VS.PythonAI.Execute(self)
27  un = VS.getUnit (self.shipiter);
28  parent = self.GetParent()
29  if (parent and un):
30  if (parent!=un):
31  if (car_lib.isCar (un)):
32  posdiff=SafeNorm(Sub (un.Position(),parent.Position())) #look 1 second ahead
33 # distInOneSec = Dot (Sub(parent.GetVelocity(),un.GetVelocity()),posdiff)
34  distInOneSec = Dot (Sub(parent.GetVelocity(),un.GetVelocity()),posdiff)
35  if (distInOneSec>un.getDistance(parent) and Dot (posdiff,un.GetOrientation()[2])>0):
36 # print "%s too close to %s \n" % (parent.getName(),un.getName())
37 # print distInOneSec
38 # print un.getDistance (parent)
39  self.restoreCruisingSpeed(0)
40  self.stopping=1
41  self.shipiter=-1 #-1 + 1 =0
42 
43  self.shipiter +=1
44  else:
45  if (self.stopping):
46  if (parent):
47  self.stopping=0
48  self.restoreCruisingSpeed(self.speed)
49  self.shipiter=0
50  return
51 hi1 = TrafficAI()
52 print 'AI creation successful'
53 hi1 = 0
54 #: 1.7; previous revision: 1.6