home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming All in One / 3D Game Programming All in One Disc.iso / 3D2E / RESOURCES / KOOB / control / server / weapons / crossbow.cs next >
Encoding:
Text File  |  2006-09-24  |  13.8 KB  |  467 lines

  1. //============================================================================
  2. // control/players/crossbow.cs
  3. //
  4. // Copyright (c) 2003,2006 Kenneth C. Finney
  5. // Portions Copyright (c) 2001 GarageGames.Com
  6. // Portions Copyright (c) 2001 by Sierra Online, Inc.
  7. //============================================================================
  8.  
  9. //-----------------------------------------------------------------------------
  10. // Crossbow weapon. *With audio profiles added*.
  11. // This file contains all the items related to this weapon
  12. // including explosions, ammo, the item and the weapon item image.
  13. // These objects rely on the item & inventory support system defined
  14. // in item.cs and inventory.cs
  15. //-----------------------------------------------------------------------------
  16.  
  17. //-----------------------------------------------------------------------------
  18. // Sounds profiles
  19.  
  20. datablock AudioProfile(CrossbowReloadSound)
  21. {
  22.    filename = "control/data/sound/crossbow_reload.ogg";
  23.    description = AudioClose3d;
  24.     preload = true;
  25. };
  26.  
  27. datablock AudioProfile(CrossbowFireSound)
  28. {
  29.    filename = "control/data/sound/crossbow_firing.ogg";
  30.    description = AudioClose3d;
  31.     preload = true;
  32. };
  33.  
  34. datablock AudioProfile(CrossbowFireEmptySound)
  35. {
  36.    filename = "control/data/sound/crossbow_firing_empty.ogg";
  37.    description = AudioClose3d;
  38.     preload = true;
  39. };
  40.  
  41. datablock AudioProfile(CrossbowExplosionSound)
  42. {
  43.    filename = "control/data/sound/crossbow_explosion.ogg";
  44.    description = AudioDefault3d;
  45.     preload = true;
  46. };
  47.  
  48.  
  49. //-----------------------------------------------------------------------------
  50. // Crossbow bolt projectile particles
  51.  
  52. datablock ParticleData(CrossbowBoltParticle)
  53. {
  54.    textureName          = "~/data/particles/smoke";
  55.    dragCoeffiecient     = 0.0;
  56.    gravityCoefficient   = -0.2;   // rises slowly
  57.    inheritedVelFactor   = 0.00;
  58.    lifetimeMS           = 500;  // lasts 0.7 second
  59.    lifetimeVarianceMS   = 150;   // ...more or less
  60.    useInvAlpha = false;
  61.    spinRandomMin = -30.0;
  62.    spinRandomMax = 30.0;
  63.  
  64.    colors[0]     = "0.56 0.36 0.26 1.0";
  65.    colors[1]     = "0.56 0.36 0.26 1.0";
  66.    colors[2]     = "0 0 0 0";
  67.  
  68.    sizes[0]      = 0.25;
  69.    sizes[1]      = 0.5;
  70.    sizes[2]      = 1.0;
  71.  
  72.    times[0]      = 0.0;
  73.    times[1]      = 0.3;
  74.    times[2]      = 1.0;
  75. };
  76.  
  77. datablock ParticleEmitterData(CrossbowBoltEmitter)
  78. {
  79.    ejectionPeriodMS = 10;
  80.    periodVarianceMS = 5;
  81.  
  82.    ejectionVelocity = 0.25;
  83.    velocityVariance = 0.10;
  84.  
  85.    thetaMin         = 0.0;
  86.    thetaMax         = 90.0;
  87.  
  88.    particles = CrossbowBoltParticle;
  89. };
  90.  
  91.  
  92. //-----------------------------------------------------------------------------
  93. // Projectile Explosion
  94.  
  95. datablock ParticleData(CrossbowExplosionParticle)
  96. {
  97.    textureName          = "~/data/particles/smoke";
  98.    dragCoefficient      = 2;
  99.    gravityCoefficient   = 0.2;
  100.    inheritedVelFactor   = 0.2;
  101.    constantAcceleration = 0.0;
  102.    lifetimeMS           = 1000;
  103.    lifetimeVarianceMS   = 150;
  104.  
  105.    colors[0]     = "0.56 0.36 0.26 1.0";
  106.    colors[1]     = "0.56 0.36 0.26 0.0";
  107.  
  108.    sizes[0]      = 0.5;
  109.    sizes[1]      = 1.0;
  110. };
  111.  
  112. datablock ParticleEmitterData(CrossbowExplosionEmitter)
  113. {
  114.    ejectionPeriodMS = 7;
  115.    periodVarianceMS = 0;
  116.    ejectionVelocity = 2;
  117.    velocityVariance = 1.0;
  118.    ejectionOffset   = 0.0;
  119.    thetaMin         = 0;
  120.    thetaMax         = 60;
  121.    phiReferenceVel  = 0;
  122.    phiVariance      = 360;
  123.    overrideAdvances = false;
  124.    particles = "CrossbowExplosionParticle";
  125. };
  126.  
  127. datablock ParticleData(CrossbowExplosionSmoke)
  128. {
  129.    textureName          = "~/data/particles/smoke";
  130.    dragCoeffiecient     = 100.0;
  131.    gravityCoefficient   = 0;
  132.    inheritedVelFactor   = 0.25;
  133.    constantAcceleration = -0.80;
  134.    lifetimeMS           = 1200;
  135.    lifetimeVarianceMS   = 300;
  136.    useInvAlpha =  true;
  137.    spinRandomMin = -80.0;
  138.    spinRandomMax =  80.0;
  139.  
  140.    colors[0]     = "0.56 0.36 0.26 1.0";
  141.    colors[1]     = "0.2 0.2 0.2 1.0";
  142.    colors[2]     = "0.0 0.0 0.0 0.0";
  143.  
  144.    sizes[0]      = 1.0;
  145.    sizes[1]      = 1.5;
  146.    sizes[2]      = 2.0;
  147.  
  148.    times[0]      = 0.0;
  149.    times[1]      = 0.5;
  150.    times[2]      = 1.0;
  151.  
  152. };
  153.  
  154. datablock ParticleEmitterData(CrossbowExplosionSmokeEmitter)
  155. {
  156.    ejectionPeriodMS = 10;
  157.    periodVarianceMS = 0;
  158.    ejectionVelocity = 4;
  159.    velocityVariance = 0.5;
  160.    thetaMin         = 0.0;
  161.    thetaMax         = 180.0;
  162.    lifetimeMS       = 250;
  163.    particles = "CrossbowExplosionSmoke";
  164. };
  165.  
  166. datablock ParticleData(CrossbowExplosionSparks)
  167. {
  168.    textureName          = "~/data/particles/spark";
  169.    dragCoefficient      = 1;
  170.    gravityCoefficient   = 0.0;
  171.    inheritedVelFactor   = 0.2;
  172.    constantAcceleration = 0.0;
  173.    lifetimeMS           = 500;
  174.    lifetimeVarianceMS   = 350;
  175.  
  176.    colors[0]     = "0.60 0.40 0.30 1.0";
  177.    colors[1]     = "0.60 0.40 0.30 1.0";
  178.    colors[2]     = "1.0 0.40 0.30 0.0";
  179.  
  180.    sizes[0]      = 0.5;
  181.    sizes[1]      = 0.25;
  182.    sizes[2]      = 0.25;
  183.  
  184.    times[0]      = 0.0;
  185.    times[1]      = 0.5;
  186.    times[2]      = 1.0;
  187. };
  188.  
  189. datablock ParticleEmitterData(CrossbowExplosionSparkEmitter)
  190. {
  191.    ejectionPeriodMS = 3;
  192.    periodVarianceMS = 0;
  193.    ejectionVelocity = 13;
  194.    velocityVariance = 6.75;
  195.    ejectionOffset   = 0.0;
  196.    thetaMin         = 0;
  197.    thetaMax         = 180;
  198.    phiReferenceVel  = 0;
  199.    phiVariance      = 360;
  200.    overrideAdvances = false;
  201.    orientParticles  = true;
  202.    lifetimeMS       = 100;
  203.    particles = "CrossbowExplosionSparks";
  204. };
  205.  
  206. datablock ExplosionData(CrossbowSubExplosion1)
  207. {
  208.    offset = 1.0;
  209.    emitter[0] = CrossbowExplosionSmokeEmitter;
  210.    emitter[1] = CrossbowExplosionSparkEmitter;
  211. };
  212.  
  213. datablock ExplosionData(CrossbowSubExplosion2)
  214. {
  215.    offset = 1.0;
  216.    emitter[0] = CrossbowExplosionSmokeEmitter;
  217.    emitter[1] = CrossbowExplosionSparkEmitter;
  218. };
  219.  
  220. datablock ExplosionData(CrossbowExplosion)
  221. {
  222.    soundProfile   = CrossbowExplosionSound;
  223.    lifeTimeMS = 1200;
  224.  
  225.    // Volume particles
  226.    particleEmitter = CrossbowExplosionEmitter;
  227.    particleDensity = 80;
  228.    particleRadius = 1;
  229.  
  230.    // Point emission
  231.    emitter[0] = CrossbowExplosionSmokeEmitter;
  232.    emitter[1] = CrossbowExplosionSparkEmitter;
  233.  
  234.    // Sub explosion objects
  235.    subExplosion[0] = CrossbowSubExplosion1;
  236.    subExplosion[1] = CrossbowSubExplosion2;
  237.  
  238.    // Camera Shaking
  239.    shakeCamera = true;
  240.    camShakeFreq = "10.0 11.0 10.0";
  241.    camShakeAmp = "1.0 1.0 1.0";
  242.    camShakeDuration = 0.5;
  243.    camShakeRadius = 10.0;
  244.  
  245.    // Dynamic light
  246.    lightStartRadius = 6;
  247.    lightEndRadius = 3;
  248.    lightStartColor = "0.5 0.5 0";
  249.    lightEndColor = "0 0 0";
  250. };
  251.  
  252.  
  253. //-----------------------------------------------------------------------------
  254. // Projectile Object
  255.  
  256. datablock ProjectileData(CrossbowProjectile)
  257. {
  258.    projectileShapeName = "~/data/models/weapons/bolt.dts";
  259.    directDamage        = 20;
  260.    radiusDamage        = 20;
  261.    damageRadius        = 1.5;
  262.    explosion           = CrossbowExplosion;
  263.    particleEmitter     = CrossbowBoltEmitter;
  264.  
  265.    muzzleVelocity      = 100;
  266.    velInheritFactor    = 0.3;
  267.  
  268.    armingDelay         = 0;
  269.    lifetime            = 5000;
  270.    fadeDelay           = 5000;
  271.    bounceElasticity    = 0;
  272.    bounceFriction      = 0;
  273.    isBallistic         = true;
  274.    gravityMod = 0.80;
  275.  
  276.    hasLight    = true;
  277.    lightRadius = 4.0;
  278.    lightColor  = "0.5 0.5 0";
  279. };
  280.  
  281. function CrossbowProjectile::onCollision(%this,%obj,%col,%fade,%pos,%normal)
  282. {
  283.    // Apply damage to the object all shape base objects
  284.    if (%col.getType() & $TypeMasks::ShapeBaseObjectType)
  285.       %col.damage(%obj,%pos,%this.directDamage,"CrossbowBolt");
  286.  
  287.    // Radius damage is a support scripts defined in radiusDamage.cs
  288.    radiusDamage(%obj,%pos,%this.damageRadius,%this.radiusDamage,"CrossbowBolt",0);
  289. }
  290.  
  291.  
  292. //-----------------------------------------------------------------------------
  293. // Ammo Item
  294.  
  295. datablock ItemData(CrossbowAmmo)
  296. {
  297.    // Mission editor category
  298.    category = "Ammo";
  299.  
  300.    // Add the Ammo namespace as a parent.  The ammo namespace provides
  301.    // common ammo related functions and hooks into the inventory system.
  302.    className = "Ammo";
  303.  
  304.    // Basic Item properties
  305.    shapeFile = "~/data/models/weapons/boltclip.dts";
  306.    mass = 1;
  307.    elasticity = 0.2;
  308.    friction = 0.6;
  309.  
  310.     // Dynamic properties defined by the scripts
  311.     pickUpName = "crossbow bolts";
  312.    maxInventory = 20;
  313. };
  314.  
  315.  
  316. //--------------------------------------------------------------------------
  317. // Weapon Item.  This is the item that exists in the world, i.e. when it's
  318. // been dropped, thrown or is acting as re-spawnable item.  When the weapon
  319. // is mounted onto a shape, the CrossbowImage is used.
  320.  
  321. datablock ItemData(Crossbow)
  322. {
  323.    // Mission editor category
  324.    category = "Weapon";
  325.  
  326.    // Hook into Item Weapon class hierarchy. The weapon namespace
  327.    // provides common weapon handling functions in addition to hooks
  328.    // into the inventory system.
  329.    className = "Weapon";
  330.  
  331.    // Basic Item properties
  332.    shapeFile = "~/data/models/weapons/crossbow.dts";
  333.    mass = 1;
  334.    elasticity = 0.2;
  335.    friction = 0.6;
  336.    emap = true;
  337.  
  338.     // Dynamic properties defined by the scripts
  339.     pickUpName = "a crossbow";
  340.     image = CrossbowImage;
  341. };
  342.  
  343.  
  344. //--------------------------------------------------------------------------
  345. // Crossbow image which does all the work.  Images do not normally exist in
  346. // the world, they can only be mounted on ShapeBase objects.
  347.  
  348. datablock ShapeBaseImageData(CrossbowImage)
  349. {
  350.    // Basic Item properties
  351.    shapeFile = "~/data/models/weapons/crossbow.dts";
  352.    emap = true;
  353.  
  354.    // Specify mount point & offset for 3rd person, and eye offset
  355.    // for first person rendering.
  356.    mountPoint = 0;
  357.    eyeOffset = "0.1 0.4 -0.6";
  358.  
  359.    // When firing from a point offset from the eye, muzzle correction
  360.    // will adjust the muzzle vector to point to the eye LOS point.
  361.    // Since this weapon doesn't actually fire from the muzzle point,
  362.    // we need to turn this off.
  363.    correctMuzzleVector = false;
  364.  
  365.    // Add the WeaponImage namespace as a parent, WeaponImage namespace
  366.    // provides some hooks into the inventory system.
  367.    className = "WeaponImage";
  368.  
  369.    // Projectile && Ammo.
  370.    item = Crossbow;
  371.    ammo = CrossbowAmmo;
  372.    projectile = CrossbowProjectile;
  373.    projectileType = Projectile;
  374.  
  375.    // Images have a state system which controls how the animations
  376.    // are run, which sounds are played, script callbacks, etc. This
  377.    // state system is downloaded to the client so that clients can
  378.    // predict state changes and animate accordingly.  The following
  379.    // system supports basic ready->fire->reload transitions as
  380.    // well as a no-ammo->dryfire idle state.
  381.  
  382.    // Initial start up state
  383.    stateName[0]                     = "Preactivate";
  384.    stateTransitionOnLoaded[0]       = "Activate";
  385.    stateTransitionOnNoAmmo[0]       = "NoAmmo";
  386.  
  387.    // Activating the gun.  Called when the weapon is first
  388.    // mounted and there is ammo.
  389.    stateName[1]                     = "Activate";
  390.    stateTransitionOnTimeout[1]      = "Ready";
  391.    stateTimeoutValue[1]             = 0.6;
  392.    stateSequence[1]                 = "Activate";
  393.  
  394.    // Ready to fire, just waiting for the trigger
  395.    stateName[2]                     = "Ready";
  396.    stateTransitionOnNoAmmo[2]       = "NoAmmo";
  397.    stateTransitionOnTriggerDown[2]  = "Fire";
  398.  
  399.    // Fire the weapon. Calls the fire script which does
  400.    // the actual work.
  401.    stateName[3]                     = "Fire";
  402.    stateTransitionOnTimeout[3]      = "Reload";
  403.    stateTimeoutValue[3]             = 0.2;
  404.    stateFire[3]                     = true;
  405.    stateRecoil[3]                   = LightRecoil;
  406.    stateAllowImageChange[3]         = false;
  407.    stateSequence[3]                 = "Fire";
  408.    stateScript[3]                   = "onFire";
  409.    stateSound[3]                    = CrossbowFireSound;
  410.  
  411.    // Play the relead animation, and transition into
  412.    stateName[4]                     = "Reload";
  413.    stateTransitionOnNoAmmo[4]       = "NoAmmo";
  414.    stateTransitionOnTimeout[4]      = "Ready";
  415.    stateTimeoutValue[4]             = 0.8;
  416.    stateAllowImageChange[4]         = false;
  417.    stateSequence[4]                 = "Reload";
  418.    stateEjectShell[4]               = true;
  419.    stateSound[4]                    = CrossbowReloadSound;
  420.  
  421.    // No ammo in the weapon, just idle until something
  422.    // shows up. Play the dry fire sound if the trigger is
  423.    // pulled.
  424.    stateName[5]                     = "NoAmmo";
  425.    stateTransitionOnAmmo[5]         = "Reload";
  426.    stateSequence[5]                 = "NoAmmo";
  427.    stateTransitionOnTriggerDown[5]  = "DryFire";
  428.  
  429.    // No ammo dry fire
  430.    stateName[6]                     = "DryFire";
  431.    stateTimeoutValue[6]             = 1.0;
  432.    stateTransitionOnTimeout[6]      = "NoAmmo";
  433.    stateSound[6]                    = CrossbowFireEmptySound;
  434. };
  435.  
  436.  
  437. //-----------------------------------------------------------------------------
  438.  
  439. function CrossbowImage::onFire(%this, %obj, %slot)
  440. {
  441.    %projectile = %this.projectile;
  442.  
  443.    // Decrement inventory ammo. The image's ammo state is update
  444.    // automatically by the ammo inventory hooks.
  445.    %obj.decInventory(%this.ammo,1);
  446.  
  447.    // Determin initial projectile velocity based on the
  448.    // gun's muzzle point and the object's current velocity
  449.    %muzzleVector = %obj.getMuzzleVector(%slot);
  450.    %objectVelocity = %obj.getVelocity();
  451.    %muzzleVelocity = VectorAdd(
  452.       VectorScale(%muzzleVector, %projectile.muzzleVelocity),
  453.       VectorScale(%objectVelocity, %projectile.velInheritFactor));
  454.  
  455.    // Create the projectile object
  456.    %p = new (%this.projectileType)() {
  457.       dataBlock        = %projectile;
  458.       initialVelocity  = %muzzleVelocity;
  459.       initialPosition  = %obj.getMuzzlePoint(%slot);
  460.       sourceObject     = %obj;
  461.       sourceSlot       = %slot;
  462.       client           = %obj.client;
  463.    };
  464.    MissionCleanup.add(%p);
  465.    return %p;
  466. }
  467.