def popup_caravan_dialog(unit, home_city, target_city, target_unit, target_tile, act_list): client.client.popup_caravan_dialog(actions.Unit(unit), city.City(home_city), city.City(target_city), actions.Unit(target_unit), target_tile, act_list)
def title_handler(self, event): """event handler of title""" #moves cursor up if event.type == KEYDOWN and event.key == K_UP: self.title.menu -= 1 if self.title.menu < 0: self.title.menu = 0 #moves cursor down elif event.type == KEYDOWN and event.key == K_DOWN: self.title.menu += 1 if self.title.menu > 2: self.title.menu = 2 #select menu item if event.type == KEYDOWN and (event.key == K_SPACE or event.key == K_z or event.key == K_RETURN): self.title.music = 0 if self.title.menu == Title.START: self.game_state = CITY self.title = None self.city = city.City() self.new_game = True elif self.title.menu == Title.CONTINUE: save.load(self, self) self.game_state = CITY self.title = None self.city = city.City() elif self.title.menu == Title.END: pygame.quit() sys.exit()
def tower_handler(self, event): """event handler of tower""" if self.tower.character_continue != None and self.tower.character_continue.is_visible == True: self.tower.character_continue.character_view_handler( self, event, self.tower.dungeon_alive_characters) return #moves the cursor up if event.type == KEYDOWN and event.key == K_UP: self.cursor_se.play() self.tower.menu -= 1 if self.tower.menu < 0: self.tower.menu = MENU_MAX #moves the cursor down elif event.type == KEYDOWN and event.key == K_DOWN: self.cursor_se.play() self.tower.menu += 1 if self.tower.menu > MENU_MAX: self.tower.menu = 0 #select the item if event.type == KEYDOWN and (event.key == K_SPACE or event.key == K_z or event.key == K_RETURN): if self.tower.menu == Tower.GO: if len(self.party.member) == 0: return self.game_state = DUNGEON for character in self.party.member: character.coordinate = [9, 19, 1] #character.coordinate = [6,10,4] self.dungeon = dungeon.Dungeon(character.coordinate[2]) self.tower = None elif self.tower.menu == Tower.CONTINUE: if len(self.tower.dungeon_alive_characters) == 0: return self.tower.character_continue = character_view.Character_view( Rect(80, 60, 480, 360), character_view.Character_view.CONTINUE_DUNGEON) self.tower.character_continue.is_visible = True elif self.tower.menu == Tower.BACK: self.game_state = CITY self.tower.menu = Tower.GO self.tower = None self.city = city.City() self.city.menu = 6 self.select_se.play() if event.type == KEYDOWN and (event.key == K_x): self.game_state = CITY self.tower.menu = Tower.GO self.tower = None self.city = city.City() self.city.menu = 6 self.cancel_se.play()
def test_city(self): antwerp = city.City('Antwerp', 34, 33) ghent = city.City('Ghent', 54, 34) kortrijk = city.City('Kortrijk', 80, 23) brussels = city.City('Brussels', 62, 44) route01 = rt.Route([antwerp, ghent, kortrijk, brussels, antwerp]) route02 = rt.Route([antwerp, kortrijk, ghent, brussels, antwerp]) route03 = rt.Route([antwerp, ghent, brussels, kortrijk, antwerp]) #population = gen.Population([route01, route02, route03], 3) self.assertEqual(antwerp.Name, 'Antwerp') route01.calculate_fitness() self.assertGreater(route01.Fitness, 0)
def temple_handler(self, event): """event handler of temple""" if self.temple.donate_money != None and self.temple.donate_money.is_visible: self.temple.donate_money.system_notify_window_handler( event, self, self.party.member) return elif self.temple.temple_cure_window != None and self.temple.temple_cure_window.is_visible: self.temple.temple_cure_window.temple_window_handler(event, self) return if event.type == KEYDOWN and event.key == K_UP: #moves the cursor up self.cursor_se.play() self.temple.menu -= 1 if self.temple.menu < 0: self.temple.menu = MENU_MAX elif event.type == KEYDOWN and event.key == K_DOWN: self.cursor_se.play() self.temple.menu += 1 if self.temple.menu > MENU_MAX: self.temple.menu = 0 if event.type == KEYDOWN and (event.key == K_SPACE or event.key == K_z or event.key == K_RETURN): if self.temple.menu == Temple.CURE: self.temple.temple_cure_window = temple_window.Temple_window( Rect(60, 50, 520, 360)) self.temple.temple_cure_window.is_visible = True elif self.temple.menu == Temple.DONATE: self.temple.donate_money = system_notify.System_notify_window( Rect(200, 120, 240, 240), system_notify.System_notify_window.DONATE) self.temple.donate_money.is_visible = True elif self.temple.menu == Temple.BACK: self.game_state = CITY self.temple.menu = Temple.CURE self.temple.music = 0 self.temple = None self.city = city.City() self.city.menu = 4 self.select_se.play() if event.type == KEYDOWN and (event.key == K_x): self.game_state = CITY self.temple.menu = Temple.CURE self.temple.music = 0 self.temple = None self.city = city.City() self.city.menu = 4 self.cancel_se.play()
def extract_map_information(self): cityFile = open(self.mapFile, "r") aLine = "#" while aLine[0] == "#": aLine = cityFile.readline() self.width = int(aLine.split()[0]) self.height = int(aLine.split()[1]) aLine = "#" while aLine[0] == "#": aLine = cityFile.readline() self.numCities = int(aLine) self.cities = [] self.cityLines = [] for aLine in cityFile: if aLine[0] == "#": continue self.cityLines.append(aLine) cityInfo = aLine.split() self.cityIDs.append(int(cityInfo[0])) if cityInfo[3] == "1": self.p1ChosenCity = int(cityInfo[0]) if cityInfo[3] == "2": self.p2ChosenCity = int(cityInfo[0]) cityInfo = list(map(int, cityInfo[:4])) + list(cityInfo[4]) + list( map(int, cityInfo[5:])) newCity = city.City(cityInfo[0], (cityInfo[1:3]), cityInfo[3], cityInfo[4], cityInfo[5], (cityInfo[6:])) self.cities.append(newCity) self.cityDictionary[int(cityInfo[0])] = newCity
def process_turn(self): if self.name == "settler": #TODO move to found city if self.target_city_tile == None: self.choose_city_location() if self.can_found_city: #move to city location moved = self.move_once() if moved: if self.grid.tiles[self.y,self.x].terrain == "hills" or \ self.grid.tiles[self.y,self.x].terrain == "forest" or \ self.grid.tiles[self.y,self.x].terrain == "jungle": pass #don't move more else: self.move_once() if (self.target_city_tile.y == self.y) and (self.target_city_tile.x == self.x): #found city self.grid.tiles[self.y, self.x].city = city.City( self.grid, self.y, self.x, self.civ) self.civ.city_list.append(self.grid.tiles[self.y, self.x].city) self.grid.tiles[self.y, self.x].city.process_turn() self.grid.tiles[self.y, self.x].unit = None self.can_found_city = False self.civ.unit_list.remove(self) #just in case this unit is accessed again somehow self.y = -1 self.x = -1
def getCity(db_connector, cities): code = input("insert city code: ") city_dic = {} for city_ in cities: if city_.getCode() == str(code): city_dic = city_ break if bool(city_dic) == True: print("\nfound in memory\n") print("code: " + city_.getCode()) print("city: " + city_.getName()) print("population: " + city_.getPopulation()) print("country: " + city_.getCountry()) else: print("\nnot found in memory... try in db\n") city_dic = db_connector.getCity(code) if bool(city_dic) == True: city_code = city_dic.get("code", "") city_name = city_dic.get("city", "") city_pop = city_dic.get("population", "") city_country = city_dic.get("country", "") new_city = city.City(city_code, city_name, city_pop, city_country) cities.append(new_city)
def parse_file(file): f = open(file, 'r') city_index = 0 cities_list = [] connections_list = [] has_parsed_cities = False for line in f: if line in ['\n', '\r\n']: has_parsed_cities = True continue if has_parsed_cities: splitted = line.split('|') con = cn.Connection(cities_list[int(splitted[0])], cities_list[int(splitted[1])]) connections_list.append(con) # create redundant connection con = cn.Connection(cities_list[int(splitted[1])], cities_list[int(splitted[0])]) connections_list.append(con) else: splitted = line.split('|') city = c.City(city_index, int(splitted[0]), int(splitted[1])) cities_list.append(city) city_index += 1 #populate connection in cities for city in cities_list: for connection in connections_list: if connection.origin is city: city.add_connection(connection) G = convert_to_networkx(cities_list, connections_list) return cities_list, connections_list, G
def __init__(self, redis, features={}): NPC.__init__(self, redis, features, 'npc') self.logger = logging.getLogger(__name__) self.generate_features('leader') #generates self.scope = "country" self.generate_features('leader' + self.scope) # generate self.kind from scope; scope.kind = absolutemonarchy self.generate_features('leader' + self.kind) # generate self.leader from leaderabsolutemonarchy_leader # generate leader.description from leaderabsolutemonarchy_leader_description if self.scope == 'country': self.location = country.Country(self.redis, {'leader': self}) elif self.scope == 'city': self.location = city.City(self.redis, {'leader': self}) elif self.scope == 'organization': self.location = organization.Organization(self.redis, {'leader': self}) else: self.location = organization.Organization(self.redis, {'leader': self}) self.scope = 'organization' self.name.title = self.leader_description[self.sex['name']] #re-render the name with the title. self.name.render()
def city_maker(): city_list.clear() for x in range(0, 8): temp_city = city.City() temp_city.xPos = testing_coordinates[x][0] temp_city.yPos = testing_coordinates[x][1] city_list.append(temp_city) return city_list
def add_traveller_city(self): traveller_city = city.City() traveller_city.special_traveller() connector = db_connector.City_Connector(self.loader) traveller_city_id = connector.write_to_db(traveller_city) connector.close_session() self.add_floating_building(traveller_city_id)
def init_map(num): CityList = list() for i in num: start = int(random.random() * 20 / num) over = int(random.random() * 20 / num) temp = list(set(config.config.CITYNAME[start:over])) tempCity = city.City(config.config.CITYNAME[i], temp) CityList.append(tempCity) return CityList
def newCity(self, peer, info): '''Checks to make sure city location is valid. If so we establish city! ''' for x in range(0, 64): for y in range(0,64): tile = self.getTile(info.positionx+x, info.positiony+y) if tile.cityid: container = proto.Container() container.newCityResponse.type = 0 container.newCityResponse.message = "A city already claims tile " + str((info.positionx+x, info.positiony+y)) messenger.send("sendData", [peer, container]) return # Grab next free id. If we are at region city limit then no build! cityids = self.cities.keys() cityid = 1 for n in range(1, self.city_limit): if n not in cityids: cityid = n break else: container = proto.Container() container.newCityResponse.type = 0 container.newCityResponse.message = "This region has reached its max limit of " + str(self.city_limit) + " cities." messenger.send("sendData", [peer, container]) return # Passed the test! Time to found! user = users.getNameFromPeer(peer) newcity = city.City(info.name, cityid, mayor = user) self.cities[cityid] = newcity updated_tiles = [] for y in range(0, 64): for x in range(0, 64): tile = self.getTile(info.positionx+x, info.positiony+y) tile.cityid = cityid updated_tiles.append(tile) container = proto.Container() container.newCityResponse.type = 1 #messenger.send("sendData", [peer, container]) print info.name, "founded with", newcity.funds #container = proto.Container() container.newCity.id = cityid container.newCity.name = info.name container.newCity.mayor = user container.newCity.population = newcity.population container.newCity.funds = newcity.funds #messenger.send("broadcastData", [container]) #container = proto.Container() for tile in updated_tiles: self.updateTile(container, tile) messenger.send("broadcastData", [container])
def makeCities(self, num_cities: int): land_tiles = [ self.getTile(x, y) for x in range(0, self.max_x) for y in range(0, self.max_y) if WATER_DICT.get(self.getTile(x, y).terrain, LAND) == LAND ] city_tiles = [choice(land_tiles) for x in range(0, num_cities)] for tile in city_tiles: tile.city = city.City() tile.road = "road"
def load_from_db(self, filter_by, filter_value): # return complete City objects cities_list = [] loaded_cities = self.session.query(base.City).filter( getattr(base.City, filter_by) == filter_value).all() for select in loaded_cities: new_city = city.City() new_city.set_from_db(select) cities_list.append(new_city) return cities_list
def __init__(self): self.c = city.City(600,500) self.personlist = list() for i in range(1000): x = (int)(100 * np.random.randn() + self.c.getCenterX()) y = (int)(100 * np.random.randn() + self.c.getCenterY()) if (x > 900): x = 900 p = person.Person(self.c,x,y) self.personlist.append(p)
def init_random_cities(self, n): self.cities = [] for i in range(0, n): p = self.random_map_point() random_population = population.Population(1311, 3) commodities = [100] * len(self.commodities) name = str(i) + "-city" new_city = city.City(name, p[0], p[1], random_population, commodities) self.cities.append(new_city)
def add_city(self, city_dict): new_city = city.City() new_city.set_from_dialog(city_dict) connector = db_connector.City_Connector(self.loader) new_city_id = connector.write_to_db(new_city) connector.close_session() self.add_floating_building(new_city_id) self.query_cities_name()
def test_distance(self): city01 = city.City('01', 0, 2) city02 = city.City('02', 4, 5) city03 = city.City('03', 4, 2) # 1D Horizontal calculation self.assertEqual(4.0, rt.Route.calculate_distance(city01, city03)) # 1D Vertical calculation self.assertEqual(3.0, rt.Route.calculate_distance(city03, city02)) # 2D Pythagoras calculation self.assertEqual(5.0, rt.Route.calculate_distance(city01, city02)) route01 = rt.Route([city01, city02, city03]) route01.calculate_fitness() self.assertLess(0, route01.Fitness) self.assertEqual(12, route01.get_distance()) route02 = rt.Route([city01, city02, city03, city01]) route02.calculate_fitness() self.assertLess(0, route02.Fitness) self.assertEqual(12, route02.get_distance())
def get_points(self, lines): i = 8 # Od 8 linii we wszystkich plikach zaczynają się dane while True: lines[i] = lines[i][:-1] # Pozbycie sie '\n' z konca linii line = lines[i].split(" ") if line[0] == 'EOF': # Sprawdzenie czy nie koniec pliku break ID = int(line[0]) X = int(line[1]) Y = int(line[2]) self.cities.append(city.City(ID, X, Y)) i += 1 return
def generate_city(self, num): for n in range(num): placed = False while not placed: x = libtcod.random_get_int(0, 1, self.w - 1) y = libtcod.random_get_int(0, 1, self.h - 1) tile = self.tiles[x][y] if tile.POI is None and tile.blocked is not True: tempCity = city.City(x, y) self.cities.append(tempCity) self.tiles[x][y].POI = tempCity placed = True print "City succeeded", x, y, tempCity.name else: print "failed", x, y
def read_city_data(self): self.file_city_data = QtGui.QFileDialog.getOpenFileName(self) self.lblProcessing.setText("Processing...") self.city = city.City() columns = _csv_data_reader(str(self.file_traffic_data)) for i in len(columns): if int(columns[2][i]) == 1: self.city.add_route(columns[0][i], columns[1][i], length=float(columns[3][i])) else: self.city.add_route(columns[1][i], columns[0][i], length=float(columns[3][i])) self.lblProcessing.setText("Done. OK") self.btnViewCity.setEnabled(True)
def test_breeding(self): city01 = city.City('01', 0, 2) city02 = city.City('02', 4, 5) city03 = city.City('03', 4, 2) city04 = city.City('04', 7, 3) city05 = city.City('05', 1, 6) city06 = city.City('06', 3, 2) city07 = city.City('07', 10, 8) sequence = [city01, city02, city03, city04, city05, city06, city07] mating_sequence = [city03, city01, city04, city07, city05, city06, city02] route01 = rt.Route(sequence) route02 = rt.Route(mating_sequence) child_route = route01.mate(route02) self.assertEqual(len(route01.Sequence), len(child_route.Sequence)) return
def test_shortest_route(self): # define 10 routes each with the same x and y - shortest route would be the sequence of those routes by name genes = [] route_length = 10 for i in range(0, route_length): genes.append(city.City(str(i), i, i)) pop_size = 100 routes = [] for i in range(0, pop_size): routes.append(rt.Route(random.sample(genes, len(genes)))) population = gen.Population(routes, pop_size, pop_size // 2) fastest_route = population.survival_of_the_fittest(30) shortest_distance = math.sqrt((route_length-1)**2 * 2) * 2 # (pythagoras to x,y = 10,10) self.assertAlmostEqual(fastest_route.get_distance(), shortest_distance)
def test_new_generation(self): city01 = city.City('01', 0, 2) city02 = city.City('02', 4, 5) city03 = city.City('03', 4, 2) city04 = city.City('04', 7, 3) city05 = city.City('05', 1, 6) city06 = city.City('06', 3, 2) city07 = city.City('07', 10, 8) genes = [city01, city02, city03, city04, city05, city06, city07] pop_size = 8 routes = [] for i in range(0, pop_size): routes.append(rt.Route(random.sample(genes, len(genes)))) population = gen.Population(routes, pop_size) new_generation = population.breed() self.assertEqual(pop_size, len(new_generation))
def addCity(self, cities): code = input("insert city code: ") name = input("insert city name: ") country = input("insert country: ") population = int(0) while population == 0: try: population = int(input("insert population: ")) except: print("please insert a valid number") query_str = "INSERT INTO PyCity.City (code, city, population, country) VALUES ('" query_str += code query_str += "', '" query_str += name query_str += "', " query_str += str(population) query_str += ", '" query_str += country query_str += "') ON DUPLICATE KEY UPDATE city = '" query_str += name + "', population = " + str(population) + ", country = '" + country + "';" self.cursor = self.db.cursor() try: self.cursor.execute(query_str) self.db.commit() print("city correctly added to database\n") except: print("some error occurred.... Try again!\n") return new_city = city.City(code, name, population, country) if self.cursor.rowcount == 1: pass else: for city_ in cities: if city_.getCode() == str(code): cities.remove(city_) break cities.append(new_city) self.cursor.close()
def dungeon_search_window_handler(self, event, game_self): if self.search_window != None and self.search_window.is_visible == True: self.search_window.search_window_handler(event, game_self) return elif self.check_window != None and self.check_window.is_visible == True: self.check_window.search_window_handler(event,game_self) return if event.type == KEYDOWN and event.key == K_x: self.menu = 0 self.is_visible = False elif event.type == KEYDOWN and (event.key ==K_UP): self.menu -= 1 if self.menu < 0: self.menu = 2 elif event.type == KEYDOWN and (event.key ==K_DOWN): self.menu += 1 if self.menu > 2: self.menu = 0 elif event.type == KEYDOWN and (event.key == K_z or event.key == K_SPACE or event.key == K_RETURN): if self.menu == 0: pass elif self.menu == 1: self.search_window = Search_window(Rect(100, 140 ,440, 100), 1) self.search_window.is_visible = True pass elif self.menu == 2: for character in game_self.party.member: game_self.dungeon_characters.append( character) game_self.party.member = [] game_self.party.alignment = 0 game_self.dungeon = None game_self.game_state = CITY game_self.city = city.City()
def open_dialog_show(self, widget): filename = None response = self.open_dialog.run() if response == 1: self.open_dialog.hide() filename = self.open_dialog.get_filename() try: self.city = city.City(filename) _, _, short_name = filename.rpartition('/') self.add_statusbar_message('openfile', 'Current city: %s' % short_name) self.populate_basic_fields(self.city) self.map = None except IOError as error: message, = error.args self.raise_error(message) self.open_dialog.hide() return filename
def test_mating_pool(self): city01 = city.City('01', 0, 2) city02 = city.City('02', 4, 5) city03 = city.City('03', 4, 2) city04 = city.City('04', 7, 3) city05 = city.City('05', 1, 6) city06 = city.City('06', 3, 2) city07 = city.City('07', 10, 8) genes = [city01, city02, city03, city04, city05, city06, city07] pop_size = 8 elite_size = 3 routes = [] for i in range(0, pop_size): routes.append(rt.Route(random.sample(genes, len(genes)))) population = gen.Population(routes, pop_size, elite_size) mating_pool = population.create_mating_pool() self.assertEqual(len(mating_pool), elite_size)