def randomMovement(fg,fac): import universe import fg_util citizen=VS.isCitizen(fac) sys=fg_util.FGSystem(fg,fac) convoywhere=fg.find("->") if (convoywhere!=-1): endpoints=[fg[0:convoywhere],fg[convoywhere+2:]] if fg.find("Insys")!=-1: return#can't go about moving insys fgs away if (sys!='nil' and fg!=fg_util.BaseFGInSystemName(sys)): l = universe.getAdjacentSystemList(sys) nthis = fg_util.NumFactionFGsInSystem(fac,sys) if (len(l)): suggestednewsys = l[vsrandom.randrange(0,len(l))] sugfac=VS.GetGalaxyFaction(suggestednewsys) sugrel=VS.GetRelation(fac,sugfac) if convoywhere==-1 and (((fac.find(sugfac)!=-1 or sugrel>.5) and citizen) or not citizen): fg_util.TransferFG( fg,fac,suggestednewsys); #debug.debug('moving '+fg+' from '+sys+' to '+ suggestednewsys) else: for i in l: ifac = VS.GetGalaxyFaction(i) #if (ifac==fac and nthis > fg_util.NumFactionFGsInSystem(fac,i)): # suggestednewsys = i likes=False if convoywhere!=-1: if endpoints[0]==i or endpoints[1]==i: likes=True elif VS.GetRelation(fac,ifac)>.5: likes=True if (likes): fg_util.TransferFG(fg,fac,i) return #debug.debug('moving '+fg+' from '+sys+' to '+ suggestednewsys) fg_util.TransferFG( fg,fac,suggestednewsys); else: pass #debug.debug("flightgroup "+fg+" in system "+sys + " is stuck")
def Siege(fac): global siegenumber global siegenumtimes global siegeprob # turns_till_siege_effective=100 if (not fac in faction_ships.siegingfactions): return numfg= fg_util.NumAllFlightgroups(fac) if (numfg): if (siegenumber==0): siegeprob = float(numfg)/float(faction_ships.siegingfactions[fac]); siegenumtimes = int (siegeprob) if (siegenumtimes==0): siegenumtimes=1 else: siegeprob =1 if siegenumber>=siegenumtimes: siegenumber=0 return 0 else: if (vsrandom.uniform(0,1)<siegeprob): fg =fg_util.RandomFlightgroup(fac) sys = fg_util.FGSystem(fg,fac) enfac=VS.GetGalaxyFaction(sys) if (not sys in faction_ships.invincible_systems): #fg_util.CheckAllShips(fac) #fg_util.CheckAllShips(enfac) if enfac == "unknown": debug.debug("exploration: "+sys) fgleader = fg_util.getFgLeaderType(fg,fac) exploration = 1 else: exploration = 0 if (VS.GetRelation(fac,enfac)<0 and neighborFaction(sys,fac)):#FIXME maybe even less than that numenemyfg = fg_util.NumFactionFGsInSystem(enfac,sys) numfriendfg = fg_util.NumFactionFGsInSystem(fac,sys) #debug.debug('siegarol enemioes '+str(numenemyfg)+ ' friends '+str(numfriendfg)) global dnewsman_ if exploration: if sys != 'nil': generate_dyn_universe.TakeoverSystem(fac,sys) #HACK, regenerate bases instnatly dnewsman_.writeDynamicString([str(Director.getSaveData(0,"stardate",0)),dnewsman_.TYPE_EXPLORATION,dnewsman_.STAGE_END,fac,enfac,dnewsman_.SUCCESS_WIN,str(getImportanceOfSystem(sys)),sys,dnewsman_.KEYWORD_DEFAULT,fg,fgleader,"unknown","unknown"]) #FIXME use keyword (ignore #keyword for now Daniel) elif (numenemyfg==0 and numfriendfg==0): #If both annihalate each other completely (unlikely but possible) facnum = VS.GetFactionIndex (fac) debug.debug('cehcking started') debug.error("DRAW error "+fg+" sys has "+sys+" has " +str(fg_util.NumFactionFGsInSystem(fac,sys))+" String is "+Director.getSaveString(0,fg_util.MakeStarSystemFGKey(sys),facnum)) if sys != 'nil': dnewsman_.writeDynamicString([str(Director.getSaveData(0,"stardate",0)),dnewsman_.TYPE_SIEGE,dnewsman_.STAGE_END,fac,enfac,dnewsman_.SUCCESS_WIN,str(getImportanceOfSystem(sys)),sys,dnewsman_.KEYWORD_DEFAULT,fg,"unknown","unknown","unknown"]) #FIXME use keyword (ignore #keyword for now Daniel) elif (numenemyfg==0 and (fg_util.CapshipInFG(fg,fac) or moveSurroundingCapshipsToSiege(fac,sys))): #If aggressor succeeded debug.debug(fac + ' took over '+ sys + ' originally owned by '+enfac) #ok now we have him... while the siege is going on the allies had better initiate the battle--because we're now defending the place... so that means if the owners are gone this place is ours at this point in time fgs = fg_util.FGsInSystem(fac,sys) if (len(fgs)>0): fgs=fgs[0] else: fgs = "unknown" if sys != 'nil': dnewsman_.writeDynamicString([str(Director.getSaveData(0,"stardate",0)),dnewsman_.TYPE_SIEGE,dnewsman_.STAGE_END,fac,enfac,dnewsman_.SUCCESS_WIN,str(getImportanceOfSystem(sys)),sys,dnewsman_.KEYWORD_DEFAULT,fgs,"unknown","unknown","unknown"]) #FIXME use keyword (ignore #keyword for now Daniel) generate_dyn_universe.TakeoverSystem(fac,sys) #HACK, regenerate bases instnatly elif (numfriendfg==0): #If aggressor lost debug.error('wtf!!') fgs = fg_util.FGsInSystem(enfac,sys) if (len(fgs)>0): fgs=fgs[0] else: fgs = "unknown" if sys != 'nil': dnewsman_.writeDynamicString([str(Director.getSaveData(0,"stardate",0)),dnewsman_.TYPE_SIEGE,dnewsman_.STAGE_END,fac,enfac,dnewsman_.SUCCESS_LOSS,str(getImportanceOfSystem(sys)),sys,dnewsman_.KEYWORD_DEFAULT,"unknown","unknown",fgs,"unknown"]) #FIXME use keyword (ignore #keyword for now Daniel) #FIXME add if statements if there is instead a (non appocalyptic) draw (if waring factions have relations "almost neutral" you could have a cease fire, or if the two factions are evenly matched and go nowhere a withdraw of the attackers might occur)!!! siegenumber+=1 return 1 else: return 0
def launch_near(self, un, forceLaunch=False): if (VS.GetGameTime() < 10 and not forceLaunch): debug.debug("hola!") return cursys = VS.getSystemFile() cursysfaction = VS.GetGalaxyFaction(cursys) # Computing probability numbers is relatively expensive, # accessing multiple times FG/Galaxy/Universe API. # Although with some optimization it becomes acceptable on # a director loop, this is still much better: we compute # probabilities roughly once per system entry, and then just # launch a single flightgroup per director execution (to spread # launch load in time and make it less intrusive on gameplay - # ie, stutter less noticeably). Outside code will take care # of ceasing to call this function when no more flightgroups # are needed. # Validate probability cache if (cursysfaction != self.probability_cache[0]) \ or (cursys != self.probability_cache[1]) \ or (faction_ships.getMaxFactions() != len(self.probability_cache[2])): debug.debug('Probability numbers:') psum = 0 probs = [] for factionnum in range(faction_ships.getMaxFactions()): faction = faction_ships.intToFaction(factionnum) num = fg_util.NumFactionFGsInSystem( faction, cursys) # will count bases... but... much quicker. if num == 1: #cannot accept counting bases in this case... num = len(fg_util.FGsInSystem(faction, cursys)) avg = float(num) / float( fg_util.MaxNumFlightgroupsInSystem( cursys)) #/float(numsigs) fortress_level = 0 if cursys in faction_ships.fortress_systems: foretress_level = faction_ships.fortress_systems[cursys] avg *= (not (VS.GetRelation(cursysfaction, faction) < 0 and cursys in faction_ships.fortress_systems) ) * fortress_level + (1 - fortress_level) debug.debug('Chance for %s ship: %g' % (faction, avg)) probs.append(avg) psum += avg self.probability_cache = (cursysfaction, cursys, probs, psum) else: debug.debug('Probability numbers cached.') # Launch a single random flightgroup in the bunch rnd = vsrandom.random() * self.probability_cache[3] p = 0 for factionnum in range(len(self.probability_cache[2])): p += self.probability_cache[2][factionnum] if (p >= rnd): #now we know that we will generate some ships! faction = faction_ships.intToFaction(factionnum) fglist = fg_util.FGsInSystem(faction, cursys) for k in range(10): #try 10 times flightgroup = fglist[vsrandom.randrange(len(fglist))] typenumbers = fg_util.GetShipsInFG(flightgroup, faction) if not len(typenumbers): continue debug.debug('FG Name: "%s", ShipTypes: %s' % (flightgroup, str(typenumbers))) launch_recycle.launch_types_around( flightgroup, faction, typenumbers, 'default', self.generation_distance * vsrandom.random() * 0.9, un, self.generation_distance * vsrandom.random() * 2, '', '', 100) break # assume after 10 tries we launched a flightgroup, we're done here break # Update DJ dj_lib.PlayMusik(0, dj_lib.HOSTILE_NEWLAUNCH_DISTANCE)