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-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 13:46:19 +02:00
|
|
|
|
2020-09-27 14:45:23 +02:00
|
|
|
game_layer = GameLayer(api_key)
|
2020-10-03 19:30:14 +02:00
|
|
|
state = game_layer.game_state
|
2020-10-03 19:34:41 +02:00
|
|
|
usePrebuiltStrategy = False
|
2020-10-03 22:59:47 +02:00
|
|
|
timeUntilRunEnds = 70
|
2020-09-27 13:46:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2020-10-03 16:08:12 +02:00
|
|
|
#game_layer.force_end_game()
|
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-03 16:08:12 +02:00
|
|
|
# exit game after timeout
|
|
|
|
start_time = time.time()
|
2020-10-03 20:06:28 +02:00
|
|
|
chartMap()
|
|
|
|
optimizeAvailableTiles()
|
2020-09-27 13:46:19 +02:00
|
|
|
while game_layer.game_state.turn < game_layer.game_state.max_turns:
|
2020-10-03 17:50:00 +02:00
|
|
|
try:
|
2020-10-03 19:48:31 +02:00
|
|
|
take_turn()
|
2020-10-03 20:06:28 +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-03 19:34:20 +02:00
|
|
|
if time_diff > timeUntilRunEnds:
|
2020-10-03 16:08:12 +02:00
|
|
|
game_layer.end_game()
|
|
|
|
exit()
|
2020-09-27 13:46:19 +02:00
|
|
|
print("Done with game: " + game_layer.game_state.game_id)
|
|
|
|
print("Final score was: " + str(game_layer.get_score()["finalScore"]))
|
|
|
|
|
2020-10-03 17:54:32 +02:00
|
|
|
def linus_take_turn():
|
2020-10-03 19:34:20 +02:00
|
|
|
freeSpace = []
|
|
|
|
|
2020-10-03 17:54:32 +02:00
|
|
|
state = game_layer.game_state
|
|
|
|
for i in range(len(state.map)-1):
|
|
|
|
for j in range(len(state.map)-1):
|
|
|
|
if state.map[i][j] == 0:
|
2020-10-03 19:34:20 +02:00
|
|
|
freeSpace.append((i,j))
|
2020-09-27 13:46:19 +02:00
|
|
|
|
2020-10-03 17:54:32 +02:00
|
|
|
#print(mylist)
|
|
|
|
|
|
|
|
if (game_layer.game_state.turn == 0):
|
2020-10-03 19:34:20 +02:00
|
|
|
game_layer.place_foundation(freeSpace[2], game_layer.game_state.available_residence_buildings[0].building_name)
|
|
|
|
the_first_residence = state.residences[0]
|
|
|
|
if the_first_residence.build_progress < 100:
|
|
|
|
game_layer.build(freeSpace[2])
|
|
|
|
if len(state.residences)==1:
|
|
|
|
game_layer.place_foundation(freeSpace[3], game_layer.game_state.available_residence_buildings[4].building_name)
|
|
|
|
the_second_residence = state.residences[1]
|
|
|
|
if the_second_residence.build_progress < 100:
|
|
|
|
game_layer.build(freeSpace[3])
|
|
|
|
elif the_first_residence.health < 70:
|
|
|
|
game_layer.maintenance(freeSpace[2])
|
|
|
|
elif the_second_residence.health < 70:
|
|
|
|
game_layer.maintenance(freeSpace[3])
|
|
|
|
elif (the_second_residence.health > 70) and not len(state.utilities) > 0:
|
|
|
|
game_layer.place_foundation(freeSpace[4], game_layer.game_state.available_utility_buildings[2].building_name)
|
|
|
|
elif (state.utilities[0].build_progress < 100):
|
|
|
|
game_layer.build(freeSpace[4])
|
|
|
|
|
2020-10-03 17:54:32 +02:00
|
|
|
else:
|
|
|
|
# messages and errors for console log
|
|
|
|
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-10-03 16:08:12 +02:00
|
|
|
|
2020-09-27 13:46:19 +02:00
|
|
|
def take_turn():
|
2020-10-03 19:34:20 +02:00
|
|
|
if not usePrebuiltStrategy:
|
2020-10-03 19:31: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
|
2020-10-03 23:01:43 +02:00
|
|
|
if something_needs_attention():
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
develop_society()
|
2020-10-03 19:31:09 +02:00
|
|
|
|
2020-09-27 16:54:14 +02:00
|
|
|
# messages and errors for console log
|
|
|
|
for message in game_layer.game_state.messages:
|
|
|
|
print(message)
|
|
|
|
for error in game_layer.game_state.errors:
|
|
|
|
print("Error: " + error)
|
|
|
|
|
2020-10-03 16:08:12 +02:00
|
|
|
|
2020-09-27 16:54:14 +02:00
|
|
|
# pre-made test strategy
|
|
|
|
# which came with
|
|
|
|
# starter kit
|
2020-10-03 19:34:20 +02:00
|
|
|
if usePrebuiltStrategy:
|
2020-09-27 16:54:14 +02:00
|
|
|
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)
|
2020-09-27 13:46:19 +02:00
|
|
|
else:
|
2020-09-27 16:54:14 +02:00
|
|
|
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)
|
|
|
|
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-03 23:01:43 +02:00
|
|
|
|
2020-10-03 19:31:09 +02:00
|
|
|
def chartMap():
|
2020-10-03 23:01:43 +02:00
|
|
|
state = game_layer.game_state
|
2020-10-03 19:31:09 +02:00
|
|
|
availableTiles = []
|
|
|
|
for x in range(len(state.map) - 1):
|
|
|
|
for y in range(len(state.map) - 1):
|
|
|
|
if state.map[x][y] == 0:
|
|
|
|
availableTiles.append((x, y))
|
2020-10-03 23:01:43 +02:00
|
|
|
|
|
|
|
|
2020-10-03 20:06:28 +02:00
|
|
|
def optimizeAvailableTiles():
|
|
|
|
pass
|
2020-10-03 23:01:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
def adjustEnergy(current_building, target_temp):
|
|
|
|
current_blueprint = game_layer.get_residence_blueprint(current_building.building_name)
|
|
|
|
|
|
|
|
base_energy_need = current_blueprint.base_energy_need
|
|
|
|
indoor_temp = current_building.temperature
|
|
|
|
degrees_per_pop = 0.04
|
|
|
|
current_pop = current_building.current_pop
|
|
|
|
outdoor_temp = game_layer.game_state.current_temp
|
|
|
|
emissivity = current_blueprint.emissivity
|
|
|
|
degrees_per_excess_mwh = 0.75
|
|
|
|
|
|
|
|
effective_energy_in = -1 * (base_energy_need + (indoor_temp + degrees_per_pop * current_pop - (indoor_temp - outdoor_temp) * emissivity - target_temp)/degrees_per_excess_mwh)
|
|
|
|
game_layer.adjust_energy_level((current_building.X, current_building.Y), effective_energy_in)
|
|
|
|
|
|
|
|
def something_needs_attention():
|
|
|
|
pass
|
|
|
|
|
|
|
|
def develop_society():
|
|
|
|
pass
|
|
|
|
|
2020-09-27 13:46:19 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|