How do I...

How do I...

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
User avatar
Guessmyname
Posts: 3301
Joined: 28 Apr 2005, 21:07

How do I...

Post by Guessmyname »

Okay. For that mech of mine in the Random WIP thread, I've a few things that I know I'll need to do in lua. I am not asking for someone to do this for me, just looking for advice / a good idea of where to start.
  • A settable moving speed; Walk or Run. Unit-wise, this should be fairly simple, but I still don't know where/how to add it to the GUI (the difference, for those wondering, is Walk is stealthier, and you can fire whilst doing it. When running, you are neither stealthy or capable of firing most weapons, but are obviously faster). I'd also like to be able to modify crushstrength and seismic signature with this, mostly so that mechs trying to sneak around don't go knocking down trees and giving themselves away; not sure how to set those via lua
  • The rifle has a grenade launcher, which I'd like to be fired by command rather than automatically. I'd also rather the unit stopped to fire it. Any advice on that end? I could just use the standard D-gun behaviour, but I'd still like to change the UI button to something more appropriate. Probably the easiest of these.
  • I also want to try and let the grenade launcher launch smoke grenades (as well as more generic damage-causing ones), idea being that it blocks LOS (for everyone). No idea if this one's even possible. I also want to make sure you can't immediately launch a normal grenade, then a smoke.
SeanHeron
Engines Of War Developer
Posts: 614
Joined: 09 Jun 2005, 23:39

Re: How do I...

Post by SeanHeron »

I could say loads, but I'll try to keep short:
- Run/Walk, we'll (Engines of War) be needing something similiar - though not necessary all the features you've described. I realise that doesn't help you much right now...
Edit: here's what we have knocking about - not in use atm though, since it's pretty buggy: http://pastebin.com/Yjs3nQRK. I'll be adding it to the Gadget Database when we've fixed it up proper.
- Grenade Launcher/ other "on order" firing weapons, we'll also be looking to use :D. Towards the bottom of following thread are some pointers that might be useful. Dunno about the forcing to stop - would guess you could solve it by setting maxvelocity to zero (so similiar to as you do it in walk/run script).
- Blocking LOS, I have no idea, sorry. I think blocking sight within the smoke would be easy; through it, I think difficult if not impossible...
User avatar
Guessmyname
Posts: 3301
Joined: 28 Apr 2005, 21:07

Re: How do I...

Post by Guessmyname »

With the 'making the unit stop' part, I'd figure it would be this sort of process:
  • Receive 'dgun' command to launch grenade
  • Check reload state for both grenade and smoke (to prevent rapidly firing both off one after the other)
  • Insert a stop command
  • Insert the dgun command again to same co-ordinates (having cancelled it above)
  • Lock out user control (to prevent interruptions / player giving another move order)
  • Aim and fire
  • Return user control
In a perfect world, the unit would simply pause to 'd-gun', then continue on with whatever it was doing before the order, but I don't even know how to start implementing that.

With the LOS Blocking, best way I can think to do it is to just dump some big thing that blocks LOS but is otherwise invisible and intangible in the way. Features can block LOS, right?
Last edited by Guessmyname on 29 Mar 2010, 14:38, edited 1 time in total.
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: How do I...

Post by zwzsg »

Adding a custom button to change the unit max speed should be fairly easy, but changing the crush strength would require changing the movement class which I don't think you can do short of morphing the unit into another.

For the grenade, maybe check KP's byte's minelauncher?
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: How do I...

Post by Argh »

Adding a custom button to change the unit max speed should be fairly easy, but changing the crush strength would require changing the movement class which I don't think you can do short of morphing the unit into another.
So, do like I did ages ago, quit bothering with that feature of the engine, and implement your own via Lua. Or just take mine from P.U.R.E. and modify it to suit your needs.
User avatar
Tribulex
A.N.T.S. Developer
Posts: 1894
Joined: 26 Sep 2009, 21:26

Re: How do I...

Post by Tribulex »

Argh wrote:
Adding a custom button to change the unit max speed should be fairly easy, but changing the crush strength would require changing the movement class which I don't think you can do short of morphing the unit into another.
smd
User avatar
Guessmyname
Posts: 3301
Joined: 28 Apr 2005, 21:07

Re: How do I...

Post by Guessmyname »

Just a few generic bits of confusion regarding the alternative weapon scripting system:

Using AimWeapon(weaponNum, heading, pitch), how do I get the headings/pitch information for each weapon? This is what I have in the weapons part of the script (note: WIP, doesn't yet have everything), though I'm not sure if I'm using it correctly:

Code: Select all

--[[
Weapons:
1 = RKT (left arm)
2 = rifle (right arm)
3 = rifle nade (right arm)
4 = rifle smokenade (right arm again)
5 = right turret
6 = left turret
]]--

function script.AimWeapon(weaponNum, heading, pitch)
        Signal(SIG_AIM)
        SetSignalMask(SIG_AIM)
		if (weaponNum == 1) then --left arm rkt launcher
			--aiming stuff
		end
		
        --[[Turn(turret, y_axis, heading, math.rad(90))
        Turn(barrel, x_axis, -pitch, math.rad(50))
        WaitForTurn(turret, y_axis)
        WaitForTurn(barrel, x_axis)]]
        StartThread(RestoreAfterDelay)
        return true
end
 
 
function script.FireWeapon(weaponNum)
        --[[Move(barrel, z_axis, -2.4, 500)
        Sleep(150)
        WaitForMove(barrel, z_axis)
        Move(barrel, z_axis, 0, 3)]]
end
 
 
function script.AimFromWeapon(weaponNum)
	if (weaponNum == 1) then
		return l_upper_arm
	end
	if (weaponNum == 2 or weaponNum == 3 or weaponNum == 4) then
		return r_upper_arm
	end
	if (weaponNum == 5) then
		return rt_flare
	end
	if (weaponNum == 6) then
		return lt_flare
	end
end
 
 
function script.QueryWeapon(weaponNum)
	if (weaponNum == 1) then
		return rkt_flare
	end
	if (weaponNum == 2) then
		return rifle_flare
	end
	if (weaponNum == 3) then
		return grenl_flare
	end
	if (weaponNum == 4) then
		return grenl_flare
	end
	if (weaponNum == 5) then
		return rt_flare
	end
	if (weaponNum == 6) then
		return lt_flare
	end
end
Can anyone help / provide an example with more than one weapon? (All I've got is that converted stumpy script and the S44 scripts... which are modular and slightly confusing)
Tobi
Spring Developer
Posts: 4598
Joined: 01 Jun 2005, 11:36

Re: How do I...

Post by Tobi »

It's exactly the same as the normal system, except that the weapon number is parametrized.

So now if you have 2 weapons, your function AimWeapon(weaponNum, heading, pitch) gets called 2 times. One time for weapon 1 and one time for weapon 2.

The call where weaponNum == 1 receives heading and pitch for aiming weapon 1, and the call where weaponNum == 2 receives heading and pitch for aiming weapon 2.

Combined with tables this enables you to code the aiming for e.g. 8 similar weapons in very little code:

Code: Select all

-- create arrays of pieces for each weapon
local barrels = {piece(weapon1_barrel, weapon2_barrel, ..., weapon8_barrel)}
local turrets = {piece(weapon1_turret, weapon2_barrel, ..., weapon8_barrel)}

function script.AimWeapon(weaponNum, heading, pitch)
  -- ensure only one AimWeapon thread per weapon runs at any one time
  local SIG_AIM = 2^weaponNum --unique signal mask for each weapon
  Signal(SIG_AIM)
  SetSignalMask(SIG_AIM)

  -- fetch the turret and barrel pieces for this weapon
  local turret = turrets[weaponNum]
  local barrel = barrels[weaponNum]

  -- animate
  Turn(turret, y_axis, heading, math.rad(90))
  Turn(barrel, x_axis, -pitch, math.rad(50))
  WaitForTurn(turret, y_axis)
  WaitForTurn(barrel, x_axis)

  -- RestoreAfterDelay takes a single parameter weaponNum now,
  -- so it knows which weapon to restore after delay
  StartThread(RestoreAfterDelay, weaponNum)

  return true
end
Other approaches allowing even more flexibility are also possible. (e.g. dispatching weapon call-ins to arbitrary functions defined in a table)
Tobi
Spring Developer
Posts: 4598
Joined: 01 Jun 2005, 11:36

Re: How do I...

Post by Tobi »

Also note that something like

Code: Select all

function script.QueryWeapon(weaponNum)
   if (weaponNum == 1) then
      return rkt_flare
   end
   if (weaponNum == 2) then
      return rifle_flare
   end
   if (weaponNum == 3) then
      return grenl_flare
   end
   if (weaponNum == 4) then
      return grenl_flare
   end
   if (weaponNum == 5) then
      return rt_flare
   end
   if (weaponNum == 6) then
      return lt_flare
   end
end
can be made shorter, clearer (IMO), and faster by using a lookup table:

Code: Select all

local queryPieces = {piece("rkt_flare", "rifle_flare", "grenl_flare", "grenl_flare", "rt_flare", "lt_flare")}

function script.QueryWeapon(weaponNum)
  return queryPieces[weaponNum]
end
Post Reply

Return to “Lua Scripts”