Download Public Code Snippets

Discussion in 'Modifications' started by Armageddon, Dec 16, 2011.

  1. Armageddon

    Armageddon Teapot

    I'm making this thread as a modders resource, I'll add little bits of code for weapons/cores/items whenever i can. Others are welcome to do the same. All code posted in this thread can be used in any manner by any coder for whatever purpose.

    A few requested snippets to start it off.

    Shotgun Fire function, at least 10 requests for this.

    Code:
    //-----------------------------------------------------------------------------
    // Shotgun - Arma 11/28/11
    //-----------------------------------------------------------------------------
     
    // Projectile for the Shotgun.
    datablock LinearProjectileData(ShotgunProjectile)
    {
       projectileShapeName = "legions/data/shapes/weapons/chaingun/tracer.dts";
       scale = "0.43 1 0.43";
       
       hasTracer = true;
       tracerWidth = 0.45; // 0.3;
       tracerLength = 40.0;
       tracerMaterialName = ChaingunTracerMaterial;
       tracerOffset = 0.3; //6
       tracerGrowthScale = 0.05;
       
       // damage
       directDamage = 3.5; //5
       radiusDamage = 0;
       damageRadius = 1.25; //2.25
       areaImpulse = 20; //30
       damageType = $DamageType::ChaingunProjectile;
       
       deleteOnExplode = false;
       
       // effects
       explosionPlayer = "ChaingunBulletExplosionPlayer";
       explosionTerrain = "ChaingunBulletExplosionTerrain";
       explosionObject = "ChaingunBulletExplosionObject";
     
       // velocity
       muzzleVelocity = 800; //1000
       forwardVelInheritFactor = 1.0;
       sideVelInheritFactor = 0.0;
     
       // timing
       armingDelay = 25;
       lifetime = 4000;
       fadeDelay = 3500;
       
       // physics
       isBallistic = false;
       gravityMod = 0.0;
       
       // chaingun ghosts don't (currently, 3/13/08) need this stuff
       sendLifetimeToGhosts = false;
       sendSourceObjectToGhosts = false;
       sendCurrTickToGhosts = false;
       
       doDistanceFade = true;
       startFadeDistance = 300;
       endFadeDistance = 450;
    };
    new ScriptObject(ShotgunIcon)
    {
       class = IconInfo;
       name = Shotgun;
       
       containerIcon = "client/gui/images/hud/weaponIcons/rocketLauncher";
       ammoIcon = "client/gui/images/hud/weaponIcons/BoltLauncherAmmo";
    };
     
    new ScriptObject(ShotgunReticle)
    {
       class = ReticleInfo;
       name = Shotgun;
       
       image = "client/gui/images/hud/reticles/BoltReticle01";
       frames = "2 1";
       
       normalFrame = 0;
       armedFrame = 1;
    };
     
    datablock ItemData(Shotgun)
    {
       class = Weapon;
       image = ShotgunImage;
       
       iconInfo = RocketLauncherIcon;
       reticleInfo = RocketLauncherReticle;
     
       longName = "Shotgun";
       shortName = "SG";
    };
     
    datablock ItemData(ShotgunAmmo)
    {
       class = Ammo;
       item = Shotgun;
    };
     
    // Shotgun image
    datablock ShapeBaseImageData(ShotgunImage)
    {
       //canLockOn = false;
       //lockAcquisitionTime = 3;
       
       class = WeaponImage;
       item = Shotgun;
       ammo = ShotgunAmmo;
       projectile = ShotgunProjectile;
       projectileType = LinearProjectile;
       shapeFile = "legions/data/shapes/Weapons/skybolt/bolttemp.dts";
       emap = true;
       cursorTexture = "client/gui/images/hud/targetingCursor";
     
       burstCount = 1;
       burstDelay = 120;
     
       mountPoint = $WeaponSlot;
       firstPerson = false;
       eyeOffset = "0.7 0.25 -0.5";
       correctMuzzleVector = true;
       ProjectileSpread = 0.8;
     
     
       // Check ammo, do appropriate activate animation
       stateName[0] = "PreActivate";
       stateTransitionOnAmmo[0] = "Activate";
       stateTransitionOnNoAmmo[0] = "ActivateNoAmmo";
       
       stateName[1] = "Activate";
       stateSequence[1] = "activate";
       stateTransitionOnTimeout[1] = "Ready";
       stateTimeoutValue[1] = 0.5;
       
       stateName[2] = "ActivateNoAmmo";
       stateSequence[2] = "activatenoammo";
       stateTransitionOnTimeout[2] = "NoAmmo";
       stateTimeoutValue[2] = 0.5;
       
       stateName[3] = "Ready";
       stateScript[3] = "onReady";
       stateWaitForTimeout[3] = false;
       stateSequence[3] = "ready";
       stateTransitionOnTriggerDown[3] = "Fire";
       stateTransitionOnNoAmmo[3] = "NoAmmo";
       
       stateName[4] = "Fire";
       stateTimeoutValue[4] = 0.17; //was 0.4;
       stateTransitionOnTimeout[4] = "CheckAmmo";
       stateSequence[4] = "fire";
       stateFire[4] = true;
       stateAllowImageChange[4] = false;
       stateFullSound[4] = SkyboltImpactSound;// RocketLauncherFireSound;
       stateScript[4] = "onFire";
       
       stateName[5] = "CheckAmmo";
       stateTransitionOnAmmo[5] = "Reload";
       stateTransitionOnNoAmmo[5] = "NoAmmoStart";
       stateAllowImageChange[5] = false;
       
       stateName[6] = "Reload";
       stateScript[6] = "onReload";
       stateSequence[6] = "reload";
       stateSequenceStartPosition[6] = 0.0;
       stateSequenceEndPosition[6] = 0.4;
       stateTransitionOnTimeout[6] = "Reloaded";
       stateTimeoutValue[6] = 0.17; //was 0.7;
       stateAllowImageChange[6] = false;
       stateFullSound[6] = SkyboltLoadingSound;
       
       stateName[7] = "Reloaded";
       stateSequence[7] = "reload";
       stateSequenceStartPosition[7] = 0.4;
       stateSequenceEndPosition[7] = 1.0;
       stateTransitionOnTimeout[7] = "Ready";
       stateTimeoutValue[7] = 0.17;
       
       stateName[8] = "NoAmmoStart";
       stateSequence[8] = "noammostart";
       stateTransitionOnTimeout[8] = "NoAmmo";
       stateTimeoutValue[8] = 2.0;
       stateWaitForTimeout[8] = false;
       stateTransitionOnTriggerDown[8] = "DryFire";
       stateTransitionOnAmmo[8] = "Reload";
       
       stateName[9] = "NoAmmo";
       stateSequence[9] = "noammo";
       stateTransitionOnTriggerDown[9] = "DryFire";
       stateTransitionOnAmmo[9] = "Reload";
       
       stateName[10] = "DryFire";
       stateTimeoutValue[10] = 0.3;
       stateWaitForTimeout[10] = true;
       stateTransitionOnTimeout[10] = "CheckAmmo";
       stateSequence[10] = "dryfire";
       stateFullSound[10] = RocketLauncherDryFireSound;
    };
     
    function ShotgunImage::onFire(%this, %obj, %slot)
    {
       if (!isObject(%obj))
          return;
     
       if (%obj.getClassName() $= "Player")
          Game.cancelInvincibility(%obj);
     
       %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));
       for(%i = 0; %i < 20; %i++)
       {
          %x = (getRandom() - 0.5) * 0.1 * 3.1415926 * %this.projectileSpread;
          %y = (getRandom() - 0.5) * 0.1 * 3.1415926 * %this.projectileSpread;
          %z = (getRandom() - 0.5) * 0.1 * 3.1415926 * %this.projectileSpread;
          %mat = MatrixCreateFromEuler(%x SPC %y SPC %z);
          %vector = MatrixMulVector(%mat, %muzzleVelocity);
          %p = new (%this.projectileType)()
          {
             dataBlock        = %projectile;
             initialVelocity  = %vector;
             initialPosition  = %obj.getMuzzlePoint(%slot);
             sourceObject     = %obj;
             sourceSlot       = %slot;
             client           = %obj.client;
          };
     MissionCleanup.add(%p);
    }
    }
    Directional Boosts, too many requests.
    Boosts the player in his/her current moving direction. Slightly modified jumppad code.
    This could also be used with function Boost::eek:nUse( %this, %obj). For testing i just threw it in my grenade onThrow function.

    Code:
    function Grenade::onThrow( %this, %obj, %source )
    {
          %BoostImpulse = 5000;
          %BoostImpulseZ = 500;
          Game.cancelInvincibility(%source);
          %moveDir = VectorNormalize(VectorMul(%source.getVelocity(), "1 1 0"));
          %impulse = VectorAdd(VectorScale(%moveDir, %BoostImpulse), "0 0" SPC %BoostImpulseZ);
          %source.applyImpulse("0 0 0", %impulse);
    }
    
     
    skypredator and Volt Cruelerz like this.
  2. Royalty

    Royalty The Aussie

    Very nice. Wish I had my server working...
     
  3. skypredator

    skypredator Member

    A shotgun? Lovely! Thanks. :)
     
  4. Royalty

    Royalty The Aussie

    How do you change the max ammo for a weapon?
    Also, I want to incorporate both of the codes in my server but where do I place them?
     
    xzzxxz likes this.
  5. Armageddon

    Armageddon Teapot

    shotgun code: Make a file named Shotgun.cs in Server/game/datablocks/weapons and paste the code into the .cs file, You'll still have to add the weapon to the loadout system and set maxinventory data in baseplayer.cs found in Server/game/datablocks/preload/BasePlayer.cs or set it in server/game/datablocks/players/TheArmorName.cs

    The boost code is just a small piece of code it can be used as i did above by replacing onthrow or you can create a new function for it.
     
  6. Royalty

    Royalty The Aussie

    How would I create a new function and where would I place it? Also is the boost code much like the code for the "Jumper's Pack In TA?
     
  7. skypredator

    skypredator Member

    I did everything you said to do to get the shotgun to work, but none of my new classes are showing up. Help?
     
  8. Royalty

    Royalty The Aussie

    Post your loadouts.cs file.
     
  9. skypredator

    skypredator Member

    Well, I deleted my newly created loadouts and edited already existing ones. I guess you'll still want it, so here.
     

    Attached Files:

  10. Royalty

    Royalty The Aussie

    I think it should be a cs. file. Try that.
     
  11. skypredator

    skypredator Member

    Triad taught me how to do that, but I had to convert it back to a .txt file to post it up on here.
     
  12. Royalty

    Royalty The Aussie

    Shotgun is so OP. I replaced mortar with SG on my server. Not gonna give the shotty to Outrider or Heavy though.
    Also Arma how can I make it so that the LR doesn't drain energy?
     
    stefygraff likes this.
  13. skypredator

    skypredator Member

    Oh, come on. The SG is like a cork gun at 12m+ :(
     
  14. Volt Cruelerz

    Volt Cruelerz Legions Developer

    This is a modder's resource thread, not Q/A thread...
     
  15. Royalty

    Royalty The Aussie

    Thank you for you help. Now please be nice and answer my question.
     
    1. open the server folder nicely
    2. search for sniper in the search bar, guess which one is the needed file and open it ( try reading the directory of the file first and see if it's nice )
    3. search for energy and read the line, if it's nice do a nice thing with it ( removing or commenting it ( putting // at the start of the line ) nicely)
    4. next time do a nice thing before making yourself look very nice
     
  16. stefygraff

    stefygraff Private Tester

    [​IMG]

    Cool, Shotgun + TDM=<3! :D
     
    skypredator likes this.
  17. Dabbleh

    Dabbleh Legions Developer

    I totally need to start to learn TorqueScript.
     
  18. Royalty

    Royalty The Aussie

    Reporting a shotgun bug. The death message isn't working. It says: Royalty was killed by *blank*
     
    skypredator likes this.
  19. Volt Cruelerz

    Volt Cruelerz Legions Developer

    Not a bug, just hasn't been coded. Open up the deathMessages.cs file and append it to be whatever you want.

    "x was pulped by y"
    "x needs to get off y's lawn"
    "y defended their property against x"

    whatever you like.
     
    xzzxxz and skypredator like this.