Announcement

Collapse
No announcement yet.

Fighting Position Script Help

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Fighting Position Script Help

    Based off Lq. Snake's deployable sandbags.

    Core Goals:
    -All players can deploy the "Fort_EnvelopeSmall" object relative to their position on the ground as a fighting position.
    -Players may place unlimited fighting positions.
    -Fighting positions may also be removed.
    -Action requires no unique object; action tied to common object like the compass or the player model itself.

    Here is what I have thus far.

    init.sqf
    Code:
    //begin init.sqf
    
    [] execVM "briefing.sqf";
    
    [["CAManBase"], 10] execVM "Scripts\DeployablePosition\showAction.sqf";
    
    //end init.sqf
    Scripts\DeployablePosition\showAction.sqf
    Code:
    // By Lq.Snake
    // Questions?  PM nthamma on TG forums
    
    private ["_storeSand", "_hasAction", "_deploySand", "_target", "_confirm", "_cancel"];
    
    _storeSand = _this select 0;
    LS_MAXSTORESAND = _this select 1;
    _hasAction = false;
    player setVariable ["ls_deployAction", 0, false];
    _confirm = player addAction ["Dig Position", "Scripts\DeployablePosition\deploySand.sqf", [], 0, true, true, "", "_target == player && (_target getVariable ""ls_deployAction"") == 1"];
    _cancel = player addAction ["Cancel Position", "Scripts\DeployablePosition\deployCancel.sqf", [], 0, true, true, "", "_target == player && (_target getVariable ""ls_deployAction"") == 1"];
    
    while {true} do
    {
    	if (!alive player) then 
    	{
    		waitUntil {alive player};
    		sleep 0.1;
    		_confirm = player addAction ["Dig Position", "Scripts\DeployablePosition\deploySand.sqf", [], 0, true, true, "", "_target == player && (_target getVariable ""ls_deployAction"") == 1"];
    		_cancel = player addAction ["Cancel Site", "Scripts\DeployablePosition\deployCancel.sqf", [], 0, true, true, "", "_target == player && (_target getVariable ""ls_deployAction"") == 1"];
    	};
    	
    	if ((count nearestObjects [player, _storeSand, 4]) >= 1  && !(_hasAction)) then
    	{	
    		_deploySand = player addAction ["Plan Position", "Scripts\DeployablePosition\deploySandConfirm.sqf", _storeSand, 6, true, true, "", "_target == player && player getVariable ""ls_deployAction"" == 0"];
    		_hasAction = true;
    	};
    	
    	if ((count nearestObjects [player, _storeSand, 4]) <= 0 && (_hasAction)) then
    	{
    		player removeAction _deploySand;
    		_hasAction = false;
    	};
    	
    	sleep 1;
    };
    // EOF
    Scripts\DeployablePosition\deploySandConfirm.sqf
    Code:
    // By Lq.Snake
    // Questions?  PM nthamma on TG forums
    
    private ["_target", "_caller", "_id", "_storeSand", "_nearestVeh", "_i", "_dirPlayer", "_Z", "_magn", "_Y", "_theta"];
    
    _target = _this select 0;
    _caller = _this select 1;
    _id = _this select 2;
    _storeSand = _this select 3;
    _nearestVeh = nearestObjects [_caller, _storeSand, 10];
    
    // =================================================================================================================================
    
    if (count _nearestVeh > 1) then
    {
    	// Calculating which element in the array of _nearestVeh is closest to player
    	for [{_i = 0}, {_i < ((count _nearestVeh) - 1)}, {_i = _i + 1}] do
    	{
    		if (player distance (_nearestVeh select _i) > player distance (_nearestVeh select (_i + 1))) then
    		{
    			ls_nearestAmmo = _nearestVeh select (_i + 1);
    		}
    		else
    		{
    			ls_nearestAmmo = _nearestVeh select _i;
    		};
    	};
    }
    else
    {
    	ls_nearestAmmo = _nearestVeh select 0;
    };
    
    // =================================================================================================================================
    
    if (ls_nearestAmmo getVariable "countSand" == 0) then
    {
    	hintSilent format["No more positions allowed.\n\nAllowed Positions: %1", ls_nearestAmmo getVariable "countSand"];
    }
    else
    {
    	_caller setVariable ["ls_deployAction", 1, false];
    	_dirPlayer = getDir _caller;
    	_Z = getPos _caller select 2;
    	_magn = 4;
    	_Y = _magn*cos(_dirPlayer);
    	_X = _magn*sin(_dirPlayer);
    	SANDBAG = "Fort_EnvelopeSmall" createVehicleLocal [(getPos _caller select 0)+_X, (getPos _caller select 1)+_Y, _Z];
    	SANDBAG setPos [(getPos _caller select 0)+_X, (getPos _caller select 1)+_Y, _Z];
    	SANDBAG setDir _dirPlayer;
    	
    	while {_caller getVariable "ls_deployAction" == 1} do
    	{
    		if (_caller distance ls_nearestAmmo > 50) exitWith
    		{
    			deleteVehicle SANDBAG;
    			_caller setVariable ["ls_deployAction", 0, false];
    			hintSilent format["Site Cancelled\n\nYou Have Travelled Too Far"];
    		};
    			
    		_dirPlayer = getDir _caller; /* Angle Phi */
    		_Z = abs((_caller weaponDirection (currentWeapon _caller)) select 2);
    		_theta = asin(_Z);
    		_magn = 1 / (tan(_theta));
    		
    		if (_magn > 4) then
    		{
    			_magn = 4;
    		};
    		if (_magn <= 2) then
    		{
    			_magn = 2;
    		};
    		
    		_Y = _magn*cos(_dirPlayer);
    		_X = _magn*sin(_dirPlayer);
    		SANDBAG setPos [(getPos _caller select 0)+_X, (getPos _caller select 1)+_Y, 0];
    		SANDBAG setDir _dirPlayer;
    		hintSilent format["Position Deployment Selected\n\nConfirm Placement\n\nDistance From Storage Source: %1 [m]", round(_caller distance ls_nearestAmmo)];
    		sleep 0.1;
    	};
    };
    Scripts\DeployablePosition\deploySand.sqf
    Code:
    // By Lq.Snake
    // Questions? PM nthamma on TG forums
    
    private ["_target", "_caller", "_id", "_posSAND", "_dirSAND", "_i", "_num"];
    
    _target = _this select 0;
    _caller = _this select 1;
    _id = _this select 2;
    _posSAND = getPos SANDBAG;
    _dirSAND = getDir SANDBAG;
    
    // =======================================
    // Checking sandbag count
    // =======================================
    
    if (ls_nearestAmmo getVariable "countSand" == 0) then
    {
    	hintSilent format["No More Positions Allowed\n\nAllowed Positions: %1", ls_nearestAmmo getVariable "countSand"];
    }
    else
    {
    	_target setVariable ["ls_deployAction", 0, false];
    	deleteVehicle SANDBAG;
    	_caller playMove "AinvPknlMstpSlayWrflDnon_medic";
    	for [{_i = 0}, {_i < 4}, {_i = _i + 1}] do 
    	{
    		hintsilent "Planning fire sector.";
    		sleep 3;
    		hintsilent "Digging hole.";
    		sleep 3;
    		hintsilent "Sodding forward parapet.";
    		sleep 3;
    		hintsilent "Concealing spoil.";
    		sleep 3;
    		hintsilent "Completing range card.";
    		sleep 3;
    		hint "Dug in!";
    		sleep 0.5;
    	};
    	_num = (ls_nearestAmmo getVariable "countSand") - 1;
    	ls_nearestAmmo setVariable ["countSand", _num, true];
    	SANDBAG = "Fort_EnvelopeSmall" createVehicle _posSAND;
    	SANDBAG setPos _posSAND;
    	SANDBAG setVehicleInit format["this addAction [""Refill Position"", ""Scripts\DeployablePosition\retrieveIndSand.sqf"", ""%1"", 0, true, true, """", ""count nearestObjects [_target, [""""%1""""], 50] >= 1""];", typeOf ls_nearestAmmo];
    	processInitCommands;
    	SANDBAG setDir _dirSAND;
    	hintsilent format["Position dug.\n\nAllowed Positions: %1 of %2", _num, LS_MAXSTORESAND];
    };
    // =================================================================================================================================
    Scripts\DeployablePosition\deployCancel.sqf
    Code:
    // By Lq.Snake
    // Questions?  PM nthamma on TG forums
    
    private ["_caller", "_num"];
    
    _caller = _this select 1;
    
    _num = ls_nearestAmmo getVariable "countSand";
    
    _caller setVariable ["ls_deployAction", 0, false];
    deleteVehicle SANDBAG;
    hintSilent format["Site Cancelled\n\nAllowed Positions: %1 of %2", _num, LS_MAXSTORESAND];
    Scripts\DeployablePosition\retrieveIndSand.sqf
    Code:
    // By Lq.Snake
    // Questions?  PM nthamma on TG forums
    
    private ["_target", "_caller", "_id", "_storeSand", "_nearestAmmo", "_num"];
    
    _target = _this select 0;
    _caller = _this select 1;
    _storeSand = _this select 3;
    _nearestAmmo = nearestObject [_caller, _storeSand]; /* Nearest object that contains the sandbags */
    
    if (_nearestAmmo getVariable "countSand" == LS_MAXSTORESAND) then
    {
    	hint format["Cannot fill position right now.\n\nDig another hole elsewhere.\n\nDon't ask why either."];
    }
    else
    {
    	_caller playMove "AinvPknlMstpSlayWrflDnon_medic";
    	for [{_i = 0}, {_i < 4}, {_i = _i + 1}] do 
    	{
    		hintsilent "Retrieving range card.";
    		sleep 3;
    		hintsilent "Refilling hole.";
    		sleep 3;
    		hintsilent "Replacing sod.";
    		sleep 3;
    	};
    	deleteVehicle _target;
    	_num = (_nearestAmmo getVariable "countSand") + 1;
    	_nearestAmmo setVariable ["countSand", _num, true];
    	hintSilent format["Position refilled.\n\nAllowed Positions: %1 of %2", _num, LS_MAXSTORESAND];
    };
    And in my init line of the soldiers is: this setVariable ["countSand", 10, true];

    So, what's broken, what can I hack out of here to remove the 'distance from vehicle' checks and deployment limit, and keep in mind when responding you might as well go ahead and rewrite the code in question because I -will- screw it up if I try doing any more scripting myself. I'm just stupid like that.

  • #2
    Re: Fighting Position Script Help

    I've got something similar to what you are asking. I'll try to modify it to what you need.

    EDIT:

    deployTrench.sqf
    Code:
    //Written by beta
    //Ideas from GeneralCarver
    //Deploys trench
    
    
    private["_pos", "_confAct", "_cancAct", "_dir", "_obj1""];
    
    _obj1 = "Fort_EnvelopeSmall" createVehicleLocal [0, 0, 0];
    
    _pos = player modelToWorld [0, 4, 0];
    _dir = (getDir player);
    [_pos, [_obj1], _dir] spawn
    {
    	private["_pos", "_dir", "_oPos", "_cancAct", "_confAct"];
    	
    	_pos = _this select 0;
    	_dir = _this select 2;
    	
    	private["_obj1"];
    	
    	_obj1 = _this select 1 select 0;
    	
    	player removeAction b_deployTrenchAct;
    	
    	_confAct = player addAction ["Confirm Deployment", "bScripts\deploy\placeTrench.sqf", ["Fort_EnvelopeSmall", [_obj1, _obj2, _obj3, _obj4]], 99, true, false, "", ""];
    	_cancAct = player addAction ["Cancel Deployment", "bScripts\deploy\cancelTrench.sqf", [[_obj1, _obj2, _obj3, _obj4]], 99, true, false, "", ""];
    	
    	while {!(player getVariable "objDeployed") && !(player getVariable "objCancelled") && alive player} do
    	{
    		_pos = player modelToWorld [0, 4, 0];
    		_dir = (getDir player);
    		
    		_obj1 setDir _dir;
    		_obj2 setDir _dir;
    		_obj3 setDir (_dir - 180);
    		_obj4 setDir (_dir - 180);
    		
    		sleep 0.1;
    	};
    	
    	player removeAction _confAct;
    	player removeAction _cancAct;
    	
    	player setVariable ["objDeployed", false, false];
    	player setVariable ["objCancelled", false, false];
    	
    	if (!alive player) then
    	{
    		deleteVehicle _obj1;
    	}
    	else
    	{
    		b_deployTrenchAct = player addAction ["Deploy Trench", "bScripts\deploy\deployTrench.sqf", [], 99, true, false, "", ""];
    	};
    };
    placeTrench.sqf
    Code:
    //Written by beta
    //Ideas from GeneralCarver
    //Places trench
    
    
    private["_type", "_id", "_pos", "_dir", "_objArray", "_nearTrenches"];
    
    _id = _this select 2;
    _type = _this select 3 select 0;
    _objArray = _this select 3 select 1;
    
    private["_newObj1", "_obj1"];
    
    _obj1 = _objArray select 0;
    
    _nearTrenches = nearestObjects [player, [_type], 25];
    _nearTrenches = _nearTrenches - [_obj1];
    
    if ((count _nearTrenches) < 1) then
    {
    	player removeAction _id;
    	player setVariable ["objDeployed", true, false];
    	player playMove "AinvPknlMstpSlayWrflDnon_medic";
    	sleep 9;
    
    	_pos = getPosATL _obj1;
    	_dir = getDir _obj1;
    	deleteVehicle _obj1;
    	_pos = [_pos select 0, _pos select 1, 0.4];
    	_newObj1 = _type createVehicle _pos;
    	_newObj1 setDir _dir;
    }
    else
    {
    	hintSilent "You cannot deploy a trench within 25m of another trench";
    };
    setupDeploy.sqf
    Code:
    //Written by beta
    //Init file
    
    
    player setVariable ["objDeployed", false, false];
    player setVariable ["objCancelled", false, false];
    
    b_deployTrenchAct = player addAction ["Deploy Trench", "bScripts\deploy\deployTrench.sqf", [], 99, true, false, "", ""];

    These are the 3 script files you will need. Run the setupDeploy.sqf script in the init.sqf for each client (shouldn't run on the server, but it probably won't do anything bad if it is).

    Mess around with the numbers to get it to the position you want. Numbers you will want to screw with are the player's direction, the modelToWorld array and the various positions of the trench.

    Comment

    Connect

    Collapse

    TeamSpeak 3 Server

    Collapse

    Advertisement

    Collapse

    Twitter Feed

    Collapse

    Working...
    X