View topic - How to get units to shoot multiple weapons



All times are UTC + 1 hour


Post new topic Reply to topic  [ 19 posts ] 
Author Message
PostPosted: 20 Mar 2012, 18:24 

Joined: 10 Feb 2012, 22:22
I am wondering how to get a unit to shoot from a different weapon depending on what weapon is equipped. This is what I have at the moment:

Code:
function script.QueryWeapon1()

   return flare

end

function script.AimFromWeapon1()

   return flare

end



function script.AimWeapon1(heading, pitch)--(weaponID, heading, pitch)
   --Spring.Echo("weapon ID is...",weaponID)
   -- Function that aims the weapon
   if (waterGunAllowed == true or magicWandP == true) then
   Spring.Echo("heyyyyy")
   --weaponID = 1
   --Spring.Echo("in aim weapon!")
   Signal(SIG_AIM)
   SetSignalMask(SIG_AIM)
    -- Each time the Signal is called, all other functions with the same SignalMask will stop running. This makes sure the tank isn't trying to fire at something, and restore the turret position, at the same time.
   Turn(turret, y_axis, heading, math.rad(90))
   --Turn(gun, x_axis, -pitch, math.rad(90))
   --Turn(waterGunVal, x_axis, -pitch, math.rad(90))
   Turn(torso, y_axis, heading, math.rad(90))
   Turn(head, y_axis, heading, math.rad(90))
   Turn(rHand, y_axis, heading, math.rad(90))
   Turn(rFoot, y_axis, heading, math.rad(90))
   Turn(lFoot, y_axis, heading, math.rad(90))
   WaitForTurn(turret, y_axis)
   --WaitForTurn(waterGunVal, x_axis)
   --WaitForTurn(gun, x_axis)
   WaitForTurn(head, y_axis)
   WaitForTurn(rHand, y_axis)
   WaitForTurn(rFoot, y_axis)
   WaitForTurn(lFoot, y_axis)
   StartThread(RestoreAfterDelay)
   --Spring.Echo("in aim weapon2!")
   -- else
   -- -- Spring.CallCOBScript(unitID, "ChangeWeapon", 1)
   -- --weaponID = 2
   -- Spring.Echo("weapon ID is...",weaponID)
   -- Signal(SIG_AIM)
   -- SetSignalMask(SIG_AIM)
   -- Move(lHand, z_axis, 15, 20)
   StartThread(RestoreAfterDelay)
   --else script.AimWeapon2(heading, pitch)
      return true
   else
      return false
   end
end

function script.FireWeapon1()
   Spring.Echo("fire!")
end

function script.QueryWeapon2()
   --if(waterGunAllowed ~= true) then
   Spring.Echo("I'm querying properly")
      return rHand
   --end
end



function script.AimFromWeapon2()
   --if(waterGunAllowed ~= true) then
   Spring.Echo("aiming from proper weapon!")
      return rHand
   --end
end

function script.AimWeapon2(heading, pitch)
   --Spring.CallCOBScript(unitID, "ChangeWeapon", 1)
   Spring.Echo("aim weapon 2!")
   if(waterGunAllowed ~= true) then
   Spring.Echo("Fist!")
   Signal(SIG_AIM)
   SetSignalMask(SIG_AIM)
   Move(rHand, z_axis, 15, 20)
   StartThread(RestoreAfterDelay)
   return true
   else return false
   end
end

function script.FireWeapon2()
   Spring.Echo("fire2!")
end


The weapons are all defined in the unitdef. Right now it sometimes performs the sounds of the first weapon when it should be actually firing the second weapon, which it never does. Thank you for any help!


Top
 Offline Profile  
 
PostPosted: 20 Mar 2012, 18:36 
Content Developer
User avatar

Joined: 13 Jan 2005, 00:46
Location: ModalitÃ
Unit script
Code:
piece head,base,left,right,l_arm,l_forearm,r_arm,r_forearm,gun1,rocket_r,rocket_l,flare1,cod,right_l,left_l,shin_l,shin_r,
foot_r,foot_l,rl1,rl2,rlgun1,rlflare1,rlgun2,rlflare2,pole_l,pole_r,
dustl,dustr,thrust1,thrust2,thrust3,thrust4,thrust5,thrust6,thrust7,thrust8;


static-var ISMOVING, bAiming, terraintype,onwater, bCanAim,shooting_num,shooting_nump,weaponchosen,boost,lastheight,lastslope;

#define SIG_AIM            2
#define SIG_AIM_2         4
#define SIG_AIM_3         8

#define dirt                1024+0
#define kampfertail          1024+1
#define shotgun             1024+2
#define redmuzzle             1024+3
#define shotgunspray         1024+4

#include "/headers/definitions.h"

//Every time the unit is moving on a slope steeper than BOOST_SLOPE_THRSHOLD the boost value is increased by one (otherwise decreased)
//Once the boost goes over BOOST_THRESHOLD the unit will use its boosters
//The boost will not go higher than BOOST_LIMIT so the cooldown after the end of the slope won't take long
#define BOOST_SLOPE_THRESHOLD [1]
#define BOOST_THRESHOLD 2
#define BOOST_LIMIT 4
#define REGULAR_SPEED 6*65536
#define BOOST_SPEED 15*65536

/*------------------------------------------------------   
/  run animations
/-----------------------------------------------------*/
walk()
{
   turn right_l    to y-axis <0>       speed <135>;
   turn left_l    to y-axis <0>       speed <130>;
   
   turn right_l    to z-axis <0>       speed <135>;
   turn left_l    to z-axis <0>       speed <130>;
   turn foot_l    to z-axis <0>       speed <130>;
   turn foot_r    to z-axis <0>       speed <130>;
      
   turn base      to x-axis <25>       speed <395>;
   
   if (weaponchosen)
   {
   turn l_forearm  to x-axis <0>       speed <250>;
   turn r_forearm  to x-axis <0>       speed <250>; 
   turn rlgun2     to x-axis <0>       speed <350>;
   turn rlgun1     to x-axis <0>       speed <350>;
   }
   else
   {      
   turn r_forearm  to x-axis <-55>    speed <350>;
   turn l_forearm  to x-axis <-55>    speed <350>;
   }

      
   if(ISMOVING)
   {

   turn base      to y-axis <0>       speed <395>;
   turn right      to x-axis <0>       speed <150>;

      
   turn left      to x-axis <45>       speed <450>;
   
   turn left_l     to x-axis <-50>    speed <430>;
   turn shin_l    to x-axis <40>       speed <330>;
   turn foot_l     to x-axis <20>       speed <535>;
   
   emit-sfx       dirt             from dustr;
   
   turn foot_r     to x-axis <-20>    speed <535>;
      
   turn base       to y-axis <5>       speed <395>;

   turn right_l    to x-axis <75>       speed <395>;
   turn shin_r     to x-axis <20>       speed <330>;
   turn foot_l     to x-axis <25>       speed <535>;
   sleep 240;
   }
   if(ISMOVING)
   {

   turn base       to y-axis <0>       speed <395>;
   turn right      to x-axis <45>       speed <450>;
   turn left      to x-axis <0>       speed <150>;
   
   turn right_l    to x-axis <-50>    speed <430>;
   turn shin_r     to x-axis <40>       speed <435>;
   turn foot_r     to x-axis <20>       speed <535>;
   
   emit-sfx       dirt             from dustl;
   
   turn foot_l     to x-axis <-20>    speed <535>;

   turn base       to y-axis <-5>       speed <395>;
   
   turn left_l     to x-axis <75>       speed <395>;
   turn shin_l     to x-axis <20>       speed <330>;   
   turn foot_r     to x-axis <25>       speed <535>;

   sleep 240;
   }
   turn right      to x-axis <0>       speed <350>;
   turn left      to x-axis <0>       speed <350>;
}

waterwalk()
{
   turn right_l    to y-axis <0>       speed <135>;
   turn left_l    to y-axis <0>       speed <130>;
   
   turn right_l    to z-axis <0>       speed <135>;
   turn left_l    to z-axis <0>       speed <130>;
   turn foot_l    to z-axis <0>       speed <130>;
   turn foot_r    to z-axis <0>       speed <130>;
      
   turn base      to x-axis <25>       speed <395>;
   

   turn r_forearm  to x-axis <-35>    speed <350>;
   turn l_forearm  to x-axis <-35>    speed <350>;
   turn rlgun2     to x-axis <15>       speed <350>;
   turn rlgun1     to x-axis <15>       speed <350>;
   
   if(ISMOVING)
   {
   turn right      to x-axis <45>       speed <550>;
   turn left      to x-axis <45>       speed <550>;
   
   turn left_l     to x-axis <50>       speed <430>;
   turn shin_l    to x-axis <40>       speed <330>;
   turn foot_l     to x-axis <20>       speed <535>;

   turn right_l     to x-axis <50>       speed <430>;
   turn shin_r    to x-axis <40>       speed <330>;
   turn foot_r     to x-axis <20>       speed <535>;
      
   emit-sfx       kampfertail         from thrust1;
   emit-sfx       kampfertail       from thrust2;
   emit-sfx       kampfertail       from thrust3;
   emit-sfx       dirt             from dustr;
   emit-sfx       dirt             from dustl;
   sleep 240;
   }
   
   turn right       to x-axis <0>       speed <235>;
   turn left       to x-axis <0>       speed <230>;   
}

watershoot()
{
   turn right_l    to y-axis <0>       speed <135>;
   turn left_l    to y-axis <0>       speed <130>;
   
   turn right_l    to z-axis <0>       speed <135>;
   turn left_l    to z-axis <0>       speed <130>;
   turn foot_l    to z-axis <0>       speed <130>;
   turn foot_r    to z-axis <0>       speed <130>;
   
   if (weaponchosen)
   {
   turn l_forearm  to x-axis <0>       speed <250>;
   turn r_forearm  to x-axis <0>       speed <250>; 
   }
   else
   {   turn r_forearm  to x-axis <-90>    speed <250>;     }
   
   turn rlgun2     to x-axis <0>       speed <350>;
   turn rlgun1     to x-axis <0>       speed <350>;
            
   if(ISMOVING)
   {
         
   turn left_l     to x-axis <50>       speed <430>;
   turn shin_l    to x-axis <40>       speed <330>;
   turn foot_l     to x-axis <20>       speed <535>;

   turn right_l     to x-axis <50>       speed <430>;
   turn shin_r    to x-axis <40>       speed <330>;
   turn foot_r     to x-axis <20>       speed <535>;
      
   emit-sfx       dirt             from dustr;
   emit-sfx       dirt             from dustl;
   sleep 240;
   }
}

shootwalk()
{
   turn right_l    to y-axis <0>       speed <135>;
   turn left_l    to y-axis <0>       speed <130>;
   
   turn right_l    to z-axis <0>       speed <135>;
   turn left_l    to z-axis <0>       speed <130>;
   turn foot_l    to z-axis <0>       speed <130>;
   turn foot_r    to z-axis <0>       speed <130>;
   if (weaponchosen)
   {
   turn l_forearm  to x-axis <0>          speed <250>;
   turn r_forearm  to x-axis <0>          speed <250>; 
   }
   else
   {   
      if (baiming)
      {   turn r_forearm  to x-axis    <-90>    speed <350>;   }
      else
      {   turn r_forearm  to x-axis    <0>    speed <250>;   }
   }

   turn rlgun2     to x-axis <0>       speed <350>;
   turn rlgun1     to x-axis <0>       speed <350>;
            
   if(ISMOVING)
   {
   turn left_l     to x-axis <-50>    speed <430>;
   turn shin_l    to x-axis <40>       speed <330>;
   turn foot_l     to x-axis <20>       speed <535>;
   
   emit-sfx       dirt             from dustr;
   
   turn foot_r     to x-axis <-20>    speed <535>;
      
   turn right_l    to x-axis <75>       speed <395>;
   turn shin_r     to x-axis <20>       speed <330>;
   turn foot_l     to x-axis <25>       speed <535>;
   sleep 240;
   }
   if(ISMOVING)
   {   
   turn right_l    to x-axis <-50>    speed <430>;
   turn shin_r     to x-axis <40>       speed <435>;
   turn foot_r     to x-axis <20>       speed <535>;
   
   emit-sfx       dirt             from dustl;
   
   turn foot_l     to x-axis <-20>    speed <535>;

   turn left_l     to x-axis <75>       speed <395>;
   turn shin_l     to x-axis <20>       speed <330>;   
   turn foot_r     to x-axis <25>       speed <535>;

   sleep 240;
   }
   
}

stopwalk ()
{
   turn base      to x-axis <0>       speed <395>;
   turn foot_l      to x-axis <0>       speed <395>;
   turn foot_r      to x-axis <0>       speed <395>;
   turn right_l    to x-axis <0>       speed <235>;
   turn left_l    to x-axis <0>       speed <230>;
   turn shin_l     to x-axis <0>       speed <235>;
   turn shin_r    to x-axis <0>       speed <230>;
   
   if (weaponchosen)
   {
   turn l_forearm  to x-axis <0>       speed <250>;
   turn r_forearm  to x-axis <0>       speed <250>;        
   turn rlgun2     to x-axis <0>       speed <350>;
   turn rlgun1     to x-axis <0>       speed <350>;
   }
   else
   {
   turn r_forearm  to x-axis <0>    speed <250>; 
      if (baiming)
      {   turn r_forearm  to x-axis    <-90>    speed <350>;   }
      else
      {   turn r_forearm  to x-axis    <0>    speed <250>;   }
   }
      
   turn right_l    to y-axis <-20>    speed <135>;
   turn left_l    to y-axis <20>       speed <130>;
   
   turn right_l    to z-axis <3>       speed <135>;
   turn left_l    to z-axis <-3>       speed <130>;
   turn foot_l    to z-axis <3>       speed <130>;
   turn foot_r    to z-axis <-3>      speed <130>;
}

stopwater()
{
   turn base      to x-axis <0>       speed <395>;
   turn foot_l      to x-axis <40>       speed <395>;
   turn foot_r      to x-axis <40>       speed <395>;
   turn right_l    to x-axis <0>       speed <235>;
   turn left_l    to x-axis <0>       speed <230>;
   turn shin_l     to x-axis <0>       speed <235>;
   turn shin_r    to x-axis <0>       speed <230>;
   
   if (weaponchosen)
   {
   turn l_forearm  to x-axis <0>       speed <250>;
   turn r_forearm  to x-axis <0>       speed <250>; 
   turn rlgun2     to x-axis <0>       speed <350>;
   turn rlgun1     to x-axis <0>       speed <350>;
   }
   else
   {
      if (baiming)
      {   turn r_forearm  to x-axis    <-90>    speed <350>;   }
      else
      {   turn r_forearm  to x-axis    <-55>    speed <350>;   }
      
   turn l_forearm  to x-axis <-55>    speed <350>;
   }
         
   turn right_l    to y-axis <-10>    speed <135>;
   turn left_l    to y-axis <10>       speed <130>;
   
   turn right_l    to z-axis <5>       speed <135>;
   turn left_l    to z-axis <-5>       speed <130>;
   turn foot_l    to z-axis <5>       speed <130>;
   turn foot_r    to z-axis <-5>      speed <130>;
   
   emit-sfx       dirt             from dustr;
   emit-sfx       dirt             from dustl;
}
/*------------------------------------------------------   
/  motion functions
/-----------------------------------------------------*/
StartMoving()
{   ISMOVING = TRUE;  }

StopMoving()
{   ISMOVING = FALSE; }


Flamingass()
   {
      while(TRUE)
      {
         if (onwater || boost > BOOST_THRESHOLD)      
         {
         emit-sfx       kampfertail       from thrust1;
         emit-sfx       kampfertail       from thrust2;
         emit-sfx       kampfertail         from thrust3;
         emit-sfx       kampfertail       from thrust4;
         emit-sfx       kampfertail       from thrust5;
         emit-sfx       kampfertail         from thrust6;
         emit-sfx       kampfertail       from thrust7;
         emit-sfx       kampfertail         from thrust8;
         }      
      sleep 130;
      }

   }
   

SlopeDetection() {
   while (1) {
      if (ISMOVING)
      {
         lastslope = get GROUND_HEIGHT(get PIECE_XZ(base)) - lastheight - BOOST_SLOPE_THRESHOLD ;
         if ( (boost <= BOOST_LIMIT) && lastslope > 0) boost = boost + 1;
         else if (boost > 0) boost = boost - 1;
         lastheight = get GROUND_HEIGHT(get PIECE_XZ(base));
      }
      else if(boost > BOOST_THRESHOLD) boost = boost - 1;
      sleep 60;
   }
}


MotionControl(pheh)
   {   
   pheh = 1;

      while (pheh)
      {       
         if (ISMOVING)
         {
            IF (onwater)
            {
               if (baiming)
               {
               bCanAim = TRUE;
               CALL-SCRIPT watershoot();
               }
                  else
                  {
                  bCanAim = FALSE;
                  CALL-SCRIPT waterwalk();
                  }
            }
            IF (!onwater)
            {
               if (boost > BOOST_THRESHOLD) {
                  while (boost > BOOST_THRESHOLD) {
                     set MAX_SPEED to BOOST_SPEED;
                     if (baiming) {
                        bCanAim = TRUE;
                        CALL-SCRIPT watershoot();
                     }
                     else {
                        bCanAim = FALSE;
                        CALL-SCRIPT waterwalk();
                     }
                     sleep 30;
                  }
                  if (onwater) set MAX_SPEED to 9*65536;
                  else set MAX_SPEED to 6*65536;
               }
               else if (baiming)
               {
                  bCanAim = TRUE;
                  CALL-SCRIPT shootwalk();
               }
               else
               {
                  bCanAim = FALSE;
                  CALL-SCRIPT walk();
               }
            }                               
         }//walking
         else
         {               
            if (!onwater)
            {      
            bCanAim = TRUE;
            CALL-SCRIPT stopwalk();
            }
            if (onwater)
            {      
            bCanAim = TRUE;
            CALL-SCRIPT stopwater();
            }
         
         }//not walking
         sleep 10;
      }
   }
   
setSFXoccupy(setSFXoccupy_argument)
{
   terraintype = setSFXoccupy_argument;
   
      if(terraintype == 2)
      {
      SET MAX_SPEED to 9*65536;
      onwater = 1;   
      }
         else
         {
         SET MAX_SPEED to 6*65536;
         onwater = 0;   
         }
}
   
//------------------------------------------------------
//start ups :)
//------------------------------------------------------
Create()
   {
   // Initial State
   ISMOVING = FALSE;
   bAiming = FALSE;
   bCanAim = TRUE;
   shooting_num=1;
   shooting_nump=1;
   weaponchosen=1;
   onwater = 0;
   boost=0;
   hide flare1;
   hide rlflare1;
   hide rlflare2;
   hide gun1;
   hide rl1;
   hide rl2;      

   start-script MotionControl();   
   start-script setSFXoccupy();
   start-script Flamingass();
   start-script SlopeDetection();
   }
   
SwitchWeapon()
   {
      if (weaponchosen == 1 )
      {
         weaponchosen =0;
         show gun1;
         show rl1;
         show rl2;   
         hide rlgun1;
         hide rlgun2;
      }
      else
      {
         weaponchosen =1;
         show rlgun1;
         show rlgun2;
         hide gun1;
         hide rl1;
         hide rl2;   
      }
   }


   
AimFromWeapon1(piecenum)
   {   piecenum=head;   }

QueryWeapon2(piecenum)
   {   piecenum=flare1; }

AimFromWeapon2(piecenum)
   {   piecenum=rlgun1;   }
   
QueryWeapon1(piecenum)
   {
    if (shooting_num==1)
    {    piecenum=rlflare1;     }
     
     if (shooting_num==2)
     {   piecenum=rlflare2;     }
   }
   
QueryWeapon3(piecenum)
   {
    if (shooting_nump==1)
    {    piecenum=rocket_r;     }
     
     if (shooting_nump==2)
     {   piecenum=rocket_l;     }
   }
//---------------------------------------------------------------------
//gun functions;
//---------------------------------------------------------------------   
RestoreAfterDelay()
   {
   sleep 1000;
   
   turn base       to y-axis <0> speed <435>;
   turn r_arm      to x-axis <0> speed <430>;
   turn left      to x-axis <0> speed <450>;
   turn right      to x-axis <0> speed <450>;
   turn l_forearm  to x-axis <0> speed <450>;
   turn l_forearm  to z-axis <0> speed <450>;
   
   turn r_forearm  to x-axis <0>    speed <250>;
   
   turn pole_l  to x-axis <0> speed <450>;
   turn pole_r  to x-axis <0> speed <450>;
   bAiming = FALSE;
   }

AimWeapon2(heading,pitch)
   {
   signal SIG_AIM;
   set-signal-mask SIG_AIM;
   
   // Announce that we would like to aim, and wait until we can
   bAiming = TRUE;
      while (!bCanAim || weaponchosen)
      {   sleep 100;   }
      
      turn r_forearm  to x-axis    <-90>          speed <350>;
      turn base       to y-axis    heading       speed <425>;
      turn right      to x-axis    (<-0>-pitch)    speed <490>;      
      turn left      to x-axis    <0>          speed <250>;   
      wait-for-turn r_forearm  around x-axis;
      wait-for-turn right around x-axis;
      wait-for-turn head around y-axis;
         
      start-script RestoreAfterDelay();
      
      return (TRUE);
   }

FireWeapon2()
   {
         move right to z-axis [-0.5] now;
         //emit-sfx 2048+1       from flare1;
         emit-sfx shotgun       from flare1;
         move right to z-axis [0] speed [3];
   }

      
AimWeapon1(heading,pitch)
   {
   signal SIG_AIM_2;
   set-signal-mask SIG_AIM_2;

   // Announce that we would like to aim, and wait until we can
   while (!bCanAim || !weaponchosen)
      {   sleep 100;   }
      
      turn base       to y-axis heading       speed <425>;
      turn left       to x-axis (<-90>-pitch) speed <450>;
      turn right       to x-axis (<-90>-pitch) speed <450>;
                  
      wait-for-turn head  around y-axis;
      wait-for-turn left    around x-axis;
      wait-for-turn right around x-axis;
      start-script RestoreAfterDelay();
   
      return(TRUE);
   }
      
FireWeapon1()
   {
         if( shooting_num == 1)
         {
         move right  to z-axis [-1.0] now;
         emit-sfx redmuzzle from rlflare2;
         move right  to z-axis [0] speed [3];
         }

         if( shooting_num == 2)
         {
         move left  to z-axis [-1.0] now;
         emit-sfx redmuzzle from rlflare1;
         move left  to z-axis [0] speed [3];
         }
   
         shooting_num=shooting_num+1;
      
         if( shooting_num == 3)
         {   shooting_num=1;   }

   }

   
AimWeapon3(heading,pitch)
   {
   signal SIG_AIM_3;
   set-signal-mask SIG_AIM_3;
   
   // Announce that we would like to aim, and wait until we can
   while (NOT bCanAim || ISMOVING || (terraintype == 2 || terraintype == 3))
      {   sleep 100;   }
      
      turn pole_r to x-axis (<-45>+pitch) speed <450>;
      turn pole_l to x-axis (<-45>+pitch) speed <450>;
               
      wait-for-turn pole_r around x-axis;         
      wait-for-turn pole_l around x-axis;
   
      return(TRUE);
   }
   
ShowLegMissiles() {
   sleep 120;
   show rocket_r;

   sleep 120;
   show rocket_l;
}   
      
FireWeapon3()
   {
      if( shooting_nump == 1)
      {
         sleep 120;
         hide rocket_r;
      }

      if( shooting_nump == 2)
      {
         sleep 120;
         hide rocket_l;
      }
   
      shooting_nump=shooting_nump+1;
      
      if( shooting_nump == 3)
      {
         call-script ShowLegMissiles();
         shooting_nump=1;
            
      }

   }

Killed( severity, corpsetype )
   {
   if (severity <= 25)
      {
      corpsetype = 1;
      explode base type      FALL;
      explode cod type      BITMAPONLY;
      explode head type      FALL;
      return( 0 );
      }

   if (severity <= 50)
      {
      corpsetype = 2;
      explode base type      FALL;
      explode cod type      FALL;
      explode head type      FALL;
      return( 0 );
      }

   if (severity <= 99)
      {
      corpsetype = 3;
      explode base type      BITMAPONLY;
      explode cod type      BITMAPONLY;
      explode head type      BITMAPONLY;
      return( 0 );
      }

   corpsetype = 3;
   explode base type      BITMAPONLY;
   explode cod type      BITMAPONLY;
   explode head type      FALL;
   return( 0 );
   }



Weapon switch command

Code:
function gadget:GetInfo()
   return {
      name = "Kampfer weapons",
      desc = "Switch kampfer weapons",
      author = "Smoth",
      date = "11-20-2010",
      license = "Public Domain",
      layer = 20, --must be greater than 10 (specialability.lua's layer)
      enabled = true
   }
end
local EditUnitCmdDesc      = Spring.EditUnitCmdDesc
local FindUnitCmdDesc      = Spring.FindUnitCmdDesc
local GetFeatureTeam      = Spring.GetFeatureTeam
local InsertUnitCmdDesc      = Spring.InsertUnitCmdDesc

local CMD_SET_KweaponsCMD   = 458238


if (gadgetHandler:IsSyncedCode()) then

   local Kweaponscmd= {
      id      = CMD_SET_KweaponsCMD,
      name   = "KWeapons",
      tooltip   = "Switch Weapon",
      type   = CMDTYPE.ICON_MODE,
      params   = { '0', 'Switch\nto\nShotgun', 'Switch\nto\nRockets'},
      texture   = "bitmaps/ui/command_buttons/cmd_kampferon.png",
   }

   function gadget:UnitCreated(unitID, unitDefID, unitTeam, builderID)
      if ( unitDefID == UnitDefNames.kampfer.id ) then
         InsertUnitCmdDesc(unitID, 500, Kweaponscmd)
         featureplacer = unitID
      end
   end
   
   function gadget:AllowCommand(unitID, unitDefID, teamID, cmdID, cmdParams, cmdOptions)
   if ( unitDefID == UnitDefNames.kampfer.id ) then
      if (cmdID == CMD_SET_KweaponsCMD) then
      
         local cmdDescID = FindUnitCmdDesc(unitID, CMD_SET_KweaponsCMD)
         
         Kweaponscmd.params[1] = cmdParams[1]
            EditUnitCmdDesc(unitID, cmdDescID, Kweaponscmd)
         Kweaponscmd.params[1] = 1

         if cmdParams[1] == 1 then
            Kweaponscmd.texture="bitmaps/ui/command_buttons/cmd_kampferoff.png"
         else
            Kweaponscmd.texture="bitmaps/ui/command_buttons/cmd_kampferon.png"
         end
         
         Spring.CallCOBScript(unitID, "SwitchWeapon",0)
         return false
      end
   end
      return true
   end
end


Top
 Offline Profile  
 
PostPosted: 20 Mar 2012, 18:43 
Content Developer
User avatar

Joined: 13 Jan 2005, 00:46
Location: ModalitÃ
You will need a command to toggle the weapon

and a way to link the script and weapon toggle

see the switch weapon function in my bos script

I could give you a more clean example but I am just taking a break right now at work and don't have enough time to give you a much cleaner example


Top
 Offline Profile  
 
PostPosted: 20 Mar 2012, 23:41 

Joined: 10 Feb 2012, 22:22
Thanks for the help. I have been trying to implement it without creating a gadget. Here is the code I have for that portion:

Code:
local function RestoreAfterDelay(unitID)
    -- Defines a local funtion to wait a bit, then move the turret back to how it was originally.
   Sleep(RESTORE_DELAY)
   if(waterGunAllowed == true) then
   Turn(turret, y_axis, 0, math.rad(90))
   --Turn(gun, x_axis, 0, math.rad(90))
   --rn(waterGunVal, x_axis, 0, math.rad(90))
   Turn(torso, y_axis, 0, math.rad(90))
   Turn(head, y_axis, 0, math.rad(90))
   Turn(rHand, y_axis, 0, math.rad(90))
   Turn(rFoot, y_axis, 0, math.rad(90))
   Turn(lFoot, y_axis, 0, math.rad(90))
   else
   Move(lHand, z_axis, 0, 20)
   end
end

function script.QueryWeapon2()
   --if(waterGunAllowed ~= true) then
   --Spring.Echo("I'm querying properly")
   --Spring.Echo("querying le 2")
      return rHand
   --end
end

function script.AimFromWeapon2()
   --if(waterGunAllowed ~= true) then
   --Spring.Echo("aiming from proper weapon!")
   --Spring.Echo("aiming from le 2")
      return rHand
   --end
end

function script.AimWeapon2(heading, pitch)
   --Spring.CallCOBScript(unitID, "ChangeWeapon", 1)
   Spring.Echo("aim weapon 2!")
   Signal(SIG_AIM)
   SetSignalMask(SIG_AIM)
   
   --if(waterGunAllowed ~= true) then
   Spring.Echo("I am fist!")
   while(weaponChosen ~= 1) do
      Sleep (100)
   end
   
   Spring.Echo("Fist!")
   
   Move(rHand, z_axis, 15, 20)
   StartThread(RestoreAfterDelay)
   return true
   -- else return false
   -- end
end

function script.FireWeapon2()
   Spring.Echo("fire2!")
end

function script.QueryWeapon1()
   -- The piece that the bullet/laser/whatever comes out of. FLARE
   --if (waterGunAllowed == true or magicWandP == true) then
   return flare
   -- else
   -- return lHand
   --else script.QueryWeapon2()
   --end
end

function script.AimFromWeapon1()
   -- The unit looks from this piece down the QueryWeapon piece, to see whether it's aiming at anything. FLARE
   --if (waterGunAllowed == true or magicWandP == true) then
   return flare
   -- else
   -- return lHand
   --end
   --else script.AimFromWeapon2()
   --end
end

function SwitchWeapon()
   if (waterGunP == true) then
      weaponChosen = 0
   elseif (magicWandP == true) then
      weaponChosen = 2
   else
      weaponChosen = 1
   end
end
      

function script.AimWeapon1(heading, pitch)--(weaponID, heading, pitch)
   --Spring.Echo("weapon ID is...",weaponID)
   -- Function that aims the weapon
   --if (waterGunAllowed == true or magicWandP == true) then
   Spring.Echo("heyyyyy")
   --weaponID = 1
   --Spring.Echo("in aim weapon!")
   Signal(SIG_AIM_2)
   SetSignalMask(SIG_AIM_2)
   --while(!bCanAim || weaponchosen != 0)
   while(weaponChosen ~=0) do
      Sleep(100)
   end
   Spring.Echo("aiming when I shouldn't")
    -- Each time the Signal is called, all other functions with the same SignalMask will stop running. This makes sure the tank isn't trying to fire at something, and restore the turret position, at the same time.
   Turn(turret, y_axis, heading, math.rad(90))
   --Turn(gun, x_axis, -pitch, math.rad(90))
   --Turn(waterGunVal, x_axis, -pitch, math.rad(90))
   Turn(torso, y_axis, heading, math.rad(90))
   Turn(head, y_axis, heading, math.rad(90))
   Turn(rHand, y_axis, heading, math.rad(90))
   Turn(rFoot, y_axis, heading, math.rad(90))
   Turn(lFoot, y_axis, heading, math.rad(90))
   WaitForTurn(turret, y_axis)
   --WaitForTurn(waterGunVal, x_axis)
   --WaitForTurn(gun, x_axis)
   WaitForTurn(head, y_axis)
   WaitForTurn(rHand, y_axis)
   WaitForTurn(rFoot, y_axis)
   WaitForTurn(lFoot, y_axis)
   StartThread(RestoreAfterDelay)
   --Spring.Echo("in aim weapon2!")
   -- else
   -- -- Spring.CallCOBScript(unitID, "ChangeWeapon", 1)
   -- --weaponID = 2
   -- Spring.Echo("weapon ID is...",weaponID)
   -- Signal(SIG_AIM)
   -- SetSignalMask(SIG_AIM)
   -- Move(lHand, z_axis, 15, 20)
   StartThread(RestoreAfterDelay)
   --else script.AimWeapon2(heading, pitch)
      return true
   --else
--      return false
--   end
end

function script.FireWeapon1()
   Spring.Echo("fire!")
end



--ename1 = UnitDefs[enemyID1].name
function script.HitByWeapon(x, z, weaponDefID, damage)
   wname = WeaponDefs[weaponDefID].name
   i, j = string.find(wname, "fist")
   if(hulaHoopP == true) then
      --if(i ~= nil) then
         newDamage = damage - (0.75*damage)
         return newDamage
      --end
   end
end
-- Mentioned in section 4.3.1.2 of MTDF v0
function script.StartMoving()
   StartThread(walk)
end

-- Mentioned in section 4.3.1.3 of MTDF v0
function script.StopMoving()
   Signal(SIG_WALK)
   legs_down()
end

-- function script.BeginTransport(passengerID)
-- end
-- function script.QueryTransport(passengerID)
-- return -1
-- end

function script.TransportDrop(passengerID)
end

--wname = WeaponDefs[weaponDefID].name

function script.TransportPickup (passengerID)
   Spring.UnitScript.AttachUnit(turret, passengerID)
   local unitDefID = Spring.GetUnitDefID(passengerID)
   wID = passengerID
   local name = UnitDefs[unitDefID].name
   --Spring.Echo("carrying a ", name)
   if (name == "wwatergun") then
      waterGunAllowed = true
      Equip()
      waterGunP = true
      Spring.SetUnitRulesParam(unitID, "waterGun", 1, {public = true})
      --waterGunVal = passengerID
      --wname = WeaponDefs[1].name
      --wname2 = WeaponDefs[2].name
   end
   if(name == "whulahoop") then
      hulaHoopP = true
      -- CHANGE THE UNIT'S HEALTH --
      local _, currentMaxHealth, _, _, _= Spring.GetUnitHealth(unitID)
      currentMaxHealth = currentMaxHealth+20
      Spring.SetUnitMaxHealth(unitID, currentMaxHealth)
      Spring.SetUnitHealth(unitID, currentMaxHealth)
      
      Spring.SetUnitRulesParam(unitID, "hulaHoop", 1, {public = true})
   end
   if(name == "wbbglove") then
      bbGloveP = true
      Spring.SetUnitRulesParam(unitID, "bbGlove", 1, {public = true})
      Spring.UnitScript.SetPieceVisibility(rHand, false)
   end
   if(name == "wmagicwand") then
      magicWandP = true
      Spring.SetUnitRulesParam(unitID, "magicWand", 1, {public = true})
   end
   if(name == "wcape") then
      capeP = true
      Spring.SetUnitRulesParam(unitID, "cape", 1, {public = true})
   end
   if(name == "wswimTrunks") then
      swimTrunksP = true
      Spring.SetUnitRulesParam(unitID, "swimTrunks", 1, {public = true})
   end
   if(name == "wrunningShoes") then
      runningShoesP = true
      Spring.SetUnitRulesParam(unitID, "runningShoes", 1, {public = true})
   end
   SwitchWeapon()
   Spring.Echo("chosen weapon is", weaponChosen)
end


Right now it is firing a weapon, but not the correct one and I'm not really even sure what one is firing. I've checked and it knows that the correct weapon is the second weapon. It prints the "heyyy" message showing it is going into AimWeapon1. Though it does not aim it. It never enters AimWeapon2 but does query it and know where to aim from for it. Is this because I do not have a gadget or am I doing something else wrong?


Top
 Offline Profile  
 
PostPosted: 21 Mar 2012, 13:05 
Content Developer
User avatar

Joined: 13 Jan 2005, 00:46
Location: ModalitÃ
you don't seem to understand that I am using weaponchosen as a bool.

Next time you post please strip the comments.


Top
 Offline Profile  
 
PostPosted: 21 Mar 2012, 15:08 

Joined: 10 Feb 2012, 22:22
No I don't understand why that's necessary. If you could explain it that would be great. I did change it to a bool in the game and am having the exact same problem.


Top
 Offline Profile  
 
PostPosted: 21 Mar 2012, 15:21 
Content Developer
User avatar

Joined: 13 Jan 2005, 00:46
Location: ModalitÃ
Do me a favor and post the source on pastebin(with the commented code removed), I only posted the kampfer source here on the site so that it could be around as a resource.


Top
 Offline Profile  
 
PostPosted: 21 Mar 2012, 16:16 

Joined: 10 Feb 2012, 22:22
http://pastebin.com/h2LA5VaK

There's the link. Now it's firing the first weapon and I get why it's doing that. However, I want the third weapon to be the default weapon when the first two are not available. How would I do that?


Top
 Offline Profile  
 
PostPosted: 21 Mar 2012, 16:20 
Content Developer
User avatar

Joined: 13 Jan 2005, 00:46
Location: ModalitÃ
so this is a toggle for the 1st and 3rd weapon only?


Top
 Offline Profile  
 
PostPosted: 21 Mar 2012, 16:23 

Joined: 10 Feb 2012, 22:22
I was just about to post about that. There will be a weapon 2. I just have 1 and 3 in there right now because I'd like to get them working first, but also keeping in mind the fact that there will be a weapon 2.


Top
 Offline Profile  
 
PostPosted: 21 Mar 2012, 16:31 
Content Developer
User avatar

Joined: 13 Jan 2005, 00:46
Location: ModalitÃ
ok, so you want to be able to toggle each weapon individually? because what I was giving you was a switch between two weapons.


Top
 Offline Profile  
 
PostPosted: 21 Mar 2012, 16:33 

Joined: 10 Feb 2012, 22:22
Yes that's what I want. I thought that was what you might be doing because of the bool but I was thrown off but the weapon 3 code you had. Do you know of a way that I can toggle between 3 weapons?


Top
 Offline Profile  
 
PostPosted: 21 Mar 2012, 16:46 
Content Developer
User avatar

Joined: 13 Jan 2005, 00:46
Location: ModalitÃ
yeah give me a second, I am at work(usa person here) so I have to do this kind of thing while waiting on large compiles.


Top
 Offline Profile  
 
PostPosted: 21 Mar 2012, 16:57 
Content Developer
User avatar

Joined: 13 Jan 2005, 00:46
Location: ModalitÃ
Code:

function SwitchWeapon()
   if (WeaponChosen >= 3) then
      WeaponChosen = 1;
   else
      WeaponChosen = WeaponChosen +1;
   end
end
   
function script.AimWeapon3(heading, pitch)
   Signal(SIG_AIM_3)
   SetSignalMask(SIG_AIM_3)

   while(weaponChosen != 3) do
      Sleep(100)
   end
   Spring.Echo("aim weapon 2!")


   Move(rHand, z_axis, 15, 20)
   StartThread(RestoreAfterDelay)
   return true
   
end

function script.AimWeapon1(heading, pitch)--(weaponID, heading, pitch)
   Signal(SIG_AIM_2)
   SetSignalMask(SIG_AIM_2)
   
   while(weaponChosen != 1) do
      Sleep(100)
   end
      Spring.Echo("heyyyyy")
   
   Turn(turret, y_axis, heading, math.rad(90))
   Turn(torso, y_axis, heading, math.rad(90))
   Turn(head, y_axis, heading, math.rad(90))
   Turn(rHand, y_axis, heading, math.rad(90))
   Turn(rFoot, y_axis, heading, math.rad(90))
   Turn(lFoot, y_axis, heading, math.rad(90))
   WaitForTurn(turret, y_axis)
   WaitForTurn(head, y_axis)
   WaitForTurn(rHand, y_axis)
   WaitForTurn(rFoot, y_axis)
   WaitForTurn(lFoot, y_axis)
   StartThread(RestoreAfterDelay)

   StartThread(RestoreAfterDelay)
   return true
end


Does this help you understand what you need to do.

Btw, you have no aimweapon2.


Top
 Offline Profile  
 
PostPosted: 21 Mar 2012, 16:59 

Joined: 10 Feb 2012, 22:22
Thank you! I've since added in all the weapon 2 functions. I will give this a try.


Top
 Offline Profile  
 
PostPosted: 21 Mar 2012, 17:03 
Content Developer
User avatar

Joined: 13 Jan 2005, 00:46
Location: ModalitÃ
be sure to initialize weaponchosen as 1 in the create() function.


Top
 Offline Profile  
 
PostPosted: 23 Mar 2012, 16:05 
Moderator
User avatar

Joined: 22 Feb 2006, 01:02
Location: cheap kitchen
what is the advantage of these funny loops
Code:
   while(weaponChosen != 1) do
      Sleep(100)
   end
over just return false?


Top
 Offline Profile  
 
PostPosted: 23 Mar 2012, 16:13 
Content Developer
User avatar

Joined: 13 Jan 2005, 00:46
Location: ModalitÃ
Iirc return false didn't work correctly


Top
 Offline Profile  
 
PostPosted: 23 Mar 2012, 16:30 
Moderator
User avatar

Joined: 22 Feb 2006, 01:02
Location: cheap kitchen
i think return false to prevent weapon from shooting is quite reliable.
(seeing how so many mods use it)
---
Code:
local activeWeapon = 1
AimWeapon(weaponNum, heading, pitch))
  if weaponNum == activeWeapon then
   ..animation blabla..
    return true
  else
     return false
  end
end


or if you want to allow multiple weapons to be active at the same time:
Code:
local activeWeapons = {}
--enable weapon 2&4, disable 1&3
activeWeapon[1]= false,
activeWeapon[2]= true,
activeWeapon[3]= false,
activeWeapon[4]= true,
AimWeapon(weaponNum, heading, pitch))
  if activeWeapon[weaponNum] then
   ..animation blabla..
    return true
  else
     return false
  end
end

To en/disable just do
activeWeapon[i]= true/false
not sure if weaponNum starts at 0 or 1 (1 i think)


Top
 Offline Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 19 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group

Site layout created by Roflcopter et al.