This repository has been archived on 2023-12-31. You can view files and clone it, but cannot push or open issues or pull requests.
Considition-2020/main.py

289 lines
12 KiB
Python
Raw Normal View History

2020-09-27 16:53:45 +02:00
# import api
import time
import sys
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-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)
# settings
time_until_run_ends = 70
2020-10-04 03:15:32 +02:00
utilities = 3
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
# global vars
rounds_between_energy = 5
EMA_temp = None
building_under_construction = None
2020-10-05 21:21:25 +02:00
available_tiles = []
queue_timeout = 1
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()
# start timeout timer
start_time = time.time()
state = game_layer.game_state
chart_map()
while state.turn < state.max_turns:
state = game_layer.game_state
try:
2020-10-04 14:23:50 +02:00
if EMA_temp is None:
EMA_temp = state.current_temp
2020-10-04 14:23:50 +02:00
ema_k_value = (2/(rounds_between_energy+1))
EMA_temp = state.current_temp * ema_k_value + EMA_temp*(1-ema_k_value)
take_turn()
except Exception:
2020-10-03 19:34:20 +02:00
print(traceback.format_exc())
game_layer.end_game()
exit()
time_diff = time.time() - start_time
if time_diff > time_until_run_ends:
game_layer.end_game()
exit()
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"]))
return (state.game_id, game_layer.get_score()["finalScore"])
2020-09-27 13:46:19 +02:00
def take_turn():
global state
# 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
else:
2020-10-06 00:18:36 +02:00
game_layer.wait()
# 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
# pre-made test strategy which came with starter kit
'''
state = game_layer.game_state
if len(state.residences) < 1:
for i in range(len(state.map)):
for j in range(len(state.map)):
if state.map[i][j] == 0:
x = i
y = j
break
game_layer.place_foundation((x, y), game_layer.game_state.available_residence_buildings[0].building_name)
else:
the_only_residence = state.residences[0]
if the_only_residence.build_progress < 100:
game_layer.build((the_only_residence.X, the_only_residence.Y))
elif the_only_residence.health < 50:
game_layer.maintenance((the_only_residence.X, the_only_residence.Y))
elif the_only_residence.temperature < 18:
blueprint = game_layer.get_residence_blueprint(the_only_residence.building_name)
energy = blueprint.base_energy_need + 0.5 \
+ (the_only_residence.temperature - state.current_temp) * blueprint.emissivity / 1 \
- the_only_residence.current_pop * 0.04
game_layer.adjust_energy_level((the_only_residence.X, the_only_residence.Y), energy)
elif the_only_residence.temperature > 24:
blueprint = game_layer.get_residence_blueprint(the_only_residence.building_name)
energy = blueprint.base_energy_need - 0.5 \
+ (the_only_residence.temperature - state.current_temp) * blueprint.emissivity / 1 \
- the_only_residence.current_pop * 0.04
game_layer.adjust_energy_level((the_only_residence.X, the_only_residence.Y), energy)
elif state.available_upgrades[0].name not in the_only_residence.effects:
game_layer.buy_upgrade((the_only_residence.X, the_only_residence.Y), state.available_upgrades[0].name)
2020-09-27 13:46:19 +02:00
else:
game_layer.wait()
for message in game_layer.game_state.messages:
print(message)
for error in game_layer.game_state.errors:
print("Error: " + error)
'''
2020-09-27 13:46:19 +02:00
2020-10-05 16:43:52 +02:00
def develop_society():
2020-10-06 00:18:36 +02:00
global state, queue_timeout, available_tiles, utilities
if queue_timeout > 1:
queue_timeout -= 1
2020-10-05 20:54:41 +02:00
# priority scores, 1 = very urgent, 0 = not urgent at all
2020-10-06 00:18:36 +02:00
# queue modifier * funds modifier * existing houses modifier
2020-10-06 01:49:23 +02:00
build_residence_score = (state.housing_queue / (15 * queue_timeout)) * (1 - (7500 / state.funds)) * (1 - (len(state.residences) / (len(available_tiles)-utilities)))
2020-10-05 20:54:41 +02:00
upgrade_residence_score = 0
2020-10-06 01:49:23 +02:00
# existing houses modifier * funds modifier * existing utilities modifier
build_utility_score = (len(state.residences) / (len(available_tiles)-utilities)) * (1 - (16000 / state.funds)) * (1 - (len(state.utilities) / utilities))
# turn modifier * funds modifier
build_upgrade_score = (1 - (state.turn / 700)) * (2 - (15000 / state.funds))
2020-10-06 00:18:36 +02:00
if len(state.residences) < 1:
2020-10-06 01:49:23 +02:00
build_residence_score = 100
decision = [
('build_residence', build_residence_score),
('upgrade_residence', upgrade_residence_score),
('build_utility', build_utility_score),
('build_upgrade', build_upgrade_score)
]
def sort_key(e):
return e[1]
decision.sort(reverse=True, key=sort_key)
print(decision)
for i in range(4):
if decision[0][0] == "build_residence": # build housing
queue_timeout = 5
#return build("ModernApartments")
if len(state.residences) < len(state.available_residence_buildings):
return build(state.available_residence_buildings[len(state.residences)].building_name)
if decision[0][0] == "build_utility": # build utilities
return build("WindTurbine")
if decision[0][0] == "upgrade_residence": # build utilities
pass
if decision[0][0] == "build_upgrade": # build upgrades
for i in range(6):
for residence in state.residences:
if state.available_upgrades[i].name not in residence.effects:
game_layer.buy_upgrade((residence.X, residence.Y), state.available_upgrades[i].name)
return True
del decision[0]
return False
2020-10-06 00:18:36 +02:00
2020-10-05 16:43:52 +02:00
def something_needs_attention():
global building_under_construction, edit_temp, maintain, state, rounds_between_energy
# check if temp needs adjusting
edit_temp = (False, 0)
# check if need for maintenance
maintain = (False, 0)
for i in range(len(state.residences)):
if state.residences[i].health < 35+rounds_between_energy*game_layer.get_residence_blueprint(state.residences[i].building_name).decay_rate:
maintain = (True, i)
if (state.turn % rounds_between_energy == i) and not state.residences[i].build_progress < 100:
edit_temp = (True, i)
if maintain[0]: # check maintenance
game_layer.maintenance((state.residences[maintain[1]].X, state.residences[maintain[1]].Y))
return True
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
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
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-05 19:54:12 +02:00
if not state.residences[building_under_construction[2]].build_progress < 100:
building_under_construction = None
2020-10-04 05:00:23 +02:00
return True
else:
building_under_construction = None
return False
else:
return False
def chart_map():
global state
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()
def adjust_energy(current_building):
global rounds_between_energy, EMA_temp, state
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-06 01:49:09 +02:00
outDoorTemp = state.current_temp * 2 - EMA_temp
temp_acceleration = (2*(21 - current_building.temperature)/(rounds_between_energy))
2020-10-06 01:49:09 +02:00
effectiveEnergyIn = ((temp_acceleration - 0.04 * current_building.current_pop + (current_building.temperature - outDoorTemp) * emissivity) / 0.75) + base_energy
2020-10-06 01:49:09 +02:00
if effectiveEnergyIn > base_energy:
game_layer.adjust_energy_level((current_building.X, current_building.Y), effectiveEnergyIn)
2020-10-05 21:35:34 +02:00
return True
2020-10-06 01:49:09 +02:00
elif effectiveEnergyIn < base_energy:
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
else:
2020-10-05 21:35:34 +02:00
return False
2020-10-05 21:21:25 +02:00
def optimize_available_tiles():
global average_x, average_y, score_list
average_x = 0
average_y = 0
score_list = []
for tile in available_tiles: # calc average coordinates
average_x += tile[0]
average_y += tile[1]
2020-10-05 21:21:25 +02:00
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)):
2020-10-05 21:21:25 +02:00
available_tiles[i] = score_list[i][1]
print("average x,y: " + str(average_x) + ", " + str(average_y))
def build(structure):
global building_under_construction, rounds_between_energy, state
2020-10-06 00:18:36 +02:00
# print("Building " + structure)
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)
for building in state.available_residence_buildings:
if structure in building.building_name:
for j in range(len(state.residences)):
building = state.residences[j]
coords_to_check = (building.X, building.Y)
2020-10-05 21:21:25 +02:00
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 building in state.available_utility_buildings:
if structure in building.building_name:
for j in range(len(state.utilities)):
building = state.utilities[j]
coords_to_check = (building.X, building.Y)
2020-10-05 21:21:25 +02:00
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
return False
2020-09-27 13:46:19 +02:00
if __name__ == "__main__":
main()