예제 #1
0
파일: city_f.py 프로젝트: Teifion/Rob3
def get_terrain_ratios(the_world):
	terrain = {}
	
	for k, v in the_world.live_cities().items():
		if v.terrain not in terrain:
			terrain[v.terrain] = 0
		terrain[v.terrain] += 1
		
		if v.terrain == 0:
			print(k)
	
	for k, v in terrain.items():
		print(map_data.terrain[k], v)
	
	from queries import mapper_q
	terrain = {}
	mterrain = mapper_q.get_all_terrain(the_world.cursor)
	
	for k, v in mterrain.items():
		if v not in terrain:
			terrain[v] = 0
		terrain[v] += 1
	
	print("")
	for k, v in terrain.items():
		print(map_data.terrain[k], v)
예제 #2
0
파일: mapper_f.py 프로젝트: Teifion/Rob3
def team_influence(the_world, team_id):
	"""Returns a list of tiles that the team has influence on and by how much"""
	city_dict = the_world.cities()
	team_dict = the_world.teams()
	
	terrain_tuples = mapper_q.get_all_terrain(the_world.cursor)
	
	tile_control = {}
	
	# First pass, we're only using our cities
	for k, c in city_dict.items():
		if c.dead > 0 or c.secret == 1: continue
		if c.team != team_id: continue
		
		city_loc = mod_ten_tuple((c.x, c.y))
		min_x = city_loc[0]-50
		max_x = city_loc[0]+50
		
		min_y = city_loc[1]-50
		max_y = city_loc[1]+50
		
		for x in range(min_x, max_x, 10):
			for y in range(min_y, max_y, 10):
				tile_loc = (x,y)
				
				# Ignore water
				if tile_loc not in terrain_tuples: continue
				
				tile_range = path_f.pythagoras(city_loc, tile_loc)
				
				if (x,y) not in tile_control:
					tile_control[(x,y)] = 0
				
				tile_control[(x, y)] += city_rules.city_control(tile_range, c, team_dict)
	
	# Second pass, reduce it
	for k, c in city_dict.items():
		if c.dead or c.secret == 1: continue
		if team_dict[c.team].active == 0: continue
		if c.team == team_id: continue
		
		city_loc = mod_ten_tuple((c.x, c.y))
		min_x = city_loc[0]-50
		max_x = city_loc[0]+50
		
		min_y = city_loc[1]-50
		max_y = city_loc[1]+50
		
		for x in range(min_x, max_x, 10):
			for y in range(min_y, max_y, 10):
				tile_loc = (x,y)
				
				# Ignore water
				if tile_loc not in tile_control: continue
				if terrain_tuples[tile_loc] == 0: continue
				
				tile_range = path_f.pythagoras(city_loc, tile_loc)
				tile_control[(x, y)] -= city_rules.city_control(tile_range, c, team_dict)
	
	
	# # Output for a map link
	# output_list = []
	# remove_list = []
	# for k, v in tile_control.items():
	# 	if v > 0.5:
	# 		output_list.append("(%s,%s,%s)" % (k[0], k[1], int(v)))
	# 	else:
	# 		remove_list.append(k)
	# 
	# for r in remove_list:
	# 	del(tile_control[r])
	# 
	# print '<a href="value_map?list=%s">Map</a>' % (":".join(output_list))
	
	return tile_control#, output_list
# 
# def total_influence():
# 	"""Returns a list of tiles that the team has influence on and by how much
# 	it will probably take a while to run"""
# 	city_dict = city.get_city_dict()
# 	team_dict = data.team.get_teams_dict_c()
# 	
# 	terrain_tuples = data.mapper.terrain_tuples()
# 	
# 	tile_control = {}
# 	
# 	# For each city, look at what land they control
# 	for k, c in city_dict.items():
# 		if c.dead or c.secret == 1: continue
# 		if team_dict[c.team].active == 0: continue# Note that IRs affect land control
# 		
# 		# Debug skip stuff
# 		if c.team in [104]:# Skip Seraphim
# 			continue
# 		
# 		city_loc = mod_ten_tuple((c.x, c.y))
# 		min_x = city_loc[0]-rules.city_rules.control_range
# 		max_x = city_loc[0]+rules.city_rules.control_range
# 		
# 		min_y = city_loc[1]-rules.city_rules.control_range
# 		max_y = city_loc[1]+rules.city_rules.control_range
# 		
# 		city_continent = get_tile_continent(city_loc)
# 		# if city_continent < 1: continue
# 		
# 		for x in range(min_x, max_x, 10):
# 			for y in range(min_y, max_y, 10):
# 				tile_loc = (x,y)
# 				
# 				# Ignore water
# 				if tile_loc not in terrain_tuples: continue
# 				if terrain_tuples[tile_loc] == 0: continue
# 				
# 				# Can't control stuff on other continents
# 				tile_continent = get_tile_continent(tile_loc)
# 				if tile_continent != city_continent: continue
# 				
# 				# Default value if it doesn't exist yet
# 				if (x,y) not in tile_control: tile_control[(x,y)] = {}
# 				
# 				tile_range = data.path.pythagoras(city_loc, tile_loc)
# 				control = rules.city_rules.city_control(tile_range, c)
# 				
# 				if control <= 0: continue
# 				
# 				if c.team not in tile_control[(x,y)]:
# 					tile_control[(x,y)][c.team] = 0
# 				tile_control[(x,y)][c.team] += control
# 	
# 	return tile_control
# 
# 
# def new_continent(name, x, y):
# 	query = """INSERT INTO map_continents (name, x, y)
# 		values
# 		('%s', %s, %s);""" % (database.escape(name), x, y)
# 	try: database.cursor.execute(query)
# 	except Exception as e:
# 		raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query))
예제 #3
0
def check_available(cursor, verbose):
	the_world = world.World(cursor)
	
	unit_dict				= the_world.units()
	unit_q.mass_get_unit_equipment(cursor, the_world._units)
	
	equipment_dict			= the_world.equipment()
	city_dict				= the_world.cities()
	terrain_tuples			= mapper_q.get_all_terrain(cursor)
	map_continent_tiles		= path_f.get_map_continent_tiles(cursor)
	
	# First we need to get the continents and terrains of each team
	team_dict = {}
	
	if verbose:
		it = cli_f.progressbar(city_dict.items(), "military_check.check_available: ", 40, with_eta = True)
	else:
		it = city_dict.items()
	
	for city_id, the_city in it:
		if the_city.dead > 0: continue
		if the_city.team not in team_dict: team_dict[the_city.team] = Team_lands(the_city.team)
		
		city_loc = (the_city.x - (the_city.x % 10), the_city.y - (the_city.y % 10))
		
		city_terrain, city_continent = 0, 0
		
		if city_loc in terrain_tuples: city_terrain = terrain_tuples[city_loc]
		if city_loc in map_continent_tiles: city_continent = map_continent_tiles[city_loc]
		
		team_dict[the_city.team].add_city(city_continent, city_terrain)
	
	# Next we'd get the techs too if possible
	
	
	# Now we go through each unit and find out if it's availiable
	not_available			= []
	for unit_id, the_unit in unit_dict.items():
		if the_unit.team < 2: continue# Skip units that are for all teams
		
		# If a team doesn't exist in the dictionary then it's because they've no cities
		if the_unit.team not in team_dict:
			# not_available.append(unit_id)
			continue
		
		checked_unit = False
		the_team = team_dict[the_unit.team]
		
		for e in the_unit.equipment:
			if checked_unit: continue
			the_equipment = equipment_dict[e]
			
			if the_equipment.continent > 0:
				if the_equipment.terrain > 0:
					# Need both
					if not the_team.check_matrix(the_equipment.continent, the_equipment.terrain):
						not_available.append(unit_id)
						checked_unit = True
				else:
					# Just continent
					if the_equipment.continent not in the_team.continents:
						not_available.append(unit_id)
						checked_unit = True
			else:
				if the_equipment.terrain > 0:
					# Just terrain
					if the_equipment.terrain not in the_team.terrain:
						not_available.append(unit_id)
						checked_unit = True
			
			# Now we can check tech level too
				
			# self.add_field("terrain",				"int")# 0 = Any terrain
			# self.add_field("tech",					"int")# 0 = No tech needed
			# self.add_field("tech_level",			"int")# 0 = No tech needed
	
	query = """UPDATE units SET available = True;"""
	try: cursor.execute(query)
	except Exception as e:
		raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query))
	
	if not_available != []:
		query = """UPDATE units
			SET available = False
				WHERE id IN (%s);""" % "".join([str(u) for u in not_available])
		
		try: cursor.execute(query)
		except Exception as e:
			raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query))