2020-09-27 16:53:45 +02:00
|
|
|
# import api
|
2020-10-03 16:08:12 +02:00
|
|
|
import time
|
2020-10-03 17:50:00 +02:00
|
|
|
import sys
|
2020-10-03 16:08:12 +02:00
|
|
|
from sys import exit
|
2020-09-27 13:46:19 +02:00
|
|
|
from game_layer import GameLayer
|
2020-10-04 03:15:32 +02:00
|
|
|
import game_state
|
2020-10-03 19:34:41 +02:00
|
|
|
import traceback
|
2020-10-07 04:04:39 +02:00
|
|
|
import random
|
2020-10-03 19:34:41 +02:00
|
|
|
|
2020-09-27 16:53:45 +02:00
|
|
|
api_key = "74e3998d-ed3d-4d46-9ea8-6aab2efd8ae3"
|
2020-09-27 13:46:19 +02:00
|
|
|
# The different map names can be found on considition.com/rules
|
2020-09-27 16:53:45 +02:00
|
|
|
map_name = "training1" # TODO: You map choice here. If left empty, the map "training1" will be selected.
|
2020-09-27 14:45:23 +02:00
|
|
|
game_layer = GameLayer(api_key)
|
2020-10-05 21:35:14 +02:00
|
|
|
# settings
|
2020-10-07 04:04:39 +02:00
|
|
|
use_regulator = False
|
|
|
|
other_upgrade_threshold = 0.25
|
2020-10-07 06:00:35 +02:00
|
|
|
time_until_run_ends = 90
|
2020-10-07 19:14:26 +02:00
|
|
|
money_reserve_multiplier = 0
|
2020-10-07 20:02:22 +02:00
|
|
|
temp_acc_multiplier = 1.125
|
2020-10-04 03:15:32 +02:00
|
|
|
|
2020-10-08 00:27:31 +02:00
|
|
|
# vars
|
2020-10-08 00:28:16 +02:00
|
|
|
EMA_temp = None
|
2020-10-08 01:59:16 +02:00
|
|
|
rounds_between_energy = 5
|
2020-10-08 00:28:16 +02:00
|
|
|
building_under_construction = None
|
2020-10-08 01:59:16 +02:00
|
|
|
available_tiles = []
|
2020-10-08 00:28:16 +02:00
|
|
|
state = None
|
2020-10-08 01:59:16 +02:00
|
|
|
queue_timeout = 1
|
2020-10-08 00:28:16 +02:00
|
|
|
edit_temp = None
|
|
|
|
maintain = None
|
2020-10-08 00:27:31 +02:00
|
|
|
|
2020-09-27 13:46:19 +02:00
|
|
|
|
|
|
|
def main():
|
2020-10-05 21:21:25 +02:00
|
|
|
global EMA_temp, rounds_between_energy, building_under_construction, available_tiles, state, queue_timeout
|
2020-10-05 21:35:14 +02:00
|
|
|
# global vars
|
2020-10-05 19:33:37 +02:00
|
|
|
rounds_between_energy = 5
|
|
|
|
EMA_temp = None
|
|
|
|
building_under_construction = None
|
2020-10-05 21:21:25 +02:00
|
|
|
available_tiles = []
|
2020-10-05 22:58:09 +02:00
|
|
|
queue_timeout = 1
|
2020-10-05 19:33:37 +02:00
|
|
|
|
2020-09-27 13:46:19 +02:00
|
|
|
game_layer.new_game(map_name)
|
|
|
|
print("Starting game: " + game_layer.game_state.game_id)
|
|
|
|
game_layer.start_game()
|
2020-10-05 19:33:37 +02:00
|
|
|
# start timeout timer
|
2020-10-03 16:08:12 +02:00
|
|
|
start_time = time.time()
|
2020-10-05 19:33:37 +02:00
|
|
|
state = game_layer.game_state
|
2020-10-05 21:35:14 +02:00
|
|
|
chart_map()
|
2020-10-05 19:33:37 +02:00
|
|
|
while state.turn < state.max_turns:
|
|
|
|
state = game_layer.game_state
|
2020-10-03 17:50:00 +02:00
|
|
|
try:
|
2020-10-04 14:23:50 +02:00
|
|
|
if EMA_temp is None:
|
2020-10-05 19:33:37 +02:00
|
|
|
EMA_temp = state.current_temp
|
2020-10-04 14:23:50 +02:00
|
|
|
ema_k_value = (2/(rounds_between_energy+1))
|
2020-10-05 19:33:37 +02:00
|
|
|
EMA_temp = state.current_temp * ema_k_value + EMA_temp*(1-ema_k_value)
|
2020-10-04 15:38:12 +02:00
|
|
|
take_turn()
|
2020-10-05 21:35:14 +02:00
|
|
|
except Exception:
|
2020-10-03 19:34:20 +02:00
|
|
|
print(traceback.format_exc())
|
2020-10-03 17:50:00 +02:00
|
|
|
game_layer.end_game()
|
|
|
|
exit()
|
2020-10-03 16:08:12 +02:00
|
|
|
time_diff = time.time() - start_time
|
2020-10-05 19:33:37 +02:00
|
|
|
if time_diff > time_until_run_ends:
|
2020-10-03 16:08:12 +02:00
|
|
|
game_layer.end_game()
|
|
|
|
exit()
|
2020-10-05 19:33:37 +02:00
|
|
|
print("Done with game: " + state.game_id)
|
2020-09-27 13:46:19 +02:00
|
|
|
print("Final score was: " + str(game_layer.get_score()["finalScore"]))
|
2020-10-05 19:33:37 +02:00
|
|
|
return (state.game_id, game_layer.get_score()["finalScore"])
|
2020-10-03 16:08:12 +02:00
|
|
|
|
2020-10-05 21:35:14 +02:00
|
|
|
|
2020-09-27 13:46:19 +02:00
|
|
|
def take_turn():
|
2020-10-05 19:33:37 +02:00
|
|
|
global state
|
2020-10-05 22:58:09 +02:00
|
|
|
# TODO Implement your artificial intelligence here.
|
|
|
|
# TODO Take one action per turn until the game ends.
|
|
|
|
# TODO The following is a short example of how to use the StarterKit
|
|
|
|
if something_needs_attention():
|
|
|
|
pass
|
2020-10-06 00:18:36 +02:00
|
|
|
elif develop_society():
|
|
|
|
pass
|
2020-10-05 22:58:09 +02:00
|
|
|
else:
|
2020-10-06 00:18:36 +02:00
|
|
|
game_layer.wait()
|
|
|
|
|
2020-10-05 22:58:09 +02:00
|
|
|
# messages and errors for console log
|
|
|
|
for message in state.messages:
|
|
|
|
print(message)
|
|
|
|
for error in state.errors:
|
|
|
|
print("Error: " + error)
|
2020-09-27 16:54:14 +02:00
|
|
|
|
2020-10-05 21:35:14 +02:00
|
|
|
|
2020-10-05 16:43:52 +02:00
|
|
|
def develop_society():
|
2020-10-07 06:00:35 +02:00
|
|
|
global state, queue_timeout, available_tiles, money_reserve_multiplier
|
2020-10-07 19:14:26 +02:00
|
|
|
queue_reset = 1
|
2020-10-05 22:58:09 +02:00
|
|
|
if queue_timeout > 1:
|
|
|
|
queue_timeout -= 1
|
2020-10-05 19:33:37 +02:00
|
|
|
|
2020-10-07 04:04:39 +02:00
|
|
|
best_residence = calculate_best_residence()
|
2020-10-07 06:00:35 +02:00
|
|
|
best_utility = calculate_best_utility()
|
2020-10-07 04:04:39 +02:00
|
|
|
best_upgrade = get_best_upgrade()
|
|
|
|
build_residence_score = 0
|
2020-10-07 06:00:35 +02:00
|
|
|
build_utility_score = 0
|
2020-10-07 04:04:39 +02:00
|
|
|
build_upgrade_score = 0
|
2020-10-05 22:58:09 +02:00
|
|
|
# priority scores, 1 = very urgent, 0 = not urgent at all
|
2020-10-07 04:04:39 +02:00
|
|
|
if len(state.residences) < 1:
|
2020-10-07 06:00:35 +02:00
|
|
|
build_residence_score = 1000
|
|
|
|
elif (current_tot_pop() - max_tot_pop() + state.housing_queue) < 0:
|
2020-10-07 04:04:39 +02:00
|
|
|
build_residence_score = 0
|
2020-10-07 06:00:35 +02:00
|
|
|
elif (current_tot_pop() - max_tot_pop() + state.housing_queue) > 15 and queue_timeout <= 0:
|
|
|
|
build_residence_score = 1000
|
2020-10-07 04:04:39 +02:00
|
|
|
elif best_residence:
|
2020-10-08 00:27:31 +02:00
|
|
|
build_residence_score = best_residence[0]
|
2020-10-07 04:04:39 +02:00
|
|
|
#
|
2020-10-05 20:54:41 +02:00
|
|
|
upgrade_residence_score = 0
|
2020-10-07 06:00:35 +02:00
|
|
|
#
|
|
|
|
if best_utility:
|
|
|
|
build_utility_score = best_utility[0]
|
|
|
|
#
|
2020-10-07 04:04:39 +02:00
|
|
|
if best_upgrade:
|
|
|
|
build_upgrade_score = best_upgrade[0]
|
2020-10-05 19:33:37 +02:00
|
|
|
|
2020-10-06 01:49:23 +02:00
|
|
|
decision = [
|
|
|
|
('build_residence', build_residence_score),
|
|
|
|
('upgrade_residence', upgrade_residence_score),
|
|
|
|
('build_utility', build_utility_score),
|
|
|
|
('build_upgrade', build_upgrade_score)
|
|
|
|
]
|
2020-10-08 00:27:31 +02:00
|
|
|
|
2020-10-06 01:49:23 +02:00
|
|
|
def sort_key(e):
|
|
|
|
return e[1]
|
|
|
|
decision.sort(reverse=True, key=sort_key)
|
2020-10-07 06:00:35 +02:00
|
|
|
# print(decision)
|
|
|
|
|
2020-10-07 04:04:39 +02:00
|
|
|
if decision[0][1] >= 0:
|
2020-10-06 01:49:23 +02:00
|
|
|
if decision[0][0] == "build_residence": # build housing
|
2020-10-07 04:04:39 +02:00
|
|
|
if best_residence:
|
2020-10-07 06:00:35 +02:00
|
|
|
queue_timeout = queue_reset
|
2020-10-07 04:04:39 +02:00
|
|
|
return build(best_residence[1])
|
2020-10-06 01:49:23 +02:00
|
|
|
if decision[0][0] == "build_utility": # build utilities
|
2020-10-07 06:00:35 +02:00
|
|
|
if best_utility:
|
|
|
|
return build_place(best_utility[1], best_utility[2])
|
2020-10-07 04:04:39 +02:00
|
|
|
if decision[0][0] == "upgrade_residence": # upgrade housing
|
2020-10-06 01:49:23 +02:00
|
|
|
pass
|
|
|
|
if decision[0][0] == "build_upgrade": # build upgrades
|
2020-10-07 04:04:39 +02:00
|
|
|
if random.random() < other_upgrade_threshold:
|
|
|
|
for residence in state.residences:
|
|
|
|
if state.available_upgrades[0].name not in residence.effects and (money_reserve_multiplier*3500 < state.funds) and ((total_income() - 6) > 50):
|
|
|
|
game_layer.buy_upgrade((residence.X, residence.Y), state.available_upgrades[0].name)
|
|
|
|
return True
|
|
|
|
if use_regulator and state.available_upgrades[5].name not in residence.effects and (money_reserve_multiplier*1250 < state.funds):
|
|
|
|
game_layer.buy_upgrade((residence.X, residence.Y), state.available_upgrades[5].name)
|
|
|
|
return True
|
|
|
|
if best_upgrade:
|
|
|
|
game_layer.buy_upgrade((best_upgrade[2].X, best_upgrade[2].Y), best_upgrade[1])
|
2020-10-06 05:06:04 +02:00
|
|
|
return True
|
2020-10-06 01:49:23 +02:00
|
|
|
return False
|
2020-10-06 00:18:36 +02:00
|
|
|
|
2020-10-05 16:43:52 +02:00
|
|
|
|
2020-10-03 23:01:43 +02:00
|
|
|
def something_needs_attention():
|
2020-10-05 22:58:09 +02:00
|
|
|
global building_under_construction, edit_temp, maintain, state, rounds_between_energy
|
2020-10-04 15:38:12 +02:00
|
|
|
|
2020-10-05 21:35:14 +02:00
|
|
|
# check if temp needs adjusting
|
2020-10-04 15:38:12 +02:00
|
|
|
edit_temp = (False, 0)
|
2020-10-05 21:35:14 +02:00
|
|
|
# check if need for maintenance
|
2020-10-04 15:38:12 +02:00
|
|
|
maintain = (False, 0)
|
|
|
|
for i in range(len(state.residences)):
|
2020-10-07 17:58:03 +02:00
|
|
|
blueprint = game_layer.get_residence_blueprint(state.residences[i].building_name)
|
2020-10-08 00:27:31 +02:00
|
|
|
if state.residences[i].health < 40+(max(((blueprint.maintenance_cost - state.funds) / (1+total_income())), 1) * blueprint.decay_rate):
|
2020-10-04 15:38:12 +02:00
|
|
|
maintain = (True, i)
|
2020-10-05 22:58:09 +02:00
|
|
|
if (state.turn % rounds_between_energy == i) and not state.residences[i].build_progress < 100:
|
|
|
|
edit_temp = (True, i)
|
2020-10-04 15:38:12 +02:00
|
|
|
|
2020-10-05 21:35:14 +02:00
|
|
|
if maintain[0]: # check maintenance
|
2020-10-04 15:38:12 +02:00
|
|
|
game_layer.maintenance((state.residences[maintain[1]].X, state.residences[maintain[1]].Y))
|
|
|
|
return True
|
2020-10-05 21:35:14 +02:00
|
|
|
elif edit_temp[0]: # adjust temp of buildings
|
2020-10-05 21:35:34 +02:00
|
|
|
return adjust_energy(state.residences[edit_temp[1]])
|
|
|
|
elif building_under_construction is not None: # finish construction
|
2020-10-05 19:33:37 +02:00
|
|
|
if (len(state.residences)-1 >= building_under_construction[2]) and (state.residences[building_under_construction[2]].build_progress < 100):
|
2020-10-04 17:52:27 +02:00
|
|
|
game_layer.build((building_under_construction[0], building_under_construction[1]))
|
2020-10-05 19:54:12 +02:00
|
|
|
if not state.residences[building_under_construction[2]].build_progress < 100:
|
|
|
|
building_under_construction = None
|
2020-10-04 17:52:27 +02:00
|
|
|
return True
|
2020-10-05 19:33:37 +02:00
|
|
|
elif (len(state.utilities)-1 >= building_under_construction[2]) and (state.utilities[building_under_construction[2]].build_progress < 100):
|
2020-10-04 05:00:23 +02:00
|
|
|
game_layer.build((building_under_construction[0], building_under_construction[1]))
|
2020-10-07 06:00:35 +02:00
|
|
|
if not state.utilities[building_under_construction[2]].build_progress < 100:
|
2020-10-05 19:54:12 +02:00
|
|
|
building_under_construction = None
|
2020-10-04 05:00:23 +02:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
building_under_construction = None
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return False
|
2020-10-03 23:01:43 +02:00
|
|
|
|
2020-10-06 02:02:28 +02:00
|
|
|
|
2020-10-07 06:00:35 +02:00
|
|
|
def max_tot_pop():
|
|
|
|
global state
|
|
|
|
max_pop = 0
|
|
|
|
for residence in state.residences:
|
|
|
|
max_pop += game_layer.get_blueprint(residence.building_name).max_pop
|
|
|
|
return max_pop
|
|
|
|
|
|
|
|
|
|
|
|
def current_tot_pop():
|
|
|
|
global state
|
|
|
|
current_pop = 0
|
|
|
|
for residence in state.residences:
|
|
|
|
current_pop += residence.current_pop
|
|
|
|
return current_pop
|
|
|
|
|
|
|
|
|
2020-10-06 05:06:04 +02:00
|
|
|
def total_income():
|
|
|
|
global state
|
|
|
|
income = 0
|
|
|
|
for residence in state.residences:
|
|
|
|
income += game_layer.get_residence_blueprint(residence.building_name).income_per_pop * residence.current_pop
|
|
|
|
return income
|
|
|
|
|
|
|
|
|
|
|
|
def get_best_upgrade():
|
|
|
|
global state
|
|
|
|
|
|
|
|
best_upgrade = []
|
|
|
|
for residence in state.residences:
|
|
|
|
cbu = calculate_best_upgrade(residence)
|
|
|
|
if cbu is not False:
|
|
|
|
score = cbu[0]
|
|
|
|
upgrade = cbu[1]
|
|
|
|
best_upgrade.append((score, upgrade, residence))
|
|
|
|
|
|
|
|
def sort_key(e):
|
|
|
|
return e[0]
|
|
|
|
best_upgrade.sort(reverse=True, key=sort_key)
|
|
|
|
if not best_upgrade:
|
|
|
|
return False
|
|
|
|
return best_upgrade[0]
|
|
|
|
|
|
|
|
|
|
|
|
def calculate_best_upgrade(current_building):
|
2020-10-06 22:53:27 +02:00
|
|
|
global state, money_reserve_multiplier
|
2020-10-06 05:06:04 +02:00
|
|
|
|
|
|
|
rounds_left = 700 - state.turn
|
|
|
|
current_pop = current_building.current_pop
|
|
|
|
blueprint = game_layer.get_blueprint(current_building.building_name)
|
|
|
|
base_energy_need = blueprint.base_energy_need
|
|
|
|
best_upgrade = []
|
|
|
|
for upgrade in state.available_upgrades:
|
|
|
|
effect = game_layer.get_effect(upgrade.effect)
|
|
|
|
if (upgrade.name not in current_building.effects) and ((total_income() + effect.building_income_increase) > 50) and (money_reserve_multiplier*upgrade.cost < state.funds):
|
|
|
|
average_outdoor_temp = (state.max_temp - state.min_temp)/2
|
|
|
|
|
|
|
|
average_heating_energy = (((21 - average_outdoor_temp) * blueprint.emissivity * effect.emissivity_multiplier) / 0.75)
|
|
|
|
old_average_heating_energy = (((21 - average_outdoor_temp) * blueprint.emissivity) / 0.75)
|
|
|
|
|
|
|
|
lifetime_energy = (base_energy_need + effect.base_energy_mwh_increase + average_heating_energy - effect.mwh_production) * rounds_left
|
|
|
|
old_lifetime_energy = (base_energy_need + old_average_heating_energy) * rounds_left
|
|
|
|
|
|
|
|
upgrade_co2 = (effect.co2_per_pop_increase * 0.03) * current_pop * rounds_left + (0.1 * lifetime_energy / 1000)
|
|
|
|
old_co2 = 0.03 * current_pop * rounds_left + (0.1 * old_lifetime_energy / 1000)
|
|
|
|
co2 = upgrade_co2 - old_co2
|
2020-10-07 04:04:39 +02:00
|
|
|
max_happiness = effect.max_happiness_increase * current_pop * rounds_left
|
2020-10-06 05:06:04 +02:00
|
|
|
|
|
|
|
score = max_happiness/10 - co2
|
2020-10-07 06:00:35 +02:00
|
|
|
# score = score / upgrade.cost
|
2020-10-06 05:06:04 +02:00
|
|
|
best_upgrade.append((score, upgrade.name))
|
|
|
|
|
|
|
|
def sort_key(e):
|
|
|
|
return e[0]
|
|
|
|
best_upgrade.sort(reverse=True, key=sort_key)
|
|
|
|
if not best_upgrade:
|
|
|
|
return False
|
|
|
|
return best_upgrade[0]
|
|
|
|
|
|
|
|
|
2020-10-07 06:00:35 +02:00
|
|
|
def calculate_best_utility():
|
|
|
|
global state, money_reserve_multiplier
|
|
|
|
|
|
|
|
best_utility = []
|
|
|
|
for utility_blueprint in state.available_utility_buildings:
|
|
|
|
if state.turn >= utility_blueprint.release_tick and (money_reserve_multiplier*utility_blueprint.cost < state.funds):
|
2020-10-07 19:16:42 +02:00
|
|
|
rounds_left = 700 - state.turn - (100 / utility_blueprint.build_speed)
|
|
|
|
|
2020-10-07 06:00:35 +02:00
|
|
|
for i in range(len(available_tiles)):
|
|
|
|
if isinstance(available_tiles[i], tuple):
|
|
|
|
score = 0
|
|
|
|
cost = utility_blueprint.cost
|
|
|
|
for effect_name in utility_blueprint.effects:
|
|
|
|
effect = game_layer.get_effect(effect_name)
|
2020-10-07 19:16:42 +02:00
|
|
|
affected_people = tile_score(available_tiles[i], effect.radius, effect_name)[0]
|
|
|
|
affected_buildings = tile_score(available_tiles[i], effect.radius, effect_name)[1]
|
2020-10-07 06:00:35 +02:00
|
|
|
cost -= effect.building_income_increase * rounds_left
|
|
|
|
happiness_increase = affected_people * effect.max_happiness_increase * rounds_left
|
2020-10-07 19:16:42 +02:00
|
|
|
co2 = affected_people * effect.co2_per_pop_increase * rounds_left - effect.mwh_production * affected_buildings * rounds_left
|
|
|
|
score += happiness_increase / 10 - co2
|
|
|
|
# print(effect_name + " gave score " + str(score))
|
2020-10-07 06:00:35 +02:00
|
|
|
# score = score / cost
|
|
|
|
best_utility.append((score, utility_blueprint.building_name, i))
|
|
|
|
|
|
|
|
def sort_key(e):
|
|
|
|
return e[0]
|
|
|
|
best_utility.sort(reverse=True, key=sort_key)
|
2020-10-07 19:16:42 +02:00
|
|
|
# print(best_utility)
|
2020-10-07 06:00:35 +02:00
|
|
|
if not best_utility:
|
|
|
|
return False
|
|
|
|
return best_utility[0]
|
|
|
|
|
|
|
|
|
2020-10-06 05:06:04 +02:00
|
|
|
def calculate_best_residence():
|
2020-10-06 22:53:27 +02:00
|
|
|
global state, money_reserve_multiplier
|
2020-10-06 05:06:04 +02:00
|
|
|
|
|
|
|
best_residence = []
|
|
|
|
for residence_blueprint in state.available_residence_buildings:
|
|
|
|
if state.turn >= residence_blueprint.release_tick and (money_reserve_multiplier*residence_blueprint.cost < state.funds):
|
2020-10-07 19:16:42 +02:00
|
|
|
rounds_left = 700 - state.turn - (100 / residence_blueprint.build_speed)
|
|
|
|
|
2020-10-06 05:06:04 +02:00
|
|
|
average_outdoor_temp = (state.max_temp - state.min_temp)/2
|
|
|
|
average_heating_energy = ((0 - 0.04 * residence_blueprint.max_pop + (21 - average_outdoor_temp) * residence_blueprint.emissivity) / 0.75)
|
|
|
|
lifetime_energy = (residence_blueprint.base_energy_need + average_heating_energy) * rounds_left
|
|
|
|
|
2020-10-07 19:16:42 +02:00
|
|
|
distinct_residences = number_of_distinct_residences(residence_blueprint.building_name)
|
|
|
|
diversity = 1 + distinct_residences[0]/10
|
|
|
|
|
2020-10-06 05:06:04 +02:00
|
|
|
co2 = 0.03 * residence_blueprint.max_pop * rounds_left + residence_blueprint.co2_cost + (0.1 * lifetime_energy / 1000)
|
2020-10-07 04:04:39 +02:00
|
|
|
max_happiness = residence_blueprint.max_happiness * residence_blueprint.max_pop * rounds_left
|
2020-10-07 19:16:42 +02:00
|
|
|
max_happiness *= diversity
|
2020-10-06 05:06:04 +02:00
|
|
|
|
2020-10-07 19:16:42 +02:00
|
|
|
diversity_bonus = 0
|
|
|
|
if distinct_residences[1]:
|
|
|
|
happy = 0
|
|
|
|
for residence in state.residences:
|
|
|
|
happy += residence.happiness_per_tick_per_pop * residence.current_pop
|
|
|
|
diversity_bonus = (happy * rounds_left / 10) / 10
|
|
|
|
|
|
|
|
score = residence_blueprint.max_pop*15 + max_happiness / 10 - co2 + diversity_bonus
|
2020-10-07 06:00:35 +02:00
|
|
|
# score = score / residence_blueprint.cost
|
2020-10-06 05:06:04 +02:00
|
|
|
best_residence.append((score, residence_blueprint.building_name))
|
|
|
|
|
|
|
|
def sort_key(e):
|
|
|
|
return e[0]
|
|
|
|
best_residence.sort(reverse=True, key=sort_key)
|
|
|
|
if not best_residence:
|
|
|
|
return False
|
|
|
|
return best_residence[0]
|
|
|
|
|
|
|
|
|
2020-10-07 19:16:42 +02:00
|
|
|
def number_of_distinct_residences(new_building):
|
|
|
|
global state
|
|
|
|
unique_names = []
|
|
|
|
for residence in state.residences:
|
2020-10-08 00:27:31 +02:00
|
|
|
if residence.building_name not in unique_names:
|
2020-10-07 19:16:42 +02:00
|
|
|
unique_names.append(residence.building_name)
|
2020-10-08 00:27:31 +02:00
|
|
|
if new_building not in unique_names:
|
2020-10-07 19:16:42 +02:00
|
|
|
unique_names.append(new_building)
|
|
|
|
return len(unique_names), True
|
|
|
|
return len(unique_names), False
|
|
|
|
|
|
|
|
|
2020-10-05 21:35:14 +02:00
|
|
|
def chart_map():
|
2020-10-05 19:33:37 +02:00
|
|
|
global state
|
2020-10-05 16:41:09 +02:00
|
|
|
for x in range(len(state.map) - 1):
|
|
|
|
for y in range(len(state.map) - 1):
|
|
|
|
if state.map[x][y] == 0:
|
2020-10-05 21:21:25 +02:00
|
|
|
available_tiles.append((x, y))
|
|
|
|
optimize_available_tiles()
|
2020-10-05 16:41:09 +02:00
|
|
|
|
2020-10-05 21:35:14 +02:00
|
|
|
|
2020-10-07 19:15:31 +02:00
|
|
|
def tile_score(tile, radius, effect):
|
2020-10-07 06:00:35 +02:00
|
|
|
global state
|
|
|
|
affected_people = 0
|
|
|
|
affected_buildings = 0
|
|
|
|
# send back # of max people in radius
|
|
|
|
for residence in state.residences:
|
|
|
|
delta_x = abs(tile[0] - residence.X)
|
|
|
|
delta_y = abs(tile[1] - residence.Y)
|
|
|
|
distance = delta_x + delta_y
|
2020-10-08 00:27:31 +02:00
|
|
|
if (distance <= radius) and effect not in residence.effects:
|
2020-10-07 06:00:35 +02:00
|
|
|
affected_people += residence.current_pop
|
|
|
|
affected_buildings += 1
|
|
|
|
return affected_people, affected_buildings
|
|
|
|
|
|
|
|
|
2020-10-06 02:02:28 +02:00
|
|
|
def optimize_available_tiles():
|
|
|
|
average_x = 0
|
|
|
|
average_y = 0
|
|
|
|
score_list = []
|
|
|
|
for tile in available_tiles: # calc average coordinates
|
|
|
|
average_x += tile[0]
|
|
|
|
average_y += tile[1]
|
|
|
|
average_x /= len(available_tiles)
|
|
|
|
average_y /= len(available_tiles)
|
|
|
|
for tile in available_tiles:
|
|
|
|
tile_score = abs(tile[0] - average_x) + abs(tile[1] - average_y)
|
|
|
|
score_list.append((tile_score, tile))
|
|
|
|
|
|
|
|
def sort_key(e):
|
|
|
|
return e[0]
|
|
|
|
score_list.sort(key=sort_key)
|
|
|
|
for i in range(len(score_list)):
|
|
|
|
available_tiles[i] = score_list[i][1]
|
|
|
|
|
|
|
|
|
2020-10-05 21:35:14 +02:00
|
|
|
def adjust_energy(current_building):
|
2020-10-07 20:02:22 +02:00
|
|
|
global rounds_between_energy, EMA_temp, state, temp_acc_multiplier
|
2020-10-05 21:35:14 +02:00
|
|
|
blueprint = game_layer.get_residence_blueprint(current_building.building_name)
|
2020-10-06 01:49:09 +02:00
|
|
|
base_energy = blueprint.base_energy_need
|
|
|
|
if "Charger" in current_building.effects:
|
|
|
|
base_energy += 1.8
|
|
|
|
|
|
|
|
emissivity = blueprint.emissivity
|
|
|
|
if "Insulation" in current_building.effects:
|
|
|
|
emissivity *= 0.6
|
2020-10-05 16:41:09 +02:00
|
|
|
|
2020-10-08 00:27:31 +02:00
|
|
|
out_door_temp = state.current_temp * 2 - EMA_temp
|
|
|
|
temp_acceleration = (2*(21 - current_building.temperature)/rounds_between_energy) * temp_acc_multiplier
|
2020-10-05 16:41:09 +02:00
|
|
|
|
2020-10-08 00:27:31 +02:00
|
|
|
effective_energy_in = ((temp_acceleration - 0.04 * current_building.current_pop + (current_building.temperature - out_door_temp) * emissivity) / 0.75) + base_energy
|
2020-10-05 16:41:09 +02:00
|
|
|
|
2020-10-08 00:27:31 +02:00
|
|
|
if effective_energy_in > base_energy:
|
|
|
|
game_layer.adjust_energy_level((current_building.X, current_building.Y), effective_energy_in)
|
2020-10-05 21:35:34 +02:00
|
|
|
return True
|
2020-10-08 00:27:31 +02:00
|
|
|
elif effective_energy_in < base_energy:
|
2020-10-06 01:49:09 +02:00
|
|
|
game_layer.adjust_energy_level((current_building.X, current_building.Y), base_energy + 0.01)
|
2020-10-05 21:35:34 +02:00
|
|
|
return True
|
2020-10-05 16:41:09 +02:00
|
|
|
else:
|
2020-10-05 21:35:34 +02:00
|
|
|
return False
|
2020-10-05 16:41:09 +02:00
|
|
|
|
|
|
|
|
2020-10-07 06:00:35 +02:00
|
|
|
def build_place(structure, i):
|
|
|
|
global building_under_construction, rounds_between_energy, state
|
|
|
|
if isinstance(available_tiles[i], tuple):
|
|
|
|
game_layer.place_foundation(available_tiles[i], structure)
|
2020-10-07 20:02:47 +02:00
|
|
|
for j in range(len(state.residences)):
|
|
|
|
building = state.residences[j]
|
|
|
|
coords_to_check = (building.X, building.Y)
|
|
|
|
if coords_to_check == available_tiles[i]:
|
|
|
|
available_tiles[i] = building
|
|
|
|
building_under_construction = (building.X, building.Y, j)
|
|
|
|
return True
|
2020-10-07 19:15:09 +02:00
|
|
|
for j in range(len(state.utilities)):
|
|
|
|
building = state.utilities[j]
|
|
|
|
coords_to_check = (building.X, building.Y)
|
|
|
|
if coords_to_check == available_tiles[i]:
|
|
|
|
available_tiles[i] = building
|
|
|
|
building_under_construction = (building.X, building.Y, j)
|
|
|
|
return True
|
|
|
|
return False
|
2020-10-07 06:00:35 +02:00
|
|
|
|
|
|
|
|
2020-10-04 03:17:11 +02:00
|
|
|
def build(structure):
|
2020-10-05 19:33:37 +02:00
|
|
|
global building_under_construction, rounds_between_energy, state
|
2020-10-05 21:21:25 +02:00
|
|
|
for i in range(len(available_tiles)):
|
|
|
|
if isinstance(available_tiles[i], tuple):
|
|
|
|
game_layer.place_foundation(available_tiles[i], structure)
|
2020-10-07 20:02:47 +02:00
|
|
|
for j in range(len(state.residences)):
|
|
|
|
building = state.residences[j]
|
|
|
|
coords_to_check = (building.X, building.Y)
|
|
|
|
if coords_to_check == available_tiles[i]:
|
|
|
|
available_tiles[i] = building
|
|
|
|
building_under_construction = (building.X, building.Y, j)
|
|
|
|
rounds_between_energy = len(state.residences)+2
|
|
|
|
return True
|
|
|
|
for j in range(len(state.utilities)):
|
|
|
|
building = state.utilities[j]
|
|
|
|
coords_to_check = (building.X, building.Y)
|
|
|
|
if coords_to_check == available_tiles[i]:
|
|
|
|
available_tiles[i] = building
|
|
|
|
building_under_construction = (building.X, building.Y, j)
|
|
|
|
return True
|
|
|
|
return False
|
2020-10-07 19:15:09 +02:00
|
|
|
return False
|
2020-10-04 03:17:11 +02:00
|
|
|
|
2020-10-03 23:01:43 +02:00
|
|
|
|
2020-09-27 13:46:19 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|