def main(tick, config, q): # How many systems are there? count = int(cp.query(payload="SELECT * FROM massive WHERE object == 'system' LIMIT 0, 0")["hits"]) # How many we want? want = int(utils.dist_skewedLeft(config['systems'])) if want == count: pass elif want >= count: # Generate up to X more systems per tick pre = '[t' + str(tick) for system in range(0, min(config['systemsVolatility'], want - count)): urly = 'y' + str(system) stars = [] for star in range(0, int(utils.dist_skewedLeft(config['stars']))): urls = urly + 's' + str(star) coordss = rndCoords(config['starDistance'], config['zFlatness']) planets = [] for planet in range(0, int(utils.dist_skewedLeft(config['planets']))): urlp = urls + 'p' + str(planet) coordsp = rndCoords(config['planetDistance'], config['zFlatness'], coordss) moons = [] for moon in range(0, int(utils.dist_skewedLeft(config['moons']))): urlm = urlp + 'm' + str(moon) utils.queue(cp.put, payload={ "object": "moon", "system_coords": rndCoords(config['moonDistance'], config['zFlatness'], coordsp), 'type': ['Gas', 'Ice', 'Rock', 'Iron', 'Mix'][utils.choose_weighted(config['moonType'])], 'size': round(utils.dist_skewedLeft(config['moonSize']), 0), 'habitability': round(utils.dist_skewedLeft(config['moonHabitability']), 1), 'richness': round(utils.dist_skewedLeft(config['moonRichness']), 1), 'materials': rndMaterials(config['moonWeightSolidsOther'], config['moonWeightMetalsIsotopes']), }, params=pre+urlm+"]", msg=' Moon ' + pre+urlm+"]" ) utils.queue(cp.put, payload={ "object": "planet", "system_coords": rndCoords(config['planetDistance'], config['zFlatness']), 'type': ['Gas', 'Ice', 'Rock', 'Iron', 'Mix'][utils.choose_weighted(config['planetType'])], 'size': round(utils.dist_skewedLeft(config['planetSize']), 0), 'habitability': round(utils.dist_skewedLeft(config['planetHabitability']), 1), 'richness': round(utils.dist_skewedLeft(config['planetRichness']), 1), 'materials': rndMaterials(config['planetWeightSolidsOther'], config['planetWeightMetalsIsotopes']), }, params=pre+urlp+"]", msg=' Planet ' + pre+urlp+"]" ) utils.queue(cp.put, payload={ "id": star, "object": "star", "system_coords": rndCoords(config['starDistance'], config['zFlatness']), }, params=pre+urls+"]", msg=' Star ' + pre+urls+"]" ) utils.queue(cp.put, payload={ "object": "system", "universe_coords": { "x": random.randrange(config['x'][0],config['x'][1]), "y": random.randrange(config['y'][0],config['y'][1]), "z": random.randrange(config['z'][0],config['z'][1]), },}, params=pre+urly+"]", msg='System ' + pre+urly+"]" ) elif want <= count: pass # TODO destroy systems return "done"
def main(tick, config, q): # Disband 'empty' factions with no colonies def disband(_id): r = cp.query(payload="SELECT *\ FROM massive\ WHERE object == 'colony' && faction == '" + _id + "'\ LIMIT 0, 0") if int(r['hits']) == 0: r = cp.query(payload="DELETE massive['" + _id + "']")['results'][0]['_id'] return 'factions: disband faction ' + _id else: return 'factions: keep faction ' + _id if tick%config['cleanFactions'] == 0: factions = cp.query(payload="\ SELECT _id\ FROM massive\ WHERE object == 'faction'\ LIMIT 0,9999") if 'results' in factions: for faction in factions['results']: utils.queue(disband, faction['_id']) # Abandon depopulation colonies def abandon(_id): r = cp.query(payload="\ UPDATE massive['" + _id + "']\ SET faction = null")['results'][0]['_id'] return 'factions: abandon colony ' + r colonies = cp.query(payload="\ SELECT _id, faction\ FROM massive\ WHERE object == 'colony' && population == 0 && faction") if 'results' in colonies: for colony in colonies['results']: utils.queue(abandon,colony['_id']) # Spawn new factions with initial colony count = int(cp.query(payload="SELECT * FROM massive WHERE object == 'faction' LIMIT 0, 0")['hits']) want = int(utils.dist_skewedLeft(config['factions'])) if want > count: planet = cp.query(payload="\ SELECT _id, size, habitability, richness, materials\ FROM massive\ WHERE\ object == 'planet' &&\ type != 'Gas' &&\ system_coords.radius < "+str(int(config['optimalDistance'][0]*config['optimalDistance'][1]))+" &&\ system_coords.radius > "+str(int(config['optimalDistance'][0]/config['optimalDistance'][1]))+"\ ORDER BY Math.abs("+str(config['optimalDistance'][0])+" - system_coords.radius) * Math.random()\ LIMIT 0, 1" )["results"][0] occupied = cp.query(payload="\ SELECT SUM(population+industry) as size\ FROM massive\ WHERE object == 'colony' && anchor == '"+planet['_id']+"'\ GROUP BY anchor") if int(occupied['hits']) == 0 or (planet['size'] >= occupied['results'][0]['size'] + 2): utils.queue(cp.put, payload={ 'object': 'faction', 'pref': { # 0 prefers first, 1 prefers last, 0.5 is indifferent. 'population_industry': utils.dist_flat(config['population_industry']), 'pacific_militaristic': utils.dist_flat(config['pacific_militaristic']), 'defence_attack': utils.dist_flat(config['defence_attack']), 'growth_expand': utils.dist_flat(config['growth_expand']), }}, params='[faction'+str(tick)+']', msg='factions: spawn faction '+str(tick) ) utils.queue(cp.put, payload={ 'object': 'colony', 'faction': 'faction'+str(tick), 'anchor': planet['_id'], 'goods': re.search("(\w*)p", planet['_id']).group(1), 'untilJoins': { 'habitability': planet['habitability'], 'richness': planet['richness'], 'materials': planet['materials'], }, 'population': 1, 'industry': 1, 'storage': { 'goods': { re.search("(\w*)p", planet['_id']).group(1): 100, }, 'solids': 100, # Upkeep for industry 'metals': 0, # For structure of skips 'isotopes': 0, # For guns of ships 'ammo': 0 # For guns to shoot }}, msg='factions: spawn colony' ) return 'done'
def rndMaterials(solidsOther, MetalsIsotopes): weights = [] # Material weights, [0]: Solids, [1]: Metals, [2]: Isotopes weights.append(round(utils.dist_skewedLeft(solidsOther), 2)) weights.append(round(utils.dist_skewedLeft(MetalsIsotopes) * (1-weights[0]), 2)) weights.append(round(1 - weights[0] - weights[1], 2)) return weights