Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
All Data Structures Namespaces Files Functions Variables
spectre.py
Go to the documentation of this file.
1 import VS
2 import Director
3 import Vector
4 import vsrandom
5 def isCar(c):
6  nam = c.getName()
7  return nam=='car' or nam == 'spectre' or nam=='porsche' or nam=='skart'
8 
9 class Environment(Director.Mission):
10  def toonear (self,vec,un):
11  vec= Vector.Sub(un.Position(),vec)
12  if (Vector.Dot(vec,vec)<1000):
13  return True
14  return False
15  def nearflag(self,un,flag):
16  return un.getDistance(flag)<0
17  def TwoDdis (self,uvec,f):
18  fvec=f.Position()
19  x=fvec[0]-uvec[0];
20  y=fvec[2]-uvec[2];
21  return x*x+y*y
22  def tweakHeight (playa,sub):
23  self.unitheight= sub.Position()[1]
24  def scoreFlag(self,un,f):
25  f.Kill()
26  VS.IOmessage(0,"game","all","Got a flag")
27  def checkUn (self):
28  for X in range (getNumPlayers()):
29  un=getPlayerX (X)
30  uvec=un.Position()
31  rad = un.rSize()
32  rad=rad*rad
33  isnull=False
34  for f in self.flags:
35  if (not f):
36  isNull=True
37  else:
38  if (TwoDdis(uvec,f)<rad):
39  scoreFlag (un,f)
40  for u in self.subs:
41  if (not f):
42  isNull=True
43  else:
44  if (TwoDdis(uvec,u)<rad):
45  tweakHeight(un,u)
46  if (isNull):
47  for i in range(len(self.subs)):
48  if (not self.subs[i]):
49  del self.subs[i]
50  return
51  for j in range(len(self.flags)):
52  if (not self.flags[j]):
53  del self.flags[j]
54  return
55 
56  def randomFlagLocation(self):
57  import vsrandom
58  if (len(self.flags)):
59  i = vsrandom.randrange(0,len(self.flags))
60  return Vector.Add((vsrandom.uniform(-20,20),vsrandom.uniform(-20,20),vsrandom.uniform(-20,20)),self.flags[i].Position())
61  return (0,0,0)
62 
63  def randomLocation(self):
64  import vsrandom
65  for i in range (50):
66  vec=(vsrandom.uniform(-self.arenasize,self.arenasize),
67  0,
69  for i in self.flags:
70  if (self.toonear (vec,i)):
71  continue
72  for i in self.obstacles:
73  if (self.toonear (vec,i)):
74  continue
75  return vec
76  return vec
77  def CreateFlag (self,name):
78  un = VS.launch("flags",name,"neutral","unit","default",1,1,self.randomLocation(),'')
79  self.flags.append(un)
80 
81  def CreateObstacle(self,name):
82  un = VS.launch("flags",name,"neutral","unit","default",1,1,self.randomLocation(),'')
83  self.obstacles.append(un)
84  def CreateSub(self,name):
85  un = VS.launch("flags",name,"neutral","unit","default",1,1,self.randomLocation(),'')
86  self.subs.append(un)
87 
88  def CreateUnit (self, name, defend):
89  evenodd= (2*(self.num_un %2) - 1)
90  if defend:
91  mod='defend'
92  vec=self.randomFlagLocation()
93  else:
94  mod='macho'
95  vec=self.randomLocation()
96  un = VS.launch ("cybernet",name,"unknown", "unit",mod,1,1,vec,'')
97  un.SetOrientation ((0,1,0),(0,0,evenodd))
98  self.num_un+=1
99  def __init__ (self,numobst, numflag,numspec,numseek, numsubs):
100  Director.Mission.__init__(self)
101  self.unitheight=0
102  self.arenasize=1000
103  self.flags=[]
104  self.obstacles=[]
105  self.subs=[]
106  print 'initing'
107  self.iter=0
108  self.num_un=0
109  for i in range(numobst):
110  self.CreateObstacle("box")
111  for i in range(numflag):
112  self.CreateFlag("flag")
113  import vsrandom
114  for i in range(numspec):
115  self.CreateUnit ("spectre",vsrandom.randrange(0,2))
116  for i in range(numseek):
117  self.CreateUnit ("skart",0)
118  for i in range (numsubs):
119  self.CreateSubs("subs");
120  def Execute(self):
121  un = VS.getUnit (self.iter)
122  if (un):
123  if (isCar(un)):
124  self.ApplyEventualEnvironment (un,VS.getPlayer())
125  self.iter+=1
126  else:
127  self.iter=0
128  def EventualExecture (self):
129  self.Execute()
130  def AlwaysExecute(self):
131  self.EventualExecute()
132  iter = VS.getUnitList ()
133  un = iter.current()
134  playa = VS.getPlayer()
135  while (un):
136  if (isCar(un)):
137  self.ApplyPerFrameEnvironment (un,playa)
138  iter.advance()
139  un = iter.current()
140  def ApplyEventualEnvironment(self,un,playa):
141  unheight=0
142  if (un.getName()=='skart' or un.isPlayerStarship()!=-1):
143  unheight= self.unitheight
144  pos=(un.Position()[0],unheight,un.Position()[2])
145  un.SetCurPosition (pos)
146 # def ApplyPerFrameEnvironment (self,un,playa):
147 # pos = un.Position()
148  vel = un.GetVelocity()
149 # if (pos[1]!=0):
150 # pos=(pos[0],0,pos[2])
151 # un.SetCurPosition(pos)
152  if (vel[1]!=0):
153  vel = (vel[0],0,vel[2])
154  un.SetVelocity(vel)
155 # if (vel[0]!=0):
156 # vel = (0,vel[1],vel[2])
157 # un.SetVelocity(vel)