Ejemplo n.º 1
0
class Carrier( ftb.Ship.Ship):
    "A Ship subclass that carries a series of launchers carrying other ships/objects" 

    def __init__( self, pShip):
        ftb.Ship.Ship.__init__( self, pShip)
        # TODO: change self.launchers to a Registry
        self.launchers = Registry()

    def AddLauncher( self, launcherName, launcher):
        if( launcherName != None and launcher != None):
            self.launchers.Register( launcher, launcherName)

    def GetLauncher( self, launcherName):
        if( launcherName != None and self.launchers.has_key( launcherName)):
            return self.launchers.GetName( launcherName)

    def GetLaunchers( self):
        return self.launchers

    def GetNumLaunches( self, launchName):
        "Iterates over all of a Carriers launchers and tallies up the number of a particular Launch aboard"
        retval = 0
        if( launchName != None):
            for launcherName in self.launchers._keyList:
                launcher = self.launchers[launcherName]
                retval = retval + launcher.GetNumLaunches( launchName)
        return retval

    def HasMoreLaunches( self, shuttle): 
        return self.GetNumLaunches( shuttle)

    def GetLaunchType( self, launcherName):
        return self.launchers.GetName( launcherName).GetLaunchType()

    def NextLaunchType( self, launcherName):
        return self.launchers.GetName( launcherName).NextLaunchType()
        
    def LaunchShip( self, shuttle, launcherName):
        return self.Launchers.GetName( launcherName).LaunchShip( shuttle)

    def LaunchShip( self, shuttle, launcherIndex):
        return self.Launchers[launcherIndex].LaunchShip( shuttle)
 def GetComplement(self):
     # NOTE: Use of this method considered ugly
     # TODO: Figure out a better way of doing this than computing it here
     totalComplement = Registry()
     for launcher in self.Launchers:
         complement = launcher.GetComplement()
         for key in complement._keyList.keys():
             val = 0
             if (totalComplement._keyList.has_key(key)):
                 val = totalComplement.GetName(key)
             count = complement.GetName(key).count
             updatedVal = val + count
             totalComplement.Register(updatedVal, key)
     return totalComplement
class LauncherGroup:
    "A proxy to manage an arbitrary group of Launchers"

    def __init__(self):
        self.Launchers = Registry()
        self.sLaunchType = None
        self.iCurrentLauncherIdx = 0
        self.iCurrentLaunchIdx = 0
        self.eLaunchMode = SINGLE

    def AddLauncher(self, launcherName, launcher):
        if (launcherName != None and launcher != None):
            self.Launchers.Register(launcher, launcherName)

    def GetLauncher(self, launcherName):
        retval = None
        if( launcherName != None and \
            self.Launcher._keyList.has_key( launcherName)):
            retval = self.Launchers.GetName(launcherName)
        return retval

    def RemoveLauncher(self, launcherName):
        retval = None
        if( launcherName != None and \
            self.Launchers._keyList.has_key( launcherName)):
            retval = self.Launchers.GetName(launcherName)
            self.Remove(launcherName)
        return retval

    def GetLaunchers(self):
        return self.Launchers._arrayList

    def GetNumLaunches(self, launchName):
        retval = 0
        totalComplement = self.GetComplement()
        if (totalComplement._keyList.has_key(launchName)):
            retval = totalComplement.GetName(launchName)
        return retval

    def HasMoreLaunches(self, launchName):
        return self.GetNumLaunches(launchName)

    def SetLaunchMode(self, mode):
        AssertMode(mode)
        self.eLaunchMode = mode

    def GetLaunchType(self):
        if (self.sLaunchType == None):
            totalComplement = self.GetComplement()
            self.sLaunchType = totalComplement._keyList.keys()[0]
        return self.sLaunchType

    def NextLaunchType(self):
        totalComplement = self.GetComplement()
        retval = None
        if (self.sLaunchType == None):
            self.GetLaunchType()
        else:
            keys = totalComplement._keyList.keys()
            startingIdx = keys.index(self.sLaunchType)
            index = startingIdx
            while (1):
                if (len(keys) - 1 == index):
                    index = 0
                elif (index < len(keys) - 1):
                    index = index + 1
                if (totalComplement[keys[index]] > 0):
                    retval = keys[index]
                    self.sLaunchType = retval
                    break
                if (index == startingIdx):
                    # we've cycled the whole dict.  we're out of ships
                    break
        return retval

    def LaunchShip(self, launch):
        "Launch a ship(s) of 'launch' type from a contained Launcher"
        if (len(self.Launchers) > 0):
            if (self.eLaunchMode == SINGLE):
                self.LaunchFromOneLauncher(launch)
            elif (self.eLaunchMode == ALL):
                self.LaunchFromAllLaunchers(launch)

    def LaunchFromOneLauncher(self, launch):
        "Launch a single ship and advance the launcher index"
        launched = 0
        numLaunchers = len(self.Launchers)
        count = 0
        currentIndex = self.iCurrentLaunchIdx
        while (not launched and count < numLaunchers):
            count = count + 1
            complement = self.GetComplement()
            bTimer = complement.GetName(launch) - 1
            launched = \
                self.Launchers[currentIndex].LaunchShip( launch, bTimer)
            if (launched == 1):
                break
            if (currentIndex < len(self.Launchers) - 1):
                currentIndex = currentIndex + 1
            else:
                currentIndex = 0
        self.iCurrentLaunchIdx = currentIndex

    def LaunchFromAllLaunchers(self, launch):
        "Try to launch a ship of 'launch' type from all launchers"
        for launcher in self.Launchers:
            complement = self.GetComplement()
            bTimer = complement.GetName(launch) - 1
            launcher.LaunchShip(launch, bTimer)

    def GetComplement(self):
        # NOTE: Use of this method considered ugly
        # TODO: Figure out a better way of doing this than computing it here
        totalComplement = Registry()
        for launcher in self.Launchers:
            complement = launcher.GetComplement()
            for key in complement._keyList.keys():
                val = 0
                if (totalComplement._keyList.has_key(key)):
                    val = totalComplement.GetName(key)
                count = complement.GetName(key).count
                updatedVal = val + count
                totalComplement.Register(updatedVal, key)
        return totalComplement

    def Equals(self, other):
        "KLUGE: Compares the launchers in this group to a passed in launcher"
        retval = 0
        for launcher in self.Launchers:
            if launcher == other:
                retval = 1
                break
        return retval
Ejemplo n.º 4
0
class Launcher:
    "A proxy for a ship's subsystem capable of launching an object"

    # pSystem   - The Hull System (targetable and destroyable) component of
    #             our launcher
    # pProperty - The ObjectEmitterProperty for the launcher
    def __init__(self, pSystem, pProperty, pShip):
        # TODO: Ensure compatability with LaunchGroup
        self.pHullSystem = pSystem
        self.pOEPProperty = pProperty
        self.Complement = Registry()
        self.bClearToLaunch = 1
        self.fLaunchInterval = 2.0
        self.sLaunchType = None
        self.pShip = pShip

    def AddLaunchable( self, \
                       launchScriptName, \
                       aiModuleName, \
                       numberOfLaunch, \
                       commandable = 1):
        if (launchScriptName != None and numberOfLaunch >= 0):
            launchable = Launchable(aiModuleName, numberOfLaunch, commandable)
            self.Complement.Register(launchable, launchScriptName)

    def RemoveLaunchable(self, launchScriptName, numberOfLaunch):
        if launchScriptName != None and \
           numberOfLaunch >= 0:
            for launchType in self.Complement:
                if launchType == launchScriptName:
                    self.Complement.Remove(launchScriptName)

    def GetComplement(self):
        return self.Complement

    def GetNumLaunches(self, launch):
        "Returns the number of Launches remaining of the requested type"
        retval = 0
        if (self.Complement._keyList.has_key(launch)):
            retval = self.Complement.GetName(launch).count
        return retval

    def HasMoreLaunches(self, launch):
        return self.GetNumLaunches(launch)

    def SetLaunchInterval(self, interval):
        self.fLaunchInterval = interval

    def SetClearToLaunch(self, clear):
        "Sets this Launcher's semaphore to allow a Launch to deply"
        self.bClearToLaunch = clear

    def GetLaunchType(self):
        "Get the current Launch type."
        if (self.sLaunchType == None):
            keys = self.Complement._keyList.keys()
            for type in keys:
                if (self.Complement.GetName(type) > 0):
                    self.sLaunchType = type
        return self.sLaunchType

    def NextLaunchType(self):
        "Cycle to the next Launch type"
        retval = None
        if (self.sLaunchType == None):
            retval = self.GetLaunchType()
        else:
            keys = self.Complement._keyList.keys()
            startingIdx = keys.index(self.sLaunchType)
            index = startingIdx
            while (1):
                if (len(keys) - 1 == index):
                    index = 0
                elif (index < len(keys) - 1):
                    index = index + 1
                if (index == startingIdx):
                    # we've cycled the whole dict.  we're out of ships
                    break
                elif (self.Complement[index] > 0):
                    retval = keys[index]
                    self.sLaunchType = retval
        return retval

    def LaunchShip(self):
        return self.LaunchShip(self.sLaunchType)

    def LaunchShip(self, launch, bTimer=None):
        "Decrement our count of this type of Launch and then launch an instance of this type of Launch"
        retval = 0
        if self.bClearToLaunch == 1 and \
           self.Complement._keyList.has_key( launch) and \
           self.Complement.GetName( launch) > 0:
            self.bClearToLaunch = 0
            launchable = self.Complement.GetName(launch)
            launchCount = launchable.count - 1
            if (bTimer == None):
                bTimer = launchCount
            self.Complement.Register( Launchable( launchable.aiModuleName, \
                                                  launchCount), \
                                      launch)
            LaunchAIShip( self.pShip, \
                          self.pOEPProperty, \
                          self.pHullSystem, \
                          launch, \
                          self.fLaunchInterval, \
                          launchable.aiModuleName, \
                          launchable.commandable, \
                          bTimer)
            retval = 1
        return retval

    def Equals(self, other):
        retval = 0
        if self.GetComplement() == other.GetComplement():
            retval = 1
        return retval