Beispiel #1
0
def supply_and_demand(the_world):
	city_dict = the_world.live_cities()
	team_dict = the_world.active_teams()
	
	the_world.mass_get_team_techs()
	the_world.mass_get_team_deities()
	the_world.mass_get_team_evolutions()
	
	# Randomise city production type
	random.seed()
	r = -1
	for k, v in city_dict.items():
		r += 1
		if r >= len(sad_rules.res_list): r = 0
		v.supply_good = r
		# v.supply_good = random.randrange(0, len(sad_rules.res_list))
	
	# Build a cache of taxes and borders
	the_world.tax_cache = {}
	# the_world.border_cache = {}
	for t1 in team_dict.keys():
		for t2 in team_dict.keys():
			if t1 == t2:
				the_world.tax_cache[(t1, t2)] = 0
			else:
				the_world.tax_cache[(t1, t2)] = the_world.get_taxes(t1, t2)
				# the_world.border_cache[(t1, t2)] = the_world.get_border(t1, t2)
	
	# Build a distance cache, K1 going to K2
	the_world.distance_cache = {}
	for k1, c1 in city_dict.items():
		for k2, c2 in city_dict.items():
			if k1 == k2:
				the_world.distance_cache[(k1, k2)] = 0
			else:
				if k2 in c1.connections:
					the_world.distance_cache[(k1, k2)] = (1 + (sad_rules.distance_percentage * c1.connections[k2]/100))
	
	# Total, Min, Max
	production = {r:[0, 9999, -9999] for r in sad_rules.res_list}
	demand = {r:[0, 9999, -9999] for r in sad_rules.res_list}
	wealth = [0, 9999, -9999]
	
	city_count = len(city_dict.keys())
	city_size = sum([c.size for i, c in city_dict.items()])/city_count
	
	# First work out demand
	for city_id, the_city in city_dict.items():
		sad_rules.produce_wealth(the_world, the_city)
		wealth[0] += the_city.wealth
		wealth[1] = min(wealth[1], the_city.wealth)
		wealth[2] = max(wealth[2], the_city.wealth)
		the_city.satisfied = False
		
		for r in sad_rules.res_list:
			d = sad_rules.demand[r](the_world, the_city, the_world._teams[the_city.team])
			demand[r][0] += d
			demand[r][1] = min(d, demand[r][1])
			demand[r][2] = max(d, demand[r][2])
			the_city.goods[r] = -d
		
		if the_city.goods["Linen"] > the_city.goods["Wool"]:
			the_city.wool_is_nice = True
		
		city_supply = sad_rules.res_list[the_city.supply_good]
		
		p = sad_rules.supply[city_supply](the_world, the_city, the_world._teams[the_city.team])
		production[city_supply][0] += p
		production[city_supply][1] = min(p, production[city_supply][1])
		production[city_supply][2] = max(p, production[city_supply][2])
		the_city.goods[city_supply] += p
		# print(sad_rules.supply["Grain"](the_city, the_world._teams[the_city.team].tech_levels), sad_rules.demand["Grain"](the_city, the_world._teams[the_city.team].tech_levels))
	
	pre_report = generate_sad_pre_report(the_world)
	
	# Now begin to satisfy it
	satisfied = False
	batches = 0
	while not satisfied:
		sad_batch(the_world)
		
		satisfied = True
		for city_id, the_city in city_dict.items():
			if not the_city.satisfied and the_city.wealth > 0:
				satisfied = False
		
		# Force a breakout
		batches += 1
		config['hops'] = min(config['hops'] + 1, config['max hops'])
		if batches > config['batches']:
			satisfied = True
	
	post_report = generate_sad_post_report(the_world)
	
	# print(pre_report['res_surplus']['Grain'])
	
	return {
		"pre": pre_report,
		"post": post_report,
		"production": production,
		"demand": demand,
		"wealth": wealth,
	}
Beispiel #2
0
def supply_and_demand(the_world):
    city_dict = the_world.live_cities()
    team_dict = the_world.active_teams()

    the_world.mass_get_team_techs()
    the_world.mass_get_team_deities()
    the_world.mass_get_team_evolutions()

    # Randomise city production type
    random.seed()
    r = -1
    for k, v in city_dict.items():
        r += 1
        if r >= len(sad_rules.res_list):
            r = 0
        v.supply_good = r
        # v.supply_good = random.randrange(0, len(sad_rules.res_list))

        # Total, Min, Max
    production = {r: [0, 9999, -9999] for r in sad_rules.res_list}
    demand = {r: [0, 9999, -9999] for r in sad_rules.res_list}
    wealth = [0, 9999, -9999]

    city_count = len(city_dict.keys())
    city_size = sum([c.size for i, c in city_dict.items()]) / city_count

    # First work out demand
    for city_id, the_city in city_dict.items():
        sad_rules.produce_wealth(the_world, the_city)
        wealth[0] += the_city.wealth
        wealth[1] = min(wealth[1], the_city.wealth)
        wealth[2] = max(wealth[2], the_city.wealth)
        the_city.satisfied = False

        for r in sad_rules.res_list:
            d = sad_rules.demand[r](the_world, the_city, the_world._teams[the_city.team])
            demand[r][0] += d
            demand[r][1] = min(d, demand[r][1])
            demand[r][2] = max(d, demand[r][2])
            the_city.goods[r] = -d

        if the_city.goods["Linen"] > the_city.goods["Wool"]:
            the_city.wool_is_nice = True

        city_supply = sad_rules.res_list[the_city.supply_good]

        p = sad_rules.supply[city_supply](the_world, the_city, the_world._teams[the_city.team])
        production[city_supply][0] += p
        production[city_supply][1] = min(p, production[city_supply][1])
        production[city_supply][2] = max(p, production[city_supply][2])
        the_city.goods[city_supply] += p
        # print(sad_rules.supply["Grain"](the_city, the_world._teams[the_city.team].tech_levels), sad_rules.demand["Grain"](the_city, the_world._teams[the_city.team].tech_levels))

    pre_report = generate_sad_pre_report(the_world)

    # Now begin to satisfy it
    satisfied = False
    batches = 0
    while not satisfied:
        sad_batch(the_world)

        satisfied = True
        for city_id, the_city in city_dict.items():
            if not the_city.satisfied and the_city.wealth > 0:
                satisfied = False

                # Force a breakout
        batches += 1
        if batches > max_batches:
            satisfied = True

    post_report = generate_sad_post_report(the_world)

    # print(pre_report['res_surplus']['Grain'])

    return {"pre": pre_report, "post": post_report, "production": production, "demand": demand, "wealth": wealth}