Overview Package Class Source Class tree Glossary
previous class      next class frames      no frames

JBAddonPersistence.JBGameRulesPersistence


00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
// ============================================================================
// JBGameRulesPersistence
// Copyright 2006 by Mitchell Davis <mitchelld02@yahoo.com>
//
// The game rules to allow weapons and other attributes to transfer over
// into the next round.
// ============================================================================

class JBGameRulesPersistence extends JBGameRules
      config
      CacheExempt;

// ============================================================================
// Structures
// ============================================================================
struct TPersistWeapons
{
  var class<Weapon> PersistentWeapon;
  var int PrimaryAmmo;
  var int SecondaryAmmo;
};

struct TPersistence
{
  var string Owner;
  var int Adrenaline;
  var int Score;
  var int Rank;      //This will be used when carrying over weapons
  var bool bWasJailed;
  var array<TPersistWeapons> WeaponList;
};

// ============================================================================
// Variables
// ============================================================================
var private array<TPersistence> CapturerList, CapturedList;
var private TeamInfo CapturedTeam;
var private int NumCapturers, NumCaptured;
var private bool bNewRound;   //When a new round starts, set to true
var private bool bUprising;   //Give the losing team the winner's weapons
var private int nHealth;      //The ammount of health to transfer

// ============================================================================
// Functions
// ============================================================================

// ============================================================================
// PreBeginPlay
//
// This function will be called on level start-up. This will initialize all
// values and set the timer.
// ============================================================================
function PreBeginPlay()
{
  NumCapturers = 0;
  NumCaptured = 0;
  bNewRound = false;
  bUprising = class'JBAddonPersistence'.default.bUprising;
  nHealth = class'JBAddonPersistence'.default.nHealth;
  SetTimer(0.5, true);
  if(bUprising == true)
    Level.Game.bAllowWeaponThrowing = false;           //disable weapon throwing
  Super.PreBeginPlay();
}

// ============================================================================
// Timer
//
// This function is called every second to determine if a new round has
// started. The reason for this approach is because in the function
// NotifyRound, each Controller's Pawn has not been created at that point.
// Using this approach allows the Pawns to be created so that their weapons
// may be carried over.
// ============================================================================
function Timer()
{
  local Controller t;

  if(bNewRound && (NumCapturers > 0 || NumCaptured > 0))
  {
    SortListByScore(CapturerList, 0, NumCapturers);
    SortListByScore(CapturedList, 0, NumCaptured);
    for(t = Level.ControllerList; t != None; t = t.nextController)
    {
      //loop through each controller's pawn and find one that is a player
      if(t.Pawn != None && t.Pawn.GetHumanReadableName() ~= CapturerList[0].Owner)
      {
        //once we find a pawn, carry over all weapons for that player.
        CarryOverWeapons();
        CleanUpArray();
        break;
      }
    }
  }

  bNewRound = false;

  Super.Timer();
}

// ============================================================================
// NotifyExecutionEnd
//
// Called when the execution sequence has been completed, directly before the
// next round starts.
// ============================================================================
function NotifyRound()
{
  Super.NotifyRound();
  bNewRound = true;
}

// ============================================================================
// NotifyPlayerJailed
//
// Called when a player enters a jail, for instance by being spawned there
// after being killed, or by being sent there after losing an arena fight, or
// by simply physically walking into it.
// ============================================================================
function NotifyPlayerJailed(JBTagPlayer TagPlayer)
{
  local JBTagTeam thisTeam;

  thisTeam = class'JBTagTeam'.static.FindFor(TagPlayer.GetTeam());

  if(thisTeam.CountPlayersJailed() == thisTeam.CountPlayersTotal())
  {
    ExtractPlayers(thisTeam.GetTeam());
  }

  Super.NotifyPlayerJailed(TagPlayer);
}

// ============================================================================
// ExtractPlayers
//
// This function will extract the pawns associated with the capturing team and
// retrieve their inventory. This information will be recorded into a
// structure to easily handle moving over inventory to the next round.
//
// The new version now extracts not only the capturing players, but also the
// captured players.
// ============================================================================
function ExtractPlayers(TeamInfo Team)
{
  local Controller thisController;

  CapturedTeam = Team;  //Keep track of which team was the captured team for
                        //health transfer.

  for(thisController = Level.ControllerList; thisController != None; thisController = thisController.NextController)
  {
    if((thisController.PlayerReplicationInfo != None) &&
       (thisController.PlayerReplicationInfo.Team != Team))
    {
      AddToCapturingList(thisController);
    }
    else if((thisController.PlayerReplicationInfo != None) &&
            (thisController.PlayerReplicationInfo.Team == Team))
    {
      AddToCapturedList(thisController);
    }
  }
}

// ============================================================================
// CarryOverWeapons
//
// Runs through the list of controllers on this level to determine who was on
// the capturing team in the previous round. If they were on that team, they
// will receive their inventory back.
// ============================================================================
function CarryOverWeapons()
{
  local int i, j;
  local int Count;  //Use this to keep track of which capturing players' weapons
                    //we are handing over to the captured players.
  local Controller thisController;

  Count = 0;
  //Make sure a new round has started.
  if(bNewRound)
  {
    for(thisController = Level.ControllerList; thisController != None; thisController = thisController.nextController)
    {
      //if not bUprising, then don't transfer weapons to opposing team
      if(!bUprising)
      {
        TransferHealth(thisController);
        for(i = 0; i < CapturerList.Length; i++)
        {
          if((thisController.Pawn != None) &&
             (thisController.Pawn.GetHumanReadableName() ~= CapturerList[i].Owner))
          {
            //We will set the adrenaline first
            SetAdrenaline(thisController, CapturerList[i].Adrenaline);
            //Loop through all the weapons in the structure array and add them
            //to the Pawn.
            for(j = 0; j < CapturerList[i].WeaponList.Length - 1; j++)
            {
              PersistWeapon(thisController.Pawn, CapturerList[i].WeaponList[j]);
            }
          }
        }
      }
      else //if bUprising is set to true, then transfer weapons over
      {
        TransferHealth(thisController);
        for(i = CapturedList.Length - 1; i >= 0; i--)
        {
          if((thisController.Pawn != None) &&
             (thisController.Pawn.GetHumanReadableName() ~= CapturedList[i].Owner))
          {
            SetAdrenaline(this Controller, CapturerList[i].Adrenaline);
            //this is used in case teams are unbalanced.
            if(Count >= CapturerList.Length - 1)
              Count = 0;
            for(j = 0; j < CapturerList[Count].WeaponList.Length - 1; j++)
            {
              PersistWeapon(thisController.Pawn, CapturerList[Count].WeaponList[j]);
            }
            Count++;
          }
        }
      }
    }
  }
}

// ============================================================================
// SetAdrenaline
//
// This function will set the current controller's adrenaline to the amount
// available from the previous round. The game already handles this, but in the
// case where a player is in mid Combo, this will make sure that the player
// receives what is left of the combo.
// ============================================================================
function bool SetAdrenaline(Controller C, int Adrenaline)
{
  C.Adrenaline = Adrenaline;

  return true;
}

// ============================================================================
// PersistWeapon
//
// This function will spawn the weapon and add the weapon to the Pawn's
// inventory.
// ============================================================================
function bool PersistWeapon(Pawn P, TPersistWeapons Weapon)
{
  local Weapon W;
  local Inventory Inv;

  if(Weapon.PersistentWeapon == None)
    return false;

  //Need to spawn the weapon from the player, and give to the player.
  W = P.Spawn(Weapon.PersistentWeapon, P);

  if(W == None)
    return false;

  //if the weapon is the assault rifle, consume all ammo first, then add
  //ammunition from the Weapon structure.
  if(W.Class == class'XWeapons.AssaultRifle')
  {
    for(Inv = P.Inventory; Inv != None; Inv = Inv.Inventory)
    {
      if(Inv.Class == class'XWeapons.AssaultRifle')
      {
        Weapon(Inv).ConsumeAmmo(0, 100);
        Weapon(Inv).ConsumeAmmo(1, 4);
        Weapon(Inv).AddAmmo(Weapon.PrimaryAmmo, 0);
        Weapon(Inv).AddAmmo(Weapon.SecondaryAmmo, 1);
      }
    }
  }
  //If the weapon is the shield gun or the translocator,
  //don't add it to the inventory
  else if((W.Class == class'XWeapons.ShieldGun') ||
          (W.Class == class'XWeapons.TransLauncher'))
  {
    return true;
  }
  else
  {
    W.AddAmmo(Weapon.PrimaryAmmo, 0);
    W.ClientWeaponSet(true);    //Prevent weapons to appear funky on next round
    P.AddInventory(W);
  }

  return true;
}

// ============================================================================
// TransferHealth
//
// Adds health to the captured players and takes away health from the
// capturing players.
// Param:    C - The current controller to add or subtract health from.
// ============================================================================
function TransferHealth(Controller C)
{
  //Check for replication info
  if(C.PlayerReplicationInfo == None)
    return;

  //If the current controller was not on the captured team,
  //take away health
  if((C.Pawn != None) && (C.PlayerReplicationInfo.Team != CapturedTeam))
  {
    C.Pawn.Health -= nHealth;
  }
  //If the current controller was on the captured team, add health
  else if((C.Pawn != None) && (C.PlayerReplicationInfo.Team == CapturedTeam))
  {
    C.Pawn.Health += nHealth;
  }
}

// ============================================================================
// AddToCapturingList
//
// Adds information about the capturing pawns into the TPersistance structure
// to carry over into the next round.
// Param:    Capturer - The controller that is on the capturing team.
//                      Used to extract adrenaline.
// ============================================================================
function AddToCapturingList(Controller Capturer)
{
  local int i;
  local Pawn P;
  local Inventory Inv;
  local JBTagPlayer TagPlayer; //Used to determine if capturer was in jail
                               //at time of winning

  if(Capturer == None || Capturer.Pawn == None)
    return;

  P = Capturer.Pawn;

  TagPlayer = class'JBTagPlayer'.static.FindFor(Capturer.PlayerReplicationInfo);
  //If tag player is in jail, then do not add that player to the list.
  //The player won't have any weapons of use for the losing team.
  if(TagPlayer.IsInJail())
    return;

  CapturerList.Insert(NumCapturers, 1);
  CapturerList[NumCapturers].Owner = P.GetHumanReadableName();
  CapturerList[NumCapturers].Adrenaline = Capturer.Adrenaline;
  CapturerList[NumCapturers].Score = Capturer.PlayerReplicationInfo.Score;

  //Loop through the inventory list and extract all inventory that are weapons
  i = 0;
  for(Inv = P.Inventory; Inv != None; Inv = Inv.Inventory)
  {
    if((Inv != None) &&
       (Inv.IsA('Weapon')))
    {
      //If we insert one element at i and try to access that element, we get
      //Accessed None warnings.
      //For some odd reason, if we insert two elements at i, everything is fine.
      //Need to investigate furthur.
      CapturerList[NumCapturers].WeaponList.Insert(i, 2);
      CapturerList[NumCapturers].WeaponList[i].PersistentWeapon = Weapon(Inv).Class;
      CapturerList[NumCapturers].WeaponList[i].PrimaryAmmo = Weapon(Inv).AmmoAmount(0);
      CapturerList[NumCapturers].WeaponList[i].SecondaryAmmo = Weapon(Inv).AmmoAmount(1);
    }
    i++;
  }

  NumCapturers++;     //Increment array
}

// ============================================================================
// AddToCapturedList
//
// Adds information about the captured pawn to the list.
// Param:    Captured - The controller that is on the captured team.
// ============================================================================
function AddToCapturedList(Controller Captured)
{
  local Pawn P;

  if(Captured == None || Captured.Pawn == None)
    return;

  P = Captured.Pawn;

  CapturedList.Insert(NumCaptured, 1);
  CapturedList[NumCaptured].Owner = P.GetHumanReadableName();
  CapturedList[NumCaptured].Score = Captured.PlayerReplicationInfo.Score;

  NumCaptured++;
}

// ============================================================================
// SortListByScore
//
// Sort the list in ascending order based on the score. The algorithm used is
// insertion sort.
// Param:    Team - The current list to sort.
//           LowerBound - The lower bound of the sort
//           UpperBound - The upper bound of the sort
// ============================================================================
function SortListByScore(out array<TPersistence> Team, int LowerBound, int UpperBound)
{
  local int i, j, Index;
  local TPersistence Temp;

  for(i = LowerBound + 1; i < UpperBound; ++i)
  {
    Index = i;
    j = i;
    while(j > 0)
    {
      if(Team[j - 1].Score < Team[Index].Score)
      {
        Temp = Team[j - 1];
        Team[j - 1] = Team[Index];
        Team[Index] = Temp;
        Index = j - 1;
      }
      --j;
    }
  }
}

// ============================================================================
// CleanUpArray
//
// Empty out the array each round.
// ============================================================================
function CleanUpArray()
{
  if(CapturerList.Length > 0)
  {
    CapturerList.Remove(0, CapturerList.Length);
    NumCapturers = 0;
  }
  if(CapturedList.Length > 0)
  {
    CapturedList.Remove(0, CapturedList.Length);
    NumCaptured = 0;
  }
}

defaultproperties
{
}

Overview Package Class Source Class tree Glossary
previous class      next class frames      no frames
Class file time: Sa 29.7.2006 00:05:44.000 - Creation time: Do 14.8.2014 09:58:42.274 - Created with UnCodeX