def overlap(c1, c2, debug=False): if not _check_area(c1, c2, debug=debug): return 0 acos = math.acos sqrt = math.sqrt r = map_data.map_image_size(c1.size)/5 R = map_data.map_image_size(c2.size)/5 d = path_f.pythagoras((c1.x, c1.y), (c2.x, c2.y)) if d > r + R: return 0 if d == 0: return math.pi * max((r*r), (R*R)) # r is the radius of circle 1 # R is the radius of circle 2 # d is the distance of the centre of circle 2 from the centre of circle 1. # cos-1 is the inverse cosine operator. try: a = r**2 * acos((d**2 + r**2 - R**2) / (2 * d * r)) b = R**2 * acos((d**2 + R**2 - r**2) / (2 * d * R)) c = (-d + r + R) * (d + r - R) * (d - r + R) * (d + r + R) c = sqrt(c) / 2 except Exception as e: # It may be that the smaller is completely covered by the bigger # C = centre, R = Radius # C1 -- C2 -- R2 ----- R1 R = map_data.map_image_size(c1.size)/2 S = map_data.map_image_size(c2.size) D = path_f.pythagoras(c1, c2) # Area = pi * r^2 if R > S + D: return math.pi * (S/2) * (S/2) print("") print("Error overlapping %s (%d) with %s (%d)" % (c1.name, c1.id, c2.name, c2.id)) print(r) print(R) print(d) raise a = r**2 * acos((d**2 + r**2 - R**2) / (2 * d * r)) b = R**2 * acos((d**2 + R**2 - r**2) / (2 * d * R)) c = (-d + r + R) * (d + r - R) * (d - r + R) * (d + r + R) c = sqrt(c) / 2 # print(a + b - c) return (a + b - c)
def overlap_percentage_square(the_city, amount): the_city.size = map_data.map_image_size(the_city.population + the_city.slaves)/2.5 area = the_city.size ** 2 percentage = (amount / area) * 100 return max(min(percentage, 100), 0)
def test_overlapping(self): for the_city, amount, correct_answer in self.city_sets: real_area = map_data.map_image_size(the_city.size)/2.5 real_area = math.pi * (real_area ** 2) r = city_f.overlap_percentage(the_city, amount) try: self.assertAlmostEqual(correct_answer, r, places=2) except Exception as e: print("") print("City size: %d" % the_city.size) print("City area: %d" % real_area) print("Overlap amount: %d" % amount) print("Correct amount: %d" % correct_answer) print("Incorrect answer: %d" % r) print("") raise
def build_supplies(cursor, verbose): city_dict = city_q.get_live_cities(cursor) city_supply_dict = {} for k, v in city_dict.items(): v.icon_size = map_data.map_image_size(v.population+v.slaves)/4 if verbose: it = cli_f.progressbar(map_resources.data_list, "cities_check.build_supplies: ", 40, with_eta = True) else: it = map_resources.data_list for r, x, y in it: this_city = (-1, 9999) for k, v in city_dict.items(): dist = path_f.pythagoras((v.x, v.y), (x, y)) if dist < v.icon_size: if this_city[1] > v.founded: this_city = (k, v.founded) if this_city[0] > 0: if this_city[0] not in city_supply_dict: city_supply_dict[this_city[0]] = [] city_supply_dict[this_city[0]].append(r) query = "UPDATE cities SET str_supplies = '';" try: cursor.execute(query) except Exception as e: raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query)) for k, v in city_supply_dict.items(): vset = [str(r) for r in set(v)] query = "UPDATE cities SET str_supplies = '{0}' WHERE id = {1:d};".format( ",".join(vset), k, ) try: cursor.execute(query) except Exception as e: raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query))
def prospective_founding_order(the_line, groups, debug=False): results = founding_order(the_line, groups, debug) if not the_line.block.msn_order: return order_block.fail(results, "Command not valid in live mode") results['queries'] = [] results['cost'] = "" if results['success']: # X and Y x, y = groups['location'].split(',') x = int(x.strip()) y = int(y.strip()) # Header results['results'] = ["[b]Founded '%s' successfully[/b]" % groups['city_name']] # Terrain terrain = mapper_q.get_terrain(the_line.the_world.cursor, x, y) results['results'].append("Terrain: %s" % map_data.terrain[terrain].title()) # Supplies supplies = [] icon_size = map_data.map_image_size(int(groups['size']))/4 for r, rx, ry in map_resources.data_list: dist = path_f.pythagoras((rx, ry), (x, y)) if dist < icon_size: supplies.append(r) if len(supplies) > 1: results['results'].append("Supplies: %s" % ", ".join([resource_list.data_dict[s].name for s in supplies])) elif len(supplies) == 1: results['results'].append("Supply: %s" % ", ".join([resource_list.data_dict[s].name for s in supplies])) return results else: return results
def test_sizes(self): for size, expected in self.city_sets: r = map_data.map_image_size(size) self.assertEqual(expected, r)
def overlap_percentage(the_city, amount): r = map_data.map_image_size(the_city.size)/5 area = math.pi * (r ** 2) percentage = (amount / area) * 100 return max(min(percentage, 100), 0)
def _check_area(c1, c2, debug=False): i1 = map_data.map_image_size(c1.size)/2.5 i2 = map_data.map_image_size(c2.size)/2.5 c1.left = c1.x - i1/2 c1.right = c1.x + i1/2 c1.top = c1.y - i1/2 c1.bottom = c1.y + i1/2 c2.left = c2.x - i2/2 c2.right = c2.x + i2/2 c2.top = c2.y - i2/2 c2.bottom = c2.y + i2/2 xlap, ylap = 0, 0 if debug: print("") # Does this sit to the left, between or right of the other? # C1 is Left if c1.left <= c2.left and c1.right <= c2.right and c1.right >= c2.left: xlap = c1.right - c2.left if debug: print("xlap 1 = %d (R1 - L2) (%d - %d)" % (xlap, c1.right, c2.left)) # C1 is Right elif c1.left >= c2.left and c1.right >= c2.right and c1.left <= c2.right: xlap = c2.right - c1.left if debug: print("xlap 2 = %d (R2 - L1) (%d - %d)" % (xlap, c2.right, c1.left)) # Between, C1 is bigger elif c1.left <= c2.left and c1.right >= c2.right: xlap = min(i1, i2) if debug: print("xlap 3 = %d min(x1, x2) min(%d, %d)" % (xlap, i1, i2)) # Between, C2 is bigger elif c1.left >= c2.left and c1.right <= c2.right: xlap = min(i1, i2) if debug: print("xlap 4 = %d min(x1, x2) min(%d, %d)" % (xlap, i1, i2)) if xlap <= 0: if debug: print("xlap <= 0") # Does this sit to the left, between or right of the other? # C1 is Above if c1.top <= c2.top and c1.bottom <= c2.bottom and c1.bottom >= c2.top: ylap = c1.bottom - c2.top if debug: print("ylap 1 = %d (B1 - T2) (%d - %d)" % (ylap, c1.bottom, c2.top)) # C1 is Below elif c1.top >= c2.top and c1.bottom >= c2.bottom and c1.top <= c2.bottom: ylap = c2.bottom - c1.top if debug: print("ylap 2 = %d (B2 - T1) (%d - %d)" % (ylap, c2.bottom, c1.top)) # Between, C1 is bigger elif c1.top <= c2.top and c1.bottom >= c2.bottom: ylap = min(i1, i2) if debug: print("ylap 3 = %d min(y1, y2) min(%d, %d)" % (ylap, i1, i2)) # Between, C2 is bigger elif c1.top >= c2.top and c1.bottom <= c2.bottom: ylap = min(i1, i2) if debug: print("ylap 4 = %d min(y1, y2) min(%d, %d)" % (ylap, i1, i2)) if ylap <= 0: if debug: print("ylap <= 0") if debug: print("") print("C1: %s x(%s, %s) y(%s, %s) (%s)" % (c1.name, c1.left, c1.right, c1.top, c1.bottom, i1)) print("C2: %s x(%s, %s) y(%s, %s) (%s)" % (c2.name, c2.left, c2.right, c2.top, c2.bottom, i2)) print("(%s * %s = %s) -> (%d, %d)" % (xlap, ylap, (xlap*ylap), c1.team, c2.team)) print("") overlap = (xlap * ylap) return overlap
def draw_city(self, cursor, city_id): city_is_special = False # Handles the_city = self.city_dict[city_id] the_team = self.team_dict[the_city.team] team_clean_name = the_team.clean_name() # Test for 1 icon image_size = map_data.map_image_size(the_city.size) if image_size == 0: return "" my_left = (the_city.x - self.left) * 2.5 - (image_size/2.0) my_top = (the_city.y - self.top) * 2.5 - (image_size/2.0) # Check it's within the picture size of the map if (the_city.x - image_size/2) < self.left: return "" if (the_city.x + image_size/2) > self.right: return "" if (the_city.y - image_size/2) < self.top: return "" if (the_city.y + image_size/2) > self.bottom: return "" # Mouseover #------------------------ # Name # clean_name = the_city.name.lower().replace(' ', '').replace("'", "") # clean_name = common.js_name(the_city.name.lower().replace(' ', '').replace("'", '')) clean_name = safe_re.sub("", the_city.name) # safe_re # Location city_x = the_city.x city_y = the_city.y if the_team.ir: city_title = ["<strong>Team</strong>: %s <span style='font-size:0.9em;'>(IR)</span><br />" % the_team.name] else: city_title = ["<strong>Team</strong>: %s<br />" % the_team.name] # Port if the_city.port == True: city_title.append('Is a port') else: city_title.append('Not a port') # Nomadic if the_city.nomadic == True: city_title.append('<br />Nomadic') # Walls team_logo = '%s_unwalled.png' % team_clean_name has_harbour_walls = False # Harbour walls id = 18 if 18 in the_city.buildings_amount and the_city.buildings_amount[18] > 0: has_harbour_walls = True city_title.append('<br />Harbour walls') if the_city.walls != []: if len(the_city.walls) > 1: if has_harbour_walls: city_title.append('<br />%d walls' % (len(the_city.walls)-1)) else: city_title.append('<br />%d walls' % len(the_city.walls)) else: if not has_harbour_walls: city_title.append('<br />1 wall') team_logo = '%s_walled.png' % team_clean_name # Supply if the_city.supplies != []: supply_string = ", ".join([resource_list.data_list[s].name for s in the_city.supplies]) city_title.append('<br />%s' % supply_string) # Terrain city_title.append('<br />Terrain type: %s' % map_data.terrain[mapper_q.get_terrain(cursor, city_x, city_y)].title()) # Area - Used for debugging overlap # area = image_size/2.5 # area = area * area # city_title.append("<br />Area: %s" % area) # Overlap if the_city.overlap > 0: city_title.append('<br />Overlap: %s%%' % the_city.overlap) # Population if the_city.team not in self.personalise: city_title.append('<br />Size: %s' % common.approx(the_city.size)) if the_city.slaves > 0: city_title.append('<br />Slaves: %s' % common.approx(the_city.slaves)) else: city_title.append('<br />Population: %s' % common.number_format(the_city.population)) if the_city.slaves > 0: city_title.append('<br />Slaves: %s' % common.number_format(the_city.slaves)) # Artefact artefact_count = 0 if city_id in self.cities_with_artefact: for a, the_artefact in self.artefact_dict.items(): if the_artefact.city == city_id: artefact_count += 1 if artefact_count > 0: city_is_special = True if artefact_count > 1: city_title.append('<br /><strong>City contains %d artefacts</strong>' % artefact_count) else: city_title.append('<br /><strong>City contains an artefact</strong>') # Wonder if city_id in self.cities_with_wonder: for w, the_wonder in self.wonder_dict.items(): if the_wonder.city == city_id: if the_wonder.completed: city_title.append('<br /><strong>City contains a wonder</strong>') city_is_special = True else: city_title.append('<br /><strong>City contains an uncompleted wonder</strong>') # Description if the_city.description != '': city_title.append('<br />%s' % the_city.description.replace("\\", "")) # GM stuff if self.gm: city_title.append('<br />Image size: %s' % image_size) # Info only for the team map if the_city.team in self.personalise: city_title.append("<br />") # Buildings? # in_progress_dict, city_buildings = the_city.get_buildings() walls = [] buildings = [] in_progress = [] for b, a in the_city.buildings_amount.items(): if self.building_dict[b].wall == True: walls.append(self.building_dict[b].name) if self.building_dict[b].wall != True: buildings.append(self.building_dict[b].name) for b, p in the_city.buildings.items(): if p > 0: in_progress.append(self.building_dict[b].name) if len(buildings) > 0: city_title.append("<br /><strong>Buildings</strong>: %s" % ", ".join(buildings)) if len(walls) > 0: city_title.append("<br /><strong>Walls</strong>: %s" % ", ".join(walls)) if len(in_progress) > 0: city_title.append("<br /><strong>In progress</strong>: %s" % ", ".join(in_progress)) if the_city.founded == common.current_turn(): new_city_style = "border: 1px solid #FFA;" city_title.append("<br />Founded: <strong>Turn %d</strong>" % the_city.founded) elif city_is_special: # new_city_style = "border: 2px dotted #FFF;" new_city_style = "" team_logo = '%s_special.png' % team_clean_name else: new_city_style = "" city_title.append("<br />Founded: Turn %d" % the_city.founded) # Land controlled? # control = "" # if the_city.team in self.personalise: # control_image_size = map_control_size(the_city.size) # if control_image_size == 0: # return "" # # c_left = (the_city.x - self.left) * 2.5 - (control_image_size/2.0) # c_top = (the_city.y - self.top) * 2.5 - (control_image_size/2.0) # # control = """<img class="city_icon" src="%(icon_path)scontrol.png" style="top:%(top)spx; left: %(left)spx;" width="%(image_size)s" height="%(image_size)s" />""" % { # "icon_path": self.icon_path, # "top": c_top, # "left": c_left, # "image_size": control_image_size, # } # # Output - Function float # output = """<img class="city_icon" id="{0[clean_name]}" src="{0[icon_path]}{0[team_logo]}" style="top:{0[top]}px; left:{0[left]}px;" width="{0[image_size]}" height="{0[image_size]}" onmouseover="$('#{0[clean_name]}_hover').fadeIn(250);" onmouseout="$('#{0[clean_name]}_hover').hide(250);"/>""".format({ # "icon_path": self.icon_path, # "clean_name": clean_name, # "top": my_top, # "left": my_left, # "image_size": image_size, # "team_logo": team_logo, # }) # Output - Fixed float output = """<img class="city_icon" id="{clean_name}" src="{icon_path}{team_logo}" style="top:{top}px; left:{left}px;{new_city}" width="{image_size}" height="{image_size}" onmouseover="$('#{clean_name}_hover').fadeIn(250);" onmouseout="$('#{clean_name}_hover').hide(250);"/>""".format( icon_path = self.icon_path, clean_name = clean_name, top = my_top, left = my_left, image_size = image_size, team_logo = team_logo, new_city = new_city_style, ) if self.mouseover == 1: hover_top = my_top - 5 hover_top = min(self.height*2.5 - 125, hover_top) hover_left = my_left + image_size + 20 hover_left = max(hover_left, 5) if hover_left > ((self.width * 2.5) - 350): hover_left = my_left - 350 # Floater self.append_last.append("""<div id="%(clean_name)s_hover" class="city_hover" style="top:%(hover_top)spx; left:%(hover_left)spx;"><div class="city_title">%(name)s %(city_x)s,%(city_y)s</div>%(city_title)s</div>""" % { "name": the_city.name, "clean_name": clean_name, "hover_top": hover_top, "hover_left": hover_left, "city_x": the_city.x, "city_y": the_city.y, "city_title": "".join(city_title), }) # Now return it return "".join(output)
def draw_city(self, cursor, city_id): city_is_special = False # Handles the_city = self.city_dict[city_id] the_team = self.team_dict[the_city.team] # Test for 1 icon image_size = map_data.map_image_size(the_city.size) # Check it's within the picture size of the map if (the_city.x - image_size/2) < self.left: return if (the_city.x + image_size/2) > self.right: return if (the_city.y - image_size/2) < self.top: return if (the_city.y + image_size/2) > self.bottom: return output = {} # System type stuff output['pixel_x'] = (the_city.x - self.left) * 2.5 - (image_size/2.0) output['pixel_y'] = (the_city.y - self.top) * 2.5 - (image_size/2.0) # Owner output['team.id'] = the_team.id output['team.name'] = the_team.name # Key stats output['id'] = the_city.id output['name'] = the_city.name output['x'] = the_city.x output['y'] = the_city.y output['port'] = the_city.port output['nomadic'] = the_city.nomadic output['founded'] = the_city.founded # Walls output['harbour_walls'] = False if 18 in the_city.buildings_amount and the_city.buildings_amount[18] > 0:# Harbour walls id = 18 output['harbour_walls'] = True output['wall_count'] = 0 if the_city.walls != []: if len(the_city.walls) > 1: if output['harbour_walls']: output['wall_count'] = len(the_city.walls) - 1 else: output['wall_count'] = len(the_city.walls) elif not output['harbour_walls']: output['wall_count'] = len(the_city.walls) # Supplies and Terrain output['supplies'] = the_city.supplies output['terrain'] = map_data.terrain[mapper_q.get_terrain(cursor, the_city.x, the_city.y)].title() city_loc = (the_city.x - (the_city.x % 10), the_city.y - (the_city.y % 10)) output['continent'] = path_f.get_map_continent_tiles(cursor).get(city_loc, -1) # Overlap output['supplies'] = the_city.overlap # Population if the_city.team not in self.personalise: output['size'] = common.napprox(the_city.size) output['slaves'] = common.napprox(the_city.slaves) else: output['size'] = the_city.size output['population'] = the_city.population output['slaves'] = the_city.slaves # Artefact artefact_count = 0 if city_id in self.cities_with_artefact: for a, the_artefact in self.artefact_dict.items(): if the_artefact.city == city_id: artefact_count += 1 output['artefacts'] = artefact_count # Wonder output['wonder'] = "None" if city_id in self.cities_with_wonder: for w, the_wonder in self.wonder_dict.items(): if the_wonder.city == city_id: if the_wonder.completed: output['wonder'] = "Complete" else: output['wonder'] = "Incomplete" # Description output['description'] = the_city.description # # Info only for the team map # if the_city.team in self.personalise: # walls = [] # buildings = [] # in_progress = [] # # for b, a in the_city.buildings_amount.items(): # if self.building_dict[b].wall == True: walls.append(self.building_dict[b].name) # if self.building_dict[b].wall != True: buildings.append(self.building_dict[b].name) # # for b, p in the_city.buildings.items(): # if p > 0: # in_progress.append(self.building_dict[b].name) # # if len(buildings) > 0: # city_title.append("<br /><strong>Buildings</strong>: %s" % ", ".join(buildings)) # # if len(walls) > 0: # city_title.append("<br /><strong>Walls</strong>: %s" % ", ".join(walls)) # # if len(in_progress) > 0: # city_title.append("<br /><strong>In progress</strong>: %s" % ", ".join(in_progress)) # Now return it return output