Player Starting Units

From WiCWiki

Revision as of 12:42, 10 December 2007 by kowalski (Talk | contribs)
(diff) ←Older revision | Current revision (diff) | Newer revision→ (diff)
Jump to: navigation, search

Contents


Create units to the player through script

If you don’t want to use starting units in your mission you can skip this chapter. Read instead about the reinforcements menu in the chapter about drop zones and the reinforcements menu.


In some mission in the single player campaign, the player does not have access to the reinforcement menu when the mission starts. In those cases we’ll give starting units to the player through script. When the player uses the reinforcements menu to order down units, the spent points will be regained if the units die. There is one issue with creating scripted units to the player and it is that by default the player will not receive any reinforcement point if the units die. We will cover how to fix this problem in a little while.


We use the allunits.py file to create the players starting units and almost every piece of code in this chapter (with some exceptions, but we’ll explain where they should be) will be written in the allunits file.


Create a group

When you want to create units, you can use the CreateGroup function. It creates a group you can give commands to. You can find more of the group commands here. Wic\python\group\group.py


CreateGroup( aName = None, aUnits = [], aTarget = None, aOwner = 0, aTeam = 0)


-The first parameter is the name of the group. Each group must have a unique name. The default value is ‘None’. If you don’t change it from ‘None’ the name of the group will be something like ‘Group14’. The main reason we change the groups name is that it is a lot easier to find the group, when debubbing, if you know what you call it.


CreateGroup('grpPlayer1', [More code here])


-The second parameter is a list with the unit or units we want to create and add to the group. The nameOfUnit parameter is the variable from the unittypes.py file containing the units hash value. By default it is an empty list that means no units are added to the group.


CreateGroup('grpPlayer1', [ NATO_LEOPARD2 ], [More code here])


-The third parameter is the position where to create the units. The default value is ‘None’. If you create an empty group the parameter can be ’None’, if not you must have a position here, else the game will crash.

CreateGroup('grpPlayer1', [ NATO_LEOPARD2 ], 'areaPlayerStartPos', [More code here])

-The fourth parameter is which player that should own the group and is decided by an integer between 0-16. The default value is 0 and means that it is the script player that owns the group. If the human player shall own the group, the value must be set to 1.

CreateGroup('grpPlayer1', [ NATO_LEOPARD2 ], 'areaPlayerStartPos', PLAYER_HUMAN, [More code here])

-The fifth and last parameter is to which team the group shall belong. The default value is 0 and means that the group is neutral.

CreateGroup('grpPlayer1', [ NATO_LEOPARD2 ], 'areaPlayerStartPos', PLAYER_HUMAN, TEAM_NATO)


The variables in the mapvars.py file

The CreateGroup function returns the group object that been created so that we can save it in a mapvars variable and have global access to it later.

The declaration of the variable is very simple. We usually use the mapvars.py file and we have most of the global variables we use in there. Almost every variable is declared with a ‘None’ as value like this.

grpPlayer1 = None

There are some exceptions for example if we want a timer to have a value from the beginning like this timer from the test map.

TIME_MANSION = 120

Simplify Owners and teams

We have some global variables that can be used to make it easier to set the owner and team of a group.

PLAYER_SCRIPT    = 0

PLAYER_HUMAN     = 1

TEAM_NEUTRAL     = 0
TEAM_USA         = 1

TEAM_NATO        = 2

TEAM_RUSSIA      = 3

TEAM_USSR        = 3


Instead of writing CreateGroup(None, [],None, 0, 0) we can write CreateGroup(None, [],None, PLAYER_SCRIPT, TEAM_NEUTRAL) which makes it a lot easier to see who owns the group and what team the group belongs to.


Create the first player unit

If we want to create a ‘Leopard 2’ tank at the players start position area, ‘areaPlayerStartPos’ that we created in ‘WICEd’ before, a tank that are controlled by the player we do like this. This code goes in the allunits.py file and must be in a function, like this. We use the Player() function because it is the players units but the most important thing is that it gets called from the server.py file.

def Player( ):
  DebugMessage( 'allunits.Player::' )

  # Create a group own by the player that has one heavy NATO tank. 
  mapvars.grpPlayer1 = CreateGroup( 'grpPlayer1', [ NATO_LEOPARD2 ], 'areaPlayerStartPos', PLAYER_HUMAN, TEAM_NATO )


We save the group in a variable in the mapvars.py file, so we’ll have global access to the group later. In this case we’ll name the group ’grpPlayer1’. We usually give the group the same name as the group variable.

The unit, ‘NATO_LEOPARD2’, is a variable from unittypes.py that has the hash value for the "NATO_Tank_Leopard2" unit.

We must call the allunits.py files Player function from the server.py file. If you have an Intro function it’s a good place to call it from, like this.

def Intro():
  allunits.Player()

Remember to write ‘allunits’ before the function name else the game won’t know where it should look for the function.

If you don’t have an Intro function you can call it from wherever you want, but you won’t see the units before the function is called.

Now you can start the game and if you’ve done everything right, you should have a single tank standing where you placed your area.

Receive AP point when the unit dies

One disadvantage when creating units to the player through script is that when the units die the player will not receive any reinforcement points. This means that a mission where the player gets unit through script and has or will get access to the reinforcement menu later, will have a different difficulty level depending on whether the player lose the starting units or not.

However, do not despair as we have some functions that make us receive points when a script created unit dies. If you want to know what a unit costs, look in the unittypes.py file.

SetUnitCreditValue

To set how many reinforcement points we want the player to receive when the unit dies we use the SetUnitCreditValue function. The SetUnitCreditValue must be use on all the units we want the player to receive points from.

SetUnitCreditValue( UnitId, aCost )


-The first parameter is the id number of the unit we want to regain points from when it dies. -The second parameter is how many points we will regain when the unit dies.


To get the right amount of reinforcement points when the Leopard tank, which we created in the grpPlayer1 group, is killed the code looks like this.

SetUnitCreditValue( mapvars.grpPlayer1.myUnits[ 0 ].myUnitId, GetUnitCost( 'NATO_Tank_Leopard2'  ))

To get the id of the tank we write mapvars.grpPlayer1.myUnits[ 0 ].myUnitId and we use the GetUnitCost function, with the unit type ‘NATO_Tank_Leopard2’, to set how many reinforcement point to receive if the tank dies.

GetUnitCost

Instead of remember the cost for each and every unit type; we can use GetUnitCost that returns the cost of one unit type. If you have more than one unit you can send them all in a list and receive the sum of all the units’ costs irrespective of whether they are the same type or not. The cost of the unit is fetched from the unit cost dictionary in the unittypes.py file. If the unit does not exist in the dictionary, the game will crash.

More about unittypes.py and the unit cost dictionary in the chapter about ‘The unit types list’.

GetUnitCost( someUnitTypes ) 

-The parameter is the name or a list of names with the units that we want to get the cost from.


SetGroupApOwner

The last thing we have to do is to set the player that should get the reinforcement points when the unit dies. We use the group method SetGroupApOwner.

aGroup.SetGroupApOwner( anOwner )

-The Parameter is the id of the player that should get the AP points.

To make sure that the player receives the AP points for the Leopard tank we use SetGroupAPOwner on the group, mapvars.grpPlayer1, and send in PLAYER_HUMAN.


mapvars.grpPlayer1.SetGroupApOwner( PLAYER_HUMAN )


This is how the Player function in allunits.py should look like.

##---------------------------------------------------------------
#---------------------------  Player ----------------------------

def Player( ):
  DebugMessage( 'allunits.Player::' )
  
 # Create a group own by the player that has one heavy NATO tank. 
 mapvars.grpPlayer1 = CreateGroup( 'grpPlayer1', [ NATO_LEOPARD2 ], 'areaPlayerStartPos', PLAYER_HUMAN, TEAM_NATO )

 # set how many reinforcements point to get when the unit dies
 SetUnitCreditValue( mapvars.grpPlayer1.myUnits[ 0 ].myUnitId, GetUnitCost( 'NATO_Tank_Leopard2' ) )

 # set with player that will get the point
 mapvars.grpPlayer1.SetGroupApOwner( PLAYER_HUMAN )

    1. ---------------------------------------------------------------
  1. --------------------------- Village ---------------------------


After we have added the line, allunits.Player(), to the Intro function in server.py it should look like this.

def Intro( ):
  DebugMessage( "Intro::" )

  # Set the players camera start position and the point it should look at. 
  cameraStartPos = GetPosition( 'areaCamStartPos' )
  cameraStartPos.myY = 170
  cameraStartLookAt = GetPosition( 'areaPlayerStartPos' )
  thePlayers[ PLAYER_HUMAN ].SetCameraPosition( cameraStartPos, cameraStartLookAt )

  # Create the player starting units.
  allunits.Player( )
 
  # Setup all Events.
  VillageSetup( )

  MansionSetup( )
  LastStandSetup( )
  DestroyAASetup( )
  RescueArtySetup( )
  DestroyHideoutsSetup( )

PostCommand

It can be cool to let the players units drive or run into the camera view.

We are quite limited on what we can do with player owned units; so if we want to do something with the units we must create them with the script player as the owner. If we do that we can give the units some order before we give them to the player.


Let us add one more Leopard tank in a new group that we save in the mapvars variable grpPlayer2. The things that are different from the previous group are the name, ‘grpPlayer2’, the position where to create the tank and that we send in PLAYER_SCRIPT instead of PLAYER_HUMAN.


mapvars.grpPlayer2 = CreateGroup( 'grpPlayer2', [ NATO_LEOPARD2 ], 'areaPlayerSpawnPos', PLAYER_SCRIPT, TEAM_NATO )


We set the cost and AP owner on the grpPlayer2 the same way as we did with grpPlayer1.


SetUnitCreditValue( mapvars.grpPlayer2.myUnits[ 0 ].myUnitId, GetUnitCost( 'NATO_Tank_Leopard2' ) )

mapvars.grpPlayer2.SetGroupApOwner( PLAYER_HUMAN )


To make the unit move from its spawn position and change owner when it reaches its goal we use two PostCommands, CMD_Regroup and CMD_SetOwner. We’ll cover more about PostCommands in the chapter about units.


CMD_Regroup moves the group to the position that we send in, in this case ’areaPlayerStartPos2’, and when it reach its position it turns and looks at the second position, the ’CP_Village’ command point.

mapvars.grpPlayer2.PostCommand(CMD_RegroupAt( 'areaPlayerStartPos2', 'CP_Village' ))

When it has reached the position and looks at the village, we change the owner.

mapvars.grpPlayer2.PostCommand( CMD_SetOwner( PLAYER_HUMAN ) )

Add the players groups to a platoon

The last thing we’ll do is to create a platoon with both player groups in, so that we later can trigger the mission failed and game over if they die. We will cover more about platoons in the chapter about units.

mapvars.pltPlayer = Platoon( [mapvars.grpPlayer1, mapvars.grpPlayer2] )

We save the platoon in a pltPlayer variable in mapvars.py and send in the players groups, mapvars.grpPlayer1 and mapvars.grpPlayer2, in a list to the Platoon function.


If you start the game now there will be two tanks, one that stands and looks away from the village and one that moves up to the first tank. When it is has reached the point it turns around and looks at the village.


After the grpPlayer2 code is added to allunits it should look like this.


##---------------------------------------------------------------
#---------------------------  Player ----------------------------

def Player( ):
  DebugMessage( 'allunits.Player::' )
     
  # Create a group own by the player that has one heavy NATO tank. 
  mapvars.grpPlayer1 = CreateGroup( 'grpPlayer1', [ NATO_LEOPARD2 ], 'areaPlayerStartPos', PLAYER_HUMAN, TEAM_NATO )
     
  # set how many reinforcements point to get when the unit dies
  SetUnitCreditValue( mapvars.grpPlayer1.myUnits[ 0 ].myUnitId, GetUnitCost( 'NATO_Tank_Leopard2' ) )
    
  # set with player that will get the point
  mapvars.grpPlayer1.SetGroupApOwner( PLAYER_HUMAN )      

  # Create one more group, this one own by the script player so that we can do stuff with it before we give #it to the player.
  mapvars.grpPlayer2 = CreateGroup( 'grpPlayer2', [ NATO_LEOPARD2 ], 'areaPlayerSpawnPos', PLAYER_SCRIPT, TEAM_NATO )
  SetUnitCreditValue( mapvars.grpPlayer2.myUnits[ 0 ].myUnitId, GetUnitCost( 'NATO_Tank_Leopard2' ) )
  mapvars.grpPlayer2.SetGroupApOwner( PLAYER_HUMAN )

  # Now lets post some command on the group, we want it to move to a position and make it look
  # at the CP before we gives it to the player.

  mapvars.grpPlayer2.PostCommand( CMD_RegroupAt( 'areaPlayerStartPos2', 'CP_Village' ) )
  mapvars.grpPlayer2.PostCommand( CMD_SetOwner( PLAYER_HUMAN ) )

  # Create a platoon and add the player groups. We are using the platoon to check if all players unit has been killed.
  mapvars.pltPlayer = Platoon( [mapvars.grpPlayer1, mapvars.grpPlayer2] )

Receive AP points from an infantry squad

If the player shall have an infantry squad as starting units, you have to set the cost for each individual unit in the squad. A squad contains up to five units and if we send in just one squad members ID we’ll receive the entire squads AP points when that particular unit dies. If the player has access to the reinforcements menu this means that the player can order down, it we are unlucky, a new squad after the first squad member dies and four still are alive. This can lead to that the player gets more units than intended.


The way it ought to be is by setting the credit value for each unit in the squad. Here is an example with a squad that have five units. The price for the squad, that is an 'US_Squad_Mechanized', is 800 AP points.

Therefore, we divide the cost, 800, by the number of units in the squad, 5, which make that for each unit in the squad that dies the player will receives 160 AP points.

SetUnitCreditValue( mapvars.grpPlayer.myUnits[ 0 ].myUnitId, GetUnitCost( 'US_Squad_Mechanized' )/5 )
SetUnitCreditValue( mapvars.grpPlayer.myUnits[ 1 ].myUnitId, GetUnitCost( 'US_Squad_Mechanized' )/5 )
SetUnitCreditValue( mapvars.grpPlayer.myUnits[ 2 ].myUnitId, GetUnitCost( 'US_Squad_Mechanized' )/5 )
SetUnitCreditValue( mapvars.grpPlayer.myUnits[ 3 ].myUnitId, GetUnitCost( 'US_Squad_Mechanized' )/5 )
SetUnitCreditValue( mapvars.grpPlayer.myUnits[ 4 ].myUnitId, GetUnitCost( 'US_Squad_Mechanized' )/5 )

Appendix

We make two new functions in allunits.py; Player() and RescueArty(), that we will execute from the Intro function in the server.py file.


Changed files in this chapter:

Server.py Allunits.py Mapvars.py


Changes in the server.py file.

def Intro( ):
  [More code here.]

  # Create the player starting units.
  allunits.Player( )


def RescueArtySetup( aKickStart = False ):
  DebugMessage( 'RescueArtySetup::' )

  allunits.RescueArty( )


Changes in the allunits.py file.

[More code here.]

#---------------------------  Player ----------------------------
def Player( ):
  DebugMessage( 'allunits.Player::' )

  # Create a group own by the player that has one heavy NATO tank. 
  mapvars.grpPlayer1 = CreateGroup( 'grpPlayer1', [ NATO_LEOPARD2 ], 'areaPlayerStartPos', PLAYER_HUMAN, TEAM_NATO )
  # set how many reinforcements point to get when the unit dies

  SetUnitCreditValue( mapvars.grpPlayer1.myUnits[ 0 ].myUnitId, GetUnitCost( 'NATO_Tank_Leopard2' ) )

  # set with player that will get the point
  mapvars.grpPlayer1.SetGroupApOwner( PLAYER_HUMAN )

  # Create one more group, this one own by the script player so that we can do stuff with it before we give #it to the player.
  mapvars.grpPlayer2 = CreateGroup( 'grpPlayer2', [ NATO_LEOPARD2 ], 'areaPlayerSpawnPos', PLAYER_SCRIPT, TEAM_NATO )
  SetUnitCreditValue( mapvars.grpPlayer2.myUnits[ 0 ].myUnitId, GetUnitCost( 'NATO_Tank_Leopard2' ) )
  mapvars.grpPlayer2.SetGroupApOwner( PLAYER_HUMAN )

  # Now lets post some command on the group, we want it to move to a position and make it look
  # at the CP before we gives it to the player.
  mapvars.grpPlayer2.PostCommand( CMD_RegroupAt( 'areaPlayerStartPos2', 'CP_Village' ) )
  mapvars.grpPlayer2.PostCommand( CMD_SetOwner( PLAYER_HUMAN ) )

  # Create a platoon and add the player groups. We are using the platoon to check if all players unit has been killed.
  mapvars.pltPlayer = Platoon( [mapvars.grpPlayer1, mapvars.grpPlayer2] )


#-------------------------  RescueArty  -------------------------   
def RescueArty( ):
  DebugMessage( 'allunits.RescueArty::' )

  mapvars.grpRescueArty = CreateGroup( 'grpRescueArty', [ NATO_LARS_SF_2 ], 'areaArtySpawn', PLAYER_SCRIPT, TEAM_NATO )
  mapvars.grpRescueArtyRu1 = CreateGroup( 'grpRescueArtyRu1', [  ], , PLAYER_SCRIPT, TEAM_USSR )


def RescueArtyAttack( ):
  DebugMessage( 'allunits.RescueArtyAttack::' )

  mapvars.grpRescueArtyRu1.CreateUnitsAtPosition( [ USSR_BMP_R, USSR_BMP_R ], 'areaKillArtyRuSpawn' )


Changes in the mapvars.py file.

##---------------------------------------------------------------
#---------------------------  Player ----------------------------
grpPlayer1 = None
grpPlayer2 = None
pltPlayer = None


##---------------------------------------------------------------
#-------------------------  RescueArty  -------------------------   
grpRescueArty = None
grpRescueArtyRu1 = None
Chapter 3: Areas and Command Points < > Chapter 5: Reactions and Actions
Personal tools
User Created Content