def GetFactionToDefend(thisfaction, fac, cursys): m = fg_util.FGsInSystem("merchant", cursys) nummerchant = len(m) m += fg_util.FGsInSystem(thisfaction, cursys) numthisfac = len(m) m += fg_util.FGsInSystem(fac, cursys) return (m, nummerchant, numthisfac)
def LookForSystemWideTrouble(faction,sys): fg = fg_util.FGsInSystem(faction,sys) for i in fg: enemyfac = faction_ships.get_enemy_of (faction) efg = fg_util.AllFGsInSystem(enemy,sys) if (len(efg)): index=vsrandom.randrange(0,len(efg))#FIXME include some sort of measure "can I win" initiateAttack(fg,faction,sys,efg[index],enemyfac)
def AddFighterTo(fgname,fac,isNew=False): sys = VS.getSystemFile() #debug.debug('add fighter') numsystems = generate_dyn_universe.systemcount[fac] if (VS.GetGalaxyFaction(sys)!=fac): try: homeworlds=faction_ships.production_centers except: homeworlds=faction_ships.homeworlds if fac in homeworlds: if type(homeworlds[fac])==type(""): sys=homeworlds[fac] else: sys=homeworlds[fac][vsrandom.randrange(0,len(homeworlds[fac]))] numfighters = int(generate_dyn_universe.XProductionRate(fac,faction_ships.fighterProductionRate)*numsystems) try: if fac in faction_ships.staticFighterProduction: numfighters+=faction_ships.staticFighterProduction[fac] except: pass if (numfighters<1): if (vsrandom.uniform(0,1)<numfighters): numfighters=1 #debug.debug("Generating "+str(numfighters)+ " fighters for "+fac+" at "+sys) if isNew: fgk=fg_util.FGsInSystem(fac,sys) if len(fgk): fgname=fgk[vsrandom.randrange(0,len(fgk))] stat=6 if fac in faction_ships.fightersPerFG: stat=faction_ships.fightersPerFG[fac] elif "default" in faction_ships.fightersPerFG: stat=faction_ships.fightersPerFG["default"] if fg_util.NumShipsInFG(fgname,fac)+numfighters<=numfighters+stat: isNew=False if isNew: fgname=generate_dyn_universe.GetNewFGName(fac) if numfighters>=1: fg_util.AddShipsToFG (fgname,fac,((faction_ships.getRandomFighter(fac),int(numfighters)),),sys) numcapships = generate_dyn_universe.XProductionRate(fac,faction_ships.capitalProductionRate)*numsystems if (numcapships<1): if (vsrandom.uniform(0,1)>numcapships): return numcapships=1 sys = fg_util.FGSystem(fgname,fac) if (1 or VS.GetGalaxyFaction(sys)!=fac): try: homeworlds=faction_ships.production_centers except: homeworlds=faction_ships.homeworlds if fac in homeworlds: if type(homeworlds[fac])==type(""): sys=homeworlds[fac] else: sys=homeworlds[fac][vsrandom.randrange(0,len(homeworlds[fac]))] cap =faction_ships.getRandomCapitol(fac) #debug.debug("Generating "+str(numcapships)+ " capship "+cap+" for "+fac+" at "+sys) fg_util.AddShipsToFG(fgname,fac,((cap,int(numcapships)),),sys)
def launch_near(self, un): if (VS.GetGameTime() < 10): debug.debug("launch_near called too soon!") return cursys = VS.getSystemFile() #numsigs=universe.GetNumSignificantsForSystem(cursys) for factionnum in range(faction_ships.getMaxFactions() - 1): faction = faction_ships.intToFaction(factionnum) fglist = fg_util.FGsInSystem(faction, cursys) if not len(fglist): debug.debug('no flight group for faction: ' + faction + ' in system ' + cursys + '.') continue num = len(fglist) debug.info( 'Probability numbers: %d %d' % (num, fg_util.MaxNumFlightgroupsInSystem(cursys))) #,numsigs 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(VS.GetGalaxyFaction(cursys), faction) < 0 and cursys in faction_ships.fortress_systems) ) * fortress_level + (1 - fortress_level) debug.debug('Chance for %s ship: %g' % (faction, avg)) rndnum = vsrandom.random() debug.debug('Random number: %g; will generate ship: %d' % (rndnum, rndnum < avg)) if rndnum < avg: #now we know that we will generate some ships! flightgroup = fglist[vsrandom.randrange(len(fglist))] typenumbers = fg_util.GetShipsInFG(flightgroup, faction) 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, '')
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 contractMissionsFor(fac, minsysaway, maxsysaway): global totalMissionNumber global insysMissionNumber totalMissionNumber = 0 insysMissionNumber = 0 facnum = faction_ships.factionToInt(fac) enemies = list(faction_ships.enemies[facnum]) script = '' cursystem = VS.getSystemFile() thisfaction = VS.GetGalaxyFaction(cursystem) preferredfaction = None if (VS.GetRelation(fac, thisfaction) >= 0): preferredfaction = thisfaction #try to stay in this territory l = [] num_wingmen = 2 num_rescue = 2 num_defend = 1 num_idefend = 2 num_bounty = 1 num_ibounty = 1 num_patrol = 1 num_ipatrol = 1 num_escort = 1 num_iescort = 1 mincount = 2 usedcats = {} plr = VS.getPlayer() if plr: for i in range(plr.numCargo()): usedcats[plr.GetCargoIndex(i).GetCategory()] = 1 for i in range(minsysaway, maxsysaway + 1): for j in getSystemsNAway(cursystem, i, preferredfaction): import dynamic_battle if (i < 2 and num_rescue > 0): if j[-1] in dynamic_battle.rescuelist: generateRescueMission(j, dynamic_battle.rescuelist[j[-1]]) if checkCreatedMission(): num_rescue -= 1 # if (0 and i==0): # generateRescueMission(j,("confed","Shadow","pirates")) l = dynamic_battle.BattlesInSystem(j[-1]) nodefend = 1 for k in l: if (VS.GetRelation(fac, k[1][1]) >= 0): if ((j[-1] == VS.getSystemFile() and num_idefend <= 0) or (j[-1] != VS.getSystemFile() and num_defend <= 0)): mungeFixerPct() debug.debug("Munged") else: nodefend = 0 generateDefendMission(j, k[1][0], k[1][1], k[0][0], k[0][1]) restoreFixerPct() if checkInsysNum(): num_idefend -= 1 if checkMissionNum(): num_defend -= 1 debug.debug("Generated defendX with insys at: " + str(num_idefend) + " and outsys at " + str(num_defend)) (m, nummerchant, numthisfac) = GetFactionToDefend(thisfaction, fac, j[-1]) if preferredfaction: for kk in faction_ships.enemies[ faction_ships.factiondict[thisfaction]]: k = faction_ships.intToFaction(kk) for mm in fg_util.FGsInSystem(k, j[-1]): if (i == 0 or vsrandom.randrange(0, 4) == 0): #fixme betterthan 4 if nodefend and len(m) and vsrandom.random() < .4: if 1: #for i in range(vsrandom.randrange(1,3)): insys = (j[-1] == VS.getSystemFile()) if (insys and num_idefend <= 0): mungeFixerPct() elif (num_defend <= 0 and not insys): mungeFixerPct() rnd = vsrandom.randrange(0, len(m)) def_fg = m[rnd] def_fac = "merchant" if rnd >= nummerchant: def_fac = thisfaction if rnd >= numthisfac: def_fac = fac generateDefendMission( j, def_fg, def_fac, mm, k) restoreFixerPct() if checkInsysNum(): num_idefend -= 1 if checkMissionNum(): num_defend -= 1 debug.debug( "Generated defendY with insys at: " + str(num_idefend) + " and outsys at " + str(num_defend)) nodefend = 0 elif ((i == 0 or vsrandom.random() < .5)): if ((j[-1] == VS.getSystemFile() and num_ibounty <= 0) or (j[-1] != VS.getSystemFile() and num_bounty <= 0)): mungeFixerPct() generateBountyMission(j, mm, k) restoreFixerPct() if checkInsysNum(): debug.debug( " decrementing INSYS bounty to " + str(num_ibounty)) num_ibounty -= 1 if checkMissionNum(): debug.debug(" decrementing bounty to " + str(num_bounty)) num_bounty -= 1 mincount = -2 if i == 0: mincount = 1 for k in range(vsrandom.randrange( mincount, 4)): ###FIXME: choose a better number than 4. if k < 0: k = 0 rnd = vsrandom.random() if (rnd < .15): # 15% - nothing pass if (rnd < .5 or i == 0): # 35% - Patrol Mission if ((j[-1] == VS.getSystemFile() and num_ipatrol <= 0) or (j[-1] != VS.getSystemFile() and num_patrol <= 0)): mungeFixerPct() if (vsrandom.randrange(0, 2) or j[-1] in faction_ships.fortress_systems): generatePatrolMission(j, vsrandom.randrange(4, 10)) else: generateCleansweepMission( j, vsrandom.randrange(4, 10), faction_ships.get_enemy_of(fac)) restoreFixerPct() if checkInsysNum(): num_ipatrol -= 1 if checkMissionNum(): num_patrol -= 1 else: # 50% - Cargo mission numcargos = vsrandom.randrange(1, 25) if numcargos > 20: numcargos = 20 category = '' if (rnd > .87 and fac != 'confed' and fac != "ISO" and fac != "militia" and fac != "homeland-security" and fac != "kilrathi" and fac != "merchant" and fac != "klkk"): category = 'Contraband' else: for myiter in range(100): carg = VS.getRandCargo(numcargos, category) category = carg.GetCategory() if ((not category in usedcats) and category[:9] != 'Fragments' and category[:10] != 'Contraband' and category.find('upgrades') != 0 and (category.find('starships') != 0 or rnd > .999)): break if (myiter != 99): category = '' # debug.debug("CATEGORY OK "+category) if not category in usedcats: generateCargoMission(j, numcargos, category, fac) usedcats[category] = 1 numescort = vsrandom.randrange(0, 2) if (numescort > len(m)): numescort = len(m) count = 0 for k in m: if (i == 0): if vsrandom.random() < .92: count += 1 continue elif (vsrandom.random() < .97): count += 1 continue f = "merchant" if count >= nummerchant: f = thisfaction if count >= numthisfac: f = fac if (vsrandom.random() < .25): if (num_wingmen > 0): generateWingmanMission(k, f) num_wingmen -= 1 elif (i == 0): if (vsrandom.random() < .25): if num_iescort <= 0: mungeFixerPct() generateEscortLocal(j, k, f) restoreFixerPct() if checkCreatedMission(): num_iescort -= 1 else: if num_escort <= 0: mungeFixerPct() generateEscortMission(j, k, f) restoreFixerPct() if checkCreatedMission(): num_escort -= 1 count += 1
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)