Okey here is the overview of how I planned the weaponsystem to work.
Further I display a script for just testing if the weaponsystem will work with minigun that shoots a CA CORCOM laser which can shoot at .
Chassis units have a NOWEAPON weapon as many as their possible weapon slots+one which is for Primary weapon range changing
When a chassis turret gets a target. Chassis unit's COB script calls a LUA function called CalculateShot. Which is for getting the heading and pitch for a specified turret(There can be three weapon types - beamweapon, ballistic, hitscan so animations for the turret would be crappy that is if NOWEAPON is a hitscan weapon it would be aiming straight on target but would shoot the bullet with an offset because it has a ballistic weapon mounted).
A needed part in BOS script of a Wheeled Tech 1 Chassis which has two slots -
Note: all LUA functions are registered
Code: Select all
piece base, sleeve1, weapbase1, hold1, fire1, weapbase2, hold2, fire2;
static-var target2, target3;
// Signal definitions
#define SIG_AIM_1 2
#define SIG_AIM_2 4
#define RESTORE_DELAY 1000
#define TARGET_ID 83
lua_CalculateShot(target, weapon, piecepoint) {return 0;}
Create()
{
hide weapbase1;
hide weapbase2;
hide hold1;
hide hold2;
hide fire1;
hide fire2;
}
AimWeapon2(heading, pitch)
{
signal SIG_AIM_1;
set-signal-mask SIG_AIM_1;
target2 = get TARGET_ID(2);
get PRINT(get TARGET_ID(2));
call-script lua_CalculateShot(target2, 2, 1);
}
AimWeapon3(heading, pitch)
{
signal SIG_AIM_2;
set-signal-mask SIG_AIM_2;
target3 = get TARGET_ID(3);
get PRINT(get TARGET_ID(3));
call-script lua_CalculateShot(target3, 3, 2);
}
Rdy2(heading2, pitch2)
{
turn weapbase1 to y-axis heading2 speed <320.000000>;
turn hold1 to x-axis <0.000000> - pitch2 speed <320.000000>;
wait-for-turn weapbase1 around y-axis;
wait-for-turn hold1 around x-axis;
start-script Restore2();
call-script lua_NewUnitShoot(heading2, pitch2, 2, target2);
return (0);
}
Rdy3(heading3, pitch3)
{
turn weapbase2 to y-axis heading3 speed <320.000000>;
turn hold2 to x-axis <0.000000> - pitch3 speed <320.000000>;
wait-for-turn weapbase2 around y-axis;
wait-for-turn hold2 around x-axis;
start-script Restore3();
call-script lua_NewUnitShoot(heading3, pitch3, 3, target3);
return (0);
}
AimFromWeapon2(piecenum)
{
piecenum = weapbase1;
}
AimFromWeapon3(piecenum)
{
piecenum = weapbase2;
}
QueryWeapon2(piecenum)
{
piecenum = fire1;
}
QueryWeapon3(piecenum)
{
piecenum = fire2;
}
SweetSpot(piecenum)
{
piecenum = base;
}
Code: Select all
function CalculateShot(unitID, unitDefID, teamID, target, weapon, piecepoint)
piecenames = Spring.GetUnitScriptNames(unitID)
piecePosX,piecePosZ = Spring.GetUnitCOBValue(unitID,true,PIECE_XZ,piecenames["weapbase"..piecepoint])
piecePosY = Spring.GetUnitCOBValue(unitID,PIECE_Y,piecenames["weapbase"..piecepoint])/65536
Spring.MoveCtrl.SetPosition(MiniTech1,piecePosX,piecePosY,piecePosZ) -- This MiniTech1 is a Minigun Tech 1 unitweapon which is created in the Initialize call-in for calculating the aim
local ud = UnitDefs[unitDefID]
if(ud.radarDistance~=nil) then
SetUnitSensorRadius(MiniTech1, "radar", ud.radarDistance)
end
if(ud.sonarDistance~=nil) then
SetUnitSensorRadius(MiniTech1, "sonar", ud.sonarDistance)
end
if(ud.sightDistance~=nil) then
SetUnitSensorRadius(MiniTech1, "los", ud.sightDistance)
end
if(target ~= -2) then --not targeting ground
GiveOrderToUnit(MiniTech1, CMD.ATTACK, target, _)
Spring.SetUnitTarget(MiniTech1, target) --MiniTech1 as a unitweapon is not allowed to target or fire without attack order because it has the commandfire = 1 in LUA-FBI file
end
Spring.CallCOBScript(MiniTech1,"Calculate", 0, unitID, target, weapon) [color=#FF0000]--aimweapon at target and return result to LUA ReturnCalculation[/color]
Code: Select all
piece flare;
static-var aunit, atarget, aweapon;
#define SIG_AIM_1 2
#define TARGET_ID 83
#define CHANGE_TARGET 98
lua_ReturnCalculation (heading, pitch, baseID, weapon){return 0;}
Create()
{
hide flare;
}
Calculate(unitID, target, weapon)
{
if(get TARGET_ID(1) == target) //heading and pitch can be gained only through AimWeapon Callin
{
atarget = target;
aweapon = weapon;
aunit = unitID; //Chassis unitID... later baseID
}
return(0);
}
AimWeapon1(heading, pitch)
{
signal SIG_AIM_1;
set-signal-mask SIG_AIM_1;
if(get TARGET_ID(1) == atarget)
{
turn flare to y-axis heading now;
turn flare to x-axis (<0> - pitch) now;
wait-for-turn flare around y-axis;
wait-for-turn flare around x-axis;
call-script lua_ReturnCalculation(heading, pitch, aunit, aweapon);
}
return(0); //NOWEAPON on chassis unit is not yet turned so unitweapon is not allowed to shoot
}
Code: Select all
function ReturnCalculation(unitID, unitDefID, teamID, heading, pitch, baseID, weapon) [color=#FF0000]--baseID is the original Chassis unit[/color]
GiveOrderToUnit(MiniTech1, CMD.STOP, _, _)
Spring.CallCOBScript(baseID, ("Rdy"..weapon), 0, heading, pitch) [color=#FF0000]--Aim Chassis turrets and call another Lua function[/color]
SetUnitSensorRadius(MiniTech1, "radar", 0)
SetUnitSensorRadius(unitID, "sonar", 0)
SetUnitSensorRadius(MiniTech1, "los", 0)
Spring.MoveCtrl.SetPosition(MiniTech1,-10000,-10000,-10000)
end
Code: Select all
lua_NewUnitShoot(heading, pitch, piecepoint, targetunit) {return 0;}
Rdy2(heading2, pitch2)
{
turn weapbase1 to y-axis heading2 speed <320.000000>;
turn hold1 to x-axis <0.000000> - pitch2 speed <320.000000>;
wait-for-turn weapbase1 around y-axis;
wait-for-turn hold1 around x-axis;
start-script Restore2();
call-script lua_NewUnitShoot(heading2, pitch2, 2, target2);
return (0);
}
Rdy3(heading3, pitch3)
{
turn weapbase2 to y-axis heading3 speed <320.000000>;
turn hold2 to x-axis <0.000000> - pitch3 speed <320.000000>;
wait-for-turn weapbase2 around y-axis;
wait-for-turn hold2 around x-axis;
start-script Restore3();
call-script lua_NewUnitShoot(heading3, pitch3, 3, target3); [color=#FF0000]//create a unitweapon and fire[/color]
return (0);
}
Code: Select all
function NewUnitShoot(unitID, unitDefID, teamID, heading, pitch, piecepoint, target) --Chassis unit has aimed and is ready to fire
piecenames = Spring.GetUnitScriptNames(unitID)
piecePosX,piecePosZ = Spring.GetUnitCOBValue(unitID,true,PIECE_XZ,piecenames["fire"..piecepoint])
piecePosY = Spring.GetUnitCOBValue(unitID,PIECE_Y,piecenames["fire"..piecepoint])/65536
local weaponUnitID = CreateUnit("miniguntech1shot",0,0,0,0,gaiaTeamID)
Spring.SetUnitCloak(weaponUnitID, 4)
Spring.SetUnitStealth(weaponUnitID, true)
Spring.MoveCtrl.Enable(weaponUnitID)
Spring.MoveCtrl.SetGravity(weaponUnitID,0)
Spring.MoveCtrl.SetPosition(weaponUnitID,piecePosX,piecePosY,piecePosZ)
target,"pos:",piecePosX,piecePosY,piecePosZ)
Spring.CallCOBScript(weaponUnitID,"FireWeapon",0,heading,pitch)
Spring.MoveCtrl.SetPosition(weaponUnitID,-10000,-10000,-10000)
Spring.DestroyUnit(weaponUnitID)
end
And here is what happens. The unitweapon gets the target and set positions right(Dont know if the heading and pitch is correct) but when it is ordered to shoot it 1)shoots from some south position 2)to the host chassis unit(the unit the unitweapon was put on) 3)doesn't shoot at the specified rate of fire(then shoots then doesnt).
Please give hints and ask questions of what you don't understand in my script!