Jump to content
twakkie

Help with optimising heli reinforcement script

Recommended Posts

Hi Guys

 

First off this isnt only my work but is based off the Ai Lift script by AAmdahl here: http://www.armaholic.com/page.php?id=25566

 

I am a novice scripter and I am trying to improve my coding skills, learn a few new tricks etc and would love your guys input. In this instance I wanted to learn about cases, functions and how to minimise the amount of if's used since the original was very reliant on it. My goal with all my scripts is to minimise the amount of code but maximise the amount of work done by it, all the while keeping the the amount of resources required to a minimum.

 

Please look at the script below and give any suggestions to it or alternatives commands/ways around things in the script. Its not 100% completed yet but very close to it. Also please dont be shy in telling me if I did something wrong or wanting to see other functions in it. At the end of the day I would like this script to be loaded as a function in the beginning of the mission and only call it up when needed.

 

Cheers Twak

 

Ps. I know there are a thousand other scripts better than mine, but creating ANOTHER heli reinforcement script wasnt the point, the point for me was to learn.

/*-------------------------------------------------------------------------------------------------------------------/
Author: 	Colgate aka Twakkie 	
Description:	This script allows the mission creator to call up a fast and low resource intesive heli reinforcement
		script while allowing different types of reinforcement/supplies to be called in.
Version: 	1.0

Original: 	heliReinforcement script based on the AiLift script by AAmdahl http://www.armaholic.com/page.php?id=25566					 						  
/-------------------------------------------------------------------------------------------------------------------*/



/*--HOW TO USE-------------------------------------------------------------------------------------------------------/

~In Editor:
Place two markers in the editor. The first being the heli spawnpoint, in this instance helispawn1. The second will be 
the heli dropoff point, in this instance helidrop1.

~How to Call:
The bare minimum needed to be executed is the heli spawn point and the heli drop off point ex:
["helispawn1", "helidrop1"] execVM "heliReinforcement.sqf";
This will spawn a blufor heli with 8 units in cargo to be deployed at the drop off marker.

The script allows further customization by adjusting the following variables:
(1) First variable is heli spawnpoint in the form of a marker
(2) Second variable is heli drop off point in the form of a marker
(3) Third variable is type of reinforcement to drop ( 1 = Soldiers Ground ||  2 = Soldiers Parachute || 3 = Vehicle Ground || 4 = Vehicle / Ammobox Parachute )
(4) Fourth variable is the ammount of units to be dropped ( 0 - 8 )
(5) Fifth variable is the side of the reinforcement ( west = Blufor || east = Opfor || Indep = Indedpendent )
(6) Sixth variable is the type of vehicle to be dropped off ( put the object name in "" )
(7) seventh variable toggles crew in the vehicle ( 1 = ON || 0 = OFF )

Using all these variables together, the command should look like this:
["helispawn1", "helidrop1", 3, 0, west, "B_Quadbike_01_F", 0] execVM "heliReinforcement.sqf";
    
/-------------------------------------------------------------------------------------------------------------------*/


//////////////////////////////
//---SETTINGS---------------//
//////////////////////////////

//Call Up Varaibles
params ["_spawn", 	// (1) First setting is spawnpoint in the form of a marker
	"_drop",	// (2) Second setting is insertion point in the form of a marker
	["_type",1],	// (3) Third setting is type as seen above ( 1 = Soldiers Ground ||  2 = Soldiers Parachute || 3 = Vehicle Ground || 4 = Vehicle / Ammobox Parachute )
	["_force",8],	// (4) Fourth setting is the ammount of enemies to be dropped ( 0 - 8 )
	["_side", west],// (5) Fifth setting is the side of the reinforcement ( west || east || Indep )
	["_veh", "B_Quadbike_01_F"],	// (6) Sixth setting is the type of vehicle to be dropped off ( put the object name in "" )
	["_crew",0]];	// (7) seventh setting toggles crew in the vehicle ( 1 = ON || 0 = OFF ) 


// Basic Units
_westHeli 		= "b_heli_Transport_01_F";
_westPilot 		= "b_helicrew_F";
_westHeliCrew 	= "b_helicrew_F";
_westUnit 		= "B_Soldier_F";
_westPara 		= "B_soldier_PG_F";
_westVehCrew 	= "B_crew_F";
_eastHeli 		= "O_Heli_Light_02_unarmed_F";
_inepHeli 		= "I_Heli_Transport_02_F";

// Other Variables
_ammount = 0;
landZone = "Land_HelipadEmpty_F" createVehicle (getmarkerpos _drop);


//////////////////////////////
//---FUNCTIONS BEGIN--------//
//////////////////////////////

//SPAWN SOLDIER//
FNC_spawnSoldiers = {				

	private["_ammount","_force","_unit","_spawn","_groupSoldiers","_heli"];
	_ammount 		= _this select 0;
	_force 			= _this select 1;
	_unit			= _this select 2;
	_spawn			= _this select 3;
	_groupSoldiers	= _this select 4;
	_heli			= _this select 5;
	
	while {_ammount < _force} do {  
		_unit createUnit [getMarkerPos _spawn, _groupSoldiers, "this moveInCargo transportHeli;", 0.6, "corporal"];
		_ammount = _ammount + 1;
		sleep 0.01;
	};
	_ammount = 0;
};

//CREATE HELI MOVE WAYPOINT//
FNC_heliWaypointMove = {			// Function to create the first waypoint for movement of the heli.
	
	private ["_waypoint","_groupPilots"];
	_groupPilots 	= _this select 0;
	
	_waypoint = _groupPilots addWaypoint [getMarkerPos _drop, 10];
	_waypoint setWayPointBehaviour "CARELESS";
	_waypoint setWayPointSpeed "NORMAL";
	_waypoint setWayPointType "MOVE";
	_waypoint setWayPointCombatMode "WHITE";
};

//CREATE HELI LAND/DROP-OFF WAYPOINT//
FNC_heliWaypointDrop = {			// Function to create waypoint to land/drop off cargo

	private ["_waypoint","_groupPilots","_reinfType"];
	_groupPilots	= _this select 0;
	_reinfType 		= _this select 1;
	
	_waypoint = _groupPilots addWaypoint [getpos landZone, 10];
	_waypoint setWayPointBehaviour "CARELESS";
	_waypoint setWayPointSpeed "NORMAL";
	_waypoint setWayPointCombatMode "WHITE";
	
	switch (_reinfType) do {
		case 1: //Infantry Land
		{
			_waypoint setWayPointType "UNLOAD";
			_waypoint setWaypointStatements ["true", "transportHeli land 'landZone'"];	
		};
		
		case 2: //Infantry Para
		{
			_waypoint setWayPointType "MOVE";
			{unassignVehicle _x; 
			_x action ["GetOut",vehicle _x];
			_x setDir getDir transportHeli;
			_x setVelocity [0,20,-20];
			sleep 0.75;
			} forEach units _groupSoldiers;
		};
		
		case 3: //Vehicle Drop-Off
		{ 
			transportHeli flyinheight 5;
			_waypoint setWayPointType "MOVE";
			waitUntil {(getPosATL transportHeli) select 2 < 15};
			transportHeli setSlingLoad objNull;
			transportHeli flyinheight 150;
		};
		
		case 4: //Vehicle Para
		{
			transportHeli setSlingLoad objNull;		
			chute = createVehicle ["B_Parachute_02_F", [100, 100, 200], [], 0, 'FLY'];
			chute setPos [position dropVeh select 0, position dropVeh select 1, (position dropVeh select 2) - 50];
			dropVeh attachTo [chute, [0, 0, -1.3]];
		};
			
		default { hint "default" };
	};
};

//CREATE HELI DELETE WAYPOINT//
FNC_heliWaypointDelete = {			// Function to create waypoint where heli is deleted

	private ["_waypoint","_groupPilots"];
	_groupPilots	= _this select 0;
	
	_waypoint = _groupPilots addWaypoint [getMarkerPos _spawn, 100];
	_waypoint setWayPointBehaviour "CARELESS";
	_waypoint setWayPointSpeed "FULL";
	_waypoint setWayPointType "MOVE";
	_waypoint setWayPointCombatMode "RED";
};

//DELETE SHUTE ON OBJECT//
FNC_shuteDetach = {			// Function to delete the shute which was wattached to pbject

	waitUntil {position dropVeh select 2 < 0.5 || isNull chute};
	detach dropVeh;
	dropVeh setPos [position dropVeh select 0, position dropVeh select 1, 0];
};

//CLEANUP UNITS/VEHICLES/OBJECTS//
FNC_cleanup = {						// Delete heli, crew and other objects spawned into the mission

	private ["_groupPilots","_reinfType"];
	_groupPilots 	= _this select 0;
	_reinfType		= _this select 1;
	
	
	deleteVehicle landZone;
	deleteVehicle transportHeli;
	{deleteVehicle _x} forEach units _groupPilots;
};

//SPAWN VEHICLES//
FNC_createVeh = {					// Create reinforcement vehicle
	
	private ["_veh","_spawn","_crew"];
	_veh	= _this select 0;
	_spawn	= _this select 1;
	_crew 	= _this select 2;
	
	dropVeh = _veh createVehicle (getMarkerPos _spawn);
	dropVeh enableRopeAttach true;
	
	if (_crew == 1) then {
		_vehCrew createUnit [getMarkerPos _spawn, _groupveh, "this moveindriver dropVeh", 0.6, "corporal"]; 
		_vehCrew createUnit [getMarkerPos _spawn, _groupveh, "this moveingunner dropVeh", 0.6, "corporal"];
	};
	
};
//--------------------------//
//---FUNCTIONS END----------//
//--------------------------//


//////////////////////////////
//---MAIN PROGRAM BEGIN-----//
//////////////////////////////

//UNIT SELECTION//
/*if (_side == west) then {
   //BLUFOR Units//
	_heli 		= _westHeli;
	_pilot 		= _westPilot;
	_crew 		= _westCrew;
	_vehCrew	= _westVehCrew;
}  else {
	if (_side = east) then {   
		//OPFOR Units//
		_heli 		= _westHeli;
		_pilot 		= _westPilot;
		_crew 		= _westCrew;
		_vehCrew	= _westVehCrew;
	} else {
		if (_side = independent) then { 
		//INDI Units//
			_heli 		= _westHeli;
			_pilot 		= _westPilot;
			_crew 		= _westCrew;
			_vehCrew	= _westVehCrew;
		};
	};
}; */

_heli 		= _westHeli;
_pilot 		= _westPilot;
_heliCrew 	= _westHeliCrew;
_vehCrew	= _westVehCrew;



//CREATE HELICOPTER & CREW//
transportHeli = createVehicle [_heli, getMarkerPos _spawn, [], 0, "FLY"]; 
_groupPilots = createGroup _side;
_groupveh = createGroup _side;
_pilot createUnit [getMarkerPos _spawn, _groupPilots, "this moveInDriver transportHeli;", 0.6, "corporal"];
_heliCrew createUnit [getMarkerPos _spawn, _groupPilots, "this moveInTurret [transportHeli, [1]];", 0.6, "corporal"];
_heliCrew createUnit [getMarkerPos _spawn, _groupPilots, "this moveInTurret [transportHeli, [2]];", 0.6, "corporal"];
_groupPilots allowFleeing 0;


//SOLDIER CREATE//
_groupSoldiers = createGroup _side;

switch (_type) do {
	case 1: //LAND ON GROUND//
	{		 
		//Create Soldiers
		_unit = _westUnit;
		[_ammount,_force,_unit,_spawn,_groupSoldiers,transportHeli] call FNC_spawnSoldiers;
		sleep 0.5;
		
		//Waypoints//;
		transportHeli flyinheight 100;
		[_groupPilots] call FNC_heliWaypointMove;
		waituntil {transportHeli distance landZone < 150};
		
		[_groupPilots,_type] call FNC_heliWaypointDrop;	
		waitUntil {(getPosATL transportHeli) select 2 > 1};
		
		{unassignVehicle _x} forEach units _groupSoldiers;
		waitUntil{{_x in transportHeli} count (units _groupSoldiers) == 0;};
		
		[_groupPilots] call FNC_heliWaypointDelete;
		
		//Cleanup//;
		transportHeli flyinheight 100;
		waitUntil{transportHeli distance (getMarkerPos _spawn) < 200};
		[_groupPilots,_type] call FNC_cleanup; 
	};

	case 2: //AIRDROP//
	{	
		//Create Soldiers
		_unit = _westPara;
		[_ammount,_force,_unit,_spawn,_groupSoldiers,transportHeli] call FNC_spawnSoldiers;
		sleep 0.5;

		//Waypoints//;
		transportHeli flyinheight 400;
		[_groupPilots] call FNC_heliWaypointMove;
		waituntil {transportHeli distance (getMarkerPos _drop) < 450};
		
		[_groupPilots,_type] call FNC_heliWaypointDrop;
		
		[_groupPilots] call FNC_heliWaypointDelete;
		
		//Delete//;
		transportHeli flyinheight 100;
		waitUntil{transportHeli distance (getMarkerPos _spawn) < 200};
		[_groupPilots,_type] call FNC_cleanup;
	};
	
	case 3: //VEHICLES GROUND//
	{	
		[_veh,_spawn,_crew] call FNC_createVeh;
		transportHeli setSlingLoad dropVeh;
		
		transportHeli flyinheight 100;
		
		//Waypoints//;
		[_groupPilots] call FNC_heliWaypointMove;
		waituntil {transportHeli distance (getMarkerPos _drop) < 200};
		
		[_groupPilots,_type] call FNC_heliWaypointDrop;

		[_groupPilots] call FNC_heliWaypointDelete;

		//Delete//;
		transportHeli flyinheight 100;
		waitUntil{transportHeli distance (getMarkerPos _spawn) < 200};
		[_groupPilots,_type] call FNC_cleanup; 
	};

	case 4: //VEHICLE PARACHUTE//
	{	
		[_veh,_spawn,_crew] call FNC_createVeh;
		transportHeli setSlingLoad dropVeh;

		transportHeli flyinheight 400;
		
		//Waypoints//;
		[_groupPilots] call FNC_heliWaypointMove;
		waituntil {transportHeli distance (getMarkerPos _drop) < 450};
		
		[_groupPilots,_type] call FNC_heliWaypointDrop;
	
		[_groupPilots] call FNC_heliWaypointDelete;
		
		//Shute Detach//
		call FNC_shuteDetach;
		
		//Delete//
		transportHeli flyinheight 100;
		waitUntil{transportHeli distance (getMarkerPos _spawn) < 200};
		[_groupPilots,_type] call FNC_cleanup; 

	}; 
};

//--------------------------//
//---MAIN PROGRAM END-------//
//--------------------------//

Share this post


Link to post
Share on other sites

Make the script less monolithic?
As someone once wrote, inline functions are not free.

To do this use a cfgfunctions.hpp

FNC_somevar = {}; is an inline function. Also with cfgfunctions.hpp it makes managing code much easier in long term maintenance.

 

A compiledFinal functions definition allows you to provide a tag. For example.

A started cfgfunctions.hpp would be this.

 

class heliRenforcement {
         tag = "HRS";
            class HeliRenforcement {
                file = "scripts\helirenforcement";
                class spawnSoldiers {};
                class heliWaypointMove {};
                class heliWaypointDrop {};
                class heliWaypointDelete {};
                class shuteDetach {};
                class cleanup {};
                class createVeh {};
            };
        };           

Share this post


Link to post
Share on other sites

You cant hard define the drop point because in game players must have the resupply ability everywhere on the map.

This is making such of work useless

Share this post


Link to post
Share on other sites

Make the script less monolithic?

As someone once wrote, inline functions are not free.

To do this use a cfgfunctions.hpp

FNC_somevar = {}; is an inline function. Also with cfgfunctions.hpp it makes managing code much easier in long term maintenance.

 

A compiledFinal functions definition allows you to provide a tag. For example.

A started cfgfunctions.hpp would be this.

 

class heliRenforcement {

         tag = "HRS";

            class HeliRenforcement {

                file = "scripts\helirenforcement";

                class spawnSoldiers {};

                class heliWaypointMove {};

                class heliWaypointDrop {};

                class heliWaypointDelete {};

                class shuteDetach {};

                class cleanup {};

                class createVeh {};

            };

        };           

 

Okay I will look at it. I wanted to add the whole script as a cfgfunction and then just sent it the variables. But your way does sound more "clean".

Will get back with an updated version.

Share this post


Link to post
Share on other sites

You cant hard define the drop point because in game players must have the resupply ability everywhere on the map.

This is making such of work useless

The script wasnt intended as a dynamic reinforce/supply script but more of 'n predetermined supply point drop off or reinforcement within a town. But it does make more sense what you are saying. You can probably just move the marker somehow while using the script. I will look into it and try and add it. But like I said, it wasnt in the initial scope of the project. If you have any other suggestions please post them.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×