Ejemplo n.º 1
0
	def add_fleet(self, fleet):
		nm = ''
		if 'arrival_turn' in fleet:
			nm = '[%d] '%(fleet['arrival_turn'] - store.max_turn(),)
		nm += fleet['name']
		cp = wx.CollapsiblePane(self, label=nm, style=wx.CP_DEFAULT_STYLE|wx.CP_NO_TLW_RESIZE)
		self.sizer.Add(cp)
		pane = cp.GetPane()
		
		u = store.get_user(fleet['user_id'])
		if u:
			owner_name = u['name']
		else:
			owner_name = '<unknown>'
		sizer = wx.BoxSizer(wx.VERTICAL)
		sizer.Add(wx.StaticText(pane, label='owner: %s'%(owner_name,)), 1, wx.EXPAND)

		speed, rng = store.get_fleet_speed_range(fleet['fleet_id'])
		if speed and rng:
			sizer.Add(wx.StaticText(pane, label='%0.2f / %0.2f'%(speed, rng)), 1, wx.EXPAND)

		#can we contrl the fleet?
		is_controlled = 'login' in u and u['login'] in config.users
		
		if is_controlled and rng >= 1 and (not 'in_transit' in fleet or fleet['in_transit']==0):
			jump_button = wx.ToggleButton(pane, label='jump')
			jump_button.fleet_id = fleet['fleet_id']
			sizer.Add(jump_button, 1, wx.EXPAND)
			
			self.Bind(wx.EVT_TOGGLEBUTTON, self.on_jump, jump_button)
		#else:
		#	print 'user %s not controllable'%(u,)
		
		for unit in store.get_fleet_units(fleet['fleet_id']):
			hbox = wx.BoxSizer(wx.HORIZONTAL)
			sizer.Add(hbox, 1, wx.EXPAND)
			
			proto = store.get_object('proto', {'proto_id':unit['proto_id']})
			if not proto:
				print 'Prototype not found for %s'%(unit,)
				continue
			obj_carp = int(unit['proto_id']), int(proto['carapace']), int(proto['color'])
			
			if is_controlled:
				planet = store.get_object('planet', {'x':self.coord[0], 'y':self.coord[1]})
				inhabited = False
				if planet and planet['user_id'] and int(planet['user_id']) > 0:
					inhabited = True
					
				# get unit actions ( colony, kill-people )
				if not inhabited:
					for action_type in [action.Action.COLONY_COLONISE, action.Action.ARC_COLONISE, action.Action.OUTPOST_COLONISE]:
						action_colonize = store.get_object('proto_action', {'proto_id':proto['proto_id'], 'proto_action_id':action_type})
						if action_colonize:
							c_action = store.get_object('action', {'unit_id':unit['unit_id']})
							if c_action:
								# already colonizing
								cancel_button = wx.Button(pane, label='Cancel colonize %s'%(action.get_colony_population(action_type)))
								cancel_button.cancel_action = u['user_id'], unit['unit_id'], c_action['cancel_id']
								self.Bind(wx.EVT_BUTTON, self.on_cancel_action, cancel_button)
								sizer.Add( cancel_button , 1, wx.EXPAND )
							else:
								colonize_button = wx.Button(pane, label='Colonize %s'%(action.get_colony_population(action_type)))
								colonize_button.action = action_type, unit['unit_id'], fleet['fleet_id'], self.coord, u['user_id']
								self.Bind(wx.EVT_BUTTON, self.on_store_action, colonize_button)
								sizer.Add( colonize_button , 1, wx.EXPAND )

				if inhabited and planet['user_id'] != u['user_id']:
					#TODO: check if our mult, or ally, and notify user about it
					action_kill_people = store.get_object('proto_action', {'proto_id':proto['proto_id'], 'proto_action_id':action.Action.KILL_PEOPLE})
					if action_kill_people:
						c_action = store.get_object('action', {'unit_id':unit['unit_id']})
						if c_action:
							# already colonizing
							cancel_button = wx.Button(pane, label='Cancel kill people')
							cancel_button.cancel_action = u['user_id'], unit['unit_id'], c_action['cancel_id']
							self.Bind(wx.EVT_BUTTON, self.on_cancel_action, cancel_button)
							sizer.Add( cancel_button , 1, wx.EXPAND )
						else:
							colonize_button = wx.Button(pane, label='Kill people')
							colonize_button.action = action.Action.KILL_PEOPLE, unit['unit_id'], fleet['fleet_id'], self.coord, u['user_id']
							self.Bind(wx.EVT_BUTTON, self.on_store_action, colonize_button)
							sizer.Add( colonize_button , 1, wx.EXPAND )

			img = image.get_image( int(unit['proto_id']), int(proto['carapace']), int(proto['color']) )
			
			if img:
				bitmap = wx.StaticBitmap(pane)
				bitmap.SetBitmap(img)
				hbox.Add(bitmap, 1, wx.EXPAND)
			#else:
			#	print 'image not found for unit %s, bc %s, carp %s, color %s'%(unit['unit_id'], int(unit['proto_id']), int(proto['carapace']), int(proto['color']) )

			name = proto['name']
			if not name:
				name = get_unit_name(int(proto['carapace']))
			hbox.Add(wx.StaticText(pane, label=name), 1, wx.EXPAND)

		border = wx.BoxSizer()
		border.Add(sizer, 1, wx.EXPAND|wx.ALL)
		pane.SetSizer(border)
		
		self.sizer.Layout()
		
		self.Bind(wx.EVT_COLLAPSIBLEPANE_CHANGED, self.OnPaneChanged, cp)
		cp.Expand()
		
		cp.Bind(wx.EVT_LEFT_DOWN, self.onFleetSelect)
		self.fleets[cp] = fleet
Ejemplo n.º 2
0
	def onExploreGeoAll(self, _):
		'upload pending events on server'
		
		explore_owned_planets = True
		
		out_dir = os.path.join(util.getTempDir(), config.options['data']['raw-dir'])
		util.assureDirClean(out_dir)
		for acc in config.accounts():
			if not 'id' in acc:
				continue
			user_id = int(acc['id'])
				
			log.info('requesting user %s info'%(acc['login'],))
			# find if there is any explore-capable fleets over unexplored planets
			# or simply tell explore to every unit =))) game server will do the rest
			
			#1. find all fleets above empty planets
			
			# get fleet position, check if planet geo is unknown
			fleet_planet = {}
			pl = {}
			
			for fleet in store.iter_objects_list('fleet', {'user_id':acc['id']} ):
				#print 'got fleet %s'%(fleet,)
				coord = get_coord(fleet)
				
				if coord in pl:
					pl[coord].add(fleet['fleet_id'])
					continue

				planet = store.get_object('planet', {'x':coord[0], 'y':coord[1]})
				#check holes and stars
				if not planet:
					continue
				
				# check if already explored
				if 'o' in planet and planet['o']:
					continue
				
				if not coord in pl:
					pl[coord] = set()
				pl[ coord ].add(fleet['fleet_id'])
				#print 'Add to exploration list planet %s'%(planet,)
			
			acts = {}
			
			# get all fleet units, check if explore capable
			for coord, planet_fleets in pl.iteritems():
				for fleet_id in planet_fleets:
					for unit in store.get_fleet_units(fleet_id):
						#print '%s %s unit %s'%(coord, fleet_id, unit)
						# ok unit
						bc = unit['proto_id']
						
						#for proto in db.prototypes(['id=%s'%(bc,)]):
						#	print 'proto %s'%(proto,)

						#type 1 probably geo explore
						action_geo_explore = store.get_object('proto_action',{'proto_id':bc, 'proto_action_id':request.RequestMaker.GEO_EXPLORE})
						if action_geo_explore:
							acts[coord] = unit['unit_id']
							break
					# no need to request more then one explore of a single planet
					if coord in acts:
						break

			self.pending_actions.user_id = int(acc['id'])
			#self.pendingActions[int(acc['id'])] = actions
			#hw_planet = db.getUserHw(acc['id'])
			#actions.createNewFleet(hw_planet, 'a_new_shiny_fleet')
			
			at_least_one = False
			for coord, unit_id in acts.iteritems():
				fleet_unit = store.get_object('fleet_unit', {'unit_id':unit_id})
				self.actions.add_action(action.ActionStore(user_id, unit_id, fleet_unit['fleet_id'], coord, action.Action.GEO_EXPLORE))
				#self.pending_actions.explore_planet( coord, unit_id )
				at_least_one = True
			
			if at_least_one:
				self.perform_actions()
Ejemplo n.º 3
0
	def onSendScouts(self, _):
		min_size = 70
		max_size = 99
		
		#helps avoid flying on the same planet with different accounts
		friend_geo_scout_ids = []
		for user in db.users():
			friend_geo_scout_ids.append(str(user['id']))
		
		for acc in config.accounts():
			user_id = int(acc['id'])

			if self.command_selected_user and user_id != self.map.selected_user_id:
				continue

			self.pending_actions.user_id = user_id
			#log.info('send scouts %s size %s'%(acc['login'], min_size))
					
			# find units that can geo-explore
			# find the ones that are already in fleets in one of our planets
			
			fly_range = 0.0
			ready_scout_fleets = {}
			# get all fleets over our planets
			for planet in store.iter_objects_list('open_planet', {'user_id':user_id}):
				#print 'open planet %s'%(planet,)
				coord = get_coord(planet)
				for fleet in store.iter_objects_list('fleet', {'user_id':user_id, 'x':coord[0], 'y':coord[1]}):
					if value_in(self.exclude_fleet_names, fleet['name']):
						continue
					units = store.get_fleet_units(fleet['fleet_id'])
					if len(units) != 1:
						#print 'fleet %s has wrong units count ( != 1 ) %s, skipping it'%(fleet, units)						
						continue
					unit = units[0]
					if int(unit['unit_id']) in self.manual_control_units:
						#print 'unit %s reserved for manual control, skipping it'%(unit,)
						continue

					if not self.is_geo_scout(unit):
						#print 'unit %s is not geo-scout, skipping it'%(unit,)
						continue
					#fly_range = max(fly_range, )
					#print 'unit %s on planet %s for fleet %s is geo-scout'%(unit, coord, fleet)
					# ok, this is geo-scout, single unit in fleet, on our planet
					#ready_scout_fleets.append((coord, fleet))
					_,r = store.get_fleet_speed_range(fleet['fleet_id'])
					fly_range = max(fly_range, r)
					ready_scout_fleets.setdefault(coord, []).append((fleet, r))
					
			
			# get possible planets to explore in nearest distance
			for coord in ready_scout_fleets.keys():
				#print 'load geo size centered at %s with range %s'%(coord, int(fly_range))
				save_load.load_geo_size_center(coord, int(fly_range))
			
			# jump to nearest/biggest unexplored planet
			exclude = set()
			for coord, fleets in ready_scout_fleets.iteritems():
				max_fly_range = 0
				for f,r in fleets:
					max_fly_range = max(max_fly_range, r)
				
				possible_planets = []
				#s<=99 - skip stars
				#dx = lt[0]-fly_range, lt[0]+fly_range
				#dy = lt[1]-fly_range, lt[1]+fly_range
				for p in store.iter_planets_size(pos=coord, fly_range=max_fly_range, size_min=min_size, bigger_first = True):
					if not (p['s']>=min_size and p['s']<=max_size):
						#print 'planet %s not fit the size'%(p,)
						continue
					dest = get_coord(p)
					if dest in exclude:
						continue
					dist = util.distance(dest, coord)
					if dist > fly_range:
						#print 'planet %s is too far away'%(p,)
						continue
						
					planet = db.get_planet(dest)
					if planet and 'o' in planet and planet['o']:
						#print 'planet %s already explored'%(p,)
						continue
					
					has_flying_geo_scouts = False
					
					# check if currently there is some explore fleet on planet, or explore fleet already fly there
					already_has_scout = False
					for fleet in store.iter_objects_list('fleet', {'x':dest[0], 'y':dest[1]}, controlled = True):
						if self.is_geo_scout_fleet(fleet['fleet_id']):
							already_has_scout = True
							break
					if already_has_scout:
						#print 'planet %s has scount fleet on it'%(p,)
						continue
						
					already_fly_geo_scouts = False
					for fleet in store.iter_objects_list('flying_fleet', {'x':dest[0], 'y':dest[1]}, controlled = True):
						if self.is_geo_scout_fleet(fleet['fleet_id']):
							already_fly_geo_scouts = True
							#self.actions.add_action( action.ActionGeoExplore(user_id, ':dest, 'fleet_id':fleet['fleet_id']}))
							break
					if already_fly_geo_scouts:
						#print 'planet %s has flying scount fleet'%(p,)
						continue

					possible_planets.append( (dist, dest) )
					#print 'add possible planet %s'%(dest,)

				for fleet, fleet_range in fleets:
					#print 'check planets for fleet %s'%(fleet,)
					for dist, planet in sorted(possible_planets):
						if dist > fleet_range:
							#print 'planet %s too scary'%(p,)
							continue
						# ok fly to it
						self.actions.add_action( action.ActionJump(user_id, fleet['fleet_id'], planet ))
						#self.pending_actions.fleetMove(fleet['id'], planet)
						exclude.add( planet )
						#print 'jump %s from %s to %s'%(fleet, coord, planet)
						possible_planets.remove( (dist, planet ) )
						break
Ejemplo n.º 4
0
	def onFlyHomeScouts(self, _):
				
		for acc in config.accounts():
			if not 'id' in acc:
				continue
			user_id = int(acc['id'])
			self.pending_actions.user_id = user_id
			#print 'fly home scouts for user %s %s'%(user_id, acc['login'])
							
			# fly scouts back to base
			
			fleets = []
			fleet_flt = ['owner_id=%s'%(user_id,)]
			
			fleet_name = None #unicode('Fleet')
			# can also filter fleets by names
			#TODO: beware escapes
			if fleet_name:
				fleet_flt.append( 'name="%s"'%(fleet_name,) ) 
			for fleet in store.iter_objects_list('fleet', {'user_id':user_id}):
				#print 'found fleet %s'%(fleet,)
				# if fleet over empty planet - jump back home
				coord = get_coord(fleet)
				coord_filter = {'x':coord[0], 'y':coord[1]}
				planet = store.get_object('planet', coord_filter )
				#TODO: allow jump on all open-planets ( not only owned by user )
				coord_filter['user_id'] = user_id
				user_open_planet = store.get_object('open_planet', coord_filter )
				if user_open_planet:
					continue

				#print 'fleet %s not at home'%(fleet['id'],)
				units = []
				for unit in store.get_fleet_units(fleet['fleet_id']):
					#print 'fleet %s has unit %s'%(fleet['id'], unit)
					units.append(unit)
				
				# not a scout fleet if more then one unit in fleet
				# if zero units - don't care about empty fleet as well
				if len(units) != 1:
					#print 'fleet %s has %s units, while required 1'%(fleet['id'], len(units))
					continue

				if int(units[0]['unit_id']) in self.manual_control_units:
					continue

				proto = store.get_object('proto', {'proto_id':units[0]['proto_id']})
				if proto['carapace'] != CARAPACE_PROBE:
					#print 'fleet %s unit %s is not a probe'%(fleet['id'], units[0])
					continue

				#jump back
				#print 'fleet %s %s needs to get home'%(coord, fleet)
				fleets.append( (coord, fleet) )					

			if not fleets:
				#print 'no scout fleets found not at home'
				continue
			
			coords = []
			for planet in store.iter_objects_list('open_planet', {'user_id':user_id}):
				coord = get_coord(planet)
				coords.append( coord )
				#print 'possible home planet %s'%(coord,)
				
			if coords == None or fleets == []:
				#print 'oops %s %s'%(coords, fleets)
				continue
			
			#print 'looking for best route for %s fleets' %(len(fleets,),)
			for coord, fleet in fleets:
				#find closest planet
				closest_planet = coords[0]
				min_distance = util.distance(coords[0], coord)
				for c in coords:
					if util.distance(coord, c) < min_distance:
						min_distance = util.distance(coord, c)
						closest_planet = c
				
				# ok, found then jump
				#print 'Jump (%s) %s'%(closest_planet, fleet)
				
				self.actions.add_action( action.ActionJump(user_id, fleet['fleet_id'], closest_planet ))
Ejemplo n.º 5
0
	def is_geo_scout_fleet(self, fleet_id):
		for u in store.get_fleet_units(fleet_id):
			if self.is_geo_scout(u):
				return True
		return False