Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
All Data Structures Namespaces Files Functions Variables
generate_dyn_universe.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 import VS
3 import Director
4 import vsrandom
5 import fg_util
6 import dj_lib # Needs to be enabled when loading.
7 import campaigns
8 from universe import getAdjacentSystemList,AllSystems
9 import debug
10 import ShowProgress
11 
12 cp=fg_util.ccp
13 maxshipsinfg=20
14 fgnames=[] #list of lists of flightgroup names for each faction
15 fgoffset=[] #list of tweak offsets
16 origfgnames=[]
17 import faction_ships
18 def XProductionRate(fac,type):
19  if fac in type:
20  return type[fac]
21  return type["default"]
22 def floatToRand(maximum):
23  if maximum==0:
24  return 0
25  tmp=vsrandom.randrange(0,int(maximum)+1)
26  if (vsrandom.uniform(0,1)<maximum-int(maximum)):
27  return tmp+1
28  return tmp
29 def GenerateCivilianFgShips (faction,factionnr,docapships):
30  lst=[]
31  capitals=faction_ships.capitals[factionnr]
32  fighters=faction_ships.fighters[factionnr]
33  numfighters=1
34  try:
35  numfighters=faction_ships.fightersPerFG["default"]
36  numfighters=faction_ships.fightersPerFG[faction]
37  except:
38  pass
39  numcapitals=0
40  if docapships:
41  try:
42  numcapitals=faction_ships.fightersPerFG["default"]
43  numcapitals=faction_ships.PerFG[faction]
44  except:
45  pass
46  fighters_per=len(fighters)
47  ht={}
48  for i in fighters:
49  numtogen=floatToRand(numfighters/float(len(fighters)))
50  if numtogen:
51  if i in ht:
52  ht[i]+=numtogen
53  else:
54  ht[i]=numtogen
55  for i in capitals:
56  numtogen=floatToRand(numcapitals/float(len(capitals)))
57  if numtogen>0:
58  if i in ht:
59  ht[i]+=numtogen
60  else:
61  ht[i]=numtogen
62  for hv in ht:
63  lst.append([hv,ht[hv]])
64  return lst
65 def GenerateFgShips (shipinfg,factionnr,friendly):
66  lst=[]
67  capship=()
68  fac = faction_ships.intToFaction(factionnr)
69  fpr=XProductionRate(fac,faction_ships.fighterProductionRate)
70  cpr=XProductionRate(fac,faction_ships.capitalProductionRate)
71  if cpr>0 and (friendly==2 or (friendly==1 and vsrandom.random()<cpr/fpr)):
72  capship=((faction_ships.getRandomCapitolInt(factionnr),1),)
73  debug.debug("Generating capital "+str(capship))
74  return ((faction_ships.getRandomFighterInt(factionnr),shipinfg),)+capship
75 
77  global fgnames,origfgnames,fgoffset
78  for fnr in xrange(faction_ships.getMaxFactions()):
79  fgnames.append(fg_util.GetRandomFGNames(-1,faction_ships.factions[fnr]))
80  fgoffset.append(0)
81  origfgnames.append(list(fgnames[-1]))
82 
83 
84 doNotAddBasesTo={"enigma_sector/heavens_gate":1,"sol_sector/celeste":1,"enigma_sector/enigma":1,"enigma_sector/niven":1,"Gemini":1,"Crucible/Cephid_17":1}
85 def AddBasesToSystem (faction,sys):
86  if (sys in doNotAddBasesTo):
87  return
88  slash = sys.find("/")
89  if (slash!= -1):
90  if (sys[0:slash] in doNotAddBasesTo):
91  return
92  if faction in faction_ships.factions:
93  fsfac= list(faction_ships.factions).index(faction)
94  numbases=0
95 # numplanets=VS.GetGalaxyProperty(sys,"num_planets");
96  numjumppoints=VS.GetNumAdjacentSystems(sys);
97  if (numjumppoints<4):
98  if (vsrandom.random()>=.25):
99  numbases=1
100  elif (vsrandom.random()>=.005):
101  if (numjumppoints<7):
102  numbases=vsrandom.randrange(1,int(numjumppoints/2)+1)
103  elif numjumppoints==7:
104  numbases=vsrandom.randrange(1,6)
105  else:
106  numbases=vsrandom.randrange(1,numjumppoints+1)
107  if numbases==0:
108  return
109  shiplist=[]
110  nums=[]
111  for i in xrange(numbases):
112  whichbase = faction_ships.bases[fsfac][vsrandom.randrange(0,len(faction_ships.bases[fsfac]))]
113  if whichbase in shiplist:
114  nums[shiplist.index(whichbase)]+=1
115  else:
116  shiplist.append(whichbase)
117  nums.append(1)
118  tn =[]
119  for i in xrange(len(shiplist)):
120  tn+=[ (shiplist[i],nums[i])]
121  fg_util.AddShipsToFG(fg_util.BaseFGInSystemName (sys),faction,tn,sys)
122 
123 numericalfaction=0
124 
125 def GetNewFGName(faction):
126  factionnr=faction_ships.factionToInt(faction)
127  global numericalfaction,fgnames,origfgnames,fgoffset
128  if(factionnr>=len(fgnames)):
129  debug.error("Faction "+faction+" unable to create fgname")
130 
131  numericalfaction+=1
132  return "Alpha_"+str(numericalfaction)
133  if (not len(fgnames[factionnr])):
134  debug.debug("Loading FG names for %s" % faction)
135  fg_util.origoffset=fgoffset[factionnr]
136  fgnames[factionnr]=fg_util.TweakFGNames(origfgnames[factionnr])
137  fgoffset[factionnr]+=1
138  k=vsrandom.randrange(0,len(fgnames[factionnr])); #pop returns item inside array
139  fgname=fgnames[factionnr][k]
140  del fgnames[factionnr][k]
141  return fgname
142 
143 _generatedsys = 0
144 _last_progress_percent = -1
145 def AddSysDict (cursys):
146  global _generatedsys, _last_progress_percent
147  #pick random fighter from insysenemies with .3 probability OR pick one from the friendlies list.
148 # debug.debug('Addsysdict')
149  sysfaction=VS.GetGalaxyFaction(cursys)
150 
152 
153  #debug.debug("Initializing system %s with %d flightgroups... " % (cursys,numflightgroups))
154  progress_percent = (float(_generatedsys) / getSystemCount())
155  if progress_percent - _last_progress_percent > 0.01:
156  ShowProgress.setProgressBar("loading",progress_percent)
157  ShowProgress.setProgressMessage("loading","Generating dynamic universe (%.2f%%)" % (100*progress_percent))
158  _last_progress_percent = progress_percent
159  _generatedsys += 1
160 
161  global fgnames, fglists
162  i=0
163  AddBasesToSystem(sysfaction, cursys)
164  adjsystems=getAdjacentSystemList(cursys)
165  for i in xrange(len(faction_ships.factions)):
166  thisfac=faction_ships.factions[i]
167  thisfactionnr=faction_ships.factionToInt(thisfac)
168  rel=VS.GetRelation(sysfaction,thisfac)
169  iscit=VS.isCitizen(thisfac)
170  if iscit and thisfac.find(sysfaction)!=-1:
171  #debug.debug("generating spc civilian for "+cursys+" faction "+thisfac)
172  #do somethign special for this military faction
173  if thisfac==sysfaction:
174  pass
175  elif iscit:
176  for neighbor in adjsystems:
177  if (VS.GetRelation(thisfac,VS.GetGalaxyFaction(neighbor))>-.05):
178  #fgname=cursys+"->"+neighbor
179  fgname="(CivReg) "+GetNewFGName(thisfac)
180  typenumbertuple=GenerateCivilianFgShips(thisfac,thisfactionnr,True)
181  fg_util.AddShipsToFG (fgname,thisfac,typenumbertuple,cursys)
182  fgname="Insystem_"+cursys;
183  typenumbertuple=GenerateCivilianFgShips(thisfac,thisfactionnr,False)
184  if iscit:
185  #debug.debug("generating gen civilian for "+cursys+" faction "+thisfac)
186  if VS.GetRelation(thisfac,sysfaction)>-.05:#brave citizens of the new order...
187  fgname=GetNewFGName(thisfac)
188  typenumbertuple=GenerateCivilianFgShips(thisfac,thisfactionnr,True)
189  fg_util.AddShipsToFG (fgname,thisfac,typenumbertuple,cursys)
190  friendlychance=.7
191  if cursys in faction_ships.fortress_systems:
192  friendlychance=faction_ships.fortress_systems[cursys]
193  #debug.debug('enemy chance for '+cursys +'= '+str(friendlychance))
194  for i in xrange (numflightgroups): #number of fgs in a system.
195  faction=sysfaction
196  friendly=0
197  if not sysfaction or sysfaction=='unknown' or vsrandom.random()>friendlychance:
198  faction=faction_ships.get_rabble_of_no_citizen(sysfaction)#why even have citizens on the list then
199  else:
200  faction=faction_ships.get_friend_of_no_citizen(sysfaction)#likewise--- maybe this should be a faction_ships fix
201  if (faction==sysfaction):
202  friendly=1
203  if (sysfaction in faction_ships.production_centers):
204  if (cursys in faction_ships.production_centers[sysfaction]):
205  friendly=2
206  #if (friendly):
207  # debug.debug(faction+" "+sysfaction+" "+cursys)
208  factionnr=faction_ships.factionToInt(faction)
209  global maxshipsinfg
210  typenumbertuple=GenerateFgShips(vsrandom.randrange(maxshipsinfg)+1,factionnr,friendly)
211  fgname=GetNewFGName(faction)
212  fg_util.AddShipsToFG (fgname,faction,typenumbertuple,cursys)
213  return i
214 
215 def ForEachSys (functio):
216  debug.debug("Getting reachable systems...")
217  systems = AllSystems()
218  debug.debug("done")
219  for sys in systems:
220  functio(sys)
221  return len(systems)
224  ForEachSys(AddSysDict)
225 
226 systemcount={None:0}
227 def getSystemCount(fac=None):
228  global systemcount
229  return systemcount[fac]
230 def CountSystems(sys):
231  fac =VS.GetGalaxyFaction(sys)
232  if fac in systemcount:
233  systemcount[fac]+=1
234  else:
235  systemcount[fac]=1
236  systemcount[None] += 1
237 def TakeoverSystem(fac,sys):
238  systemcount[VS.GetGalaxyFaction(sys)]-=1
239  VS.SetGalaxyFaction(sys,fac)
240  systemcount[fac]+=1
241  AddBasesToSystem(fac,sys)
242 
243 hasUniverse=False
244 
245 genUniverse=-1
247  global genUniverse, hasUniverse
248  if cp>=0:
249  ShowProgress.activateProgressScreen("loading",force=True)
250  debug.debug('Purging...')
251  for i in fg_util.AllFactions():
253  systemcount[i]=0
254  debug.debug('StartSystemCount')
255  ForEachSys(CountSystems)
256  debug.debug(systemcount)
257  debug.debug('EndSystemCount')
258  genUniverse=0
259  needNewUniverse=0
260  curfaclist = fg_util.AllFactions()
261  reflist = fg_util.ReadStringList(cp,"FactionRefList")
262 
263  if (reflist !=curfaclist):
264  needNewUniverse = 1
265  debug.debug('reflist is '+str(reflist))
266  debug.debug('curfaclist is '+str(curfaclist))
267 
269  needNewUniverse = 1
270  debug.warn('save using legacy FG format... resetting universe to reformat')
272 
273  if needNewUniverse:
274  fg_util.WriteStringList(cp,"FactionRefList",curfaclist)
275  debug.debug('generating ships... ... ...')
276  GenerateAllShips() ###Insert number of flight groups and max ships per fg
277  debug.debug('placing ships... ... ...')
278  genUniverse=MakeUniverse()
279  #now every system has distributed ships in the save data!
280  else:
282  debug.debug("Second Load")
283  for i in xrange(len(fgnames)):
284  fg_util.origfgoffset=fgoffset[i]
285  fgnames[i]=fg_util.TweakFGNames(origfgnames[i])
286  fgoffset[i]+=1
288  hasUniverse=True;
289  #TODO: add ships to current system (for both modes) uru?
291  else:
292  debug.error('fatal error: no cockpit')
293 
295  if VS.networked():
296  dj_lib.enable()
297  #debug.debug('Not generating dyn universe: Networked game')
298  return False
299 
300  sys = VS.getSystemFile()
301  if not VS.GetNumAdjacentSystems(sys):
302  #debug.debug('Not generating dyn universe: System has no jumps or is not in Universe XML.')
303  return False
304 
305  dj_lib.enable()
306  #curfaclist = fg_util.AllFactions()
307  #reflist = fg_util.ReadStringList(cp,"FactionRefList")
308  #if (reflist == curfaclist):
309  # debug.debug('Generating dyn universe!');
310  if not hasUniverse:
312  return True
313  return False