Scripts:Aircraft Only

From BF2 Technical Information Wiki
Jump to navigation Jump to search

Purpose

Makes playing outside of an aircraft rather pointless, as you only get 25 seconds before you start to take damage. This was needed as an admin script on the Coop Flight School server, as there are limited slots, and players on the ground treating it like a normal server were using slots intended for learner pilots.

Could be adapted for use with other vehicles, if for example you wanted to have tank battles or DPV races.

Installation

Requires Modmanager.

Save the following to a file named mm_aironly.py in the [bf2server]/admin/modules/ folder, and add a line to enable it in modmanager.con (usually found in [bf2server]/mods/bf2/settings/).

The Code

"""Aircraft Only module.

===== About =====
This ModManager script prevents players from playing as infantry or in ground vehicles,
by causing damage to players who exit the vehicles they're spawned in.  Players are given
a warning on exiting, then 25 seconds later they start to receive damage on a 5 second
interval, with further warnings.  If they get back into the vehicle then the damage stops.
If they try to get round it by standing near a resupply/crate/medic then the damage rate
will increase to counter the healing.

===== Config =====
 Enter the allowed classes in the RequiredTypes list below

===== History =====

 v2.1 - 20080507:
 v2 bugfix

 timers are per player to fix bug from sharing that would punish at wrong time/crash server (no OOP experience before this script)

 vehicle parent added to allow gunners/copilots/passengers (still gives warning when switching seats though)

 damage switched from 10/2s to 25/5s and delay upped to 20s from 25s

 everything reset correctly at round end

===== Credit =====

By polarity, loosely based on vehicle class limit
module by graag42, and other scripts from bf2tech.org

Available from bf2tech.org and flightschool.polar-lights.org.uk
You're free to use and modify this script
"""

import bf2
import host
import mm_utils
from bf2.stats.constants import *

# Set the version of your module here
__version__ = 2.1

# Set the required module versions here
__required_modules__ = {
    'modmanager': 1.0
}

# Does this module support reload ( are all its reference closed on shutdown? )
__supports_reload__ = True

# Set the description of your module here
__description__ = "VehicleClassOnly v%s" % __version__

#Required types are safe to be in
RequiredTypes = [VEHICLE_TYPE_HELICOPTER, VEHICLE_TYPE_AVIATOR]       

class NoWalk( object ):

    def __init__( self, modManager ):

        self.mm = modManager
        self.__state = 0
       
    def onExitVehicle( self, player, vehicle):
        if self.__state == 1:
            if ( (not player.isAIPlayer()) & player.isAlive()): #exited vehicle intentionally
                if not player.updateTimer:
                    self.StartTimer( player )
               
    def onEnterVehicle( self, player, vehicle, freeSoldier):
        if self.__state == 1:
            if (not player.isAIPlayer()):
                vehicleType = getVehicleType(vehicle.templateName)
                try:
                    parentVehicle = vehicle.getParent()
                    parentType = getVehicleType(parentVehicle.templateName)
                except:
                    parentType = 12
                if ( vehicleType in RequiredTypes ) | ( parentType in RequiredTypes ):
                    if player.updateTimer:
                        self.ResetPlayer( player )
                else:
                    if not player.updateTimer:
                        self.StartTimer( player )

    def onPlayerConnect(self, player ):
        if self.__state == 1:
            if (not player.isAIPlayer()):
                player.updateTimer = None
                self.ResetPlayer( player )

    def onPlayerSpawn(self, player, body):
        if self.__state == 1:
            if (not player.isAIPlayer()):
                self.ResetPlayer( player )
                bf2.Timer(self.PostSpawnTest, 1, 1, player )

    def PostSpawnTest(self, player):
        if 1 != self.__state:
            return 0
        if (not player.isAIPlayer()):
            vehicle = player.getVehicle()
            vehicleType = getVehicleType(vehicle.templateName)
            try:
                parentVehicle = vehicle.getParent()
                parentType = getVehicleType(parentVehicle.templateName)
            except:
                parentType = 12
            if ( vehicleType in RequiredTypes ) | ( parentType in RequiredTypes ):           
                if player.updateTimer:
                    self.ResetPlayer( player )
            else:
                if not player.updateTimer:
                    self.StartTimer( player )

    def onPlayerDeath(self, player, body):
        if self.__state == 1:
            if (not player.isAIPlayer()):
                self.ResetPlayer( player )

    def StartTimer( self, player ):
        if 1 != self.__state:
            return 0
        mm_utils.msg_player( player.index, "Fighting on the ground is not permitted in this server." )
        mm_utils.msg_player( player.index, "You must enter an aircraft within 25 seconds to avoid punishment." )
        player.updateTimer = bf2.Timer(self.onUpdate, 25, 1, player )
        player.updateTimer.setRecurring(5)

    def StopTimer( self, player ):
        if 1 != self.__state:
            return 0
        if player.updateTimer:
            player.updateTimer.destroy()
            player.updateTimer = None
       

    def onUpdate( self, player ):
        if 1 != self.__state:
            return 0
        body = player.getDefaultVehicle()
        damage = body.getDamage()
        if damage > 0:    #If you're not dead...
            if ((player.lastDamage <= damage) & (player.lastDamage != None) & (player.damageRate < 3000)):  #if you've been getting healed
                player.damageRate += 25
                mm_utils.msg_player( player.index, "You can't avoid punishment by healing.  Increasing damage rate." )
            elif (player.lastDamage >= damage):
                player.damageRate = 25
                mm_utils.msg_player( player.index, "Pilots must use aircraft to avoid punishment for being AWOL." )
            else:
                mm_utils.msg_player( player.index, "Pilots must use aircraft to avoid punishment for being AWOL." )
            player.lastDamage = damage
            body.setDamage(damage-player.damageRate)
            if body.getDamage() < 0:
                mm_utils.msg_server( "Pilot %s was killed for going AWOL." % player.getName() )

    def ResetPlayer( self, player ):
        player.damageRate = 25
        player.lastDamage = None
        self.StopTimer( player )

    def ResetAll( self ):
        for player in bf2.playerManager.getPlayers():
            if (not player.isAIPlayer()):
                self.ResetPlayer( player )
               
    def onGameStatusChanged( self, status ):
    # Enables the idle check timer
    # Import variables, and update the game status:
        global gamestatus
        gamestatus = status
        if gamestatus == bf2.GameStatus.EndGame:
            self.ResetAll()

    def init( self ):
        if 0 == self.__state:
            host.registerHandler( 'ExitVehicle', self.onExitVehicle, 1 )
            host.registerHandler( 'EnterVehicle', self.onEnterVehicle, 1 )
            host.registerHandler( 'PlayerConnect', self.onPlayerConnect, 1 )
            host.registerHandler( 'PlayerSpawn', self.onPlayerSpawn, 1 )
            host.registerHandler( 'PlayerDeath', self.onPlayerDeath, 1 )
            host.registerGameStatusHandler(self.onGameStatusChanged)
           
           
        # Update to the running state
        self.__state = 1

    def shutdown( self ):
        """Shutdown and stop processing."""

        # Unregister game handlers and do any other
        # other actions to ensure your module no longer affects
        # the game in anyway

        self.ResetAll()

        # Flag as shutdown as there is currently way to:
        # host.unregisterHandler
        self.__state = 2

    def update( self ):
        """Process and update.
        Note: This is called VERY often processing in here should
        be kept to an absolute minimum.
        """
        pass

def mm_load( modManager ):
    """Creates and returns your object."""
    return NoWalk( modManager )

Credits

Mainly to Graag, as his class limit script was my main reference for this, my first attempt at BF2 python coding.