Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
universe.py
Go to the documentation of this file.
1 ##import ai_flyto_jumppoint
2 ##current_system=""
3 ##last_system=""
4 ##old_system=""
5 ##system_map={}
6 ##outstr=""
7 
8 import VS
9 import vsrandom
10 import faction_ships
11 import Director
12 import launch
13 import debug
14 
15 def catInCatList (cat,catlist):
16  for i in catlist:
17  loc =cat.find (i)
18  if (loc==0):
19  return 1
20  return 0
21 
22 def adjustUnitCargo(un,cat,pr,qr):
23  numcargo = un.numCargo()
24  carglist =[]
25  for i in range(numcargo):
26  carg = un.GetCargoIndex(i)
27  if (len(cat)==0 or catInCatList(carg.GetCategory(),cat)):
28  carg.SetPrice (pr*carg.GetPrice())
29  carg.SetQuantity (int(qr*carg.GetQuantity()))
30  carglist += [carg]
31  for carg in carglist:
32  un.removeCargo (carg.GetCategory(),carg.GetQuantity(),1)
33  for carg in carglist:
34  un.addCargo(carg)
35  carglist=0
36 
37 #universe.systemCargoDemand (("Natural_Products","starships",),.0001,1000)
38 def systemCargoDemand (category,priceratio,quantratio,ships=1,planets=1):
39  i = VS.getUnitList()
40  un = i.current()
41  while (not un.isNull()):
42  if (un.isPlayerStarship()==-1):
43  isplanet = un.isPlanet()
44  if ( (isplanet and planets) or ((not isplanet) and ships)):
45  adjustUnitCargo(un,category,priceratio,quantratio)
46  i.advance()
47  un=i.current()
48 
49 def setFirstSaveData(player,key,val):
50  mylen = Director.getSaveDataLength(player,key)
51  if (mylen>0):
52  Director.putSaveData(player,key,0,val)
53  else:
54  Director.pushSaveData(player,key,val)
55 
56 def getAdjacentSystems (currentsystem, sysaway, jumps=(),preferredfaction=''):
57  """returns a tuple in the format ("[lastsystem]",(system1,system2,system3,...))"""
58  if preferredfaction=='':
59  preferredfaction=VS.GetGalaxyProperty(currentsystem,"faction")
60  max=VS.GetNumAdjacentSystems(currentsystem)
61  if ((sysaway<=0) or (max<=0)):
62 # _io.message (1,"game","all","Your final destination is %s" % (currentsystem))
63  return (currentsystem,jumps)
64  else:
65  syslist=[]
66  numadj=VS.GetNumAdjacentSystems(currentsystem)
67  for i in range(numadj):
68  cursys=VS.GetAdjacentSystem(currentsystem,i)
69  if preferredfaction!=None:
70  if VS.GetGalaxyProperty(cursys,"faction")!=preferredfaction:
71  continue
72  if ((cursys in jumps) or (cursys == VS.getSystemFile())):
73  continue
74  syslist.append(cursys)
75  if not len(syslist):
76  return getAdjacentSystems(currentsystem,0,jumps)
77  nextsystem=syslist[vsrandom.randrange(0,len(syslist))]
78 # _io.message (1,"game","all","Jump from %s to %s." % (currentsystem,nextsystem))
79  return getAdjacentSystems(nextsystem,sysaway-1,jumps+(nextsystem,))
80 
81 #def getAdjacentSystems (currentsystem, num_systems_away):
82 # return nearsys (currentsystem,num_systems_away,())
83 
84 def getAdjacentSystemList (tothissystem):
85  list=[]
86  max=VS.GetNumAdjacentSystems(tothissystem);
87  for i in range(max):
88  list.append(VS.GetAdjacentSystem(tothissystem,i))
89  return list
90 
92  jp_list=getJumppointList()
93  size=len(jp_list)
94  if (size>0):
95  return jp_list[vsrandom.randrange(0,size)]
96  else:
97  return VS.Unit()
98 
100  jp_list=[]
101  i = VS.getUnitList()
102  i.advanceNJumppoint(0)
103  while i.notDone():
104  jp_list.append(i.current())
105  i.advanceJumppoint()
106  return jp_list
107 
109  num=un.isPlayerStarship()
110  if (num<0):
111  return "all"
112  else:
113  return "p"+str(num)
114 
115 def punish (you,faction,difficulty):
116  VS.AdjustRelation(you.getFactionName(),faction,difficulty*-.01,1)
117  if (difficulty>=2):
118  VS.IOmessage (0,"mission",getMessagePlayer(you),"#ff0000Your idiocy will be punished.")
119  VS.IOmessage (0,"mission",getMessagePlayer(you),"#ff0000You had better run for what little life you have left.")
120  for i in range(difficulty):
122  newunit=launch.launch_wave_around_unit("shadow", faction, un, "default", 1, 200.0,400.0,you)
123  newunit.setFgDirective("B")
124  newunit.SetTarget(you)
125 
126 def _tmpint(str,default):
127  try:
128  return int(str)
129  except:
130  return default
131 
133  ret=[]
134  iter= VS.getUnitList()
135  iter.advanceNSignificant(0)
136  while (iter.notDone()):
137  un = iter.current()
138  debug.debug('Found sig unit: '+un.getName()+' ('+un.getFullname()+')')
139  if not un.isSignificant():
140  debug.error('Unit '+un.getName()+' ('+un.getFullname()+') is not significant!')
141  ret.append(iter.current())
142  iter.advanceSignificant()
143  return ret
144 
146  numjmp=VS.GetNumAdjacentSystems(cursys)
147  return _tmpint(VS.GetGalaxyProperty(cursys,"num_planets"),3)+_tmpint(VS.GetGalaxyProperty(cursys,"num_moons"),4)+_tmpint(VS.GetGalaxyProperty(cursys,"num_gas_giants"),2)+_tmpint(VS.GetGalaxyProperty(cursys,"num_starbases"),1)+numjmp
148 #use go_somewhere_significant instead:
149 ##def __init__(): #(?)
150 ## outstr=_string.new()
151 ## current_system=_std.GetSystemName()
152 ## last_system=_std.GetSystemName()
153 ## old_system=_std.GetSystemName()
154 ## system_map=_omap.new()
155 ## _omap.set(system_map,current_system,current_system)
156 ##
157 ##def Execute():
158 ## jumped=false
159 ## current_system=_std.GetSystemName()
160 ## if(current_system!=last_system):
161 ## // we have jumped
162 ## _io.sprintf(outstr,"jumped from %s to %s",last_system,current_system)
163 ## _io.message(0,"game","all",outstr)
164 ## old_system=last_system
165 ## last_system=_std.GetSystemName()
166 ## jumped=true
167 ## return jumped
168 
169 def greet(greetingText,enemy=None,you=None):
170  for i in range(len(greetingText)):
171  color="#ff0000"
172  text=greetingText[i]
173  if type(greetingText[i])==tuple:
174  if not len(greetingText[i]):
175  continue
176  if len(greetingText[i])>1 and greetingText[i][1]:
177  color="#0000ff"
178  if len(greetingText[i])>2 and greetingText[i][2] and you:
179  VS.playSound(greetingText[i][2],(0.,0.,0.),(0.,0.,0.))
180  if enemy:
181  you.communicateTo(enemy,-1)
182  #else: doesn't actually do anything
183  # you.communicateTo(VS.Unit(),-1)
184  text=greetingText[i][0]
185  if (enemy):
186  fromname=enemy.getFlightgroupName()+", "+enemy.getName()+"#000000"
187  else:
188  fromname="[Unidentified]"
189  if (you):
190  toname = getMessagePlayer(you)
191  else:
192  toname="all"
193  VS.IOmessage (8+i*4,color+fromname,toname,"#ff0000"+text+"#000000")
194 
196  iter = VS.getUnitList()
197  while iter.notDone():
198  if VS.getPlayer().isDocked(iter.current()) or iter.current().isDocked(VS.getPlayer()):
199  return iter.current()
200  iter.advance()
201  return iter.current()
202 
204  un = getDockedBase()
205  if (un):
206  return (un.getName(),un.getFullname())
207  return ('','')
208 
209 def ReachableSystems(startingsys):
210  rv_list=[]
211  closed={}
212  opened=[startingsys]
213  while len(opened):
214  openmore=[]
215  for sys in opened:
216  if not sys in closed:
217  closed[sys]=1
218  rv_list.append(sys)
219  openmore += getAdjacentSystemList(sys)
220  opened=openmore
221  return rv_list
222 
224  sys=VS.getSystemFile()
225  if (VS.GetGalaxyProperty("Sol/Sol","jumps")!="" and VS.GetGalaxyProperty("Sol/Sol","faction")!=""):
226  sys="Sol/Sol"
227  return ReachableSystems(sys)
228 
229 def addTechLevel(level, addToBase=True):
230  try:
231  import faction_ships
232  upgrades=faction_ships.earnable_upgrades[level]
233  except:
234  debug.warn("No tech level named "+str(level))
235  return
236  bas=getDockedBase()
237  if (not bas):
238  import unit
239  import vsrandom
240  debug.debug("getting significant for upgrade addage")
241  bas = unit.getSignificant(vsrandom.randrange(1,20),1,0);
242  for upgrade in upgrades:
243  if (len(upgrade)<5):
244  debug.debug("Upgrade list not big enough to add to tech")
245  print upgrade
246  continue
247  import Director
248  import VS
249  cp = VS.getCurrentPlayer()
250  siz = Director.getSaveStringLength(cp,"master_part_list_content")
251  doIt=True
252  for index in range (siz):
253  if (Director.getSaveString(cp,"master_part_list_content",index)==upgrade[0]):
254  doIt=False
255  if (doIt):
256  debug.debug("added UPGRADE AS FOLLOWS to tech level ")
257  print upgrade
258  Director.pushSaveString(cp,"master_part_list_content",str(upgrade[0]))
259  Director.pushSaveString(cp,"master_part_list_category",str(upgrade[1]))
260  Director.pushSaveString(cp,"master_part_list_price",str(upgrade[2]))
261  Director.pushSaveString(cp,"master_part_list_mass",str(upgrade[3]))
262  Director.pushSaveString(cp,"master_part_list_volume",str(upgrade[4]))
263  if (len(upgrade)>5):
264  Director.pushSaveString(cp,"master_part_list_description",str(upgrade[5]))
265  else:
266  Director.pushSaveString(cp,"master_part_list_description","No description")
267  if (bas and addToBase):
268  debug.debug(" adding "+str(upgrade[0]) +" to base "+bas.getName())
269  cargo=VS.Cargo(str(upgrade[0]),str(upgrade[1]),float(upgrade[2]),1,float(upgrade[3]),float(upgrade[4]))
270  bas.forceAddCargo(cargo)