Server Control Modules


Any game play features you want to implement should probably be done as a server control module, or part of one. If you are going to make a multiplayer online game, that should back there will change to a must. The only way we can ensure a level playing field and game play code security is to run the code on the server, and not on the client.

control/server/server.cs

On the server side, the server module is probably the single most influential module. It carries the server control oriented GameConnection methods for handling players and other game objects, as well as straightforward server control routines.

Type in the following code and save it as C:\Emaga5\control\server\server.cs.

 //============================================================================ // control/server/server.cs // Copyright (c) 2003 by Kenneth C. Finney. //============================================================================ function OnServerCreated() //---------------------------------------------------------------------------- // Once the engine has fired up the server, this function is called //---------------------------------------------------------------------------- {    Exec("./misc/camera.cs");    Exec("./misc/shapeBase.cs");    Exec("./misc/item.cs");    Exec("./players/player.cs");    Exec("./players/beast.cs");    Exec("./players/ai.cs");    Exec("./weapons/weapon.cs");    Exec("./weapons/crossbow.cs"); } function StartGame() {    if ($Game::Duration) // Start the game timer       $Game::Schedule = Schedule($Game::Duration * 1000, 0, "onGameDurationEnd");    $Game::Running = true;    schedule( 2000, 0, "CreateBots"); } function OnMissionLoaded() {    StartGame(); } function OnMissionEnded() {    Cancel($Game::Schedule);    $Game::Running = false; } function GameConnection::OnClientEnterGame(%this) {    // Create a new camera object.    %this.camera = new Camera() {       dataBlock = Observer;    };    MissionCleanup.Add( %this.camera );    %this.SpawnPlayer(); } function GameConnection::SpawnPlayer(%this) {    %this.CreatePlayer("0 0 201 1 0 0 0"); } function GameConnection::CreatePlayer(%this, %spawnPoint) {    if (%this.player > 0)//The player should NOT already have an avatar object.    {                       // If he does, that's a Bad Thing.       Error( "Attempting to create an angus ghost!" );    }    // Create the player object    %player = new Player() {       dataBlock = HumanMaleAvatar;  // defined in players/player.cs       client = %this;              // the avatar will have a pointer to its    };                               // owner's GameConnection object    %player.SetTransform(%spawnPoint); // where to put it    // Update the camera to start with the player    %this.camera.SetTransform(%player.GetEyeTransform());    %player.SetEnergyLevel(100);    // Give the client control of the player    %this.player = %player;    %this.setControlObject(%player); } function GameConnection::OnDeath(%this, %sourceObject, %sourceClient, %damageType, %damLoc) {    // Switch the client over to the death cam and unhook the player object.    if (IsObject(%this.camera) && IsObject(%this.player))    {       %this.camera.SetMode("Death",%this.player);       %this.setControlObject(%this.camera);    }       %this.player = 0;       if (%damageType $= "Suicide" || %sourceClient == %this)    {    }    else    {       // good hit    } } //============================================================================ // Server commands //============================================================================ function ServerCmdToggleCamera(%client) {    %co = %client.getControlObject();    if (%co == %client.player)    {       %co = %client.camera;       %co.mode = toggleCameraFly;    }    else    {       %co = %client.player;       %co.mode = observerFly;    }    %client.SetControlObject(%co); } function ServerCmdDropPlayerAtCamera(%client) {    if ($Server::DevMode || IsObject(EditorGui))    {       %client.player.SetTransform(%client.camera.GetTransform());       %client.player.SetVelocity("0 0 0");       %client.SetControlObject(%client.player);    } } function ServerCmdDropCameraAtPlayer(%client) {    %client.camera.SetTransform(%client.player.GetEyeTransform());    %client.camera.SetVelocity("0 0 0");    %client.SetControlObject(%client.camera); } function ServerCmdUse(%client,%data) {    %client.GetControlObject().use(%data); } // stubs function ClearCenterPrintAll() { } function ClearBottomPrintAll() { } 

The first function in this module, OnServerCreated, is pretty straightforward. When called, it loads all the specific game play modules we need.

After that comes StartGame, which is where we put stuff that is needed every time a new game starts. In this case if we have prescribed game duration, then we start the game timer using the Schedule function.

Schedule is an extremely important function, so we'll spend a little bit of time on it here. The usage syntax is:

 %event = Schedule(time, reference, command, <param1...paramN>) 

The function will schedule an event that will trigger in time milliseconds and execute command with parameters. If reference is not 0, then you need to make sure that reference is set to be a valid object handle. When the reference object is deleted, the scheduled event is discarded if it hasn't already fired. The Schedule function returns an event ID number that can be used to track the scheduled event or cancel it later before it takes place.

In the case of our game timer, there is no game duration defined, so the game is open-ended, and the Schedule call will not take place. If, for example, $Game::Duration had been set to 1,800 (for 30 minutes times 60 seconds per minute), then the call to schedule would have had the first parameter set to 1,800 times 1,000, or 1,800,000, which is the number of milliseconds in 30 minutes.

OnMissionLoaded is called by LoadMission once the mission is finished loading. All it really does is start up the game play, but this is an ideal location to insert code that needs to adjust its capabilities based upon whatever mission was loaded.

The next function, OnMissionEnded, is called at the conclusion of the running of a mission, usually in the DestroyServer function. Here it cancels the end-of-game event that has been scheduled; if no game duration was scheduled—as is our case at the moment—then nothing happens, quietly.

After that is the GameConnection::OnClientEnterGame method. This method is called when the client has been accepted into the game by the server—the client has not actually entered the game yet though. The server creates a new observer mode camera and adds it to the MissionCleanup group. This group is used to contain objects that will need to be removed from memory when a mission is finished. Then we initiate the spawning of the player's avatar into the game world.

The GameConnection::SpawnPlayer is a "glue" method, which will have more functionality in the future. Right now we use it to call the CreatePlayer method with a fixed transform to tell it where to place the newly created player-avatar. Normally this is where we would place the player spawn decision code. It might also call a function that would figure out the spawn point transforms by looking up spawn markers. Once we know where the player will spawn, then we would create the avatar by calling CreatePlayer.

GameConnection::CreatePlayer is the method that creates the player's avatar object, sets it up, and then passes control of the avatar to the player. The first thing to watch out for is that we must ensure that the GameConnection does not already, or still, have an avatar assigned to it. If it does, then we risk creating what the GarageGames guys call an Angus Ghost. This is a ghosted object, on all the clients, that has no controlling client scoped to it. We don't want that! Once that is sorted out, we create the new avatar, give it some energy, and pass control to the player, the same way we did previously in Chapter 4.

GameConnection::onDeath is called from a player's Damage handler method if the player's damage exceeds a certain amount. What we do is switch the client over to the death cam and unhook the player object. This allows the player to swivel his view in orbit around the "corpse" of his avatar until he decides to respawn. There is a code block containing the comment "good hit" where we would add code to provide points scoring and other game play functionality if we want it. We can also penalize a player for committing suicide, by either evaluating the damage type or the ID of the owner of the weapon that killed the player.

There then are a series of ServerCmd message handlers that change whether the player controls the camera or the avatar based on the message received.

ServerCmdToggleCamera alternates between attaching the player to the camera or to his avatar as the control object. Each time the function is called, it checks to see which object is the control object—camera or avatar—and then selects the other one to be the new control object.

ServerCmdDropPlayerAtCamera will move the player's avatar to wherever the camera object is currently located and sets the player-avatar's velocity to 0. The control object is always set to be the player's avatar when the function exits.

ServerCmdDropCameraAtPlayer does just the opposite. It sets the camera's transform to match the player-avatar's and then sets the velocity to 0. The control object is always set to be the camera when the function exits.

The next function, ServerCmdUse, is an important game play message handler. We call this function whenever we want to activate or otherwise use an object that the player controls, "has mounted," or holds in inventory. When called, this function figures out the handle of the client's control object and then passes the data it has received to that object's use method. The data can be anything but is often the activation mode or sometimes a quantity (like a powerup or health value). You'll see how the back end of this works later in the item module.

control/server/players/player.cs

This is "the biggie." You will probably spend more time working with, tweaking, adjusting, and yes, possibly even cursing this module—or your own variations of this module— than any other.

Type in the following code and save it as C:\Emaga5\control\server\players\player.cs.

 //============================================================================ // control/server/players/player.cs // Copyright (c) 2003 by Kenneth C. Finney. //============================================================================ datablock PlayerData(HumanMaleAvatar) {    className = MaleAvatar;    shapeFile = "~/data/models/avatars/human/player.dts";    emap = true;    renderFirstPerson = false;    cameraMaxDist = 3;    mass = 100;    density = 10;    drag = 0.1;    maxdrag = 0.5;    maxDamage = 100;    maxEnergy = 100;    maxForwardSpeed = 15;    maxBackwardSpeed = 10;    maxSideSpeed = 12;    minJumpSpeed = 20;    maxJumpSpeed = 30;    runForce = 1000;    jumpForce = 1000;    runSurfaceAngle = 40;    jumpSurfaceAngle = 30;    runEnergyDrain = 0.05;    minRunEnergy = 1;    jumpEnergyDrain = 20;    minJumpEnergy = 20;    recoverDelay = 30;    recoverRunForceScale = 1.2;    minImpactSpeed = 10;    speedDamageScale = 3.0;    repairRate = 0.03;    maxInv[Copper] = 9999;    maxInv[Silver] = 99;    maxInv[Gold] = 9;    maxInv[Crossbow] = 1;    maxInv[CrossbowAmmo] = 20; }; //============================================================================ // Avatar Datablock methods //============================================================================ function MaleAvatar::onAdd(%this,%obj) {    %obj.mountVehicle = false;    // Default dynamic Avatar stats    %obj.setRechargeRate(0);    %obj.setRepairRate(%this.repairRate); } function MaleAvatar::onRemove(%this, %obj) {    %client = %obj.client;    if (%client.player == %obj)    {       %client.player = 0;    } } function MaleAvatar::onCollision(%this,%obj,%col,%vec,%speed) {    %obj_state = %obj.getState();    %col_className = %col.getClassName();    %col_dblock_className = %col.getDataBlock().className;    %colName = %col.getDataBlock().getName();    if ( %obj_state $= "Dead")       return;    if (%col_className $= "Item" || %col_className $= "Weapon" )    {       %obj.pickup(%col);    } } //============================================================================ // HumanMaleAvatar (ShapeBase) class methods //============================================================================ function HumanMaleAvatar::onImpact(%this,%obj,%collidedObject,%vec,%vecLen) {    %obj.Damage(0, VectorAdd(%obj.getPosition(),%vec),       %vecLen * %this.speedDamageScale, "Impact"); } function HumanMaleAvatar::Damage(%this, %obj, %sourceObject, %position, %damage, %damageType) {    if (%obj.getState() $= "Dead")       return;    %obj.applyDamage(%damage);    %location = "Body";    %client = %obj.client;    %sourceClient = %sourceObject ? %sourceObject.client : 0;    if (%obj.getState() $= "Dead")    {       %client.onDeath(%sourceObject, %sourceClient, %damageType, %location);    } } function HumanMaleAvatar::onDamage(%this, %obj, %delta) {    if (%delta > 0 && %obj.getState() !$= "Dead")    {       // Increment the flash based on the amount.       %flash = %obj.getDamageFlash() + ((%delta / %this.maxDamage) * 2);       if (%flash > 0.75)          %flash = 0.75;       if (%flash > 0.001)       {         %obj.setDamageFlash(%flash);       }       %obj.setRechargeRate(-0.0005);       %obj.setRepairRate(0.0005);    } } function HumanMaleAvatar::onDisabled(%this,%obj,%state) {    %obj.clearDamageDt();    %obj.setRechargeRate(0);    %obj.setRepairRate(0);    %obj.setImageTrigger(0,false);    %obj.schedule(5000, "startFade", 5000, 0, true);    %obj.schedule(10000, "delete"); } 

The first code block is a data block definition for a data block called HumanMaleAvatar of the PlayerData data block class. Table 5.2 provides a quick reference description of the items in this data block.

Table 5.2: Emaga5 Avatar Properties

Property

DescriptionNeural Network

className

Defines an arbitrary class that the avatar can belong to.

shapeFile

Specifies the file that contains the 3D model of the avatar.

emap

Enables environment mapping on the avatar model.

renderFirstPerson

When true, causes the avatar model to be visible when in first-person point-of-view mode.

cameraMaxDist

Maximum distance from the avatar for the camera in third-person point-of-view mode.

mass

The mass of the avatar in terms of the game world.

density

Arbitrarily defines density. Low-density players will float in water.

drag

Slows down the avatar through simulated friction.

maxDrag

Maximum allowable drag.

maxEnergy

Maximum energy allowed.

maxDamage

Maximum damage points that can be sustained before avatar is killed.

maxForwardSpeed

Maximum speed allowable when moving forward.

maxBackwardSpeed

Maximum speed allowable when moving backward.

maxSideSpeed

Maximum speed allowable when moving sideways (strafing).

minJumpSpeed

Below this speed, you can't make the avatar jump.

maxJumpSpeed

Above this speed, you can't make the avatar jump.

jumpForce

The force, and therefore the acceleration, when jumping.

runForce

The force, and therefore the acceleration, when starting to run.

runSurfaceAngle

Maximum slope (in degrees) that the avatar can run on.

jumpSurfaceAngle

Maximum slope (in degrees) that the avatar can jump on, usually somewhat less than RunSurfaceAngle.

runEnergyDrain

How quickly energy is lost when the player is running.

minRunEnergy

Below this, the player will not move.

jumpEnergyDrain

How quickly energy is lost when the player jumps.

minJumpEnergy

Below this, the player can't jump anymore.

recoverDelay

How long it takes after a landing from a fall or jump, measured in ticks, where 1 tick = 32 milliseconds.

recoverRunForceScale

How much to scale the run force by while in the postlanding recovery state.

minImpactSpeed

Above this speed, an impact will cause damage.

speedDamageScale

Used to impart speed-scaled damage.

repairRate

How quickly damage is repaired when first aid or health is applied.

maxInv[Copper]

Maximum number of copper coins that the player can carry.

maxInv[Silver]

Maximum number of silver coins that the player can carry.

maxInv[Gold]

Maximum number of gold coins that the player can carry.

maxInv[Crossbow]

Maximum number of crossbows that the player can carry.

maxInv[CrossbowAmmo]

Maximum amount of crossbow ammunition that the player can carry.

A brief word about the classname property: It's a GameBase classname property for this data block, which in this case is MaleAvatar. We use this class name to provide a place to hang various methods, which are defined later in the module.

In Chapter 3 we encountered environment mapping, which is a rendering technique that provides a method of taking the game world appearance and surroundings into account when rendering an object. You can enable environment mapping when rendering the avatar model by setting the emap property to true.

If we set the property renderFirstPerson to true, then when we are playing in first-person point-of-view mode, we will be able to see our avatar, our "body," as we look around. With it set to false, then we won't see it, no matter which way we look.

To control your avatar's energy depletion, you will want to adjust the following properties: maxEnergy, runEnergyDrain, minRunEnergy, jumpEnergyDrain, and minJumpEnergy. Generally, the minimum jump energy should be set higher than the minimum run energy. Also, jump energy drain should be faster, thus a higher number, than the run energy drain value.

Next is a series of methods that are used when dealing with the avatar as a GameBase class.

The first, the MaleAvatar::onAdd, is the method called whenever a new instance of an avatar is added to the game. In this case we initialize a few variables and then transfer the value of the data block's repairRate property (remember that a data block is static and unchangeable once transferred to the client) to Player object in order to have it available for later use. The %obj parameter refers to the Player object handle.

Of course, we also need to know what to do when it's time to remove the avatar, which is what MaleAvatar::onRemove does. It's nothing spectacular—it just sets the handle properties to 0 and moves on.

One of the methods that gets the most exercise from a healthy and active avatar is the MaleAvatar::onCollision method. This method is called by the engine whenever it establishes that the avatar has collided with some other collision-enabled object. Five parameters are provided: The first is the handle of this data block, the second is the handle of the player object, the third is the handle of the object that hit us (or that we hit), the fourth is the relative velocity vector between us and the object we hit, and the fifth is the scalar speed of the object we hit. Using these inputs, we can do some pretty fancy collision calculations.

What we do, though, is just find out what the state of our avatar is (alive or dead) and what kind of object we hit. If we are dead (our avatar's body could be sliding down a hill, for example), we bail out of this method; otherwise we try to pick up the item we hit, providing it is an item or a weapon.

The engine calls HumanMaleAvatar::onImpact when our avatar hits something. Unlike onCollision, this method detects any sort of impact, not just a collision with an item in the world. Collisions occur between ShapeBase class things, like items, player avatars, vehicles, and weapons. Impacts occur with those things, as well as terrain and interiors. So, onImpact provides essentially the same five parameters. We use that data to calculate how much damage the player should incur, and we apply that damage to the avatar's object using its Damage method.

The HumanMaleAvatar::Damage is where we try to ascertain what effect on the avatar the damage will have. If we want to implement hit boxes, or damage calculations based on object components, we would do that here. In this case if the player is dead, we again bail. If not, we apply the damage (which increases the accumulated damage value) and then obtain the object's current state. If the object is now dead, we call the OnDeath handler and exit the function.

Next is the HumanMaleAvatar::onDamage method, which is activated by the engine whenever the object's damage value changes. This is the method we want to use when applying some sort of special effect to the player when damage occurs—like making the screen flash or using some audio. In this case we do flash the screen, and we also start a slow energy drain caused by the damage. At the same time, we start a slow damage repair, which means that after some period of time, we will have regained some of our health (negative health equals positive damage).

When the player's damage exceeds the maxDamage value, the player object is set to the disabled state. When that happens, the function HumanMaleAvatar::onDisabled is called. This is where we deal with the final stages of the death of a player's avatar. What we are doing is resetting all the various repair values, disabling any mounted weapons, and then beginning the process of disposing of the corpse. We keep it around for a few seconds before letting it slowly fade away.

control/server/weapons/weapon.cs

The tutorial install kit doesn't like to create empty folders, so you will have to create the weapons folder in the server tree, as follows: C:\Emaga5\control\server\weapons\.

This Weapon module contains name space helper methods for Weapon and Ammo classes that define a set of methods that are part of dynamic name spaces class. All ShapeBase class images are mounted into one of eight slots on a shape.

There are also hooks into the inventory system specifically for use with weapons and ammo. Go ahead and type in the following module and save it as C:\Emaga5\control\server\weapons\weapon.cs.

 //============================================================================ // control/server/weapons/weapon.cs // Copyright (c) 2003 Kenneth C. Finney // Portions Copyright (c) 2001 GarageGames.com // Portions Copyright (c) 2001 by Sierra Online, Inc. //============================================================================ $WeaponSlot = 0; function Weapon::OnUse(%data,%obj) {    if (%obj.GetMountedImage($WeaponSlot) != %data.image.GetId())    {       %obj.mountImage(%data.image, $WeaponSlot);       if (%obj.client)          MessageClient(%obj.client, 'MsgWeaponUsed', '\c0Weapon selected');    } } function Weapon::OnPickup(%this, %obj, %shape, %amount) {    if (Parent::OnPickup(%this, %obj, %shape, %amount))    {       if ( (%shape.GetClassName() $= "Player" ||             %shape.GetClassName() $= "AIPlayer" ) &&             %shape.GetMountedImage($WeaponSlot) == 0)       {          %shape.Use(%this);       }    } } function Weapon::OnInventory(%this,%obj,%amount) {    if (!%amount && (%slot = %obj.GetMountSlot(%this.image)) != -1)       %obj.UnmountImage(%slot); } function WeaponImage::OnMount(%this,%obj,%slot) {    if (%obj.GetInventory(%this.ammo))       %obj.SetImageAmmo(%slot,true); } function Ammo::OnPickup(%this, %obj, %shape, %amount) {    if (Parent::OnPickup(%this, %obj, %shape, %amount))    {    } } function Ammo::OnInventory(%this,%obj,%amount) {    for (%i = 0; %i < 8; %i++)    {       if ((%image = %obj.GetMountedImage(%i)) > 0)          if (IsObject(%image.ammo) && %image.ammo.GetId() == %this.GetId())             %obj.SetImageAmmo(%i,%amount != 0);    } } function RadiusDamage(%sourceObject, %position, %radius, %damage, %damageType, %impulse) {    InitContainerRadiusSearch(%position, %radius, $TypeMasks::ShapeBaseObjectType);    %halfRadius = %radius / 2;    while ((%targetObject = ContainerSearchNext()) != 0) {       %coverage = CalcExplosionCoverage(%position, %targetObject,          $TypeMasks::InteriorObjectType | $TypeMasks::TerrainObjectType |          $TypeMasks::ForceFieldObjectType | $TypeMasks::VehicleObjectType);       if (%coverage == 0)          continue;       %dist = ContainerSearchCurrRadiusDist();       %distScale = (%dist < %halfRadius)? 1.0:          1.0 - ((%dist - %halfRadius) / %halfRadius);       %targetObject.Damage(%sourceObject, %position,          %damage * %coverage * %distScale, %damageType);       if (%impulse) {          %impulseVec = VectorSub(%targetObject.GetWorldBoxCenter(), %position);          %impulseVec = VectorNormalize(%impulseVec);          %impulseVec = VectorScale(%impulseVec, %impulse * %distScale);          %targetObject.ApplyImpulse(%position, %impulseVec);       }    } } 

The weapon management system contained in this module assumes all primary weapons are mounted into the slot specified by the $WeaponSlot variable.

The first method defined, Weapon::onUse, describes the default behavior for all weapons when used: mount it into the object's $WeaponSlot weapon slot, which is currently set to slot 0. A message is sent to the client indicating that the mounting action was successful. Picture this: You are carrying a holstered pistol. When the Use command is sent to the server after being initiated by some key binding, the pistol is removed from the holster, figuratively speaking, and placed in image slot 0, where it becomes visible in the player's hand. That's what takes place when you "use" a weapon.

The next method, Weapon::onPickup, is the weapon's version of what happens when you collide with a weapon, and the onCollision method of the MaleAvatar decides you need to pick this weapon up. First, the parent Item method performs the actual pickup, which involves the act of including the weapon in our inventory. After that has been handled, we get control of the process here. What we do is automatically use the weapon if the player does not already have one in hand.

When the Item inventory code detects a change in the inventory status, the Weapon::onInventory method is called in order to check if we are holding an instance of the weapon in a mount slot, in case there are none showing in inventory. When the weapon inventory has changed, make sure there are no weapons of this type mounted if there are none left in inventory.

The method WeaponImage::onMount is called when a weapon is mounted (used). We use it to set the state according to the current inventory.

If there are any special effects we want to invoke when we pick up a weapon, we would put them in the Ammo::onPickup method. The parent Item method performs the actual pickup, and then we take a crack at it. If we had booby-trapped weapons, this would be a good place to put the code.

Generally, ammunition is treated as an item in its own right. The Ammo::onInventory method is called when ammo inventory levels change. Then we can update any mounted images using this ammo to reflect the new state. In the method we cycle through all the mounted weapons to examine each mounted weapon's ammo status.

RadiusDamage is a pretty nifty function that we use to apply explosion effects to objects within a certain distance from where the explosion occurred and to impart an impulse force on each object to move it if called for.

The first statement in the function uses InitContainerRadiusSearch to prepare the container system for use. It basically indicates that the engine is going to search for all objects of the type $TypeMasks::ShapeBaseObjectType located within %radius distance from the location specified by %position. See Table A.1 in Appendix A for a list of available type masks. Once the container radius search has been set up, we then will make successive calls to ContainerSearchNext. Each call will return the handle of the objects found that match the mask we supplied. If the handle is returned as 0, then the search has finished.

So we enter a nicely sized while loop that will continue as long as ContainerSearchNext returns a valid object handle (nonzero) in %targetObject. With each object found, we calculate how much of the object is affected by the explosion but only apply this calculation based on how much of the explosion is blocked by certain types of objects. If an object of one of these types has completely blocked the explosion, then the explosion coverage will be 0.

Then we use the ContainerSearchCurrRadiusDist to find the approximate radius of the affected object and subtract that value from the center-of-explosion to center-of-object distance to get the distance to the nearest surface of the object. Next, damage is applied that is proportional to this distance. If the nearest surface of the object is less than half the radius of the explosion away, then full damage is applied.

Finally, a proportional impulse force vector, if appropriate, is applied using modified distance scale. This has the effect of pushing the object away from the center of the blast.

control/server/weapons/crossbow.cs

For each weapon in our game, we need a definition module that contains the specifics for that weapon—its data blocks, methods, particle definitions (if they are going to be unique to the weapon), and other useful stuff.

There is a lot of material here, so if you want to exclude some stuff to cut back on typing, then leave out all of the particle and explosion data blocks. You won't get any cool-looking explosions or smoke trails, and you will get some error warnings in your console log file, but the weapon will still work.

The crossbow is a somewhat stylized and fantasy-based crossbow—rather medieval in flavor. It fires a burning bolt projectile that explodes like a grenade on impact. It's cool.

Type in the following code and save it as C:\Emaga5\control\server\weapons\crossbow.cs.

 //============================================================================ // control/server/weapons/crossbow.cs // Copyright (c) 2003 Kenneth C. Finney // Portions Copyright (c) 2001 GarageGames.com // Portions Copyright (c) 2001 by Sierra Online, Inc. //============================================================================ datablock ParticleData(CrossbowBoltParticle) {    textureName             = "~/data/particles/smoke";    dragCoefficient        = 0.0;    gravityCoefficient      = -0.2; // rises slowly    inheritedVelFactor      = 0.00;    lifetimeMS               = 500; // lasts 0.7 second    lifetimeVarianceMS      = 150;  // ...more or less    useInvAlpha = false;    spinRandomMin = -30.0;    spinRandomMax = 30.0;    colors[0]     = "0.56 0.36 0.26 1.0";    colors[1]     = "0.56 0.36 0.26 1.0";    colors[2]     = "0 0 0 0";    sizes[0]      = 0.25;    sizes[1]      = 0.5;    sizes[2]      = 1.0;    times[0]      = 0.0;    times[1]      = 0.3;    times[2]      = 1.0; }; datablock ParticleEmitterData(CrossbowBoltEmitter) {    ejectionPeriodMS = 10;    periodVarianceMS = 5;    ejectionVelocity = 0.25;    velocityVariance = 0.10;    thetaMin          = 0.0;    thetaMax          = 90.0;    particles = CrossbowBoltParticle; }; datablock ParticleData(CrossbowExplosionParticle) {    textureName          = "~/data/particles/smoke";    dragCoefficient      = 2;    gravityCoefficient   = 0.2;    inheritedVelFactor   = 0.2;    constantAcceleration = 0.0;    lifetimeMS            = 1000;    lifetimeVarianceMS   = 150;    colors[0]     = "0.56 0.36 0.26 1.0";    colors[1]     = "0.56 0.36 0.26 0.0";    sizes[0]      = 0.5;    sizes[1]      = 1.0; }; datablock ParticleEmitterData(CrossbowExplosionEmitter) {    ejectionPeriodMS = 7;    periodVarianceMS = 0;    ejectionVelocity = 2;    velocityVariance = 1.0;    ejectionOffset   = 0.0;    thetaMin          = 0;    thetaMax          = 60;    phiReferenceVel  = 0;    phiVariance       = 360;    particles        = "CrossbowExplosionParticle"; }; datablock ParticleData(CrossbowExplosionSmoke) {    textureName           = "~/data/particles/smoke";    dragCoefficient     = 100.0;    gravityCoefficient   = 0;    inheritedVelFactor   = 0.25;    constantAcceleration = -0.80;    lifetimeMS           = 1200;    lifetimeVarianceMS  = 300;    useInvAlpha = true;    spinRandomMin = -80.0;    spinRandomMax = 80.0;    colors[0]     = "0.56 0.36 0.26 1.0";    colors[1]     = "0.2 0.2 0.2 1.0";    colors[2]     = "0.0 0.0 0.0 0.0";    sizes[0]      = 1.0;    sizes[1]      = 1.5;    sizes[2]      = 2.0;    times[0]      = 0.0;    times[1]      = 0.5;    times[2]      = 1.0; }; datablock ParticleEmitterData(CrossbowExplosionSmokeEmitter) {    ejectionPeriodMS = 10;    periodVarianceMS = 0;    ejectionVelocity = 4;    velocityVariance = 0.5;    thetaMin          = 0.0;    thetaMax          = 180.0;    lifetimeMS        = 250;    particles = "CrossbowExplosionSmoke"; }; datablock ParticleData(CrossbowExplosionSparks) {    textureName           = "~/data/particles/spark";    dragCoefficient       = 1;    gravityCoefficient    = 0.0;    inheritedVelFactor    = 0.2;    constantAcceleration  = 0.0;    lifetimeMS             = 500;    lifetimeVarianceMS    = 350;    colors[0]    = "0.60 0.40 0.30 1.0";    colors[1]    = "0.60 0.40 0.30 1.0";    colors[2]    = "1.0 0.40 0.30 0.0";    sizes[0]     = 0.5;    sizes[1]     = 0.25;    sizes[2]     = 0.25;    times[0]     = 0.0;    times[1]     = 0.5;    times[2]     = 1.0; }; datablock ParticleEmitterData(CrossbowExplosionSparkEmitter) {    ejectionPeriodMS = 3;    periodVarianceMS = 0;    ejectionVelocity = 13;    velocityVariance = 6.75;    ejectionOffset   = 0.0;    thetaMin          = 0;    thetaMax          = 180;    phiReferenceVel  = 0;    phiVariance       = 360;    overrideAdvances = false;    orientParticles  = true;    lifetimeMS        = 100;    particles = "CrossbowExplosionSparks"; }; datablock ExplosionData(CrossbowSubExplosion1) {    offset = 1.0;    emitter[0] = CrossbowExplosionSmokeEmitter;    emitter[1] = CrossbowExplosionSparkEmitter; }; datablock ExplosionData(CrossbowSubExplosion2) {    offset = 1.0;    emitter[0] = CrossbowExplosionSmokeEmitter;    emitter[1] = CrossbowExplosionSparkEmitter; }; datablock ExplosionData(CrossbowExplosion) {    lifeTimeMS = 1200;    particleEmitter = CrossbowExplosionEmitter; // Volume particles    particleDensity = 80;    particleRadius = 1;    emitter[0] = CrossbowExplosionSmokeEmitter; // Point emission    emitter[1] = CrossbowExplosionSparkEmitter;    subExplosion[0] = CrossbowSubExplosion1; // Sub explosion objects    subExplosion[1] = CrossbowSubExplosion2;    shakeCamera = true;                   // Camera Shaking    camShakeFreq = "10.0 11.0 10.0";    camShakeAmp = "1.0 1.0 1.0";    camShakeDuration = 0.5;    camShakeRadius = 10.0;    lightStartRadius = 6;                // Dynamic light    lightEndRadius = 3;    lightStartColor = "0.5 0.5 0";    lightEndColor = "0 0 0"; }; datablock ProjectileData(CrossbowProjectile) {    projectileShapeName = "~/data/models/weapons/bolt.dts";    directDamage         = 20;    radiusDamage         = 20;    damageRadius         = 1.5;    explosion            = CrossbowExplosion;    particleEmitter     = CrossbowBoltEmitter;    muzzleVelocity      = 100;    velInheritFactor    = 0.3;    armingDelay          = 0;    lifetime             = 5000;    fadeDelay            = 5000;    bounceElasticity    = 0;    bounceFriction      = 0;    isBallistic         = true;    gravityMod = 0.80;    hasLight    = true;    lightRadius = 4.0;    lightColor = "0.5 0.5 0"; }; function CrossbowProjectile::OnCollision(%this,%obj,%col,%fade,%pos,%normal) {    if (%col.getType() & $TypeMasks::ShapeBaseObjectType)       %col.damage(%obj,%pos,%this.directDamage,"CrossbowBolt"); RadiusDamage(%obj,%pos,%this.damageRadius,%this.radiusDamage,"CrossbowBolt",0); } datablock ItemData(CrossbowAmmo) {    category = "Ammo";    className = "Ammo";    shapeFile = "~/data/models/weapons/boltclip.dts";    mass = 1;    elasticity = 0.2;    friction = 0.6;       // Dynamic properties defined by the scripts       pickUpName = "crossbow bolts";    maxInventory = 20; }; datablock ItemData(Crossbow) {    category = "Weapon";    className = "Weapon";    shapeFile = "~/data/models/weapons/crossbow.dts";    mass = 1;    elasticity = 0.2;    friction = 0.6;    emap = true;    pickUpName = "a crossbow";    image = CrossbowImage; }; datablock ShapeBaseImageData(CrossbowImage) {    shapeFile = "~/data/models/weapons/crossbow.dts";    emap = true;    mountPoint = 0;    eyeOffset = "0.1 0.4 -0.6";    correctMuzzleVector = false;    className = "WeaponImage";    item = Crossbow;    ammo = CrossbowAmmo;    projectile = CrossbowProjectile;    projectileType = Projectile;    stateName[0]                      = "Preactivate";    stateTransitionOnLoaded[0]       = "Activate";    stateTransitionOnNoAmmo[0]       = "NoAmmo";    stateName[1]                      = "Activate";    stateTransitionOnTimeout[1]      = "Ready";    stateTimeoutValue[1]              = 0.6;    stateSequence[1]                  = "Activate";    stateName[2]                      = "Ready";    stateTransitionOnNoAmmo[2]       = "NoAmmo";    stateTransitionOnTriggerDown[2] = "Fire";    stateName[3]                     = "Fire";    stateTransitionOnTimeout[3]     = "Reload";    stateTimeoutValue[3]             = 0.2;    stateFire[3]                     = true;    stateRecoil[3]                   = LightRecoil;    stateAllowImageChange[3]        = false;    stateSequence[3]                 = "Fire";    stateScript[3]                   = "onFire";    stateName[4]                     = "Reload";    stateTransitionOnNoAmmo[4]      = "NoAmmo";    stateTransitionOnTimeout[4]     = "Ready";    stateTimeoutValue[4]             = 0.8;    stateAllowImageChange[4]        = false;    stateSequence[4]                 = "Reload";    stateEjectShell[4]               = true;    stateName[5]                      = "NoAmmo";    stateTransitionOnAmmo[5]         = "Reload";    stateSequence[5]                  = "NoAmmo";    stateTransitionOnTriggerDown[5]  = "DryFire";    stateName[6]                      = "DryFire";    stateTimeoutValue[6]             = 1.0;    stateTransitionOnTimeout[6]     = "NoAmmo"; }; function CrossbowImage::onFire(%this, %obj, %slot) {    %projectile = %this.projectile;    %obj.decInventory(%this.ammo,1);    %muzzleVector = %obj.getMuzzleVector(%slot);    %objectVelocity = %obj.getVelocity();    %muzzleVelocity = VectorAdd(       VectorScale(%muzzleVector, %projectile.muzzleVelocity),       VectorScale(%objectVelocity, %projectile.velInheritFactor));    %p = new (%this.projectileType)() {       dataBlock        = %projectile;       initialVelocity = %muzzleVelocity;       initialPosition = %obj.getMuzzlePoint(%slot);       sourceObject    = %obj;       sourceSlot      = %slot;       client          = %obj.client;    };    MissionCleanup.add(%p);    return %p; } 

We will cover the contents of the particle, explosion, and weapon datablocks in detail in later chapters when we start creating our own weapons. Therefore we will skip discussion of these elements for now and focus on the data block's methods.

The first method, and one of the most critical, is the CrossbowProjectile::OnCollision method. When called, it looks first to see if the projectile has collided with the right kind of object. If so, then the projectile's damage value is applied directly to the struck object. The method then calls the RadiusDamage function to apply damage to surrounding objects, if applicable.

When shooting the crossbow, the CrossbowImage::onFire method is used to handle the aspects of firing the weapon that cause the projectile to be created and launched. First, the projectile is removed from inventory, and then a vector is calculated based upon which way the muzzle is facing. This vector is scaled by the specified muzzle velocity of the projectile and the velocity inherited from the movement of the crossbow (which gets that velocity from the movement of the player).

Finally, a new projectile object is spawned into the game world at the location of the weapon's muzzle—the projectile possesses all of the velocity information at the time of spawning, so when added, it immediately begins coursing toward its target.

The projectile is added to the MissionCleanup group before the method exits.

control/server/misc/item.cs

This module contains the code needed to pick up and create items, as well as definitions of specific items and their methods. Type in the following code and save it as C:\Emaga5\control\server\misc\item.cs.

 //============================================================================ // control/server/misc/item.cs //  Copyright (c) 2003 by Kenneth C. Finney. //============================================================================ $RespawnDelay = 20000; $LoiterDelay = 10000; function Item::Respawn(%this) {    %this.StartFade(0, 0, true);    %this.Hide(true);    // Schedule a resurrection    %this.Schedule($RespawnDelay, "Hide", false);    %this.Schedule($RespawnDelay + 10, "StartFade", 3000, 0, false); } function Item::SchedulePop(%this) {    %this.Schedule($LoiterDelay - 1000, "StartFade", 3000, 0, true);    %this.Schedule($LoiterDelay, "Delete"); } function ItemData::OnThrow(%this,%user,%amount) {    // Remove the object from the inventory    if (%amount $= "")       %amount = 1;    if (%this.maxInventory !$= "")       if (%amount > %this.maxInventory)          %amount = %this.maxInventory;    if (!%amount)       return 0;    %user.DecInventory(%this,%amount);    %obj = new Item()       datablock = %this;       rotation = "0 0 1 " @ (GetRandom() * 360);       count = %amount;    };    MissionGroup.Add(%obj);    %obj.SchedulePop();    return %obj; } function ItemData::OnPickup(%this,%obj,%user,%amount) {    %count = %obj.count;    if (%count $= "")       if (%this.maxInventory !$= "") {          if (!(%count = %this.maxInventory))             return;       }       else          %count = 1;    %user.IncInventory(%this,%count);    if (%user.client)       MessageClient(%user.client, 'MsgItemPickup', '\c0You picked up %1', %this.pickup- Name);    if (%obj.IsStatic())       %obj.Respawn();    else       %obj.Delete();    return true; } function ItemData::Create(%data) {    %obj = new Item() {       dataBlock = %data;       static = true;       rotate = true;    };    return %obj; } datablock ItemData(Copper) {    category = "Coins";    // Basic Item properties    shapeFile = "~/data/models/items/kash1.dts";    mass = 0.7;    friction = 0.8;    elasticity = 0.3;    respawnTime = 30 * 60000;    salvageTime = 15 * 60000;    // Dynamic properties defined by the scripts    pickupName = "a copper coin";    value = 1; }; datablock ItemData(Silver) {    category = "Coins";    // Basic Item properties    shapeFile = "~/data/models/items/kash100.dts";    mass = 0.7;    friction = 0.8;    elasticity = 0.3;    respawnTime = 30 * 60000;    salvageTime = 15 * 60000;    // Dynamic properties defined by the scripts    pickupName = "a silver coin";    value = 100; }; datablock ItemData(Gold) {    category = "Coins";    // Basic Item properties    shapeFile = "~/data/models/items/kash1000.dts";    mass = 0.7;    friction = 0.8;    elasticity = 0.3;    respawnTime = 30 * 60000;    salvageTime = 15 * 60000;    // Dynamic properties defined by the scripts    pickupName = "a gold coin";    value = 1000; }; datablock ItemData(FirstAidKit) {    category = "Health";    // Basic Item properties    shapeFile = "~/data/models/items/healthPatch.dts";    mass = 1;    friction = 1;    elasticity = 0.3;    respawnTime = 600000;    // Dynamic properties defined by the scripts    repairAmount = 200;    maxInventory = 0; // No pickup or throw }; function FirstAidKit::onCollision(%this,%obj,%col) {    if (%col.getDamageLevel() != 0 && %col.getState() !$= "Dead" )    {       %col.applyRepair(%this.repairAmount);       %obj.respawn();       if (%col.client)       {          messageClient             (%col.client,'MSG_Treatment','\c2Medical treatment applied');       }    } } 

$RespawnDelay and $LoiterDelay are variables used to manage how long it takes to regenerate static items or how long they take to disappear when dropped.

After an item has been picked, if it is a static item, a new copy of that item will eventually be added to the game world using the Item::respawn method. The first statement in this method fades the object away, smoothly and quickly. Then the object is hidden, just to be sure. Finally, we schedule a time in the future to bring the object back into existence—the first event removes the object from hiding, and the second event fades the object in smoothly and slowly over a period of three seconds.

If we drop an item, we may want to have it removed from the game world to avoid object clutter (and concomitant bandwidth loss).We can use the Item::schedulePop method to make the dropped object remove itself from the world after a brief period of loitering. The first event scheduled is the start of a fade-out action, and after one second the object is deleted.

We can get rid of held items by throwing them using the ItemData::onThrow method. It removes the object from inventory, decrements the inventory count, creates a new instance of the object for inclusion in the game world, and adds it. It then calls the SchedulePop method just described to look after removing the object from the game world.

The ItemData::onPickup method is the one used by all items—it adds the item to the inventory and then sends a message to the client to indicate that the object has been picked up. If the object picked was a static one, it then schedules an event to add a replacement item into the world. If not, then the instance picked is deleted, and we see it no more.

The ItemData::Create method is the catchall object creation method for items. It creates a new data block based upon the passed parameter and sets the static and rotate properties to true before returning.

Next comes a collection of data blocks defining our coin and first-aid items. We will cover first-aid items more in Chapter 16.

The last method of interest is FirstAidKit::onCollision. This method will restore some health, by applying a repair value, to colliding objects if it needs it. Once the treatment has been applied, a message is sent to the client for display.




3D Game Programming All in One
3D Game Programming All in One (Course Technology PTR Game Development Series)
ISBN: 159200136X
EAN: 2147483647
Year: 2006
Pages: 197

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net