Esempio n. 1
0
	def update(self, tran, obj):
		if not (hasattr(obj,'customname')): #added in 0.5.64
			obj.customname = None
			obj.allowmerge = 1
		# if there are no ships -> disband fleet
		if not len(obj.ships) or obj.owner == OID_NONE:
			log.warning(obj.oid, "FLEET - no ships in the fleet -- disbanding")
			self.cmd(obj).disbandFleet(tran, obj)
			return
		# check for duplicates (TODO: remove me, bug was fixed)
		#for ship1 in obj.ships:
		#	duplicates = 0
		#	for ship2 in obj.ships:
		#		if ship1 is ship2:
		#			duplicates += 1
		#	if duplicates != 1:
		#		# regenerate ships
		#		newShips = []
		#		for designID, hp, shield, exp in obj.ships:
		#			newShips.append([designID, hp, shield, exp])
		#		obj.ships = newShips
		#		raise ServerException("Ship duplicates in %s" % obj)
		#
		obj.origScannerPwr = 0
		obj.operEn = 0
		obj.operProd = 0.0
		obj.maxEn = 0
		obj.maxSpeed = 999999.9
		obj.combatPwr = 0
		obj.isMilitary = 0
		#ships = {}
		# find
		player = tran.db.get(obj.owner, None)
		if not player or player.type not in PLAYER_TYPES or obj.oid not in player.fleets:
			# disband fleet when owner is invalid
			log.warning(obj.oid, "Disbanding fleet - invalid owner", obj)
			self.cmd(obj).disbandFleet(tran, obj)
			return
		obj.signature = 0
		remove = []
		idx = 0
		for designID, hp, shield, exp in obj.ships:
			if designID in player.shipDesigns:
				tech = player.shipDesigns[designID]
				obj.origScannerPwr = max(tech.scannerPwr, obj.origScannerPwr)
				obj.operEn += tech.operEn
				obj.operProd += tech.buildProd * Rules.operProdRatio
				obj.maxEn += tech.storEn
				obj.maxSpeed = min(obj.maxSpeed, tech.speed)
				obj.signature += tech.signature
				obj.combatPwr += int(tech.combatPwr * float(hp + shield) / (tech.maxHP + tech.shieldHP))
				obj.isMilitary = obj.isMilitary or tech.isMilitary
				#ships[tech.signature] = ships.get(tech.signature, 0) + 1
				if obj.ships[idx][1] > tech.maxHP:
					log.debug(obj.oid, "Too high maxHP for ship, player", obj.owner)
					obj.ships[idx][1] = min(obj.ships[idx][1], tech.maxHP)
			else:
				# TODO track this problem
				log.warning("Player has not this designID", player.oid, designID)
				remove.append([designID, hp, shield, exp])
			idx += 1
		# delete ships intended for removal
		for shipSpec in remove:
			obj.ships.remove(shipSpec)
		# misc
		obj.signature = min(obj.signature, Rules.maxSignature)
		obj.signature = max(obj.signature,1) #require fleet signature to be at least 1 now that we removed that from a per-ship basis
		obj.speed = obj.maxSpeed
		# storage
		obj.storEn = min(obj.storEn, obj.maxEn)
		# sort ships only when there is no combat
		# this prevents resorting fleets in combat
		if obj.combatCounter == 0:
			obj.ships = ShipUtils.sortShips(obj.ships)
		else:
			log.debug("Skipping ship (re)sorting [fleet in combat]", obj.oid)
		# closest system
		if not tran.db.has_key(obj.closeSystem) or tran.db[obj.closeSystem].type not in (T_SYSTEM, T_WORMHOLE):
			if obj.orbiting == OID_NONE:
				log.debug("No close system for fleet", obj.oid)
				# select any system
				systemID = tran.db[tran.db[OID_UNIVERSE].galaxies[0]].systems[0]
				obj.closeSystem = systemID
				log.debug(obj.oid, "Setting NULL close system to", systemID)
			else:
				log.debug(obj.oid, "Generating close system from orbiting", obj.orbiting)
				obj.closeSystem = obj.orbiting
			system = tran.db[obj.closeSystem]
			if obj.oid not in system.closeFleets:
				system.closeFleets.append(obj.oid)
		# verify close system
		if tran.db.has_key(obj.closeSystem):
			system = tran.db[obj.closeSystem]
			if system.type in (T_SYSTEM, T_WORMHOLE):
				if obj.oid not in system.closeFleets:
					log.debug("Adding fleet", obj.oid, "into closeFleets of", system.oid)
					system.closeFleets.append(obj.oid)
			else:
				log.debug(obj.oid, "Close system is not a system")
				obj.closeSystem = OID_NONE
		else:
			log.debug(obj.oid, "Close system does not exists")
			obj.closeSystem = OID_NONE
		# compute scanner pwr
		if obj.closeSystem:
				system = tran.db[obj.closeSystem]
				emrLevel = tran.db[system.compOf].emrLevel
				obj.scannerPwr = int(obj.origScannerPwr * (2.0 - emrLevel))
		# replace obsolete commands
		for actionTuple in obj.actions[:]:
			try:
				action, target, actionData = actionTuple
			except:
				log.warning(obj.oid, "Removing action", actionTuple)
				obj.actions.remove(actionTuple)
		index = 0
		for action, target, actionData in obj.actions:
			if action >= 2 and action <= 100:
				# this is an old action -> replace it by move command if available
				if target != OID_NONE:
					log.debug(obj.oid, "Replacing action", action, "by action MOVE")
					obj.actions[index][0] = FLACTION_MOVE
				else:
					# replace by none action
					log.debug(obj.oid, "Replacing action", action, "by action NONE")
					obj.actions[index] = (FLACTION_NONE, None, None)
			if action == FLACTION_DEPLOY and actionData not in player.shipDesigns:
				# deployment of scrapped ship
				log.debug(obj.oid, "invalid ship to deploy")
				obj.actions[index] = (FLACTION_NONE, None, None)
			index += 1