def EnterSet(pObject, pEvent):
	pObject.CallNextHandler(pEvent)
	
	pShip = App.ShipClass_Cast(pEvent.GetDestination())
	if pShip:
		pSet = pShip.GetContainingSet()
		if not pSet or pSet.GetName() == "warp":
			return
		
		sIncomingShipRace = GetRaceFromShip(pShip)
		
		lStationaryShipsInSet = GetStationaryShipsIn(pShip.GetContainingSet())
		lRaceWarningDone = []
		for pStation in lStationaryShipsInSet:
			sStationRace = GetRaceFromShip(pStation)
			if sStationRace != sIncomingShipRace and Races.has_key(sStationRace) and not Races[sStationRace].IsFriendlyRace(sIncomingShipRace) and Distance(pStation, pShip) < App.UtopiaModule_ConvertKilometersToGameUnits(MAX_DETECT_DISTANCE) and sStationRace not in lRaceWarningDone:
				WarnRaceForIncoming(sStationRace, pShip, sIncomingShipRace)
				lRaceWarningDone.append(sStationRace)
def getSurrenderChanceAgainst(pGroup, pSet):
        debug(__name__ + ", getSurrenderChanceAgainst")
        lpGroup = pGroup.GetActiveObjectTupleInSet(pSet)
        lRaces = []
        numRaces = 0
        fCurSurrenderChance = 0.0
        for ship in lpGroup:
                pShip = App.ShipClass_Cast(ship)
                sCurRace = GetRaceFromShip(pShip)
                if not sCurRace in lRaces:
                        if Races.has_key(sCurRace):
                                lRaces.append(sCurRace)
                                numRaces = numRaces + 1
                                fCurSurrenderChance = fCurSurrenderChance + Races[sCurRace].GetPeaceValue()
        
        if numRaces == 0:
                return 1.0
        return fCurSurrenderChance / numRaces
def ObjectKilledHandler(pObject, pEvent):
	debug(__name__ + ", ObjectKilledHandler")
	pObject.CallNextHandler(pEvent)
	
	pShip = App.ShipClass_Cast(pEvent.GetDestination())
	if pShip and pShip.GetShipProperty().IsStationary():
		sRace = GetRaceFromShip(pShip)
		sSystemName = GetSystemShortName(pShip.GetContainingSet())
		
		lStationaryShipsInSet = GetStationaryShipsIn(pShip.GetContainingSet())
		AnotherStationOfThisRaceInSystem = 0
		for pStation in lStationaryShipsInSet:
			sStationRace = GetRaceFromShip(pStation)

			if sStationRace == sRace and not pStation.IsDead() and not pStation.IsDying():
				AnotherStationOfThisRaceInSystem = 1
				break
				
		if Races.has_key(sRace) and not AnotherStationOfThisRaceInSystem:
			Races[sRace].RemoveSystem(sSystemName)
def EmergAbandon(pObject, pEvent):
	debug(__name__ + ", EmergAbandon")
	pObject.CallNextHandler(pEvent)

        # Get the ship that was hit
        pShip = App.ShipClass_Cast(pEvent.GetDestination())
	sRace = GetRaceFromShip(pShip)
	
	# don't do anything for the player
	if pShip and not pShip.IsDead() and not pShip.IsDying() and pShip.GetAI() and sRace and not pShip.IsPlayerShip() and not pShip.GetObjID() in lAbandonDone:
		if pShip.GetHull() and pShip.GetHull().GetConditionPercentage() < 0.2:
			# always add them to the list, doesn't matter if we really did
			lAbandonDone.append(pShip.GetObjID())
			if not Races.has_key(sRace):
				return
			fPeaceValue = Races[sRace].GetPeaceValue()
			# use the peace value of the race to determine chance of abandon ship
			# also add a success quote of 30%
			if chance(int(fPeaceValue * 100)) and chance(iAbandonShipSuccessQuote):
				StartAbandonShip(pShip)
def SurrenderAI(pAttacker, pDamagedShip, fStatus):
        debug(__name__ + ", SurrenderAI")
        global lSurrenderDone
        
        sDamagedRace = GetRaceFromShip(pDamagedShip)
        if not Races.has_key(sDamagedRace):
                return
        iDamagedShipPeaceVal = Races[sDamagedRace].GetPeaceValue() * 100
        sAttackerGroup = getGroupFromShip(pAttacker.GetName())
        sDamagedGroup = getGroupFromShip(pDamagedShip.GetName())
        pAttackerGroup = getGroup(sAttackerGroup)
        pSet = pDamagedShip.GetContainingSet()
        fRaceSurrenderChance = getSurrenderChanceAgainst(pAttackerGroup, pSet)
        iSurrenderChance = int(((((1-fStatus) + fRaceSurrenderChance) / 2.0) * 100))
        
        # check change side
        if not SurrenderLock and not pDamagedShip.IsDead() and not pDamagedShip.IsDying() and pDamagedShip.GetAI() and GetFleetDamage(sDamagedRace, sDamagedGroup, pSet) and RaceCanChangeSide(sDamagedRace) and chance(iChangeSideChance):
                LockSurrender()
                lSurrenderDone.append(pDamagedShip.GetObjID())
                addShipsOfGroupAndSameRaceFromSetToGroup(sDamagedRace, pSet, sDamagedGroup, sAttackerGroup)
                if sDamagedRace == "Cardassian":
                        Say("CardassianChangeSide", "Helm", "data/TGL/Surrender.tgl")
                else:
                        Say("ChangeSide", "Helm", "data/TGL/Surrender.tgl")
        
        elif not SurrenderLock and not pDamagedShip.IsDead() and not pDamagedShip.IsDying() and pDamagedShip.GetAI() and chance(iSurrenderChance) and chance(80) and not pDamagedShip.GetObjID() in lSurrenderDone and chance(iSurrenderChance) and chance(iDamagedShipPeaceVal):
                LockSurrender()
                lSurrenderDone.append(pDamagedShip.GetObjID())
                pGroup = getGroup(sDamagedGroup)
                pNeutralGroup = getGroup("neutral")
                if pGroup:
                        RemoveNameFromGroup(pGroup, pDamagedShip.GetName())
                        if not pNeutralGroup.IsNameInGroup(pDamagedShip.GetName()):
                                pNeutralGroup.AddName(pDamagedShip.GetName())
                        pDamagedShip.ClearAI()
                CheckFleetFollowSurrender(sDamagedRace, sDamagedGroup, pSet)
                Say("Surrender", "Helm", "data/TGL/Surrender.tgl")
                SetAlertLevel(pDamagedShip, "green")
	else:
        	# lock it for a random time
        	LockSurrender(App.g_kSystemWrapper.GetRandomNumber(10))