refactor and clear up code (whitespaces etc)

This commit is contained in:
Thefeli73 2020-10-05 21:35:14 +02:00
parent 3b55e5a597
commit a830dd5d81

31
main.py
View File

@ -31,7 +31,7 @@ def main():
# start timeout timer # start timeout timer
start_time = time.time() start_time = time.time()
state = game_layer.game_state state = game_layer.game_state
chartMap() chart_map()
while state.turn < state.max_turns: while state.turn < state.max_turns:
state = game_layer.game_state state = game_layer.game_state
try: try:
@ -40,7 +40,7 @@ def main():
ema_k_value = (2/(rounds_between_energy+1)) ema_k_value = (2/(rounds_between_energy+1))
EMA_temp = state.current_temp * ema_k_value + EMA_temp*(1-ema_k_value) EMA_temp = state.current_temp * ema_k_value + EMA_temp*(1-ema_k_value)
take_turn() take_turn()
except: except Exception:
print(traceback.format_exc()) print(traceback.format_exc())
game_layer.end_game() game_layer.end_game()
exit() exit()
@ -52,6 +52,7 @@ def main():
print("Final score was: " + str(game_layer.get_score()["finalScore"])) print("Final score was: " + str(game_layer.get_score()["finalScore"]))
return (state.game_id, game_layer.get_score()["finalScore"]) return (state.game_id, game_layer.get_score()["finalScore"])
def take_turn(): def take_turn():
global state global state
if not use_prebuilt_strategy: if not use_prebuilt_strategy:
@ -68,7 +69,6 @@ def take_turn():
for error in state.errors: for error in state.errors:
print("Error: " + error) print("Error: " + error)
# pre-made test strategy which came with starter kit # pre-made test strategy which came with starter kit
if use_prebuilt_strategy: if use_prebuilt_strategy:
state = game_layer.game_state state = game_layer.game_state
@ -107,6 +107,7 @@ def take_turn():
for error in game_layer.game_state.errors: for error in game_layer.game_state.errors:
print("Error: " + error) print("Error: " + error)
def develop_society(): def develop_society():
global state, queue_timeout, available_tiles global state, queue_timeout, available_tiles
@ -140,6 +141,7 @@ def develop_society():
else: else:
game_layer.wait() game_layer.wait()
def something_needs_attention(): def something_needs_attention():
global building_under_construction, edit_temp, maintain, state global building_under_construction, edit_temp, maintain, state
@ -148,13 +150,13 @@ def something_needs_attention():
for i in range(len(state.residences)): for i in range(len(state.residences)):
if (state.turn % rounds_between_energy == i) and not state.residences[i].build_progress < 100: if (state.turn % rounds_between_energy == i) and not state.residences[i].build_progress < 100:
edit_temp = (True, i) edit_temp = (True, i)
#check if need for maintainance # check if need for maintenance
maintain = (False, 0) maintain = (False, 0)
for i in range(len(state.residences)): for i in range(len(state.residences)):
if state.residences[i].health < 41+rounds_between_energy*game_layer.get_residence_blueprint(state.residences[i].building_name).decay_rate: if state.residences[i].health < 41+rounds_between_energy*game_layer.get_residence_blueprint(state.residences[i].building_name).decay_rate:
maintain = (True, i) maintain = (True, i)
if maintain[0]: #check maintainance if maintain[0]: # check maintenance
game_layer.maintenance((state.residences[maintain[1]].X, state.residences[maintain[1]].Y)) game_layer.maintenance((state.residences[maintain[1]].X, state.residences[maintain[1]].Y))
return True return True
elif edit_temp[0]: # adjust temp of buildings elif edit_temp[0]: # adjust temp of buildings
@ -177,7 +179,7 @@ def something_needs_attention():
else: else:
return False return False
def chartMap(): def chart_map():
global state global state
for x in range(len(state.map) - 1): for x in range(len(state.map) - 1):
for y in range(len(state.map) - 1): for y in range(len(state.map) - 1):
@ -185,19 +187,20 @@ def chartMap():
available_tiles.append((x, y)) available_tiles.append((x, y))
optimize_available_tiles() optimize_available_tiles()
def adjustEnergy(currentBuilding):
def adjust_energy(current_building):
global rounds_between_energy, EMA_temp, state global rounds_between_energy, EMA_temp, state
blueprint = game_layer.get_residence_blueprint(currentBuilding.building_name) blueprint = game_layer.get_residence_blueprint(current_building.building_name)
outDoorTemp = state.current_temp * 2 - EMA_temp outDoorTemp = state.current_temp * 2 - EMA_temp
temp_acceleration = (2*(21 - currentBuilding.temperature)/(rounds_between_energy)) temp_acceleration = (2*(21 - current_building.temperature)/(rounds_between_energy))
effectiveEnergyIn = ((temp_acceleration - 0.04 * currentBuilding.current_pop + (currentBuilding.temperature - outDoorTemp) * blueprint.emissivity) / 0.75) + blueprint.base_energy_need effectiveEnergyIn = ((temp_acceleration - 0.04 * current_building.current_pop + (current_building.temperature - outDoorTemp) * blueprint.emissivity) / 0.75) + blueprint.base_energy_need
if effectiveEnergyIn > blueprint.base_energy_need: if effectiveEnergyIn > blueprint.base_energy_need:
game_layer.adjust_energy_level((currentBuilding.X, currentBuilding.Y), effectiveEnergyIn) game_layer.adjust_energy_level((current_building.X, current_building.Y), effectiveEnergyIn)
elif effectiveEnergyIn < blueprint.base_energy_need: elif effectiveEnergyIn < blueprint.base_energy_need:
game_layer.adjust_energy_level((currentBuilding.X, currentBuilding.Y), blueprint.base_energy_need + 0.01) game_layer.adjust_energy_level((current_building.X, current_building.Y), blueprint.base_energy_need + 0.01)
else: else:
print("you did it!") print("you did it!")
game_layer.wait() game_layer.wait()
@ -208,7 +211,7 @@ def optimize_available_tiles():
average_x = 0 average_x = 0
average_y = 0 average_y = 0
score_list = [] score_list = []
for tile in available_tiles: #calc average coords for tile in available_tiles: # calc average coordinates
average_x += tile[0] average_x += tile[0]
average_y += tile[1] average_y += tile[1]
average_x /= len(available_tiles) average_x /= len(available_tiles)
@ -217,6 +220,7 @@ def optimize_available_tiles():
for tile in available_tiles: for tile in available_tiles:
tile_score = abs(tile[0] - average_x) + abs(tile[1] - average_y) tile_score = abs(tile[0] - average_x) + abs(tile[1] - average_y)
score_list.append((tile_score, tile)) score_list.append((tile_score, tile))
def sort_key(e): def sort_key(e):
return e[0] return e[0]
print("Sorting tile list") print("Sorting tile list")
@ -225,6 +229,7 @@ def optimize_available_tiles():
available_tiles[i] = score_list[i][1] available_tiles[i] = score_list[i][1]
print("average x,y: " + str(average_x) + ", " + str(average_y)) print("average x,y: " + str(average_x) + ", " + str(average_y))
def build(structure): def build(structure):
global building_under_construction, rounds_between_energy, state global building_under_construction, rounds_between_energy, state
print("Building " + structure) print("Building " + structure)