Compare commits

...
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.

28 Commits

Author SHA1 Message Date
Thefeli73
5ac717cd7b Merge branch 'felix-lek' 2020-10-08 04:47:48 +02:00
Thefeli73
3271e4be56 småskit 2020-10-08 04:44:03 +02:00
Thefeli73
38a3ea5e2e auto use regulator 2020-10-08 03:57:17 +02:00
Thefeli73
4a649b9124 mall/charger fix 2020-10-08 03:50:44 +02:00
Thefeli73
95c0ab761a round buffer 2020-10-08 03:50:33 +02:00
Thefeli73
1b298bb72f minor changes 2020-10-08 02:50:05 +02:00
Thefeli73
a7d1981d71 ema length 2020-10-08 02:49:39 +02:00
Thefeli73
21ef5b75da build residences near utilities 2020-10-08 02:00:43 +02:00
Thefeli73
259fdc14e8 hotfix 2020-10-08 01:59:16 +02:00
Thefeli73
7e2599c335 fix none to None 2020-10-08 00:28:16 +02:00
Thefeli73
2c3d4897e7 clear junk and remove some soft warnings 2020-10-08 00:27:31 +02:00
Thefeli73
d25f6f2ac1 fix for building functions 2020-10-07 20:02:47 +02:00
Thefeli73
17fc759321 temp acceleration multiplier 2020-10-07 20:02:22 +02:00
Thefeli73
5637da5a34 check building diversity and take account for build delay in score 2020-10-07 19:16:42 +02:00
Thefeli73
fd424496bb tilescore check effect properly 2020-10-07 19:15:31 +02:00
Thefeli73
96633a9e98 fix build position 2020-10-07 19:15:09 +02:00
Thefeli73
6af8e1a46d optimize vars 2020-10-07 19:14:26 +02:00
Thefeli73
87356ad8bd förbättra loq health detection 2020-10-07 17:58:03 +02:00
Thefeli73
68cceee9fa launcher remove input 2020-10-07 13:41:44 +02:00
Thefeli73
00b96b09c3 fixes 2020-10-07 06:52:30 +02:00
Thefeli73
dc3e5c1544 fixa lite i launchern 2020-10-07 06:03:43 +02:00
Thefeli73
3e20b79fa3 HS 7800. massa helper functions, calculate best util, tile score, etc 2020-10-07 06:00:35 +02:00
Thefeli73
cc693ee83d some fixes, big work on devsociety and get_best_ 2020-10-07 04:04:39 +02:00
Thefeli73
cf977331e3 PARALLELISERAD LAUNCHER!!! 2020-10-07 03:34:42 +02:00
Thefeli73
d3a4849fd1 patch launcher 2020-10-07 00:43:39 +02:00
Thefeli73
9f759e8d21 purge premade code 2020-10-07 00:24:15 +02:00
Thefeli73
1d238ecc2f clean some code 2020-10-06 22:53:27 +02:00
Thefeli73
5180e293d7 launcher exiter 2020-10-06 12:16:19 +02:00
3 changed files with 265 additions and 136 deletions

View File

@ -1,8 +1,8 @@
from game_layer import GameLayer
api_key = "74e3998d-ed3d-4d46-9ea8-6aab2efd8ae3"
game_layer = GameLayer(api_key)
state = game_layer.game_state
game_layer.force_end_game()
game_layer.force_end_game()
game_layer.force_end_game()
game_layer.force_end_game()
def clear_it():
game_layer.force_end_game()
game_layer.force_end_game()
game_layer.force_end_game()
game_layer.force_end_game()

View File

@ -1,7 +1,22 @@
import main
number_of_launches = 6
result_list = []
for i in range(number_of_launches):
result_list.append(main.main())
for result in result_list:
print("Game " + result[0] + " had a score of: " + str(result[1]))
import clearGames
from multiprocessing import Pool
proc_running = 4 # MAX 4!!!
def run_main(n):
result = main.main()
return result
def launch(list):
for result in list:
print("Game " + result[0] + " had a score of: " + str(result[1]))
if __name__ == '__main__':
clearGames.clear_it()
with Pool(proc_running) as p:
results = p.map(run_main, range(proc_running))
launch(results)

364
main.py
View File

@ -5,22 +5,37 @@ from sys import exit
from game_layer import GameLayer
import game_state
import traceback
import random
api_key = "74e3998d-ed3d-4d46-9ea8-6aab2efd8ae3"
# The different map names can be found on considition.com/rules
map_name = "training1" # TODO: You map choice here. If left empty, the map "training1" will be selected.
game_layer = GameLayer(api_key)
# settings
time_until_run_ends = 70
utilities = 3
money_reserve_multiplier = 1.5
use_regulator = False # turns on if map max temp >21c
other_upgrade_threshold = 0.5
time_until_run_ends = 90
money_reserve_multiplier = 0.5
temp_acc_multiplier = 1.125
rounds_between_energy = 5
round_buffer = 78
# vars
EMA_temp = None
building_under_construction = None
available_tiles = []
state = None
queue_timeout = 1
edit_temp = None
maintain = None
def main():
global EMA_temp, rounds_between_energy, building_under_construction, available_tiles, state, queue_timeout
global EMA_temp, rounds_between_energy, building_under_construction, available_tiles, state, queue_timeout, use_regulator
# global vars
rounds_between_energy = 5
EMA_temp = None
ema_length = 16
building_under_construction = None
available_tiles = []
queue_timeout = 1
@ -32,12 +47,14 @@ def main():
start_time = time.time()
state = game_layer.game_state
chart_map()
if state.max_temp > 21:
use_regulator = True
while state.turn < state.max_turns:
state = game_layer.game_state
try:
if EMA_temp is None:
EMA_temp = state.current_temp
ema_k_value = (2/(rounds_between_energy+1))
ema_k_value = (2/(ema_length+1))
EMA_temp = state.current_temp * ema_k_value + EMA_temp*(1-ema_k_value)
take_turn()
except Exception:
@ -71,63 +88,36 @@ def take_turn():
for error in state.errors:
print("Error: " + error)
# 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)
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)
'''
def develop_society():
global state, queue_timeout, available_tiles, utilities
global state, queue_timeout, available_tiles, money_reserve_multiplier
queue_reset = 1
if queue_timeout > 1:
queue_timeout -= 1
best_residence = calculate_best_residence()
best_utility = calculate_best_utility()
best_upgrade = get_best_upgrade()
build_residence_score = 0
build_utility_score = 0
build_upgrade_score = 0
# priority scores, 1 = very urgent, 0 = not urgent at all
# queue modifier * funds modifier * existing houses modifier
build_residence_score = (state.housing_queue / (15 * queue_timeout)) * (1 - (7500 / (1 + state.funds))) * (1 - (len(state.residences) / (1 + len(available_tiles) - utilities)))
upgrade_residence_score = 0
# existing houses modifier * funds modifier * existing utilities modifier
build_utility_score = (len(state.residences) / (1 + len(available_tiles)-utilities)) * (1 - (16000 / (1 + state.funds))) * (1 - (len(state.utilities) / utilities))
# turn modifier * funds modifier
build_upgrade_score = (1 - (state.turn / 700)) * (2 - (15000 / (1 + state.funds)))
if len(state.residences) < 1:
build_residence_score = 100
build_residence_score = 1000
elif (current_tot_pop() - max_tot_pop() + state.housing_queue) < 0:
build_residence_score = 0
elif (current_tot_pop() - max_tot_pop() + state.housing_queue) > 15 and queue_timeout <= 0:
build_residence_score = 1000
elif best_residence and best_residence[0] > 0:
build_residence_score = best_residence[0]
#
upgrade_residence_score = 0
#
if best_utility and best_utility[0] > 0:
build_utility_score = best_utility[0]
#
if best_upgrade and best_upgrade[0] > 0:
build_upgrade_score = best_upgrade[0]
decision = [
('build_residence', build_residence_score),
@ -135,42 +125,40 @@ def develop_society():
('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][1] >= 0:
if decision[0][0] == "build_residence": # build housing
queue_timeout = 5
#if len(state.residences) < len(state.available_residence_buildings):
# return build(state.available_residence_buildings[len(state.residences)].building_name)
#else:
cbr = calculate_best_residence()
if cbr:
return build(cbr[1])
if best_residence:
queue_timeout = queue_reset
if best_residence[2]:
return build_place(best_residence[1], best_residence[2])
else:
return build(best_residence[1])
if decision[0][0] == "build_utility": # build utilities
#return build("WindTurbine")
pass
if decision[0][0] == "upgrade_residence": # build utilities
if best_utility:
return build_place(best_utility[1], best_utility[2])
if decision[0][0] == "upgrade_residence": # upgrade housing
pass
if decision[0][0] == "build_upgrade": # build upgrades
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 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
gbp = get_best_upgrade()
if gbp:
game_layer.buy_upgrade((gbp[2].X, gbp[2].Y), gbp[1])
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])
return True
del decision[0]
return False
def something_needs_attention():
global building_under_construction, edit_temp, maintain, state, rounds_between_energy
@ -179,7 +167,8 @@ def something_needs_attention():
# 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:
blueprint = game_layer.get_residence_blueprint(state.residences[i].building_name)
if state.residences[i].health < 40+(max(((blueprint.maintenance_cost - state.funds) / (1+total_income())), 1) * blueprint.decay_rate):
maintain = (True, i)
if (state.turn % rounds_between_energy == i) and not state.residences[i].build_progress < 100:
edit_temp = (True, i)
@ -197,7 +186,7 @@ def something_needs_attention():
return True
elif (len(state.utilities)-1 >= building_under_construction[2]) and (state.utilities[building_under_construction[2]].build_progress < 100):
game_layer.build((building_under_construction[0], building_under_construction[1]))
if not state.residences[building_under_construction[2]].build_progress < 100:
if not state.utilities[building_under_construction[2]].build_progress < 100:
building_under_construction = None
return True
else:
@ -207,6 +196,22 @@ def something_needs_attention():
return False
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
def total_income():
global state
income = 0
@ -235,7 +240,7 @@ def get_best_upgrade():
def calculate_best_upgrade(current_building):
global state
global state, money_reserve_multiplier
rounds_left = 700 - state.turn
current_pop = current_building.current_pop
@ -247,19 +252,20 @@ def calculate_best_upgrade(current_building):
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)
average_heating_energy = max((((21 - average_outdoor_temp) * blueprint.emissivity * effect.emissivity_multiplier) / 0.75), 0)
old_average_heating_energy = max((((21 - average_outdoor_temp) * blueprint.emissivity) / 0.75), 0)
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)
upgrade_co2 = (effect.co2_per_pop_increase + 0.03) * current_pop * rounds_left + (0.1 * lifetime_energy / 1000)
if "Mall.2" in current_building.effects and upgrade.name == "Charger":
upgrade_co2 = (effect.co2_per_pop_increase - 0.009 + 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
max_happiness = effect.max_happiness_increase * rounds_left
max_happiness = effect.max_happiness_increase * current_pop * rounds_left
score = max_happiness/10 - co2
# score = score / upgrade.cost
best_upgrade.append((score, upgrade.name))
def sort_key(e):
@ -270,22 +276,88 @@ def calculate_best_upgrade(current_building):
return best_upgrade[0]
def calculate_best_residence():
global state
def calculate_best_utility():
global state, money_reserve_multiplier, round_buffer
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):
rounds_left = 700 - state.turn - (100 / utility_blueprint.build_speed) - round_buffer
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)
affected_people = tile_score(available_tiles[i], effect.radius, effect_name)[0]
affected_buildings = tile_score(available_tiles[i], effect.radius, effect_name)[1]
cost -= effect.building_income_increase * rounds_left
happiness_increase = affected_people * effect.max_happiness_increase * rounds_left
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))
# 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)
# print(best_utility)
if not best_utility:
return False
return best_utility[0]
def calculate_best_residence():
global state, money_reserve_multiplier, round_buffer
rounds_left = 700 - state.turn
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):
rounds_left = 700 - state.turn - (100 / residence_blueprint.build_speed) - round_buffer
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
co2 = 0.03 * residence_blueprint.max_pop * rounds_left + residence_blueprint.co2_cost + (0.1 * lifetime_energy / 1000)
max_happiness = residence_blueprint.max_happiness * rounds_left
distinct_residences = number_of_distinct_residences(residence_blueprint.building_name)
diversity = 1 + distinct_residences[0]/10
score = residence_blueprint.max_pop*15 + max_happiness/10 - co2
best_residence.append((score, residence_blueprint.building_name))
co2 = 0.03 * residence_blueprint.max_pop * rounds_left + residence_blueprint.co2_cost + (0.1 * lifetime_energy / 1000)
max_happiness = residence_blueprint.max_happiness * residence_blueprint.max_pop * rounds_left
max_happiness *= diversity
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
# score = score / residence_blueprint.cost
# calculate tiles near utils
best_foundation_tile = []
for i in range(len(available_tiles)):
tile = available_tiles[i]
if isinstance(tile, tuple):
for utility in state.utilities:
for effect_name in utility.effects:
effect = game_layer.get_effect(effect_name)
delta_x = abs(tile[0] - utility.X)
delta_y = abs(tile[1] - utility.Y)
distance = delta_x + delta_y
if (distance <= effect.radius):
best_foundation_tile.append((distance, i))
def sort_key(e):
return e[0]
best_foundation_tile.sort(key=sort_key)
if best_foundation_tile:
best_residence.append((score, residence_blueprint.building_name, best_foundation_tile[0][1]))
else:
best_residence.append((score, residence_blueprint.building_name, False))
def sort_key(e):
return e[0]
@ -295,6 +367,18 @@ def calculate_best_residence():
return best_residence[0]
def number_of_distinct_residences(new_building):
global state
unique_names = []
for residence in state.residences:
if residence.building_name not in unique_names:
unique_names.append(residence.building_name)
if new_building not in unique_names:
unique_names.append(new_building)
return len(unique_names), True
return len(unique_names), False
def chart_map():
global state
for x in range(len(state.map) - 1):
@ -304,8 +388,22 @@ def chart_map():
optimize_available_tiles()
def tile_score(tile, radius, effect):
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
if (distance <= radius) and effect not in residence.effects:
affected_people += residence.current_pop
affected_buildings += 1
return affected_people, affected_buildings
def optimize_available_tiles():
global average_x, average_y, score_list
average_x = 0
average_y = 0
score_list = []
@ -323,11 +421,10 @@ def optimize_available_tiles():
score_list.sort(key=sort_key)
for i in range(len(score_list)):
available_tiles[i] = score_list[i][1]
print("average x,y: " + str(average_x) + ", " + str(average_y))
def adjust_energy(current_building):
global rounds_between_energy, EMA_temp, state
global rounds_between_energy, EMA_temp, state, temp_acc_multiplier
blueprint = game_layer.get_residence_blueprint(current_building.building_name)
base_energy = blueprint.base_energy_need
if "Charger" in current_building.effects:
@ -337,48 +434,65 @@ def adjust_energy(current_building):
if "Insulation" in current_building.effects:
emissivity *= 0.6
outDoorTemp = state.current_temp * 2 - EMA_temp
temp_acceleration = (2*(21 - current_building.temperature)/(rounds_between_energy))
out_door_temp = state.current_temp * 2 - EMA_temp
temp_acceleration = (2*(21 - current_building.temperature)/rounds_between_energy) * temp_acc_multiplier
effectiveEnergyIn = ((temp_acceleration - 0.04 * current_building.current_pop + (current_building.temperature - outDoorTemp) * emissivity) / 0.75) + base_energy
effective_energy_in = ((temp_acceleration - 0.04 * current_building.current_pop + (current_building.temperature - out_door_temp) * emissivity) / 0.75) + base_energy
if effectiveEnergyIn > base_energy:
game_layer.adjust_energy_level((current_building.X, current_building.Y), effectiveEnergyIn)
if effective_energy_in > base_energy:
game_layer.adjust_energy_level((current_building.X, current_building.Y), effective_energy_in)
return True
elif effectiveEnergyIn < base_energy:
elif effective_energy_in < base_energy:
game_layer.adjust_energy_level((current_building.X, current_building.Y), base_energy + 0.01)
return True
else:
return False
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)
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
def build(structure):
global building_under_construction, rounds_between_energy, state
# print("Building " + structure)
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)
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)
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.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
return False
if __name__ == "__main__":