Massassi Forums Logo

This is the static archive of the Massassi Forums. The forums are closed indefinitely. Thanks for all the memories!

You can also download Super Old Archived Message Boards from when Massassi first started.

"View" counts are as of the day the forums were archived, and will no longer increase.

ForumsCog Forum → Auto Gun Help Needed
Auto Gun Help Needed
2004-09-07, 1:03 PM #1
This is the cog:

Code:
# Weap Concrifle.cog
# Trying to make it fire automatically when an enemy comes in 
# Player Point of View


symbols

model       povModel=conv.3do                   local
model       weaponMesh=cong.3do                 local

keyframe    mountAnim=ConVmnt.key               local
keyframe    dismountAnim=ConVdis.key            local
keyframe    povfireAnim=ConVpst1.key            local
keyframe    holsterAnim=kyhlstr.key             local

sound       mountSound=df_rif_ready.wav         local
sound       dismountSound=PutWeaponAway01.wav   local
sound       fireSound=concuss5.wav              local
sound       outSound=concuss1.wav               local

flex        fireWait=1.0                        local
flex        powerBoost                          local
flex        autoAimFOV=45                      local
flex        autoAimMaxDist=99999999999                    local
flex        holsterWait                         local

int         dummy                               local

template    projectile=+concbullet              local
template    projectile3=+concblast2             local

thing       player                              local
int         trackID=-1                          local
int         mode                                local
int         holsterTrack                        local

int         selectMode=1                  local

message     activated
message     deactivated
message     selected
message     deselected
message     newplayer
message     autoselect
message     fire
message     timer

end

# ========================================================================================

code

fire:
   // Check that the player is still alive.
   if(GetThingHealth(player) <= 0)
   {
      Return;
   }

   // Check Ammo - If we are out, autoselect best weapon.
   if(GetInv(player, 12) < 8.0)
   {
      PlaySoundThing(outSound, player, 1.0, -1, -1, 0x80);
      if(GetAutoSwitch() & 1)
         SelectWeapon(player, AutoSelectWeapon(player, 1));
      Return;
   }

   SetPOVShake('0.0 -.03 0.0', '4.0 0.0 0.0', .05, 80.0);
   if(mode == 0)
   {
      dummy = FireProjectile(player, projectile, fireSound, 18, '0.02 0.15 0.0', '0 0 0', 1.0, 0x20, autoAimFOV, autoAimMaxDist);
      ChangeInv(player, 12, -8.0);
   }
   else
   {
      dummy = FireProjectile(player, projectile3, fireSound, 18, '0.0 0.0 0.0', '0 0 0', 1.0, 0x20, autoAimFOV, autoAimMaxDist);
      ChangeInv(player, 12, -4.0);
   }

   jkPlayPOVKey(player, povfireAnim, 1, 0x38);

   // Provide a kick backwards
   ApplyForce(player, VectorScale(GetThingLVec(player), -80));

   powerBoost = GetInv(player, 63);
   ChangeFireRate(player, fireWait/powerBoost);
   SetTimerEx(0.01, dummy, mode, 0.0);

   Return;

# ........................................................................................

activated:
   player = GetSourceRef();
   mode = GetSenderRef();

   jkSetWaggle(player, '0.0 0.0 0.0', 0);

   powerBoost = GetInv(player, 63);
   ActivateWeapon( player, fireWait/powerBoost, mode );

   Return;

# ........................................................................................

deactivated:
   player = GetSourceRef();
   mode = GetSenderRef();

   jkSetWaggle(player, '10.0 7.0 0.0', 350);
   DeactivateWeapon( player, mode );

   Return;

# ........................................................................................

selected:

   player = GetSourceRef();

   // Play external mounting animation
   PlayMode(player, 41);

   // Setup the meshes and models.
   jkSetPOVModel(player, povModel);
   SetArmedMode(player, 1);
   jkSetWeaponMesh(player, weaponMesh);
   jkSetWaggle(player, '10.0 7.0 0.0', 350);

   // Play mounting sound.
   PlaySoundThing(mountSound, player, 1.0, -1.0, -1.0, 0x80);

   // Play the animation (NOLOOP + UNIQUE + ENDPAUSE).
   // The animation is held at the last frame after it is played.
   trackID = jkPlayPOVKey(player, mountAnim, 0, 20);
   SetMountWait(player, GetKeyLen(mountAnim));

   // Clear saber flags, and allow activation of the weapon
   jkClearFlags(player, 0x5), 
   SetCurWeapon(player, 9);

   // Check Ammo - If we are out, autoselect best weapon.
   if(GetInv(player, 12) < 2.0)
   {
      if(GetAutoSwitch() != 99)
       SelectWeapon(player, 1);
   }

Return;

# ........................................................................................

deselected:

   PlaySoundThing(dismountSound, player, 1.0, -1, -1, 0x80);

Return;

# ........................................................................................

newplayer:
SetMapModeFlags(0xc);
SetThingMass(player, 9999999999);
Return;

# ........................................................................................

autoselect:
   player = GetSourceRef();

   // If the player has the weapon
   if(GetInv(player, 9) != 0.0)
   {
      // If the player has ammo
      if(GetInv(player, 12) > 7.0)
      {

         ReturnEx(1000.0);
         Return;

      }
      else
      {
      }
   }
   else
   {
   }
   ReturnEx(-1.0);

   Return;

# ........................................................................................

timer:

   if((GetParam(0) == 1))
   {
    Return;
   }
   projectile = GetSenderID();
   target = FirstThingInView(player, 45, 50, 0x400);
   if(target == -1)
   {
    Return;
   }
   if((GetThingCurGeoMode(target) < 3))
   {
    Return;
   }
   if((GetThingFlags(target) & 10))
   {
    Return;
   }
   veldif=VectorSub(GetThingVel(projectile), GetThingVel(target));  
   speed=VectorLen(veldif);  
   distance=VectorDist(GetThingPos(target), GetThingPos(projectile));  
   time=distance / speed;  
   moved=VectorScale(GetThingVel(target), time);  
   meetpos=VectorAdd(GetThingPos(target), moved);  
   lvec=VectorNorm(VectorSub(meetpos, GetThingPos(projectile))); 
   SetThingLook(projectile, lvec); 
   projspeed=VectorLen(GetThingVel(projectile)); 
   SetThingVel(hacked, VectorScale(lvec, projspeed)); 

Return;

end

[Use code tags!]


I would really appreciate any help.....I looked at quib's example and couldnt figure out how to implement it....

Arden Lyn
2004-09-07, 2:30 PM #2
I just took the dflt conc cog and modified it to have an automatic fire on it.
Hopefully this is helpful. :)
May the mass times acceleration be with you.
2004-09-07, 2:32 PM #3
Doh! forgot to attach the cog :o
May the mass times acceleration be with you.
2004-09-07, 2:48 PM #4
My only suggestion is to use
Code:
dummy = FireProjectile(player, projectile, fireSound, 18, '0.02 0.15 0.0', '0 0 0', 1.0, 0x60, autoAimFOV, autoAimMaxDist);

instead of
Code:
dummy = FireProjectile(player, projectile, fireSound, 18, '0.02 0.15 0.0', '0 0 0', 1.0, 0x20, autoAimFOV, autoAimMaxDist);
vel = VectorLen(GetThingVel(dummy));
SetThingLook(dummy, VectorSub(GetThingPos(target), GetThingPos(player)));
SetThingVel(dummy, VectorScale(GetThingLVec(dummy), vel));

to autoaim. It'll save on packetsize and junk. The user will just have to have auto-aiming turned on in the options window.

The auto-aiming should work just about as good, and you won't waste bandwidth sending velocity and lookvector adjustments.

QM
2004-09-07, 5:20 PM #5
Thank you very much. You guys are awsome.
2004-09-07, 6:07 PM #6
Quote:
Originally posted by Quib Mask
The user will just have to have auto-aiming turned on in the options window.

The auto-aiming should work just about as good, and you won't waste bandwidth sending velocity and lookvector adjustments.

QM
Heh, didn't think about having JK set the autoaim...
at least I know I can make a functional autoaim code ;)

And glad we could help. :)
May the mass times acceleration be with you.
2004-09-07, 6:16 PM #7
Hehe i like how it does secondary fire if you are too close to the enemy. Do you think I could customize that?

So if the enemy is close enough that if I used primary it would damage me, it uses secondary fire instead....Like what you did, except the distance line is a little further. How did you do that?


Hehe thank you again this is the greatest cog ever......
2004-09-07, 7:01 PM #8
Quote:
Originally posted by Arden Lyn
Hehe i like how it does secondary fire if you are too close to the enemy. Do you think I could customize that?

So if the enemy is close enough that if I used primary it would damage me, it uses secondary fire instead....Like what you did, except the distance line is a little further. How did you do that?


Hehe thank you again this is the greatest cog ever......

On lines 251-2, it says "if(curdist < 0.5) mode = 1; [breakline] else mode = 0;" (curdist is set in the potential loop, and is the distance between you and your target). To make the distance larger, increase the value in "if(curdist < 0.5)" to something higher like "if(curdist < 0.8)"
May the mass times acceleration be with you.
2004-09-08, 4:21 AM #9
Haha this cog owns...

I see now how to change the distance.
Thank you very much.

Arden Lyn


P.S.

Quib - I took off the few lines and you were right, it works great.
2004-09-08, 7:20 AM #10
Quote:
Originally posted by Arden Lyn:
Thank you very much. You guys are awsome.


Yet again, a prime example of why Massassi Forums is the place to be :). Always willing and (more importantly) able ;) :D

-Jackpot
"lucky_jackpot is the smily god..." -gothicX
"Life is mostly froth and bubble, but two things stand in stone,
Kindness in another's trouble, courage in your own"
- "Ye Wearie Wayfarer"
|| AI Builder: compatible with both JK & MotS || My website ||

↑ Up to the top!