Data Realms Fan Forums
http://45.55.195.193/

Advanced Missile Launcher
http://45.55.195.193/viewtopic.php?f=73&t=15927
Page 1 of 1

Author:  weegee [ Sun Jul 19, 2009 6:17 pm ]
Post subject:  Advanced Missile Launcher

Don't know if spmebody already did this, but I rewrote current missile launcher script so it catch targets at which you aim rather than choosing closest one. If missile can't find target when shot it simply fall with disabled engines. That and increased targeting range (500 -> 750) are the only differences from standard missile launcher. Code is commented so I guess it can serve as an example of CastMORay usage. Currently selected target blinks white for 50 ms.

Attachments:
HomingMissile.rte.zip [3.92 KiB]
Downloaded 212 times

Author:  piipu [ Sun Jul 19, 2009 6:32 pm ]
Post subject:  Re: Advanced Missile Launcher

You could replace this:
Code:
      -- MORay is very accurate so you need to aim carefully to catch something
      -- that's ok with large vessels but it can make harder to hit actors
      -- Thats why we need to make three rays which make 10 degrees scanning ray
      -- first we try direct hit, then +- 5 degrees rays
      local angle = self.parent:GetAimAngle(true);
      local wideangle = {};
      local targetfound = false;

      wideangle[1] = angle;
      wideangle[2] = angle - 0.088; -- Angle in radians
      wideangle[3] = angle + 0.088;
      
      --Start scanning
      for i = 1,3 do
         -- When creating starting point for scanning ray we need it to be outside of firing actor
         -- or CastMORay will detect firing actor and make it target. Targets closer than 90 pixels will
         -- be ignored
         local raystartvector = self.parent.Pos - Vector( -math.cos(wideangle[i]) * 90 , math.sin(wideangle[i]) * 90);
         -- Offset for CastMORay , it don't need to know acrot position, only angles
         -- 750 is the length of scanning ray
         local rayendvector = Vector( math.cos(wideangle[i]) * 750 , - math.sin(wideangle[i]) * 750);
         
         -- Cast ray and get id of MO hit by ray
         local moid = SceneMan:CastMORay(raystartvector , rayendvector , 0 , 0 , false , 3);
         
         -- SceneMan.g_NoMOID returned if we hit ground or hit nothing
         if moid ~= SceneMan.g_NoMOID then
            -- Get MO object from stored id
            local mo = MovableMan:GetMOFromID(moid);

            -- Always check if found object is still alive
            if mo ~= nil then
               -- Find wich actor found MO belong to
               for actor in MovableMan.Actors do
                  if mo.RootID == actor.ID then
                     self.target = actor;
                     self.target:FlashWhite(50);
                     targetfound = true;
                     break;
                  end
               end --for
            end --if
         end --if
         if targetfound then
            break;
         end
      end --for

With this:
Code:
local maxangledifference = 0.088
for actor in MovableMan.Actors do
    local vectortoactor = actor.Pos - self.Pos
    if math.abs(vectortoactor.AbsRadAngle - self.Vel.AbsRadAngle) < macangledifference and vectortoactor.Magnitude < 750 then
      self.target = actor
      maxangledifference = vectortoactor.AbsRadAngle
    end
end
I used that in some version of the Swarmer in Lua guns, though I seem to have lost the script somewhere.
Edit: Forgot the distance check. Added it.

Author:  weegee [ Sun Jul 19, 2009 6:47 pm ]
Post subject:  Re: Advanced Missile Launcher

Will this compact version check for terrain hits?

Author:  piipu [ Sun Jul 19, 2009 6:50 pm ]
Post subject:  Re: Advanced Missile Launcher

Damn, I knew I forgot something.
Code:
local maxangledifference = 0.088
for actor in MovableMan.Actors do
    local vectortoactor = actor.Pos - self.Pos
    if math.abs(vectortoactor.AbsRadAngle - self.Vel.AbsRadAngle) < macangledifference and vectortoactor.Magnitude < 750 then
      self.calcvel = self.Vel:SetMagnitude(750)
      i = SceneMan:CastMORay(self.Pos,self.calcvel,0,0,false,5)
      self.thingie = MovableMan:GetMOFromID(i)
      if self.thingie ~= nil and self.thingie.RootID ~= actor.ID then
        self.target = actor
        maxangledifference = vectortoactor.AbsRadAngle
      end
    end
end
Edit:Missed an end.

Author:  weegee [ Sun Jul 19, 2009 6:58 pm ]
Post subject:  Re: Advanced Missile Launcher

Cool, hope somebody will find this before asking :)

Page 1 of 1 All times are UTC [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/