Jump to content
Sign in to follow this  
mikepetunia

createUnit - tell to get in as gunner if gunner of static gun dies?

Recommended Posts

Hey all.

First post here, so please excuse any stupid mistakes!

I'm spawning in an object server-side (server_functions.sqf) as part of a little add-on, and use the following code:

// add static gun
  _biggun = "DSHKM_TK_INS_EP1" createVehicle [6938.8813, 11350.258, 3.0517578];
  _biggun addEventHandler ["HandleDamage", {false}];
  _biggun setDir 120.91074;
  _biggun addeventhandler ["fired", {(_this select 0) setvehicleammo 1}];
  sleep 0.5;

I then assign an AI to the gun with this:

"RUS_Soldier_GL" createUnit [getMarkerPos "M2ai2", _aiGroup3, "newUnit2 = this; this moveInGunner _biggun", 1, "corporal"];

However I'd like to spawn a 2nd solider, and tell him to get in the gun too - IF there isn't a gunner.

I want this for two reasons:

1) If the gunner dies, I want another soldier to take over!

2) Sometimes, not sure why, the soldier doesn't spawn on server boot! The gun does... odd as it's in the same sqf script! but sometimes the AI unit doesn't... My thinking is that by spawning a 2nd unit (perhaps in a different sqf file), it will be less likely to happen.

If anyone has any ideas about 2) above (why the AI units sometimes don't spawn, even though the static guns, in the same sqf file, do!) that would be amazing too!

Here is the full file (I am NO sqf coder! So feel free to laugh at me and tell me how terrible my scripting is!):

// Adding HMMWV M2 enemy, static guns, and infantry to man them

private ["_aiGroup","_aiGroup3"];

_aiGroup = createGroup east;

_aiGroup3 = createGroup east;

_aiGroup setVariable ["Mission",1,true];

_aiGroup3 setVariable ["Mission",1,true];

// Adding group using spawnvehicle

[getMarkerPos "devilsm21st", random 360, "BRDM2_INS", _aiGroup] call BIS_fnc_spawnVehicle;

//Adding vehicle via createvehicle

_veh = "HMMWV_M1151_M2_CZ_DES_EP1" createVehicle (getMarkerPos "devilsm21st");

_veh addeventhandler ["fired", {(_this select 0) setvehicleammo 1}];

_veh addeventhandler ["fired", {(_this select 0) setFuel 1}];

sleep 0.5;

"RUS_Soldier_GL" createUnit [getMarkerPos "M2ai1", _aiGroup, "newUnit = this; this moveInDriver _veh", 1, "Major"];

"RUS_Soldier_GL" createUnit [getMarkerPos "M2ai1", _aiGroup, "newUnit = this; this moveInGunner _veh", 1, "corporal"];

"RUS_Soldier_GL" createUnit [getMarkerPos "M2ai2", _aiGroup, "newUnit = this; this moveInCargo _veh", 1, "corporal"];

"RUS_Soldier_GL" createUnit [getMarkerPos "M2ai2", _aiGroup, "newUnit = this; this moveInCargo _veh", 1, "corporal"];

_aiGroup setVariable ["Mission",1,true];

//set skills for the above units

newUnit setSkill ["aimingAccuracy",1];

newUnit setSkill ["aimingShake",1];

newUnit setSkill ["aimingSpeed",1];

newUnit setSkill ["endurance",1];

newUnit setSkill ["spotDistance",1];

newUnit setSkill ["spotTime",1];

newUnit setSkill ["courage",1];

newUnit setSkill ["reloadSpeed",1];

newUnit setSkill ["commanding",1];

newUnit setSkill ["general",1];

//sleep 0.5;

// Reduce Damage for createvehicle spawned vehicle

_veh setVariable ["selections", []];

_veh setVariable ["gethit", []];

_veh addEventHandler

[

"HandleDamage",

{

_veh = _this select 0;

_selections = _veh getVariable ["selections", []];

_gethit = _veh getVariable ["gethit", []];

_selection = _this select 1;

if !(_selection in _selections) then

{

_selections set [count _selections, _selection];

_gethit set [count _gethit, 0];

};

_i = _selections find _selection;

_olddamage = _gethit select _i;

_damage = _olddamage + ((_this select 2) - _olddamage) * 0.25;

_gethit set [_i, _damage];

_damage;

}

];

// add static gun

_biggun = "DSHKM_TK_INS_EP1" createVehicle [6938.8813, 11350.258, 3.0517578];

_biggun addEventHandler ["HandleDamage", {false}];

_biggun setDir 120.91074;

_biggun addeventhandler ["fired", {(_this select 0) setvehicleammo 1}];

sleep 0.5;

// make AI for gun and tell them to move in as gunner

"RUS_Soldier_GL" createUnit [getMarkerPos "M2ai2", _aiGroup3, "newUnit2 = this; this moveInGunner _biggun", 1, "corporal"];

// static gun ends

// add static gun2

_biggun2 = "DSHKM_TK_INS_EP1" createVehicle [6911.6792, 11406.456, -0.10166994];

_biggun2 addEventHandler ["HandleDamage", {false}];

_biggun2 setDir 196.36488;

_biggun2 addeventhandler ["fired", {(_this select 0) setvehicleammo 1}];

sleep 0.5;

// make AI for gun and tell them to move in as gunner

"RUS_Soldier_GL" createUnit [getMarkerPos "DevilsAI", _aiGroup3, "newUnit3 = this; this moveInGunner _biggun2", 1, "corporal"];

// static gun ends

// Make AI with RPGs

"TK_INS_Soldier_AT_EP1" createUnit [getMarkerPos "DevilsAI", _aiGroup3,"newUnit4 = this;", 1, "corporal"];

"TK_INS_Soldier_AT_EP1" createUnit [getMarkerPos "DevilsAI", _aiGroup3,"newUnit4 = this;", 1, "corporal"];

"INS_Soldier_AA" createUnit [getMarkerPos "DevilsAI", _aiGroup3,"newUnit4 = this;", 1, "corporal"];

"RUS_Soldier_GL" createUnit [getMarkerPos "DevilsAI", _aiGroup3,"newUnit4 = this;", 1, "corporal"];

"USMC_SoldierS_SniperH" createUnit [getMarkerPos "DevilsAI", _aiGroup3,"newUnit4 = this;", 1, "corporal"];

"USMC_SoldierS_SniperH" createUnit [getMarkerPos "DevilsAI", _aiGroup3,"newUnit4 = this;", 1, "corporal"];

// Stop them running away

_aiGroup allowFleeing 0;

_aiGroup3 allowFleeing 0;

// Add Waypoints for M2

_this = _aiGroup addWaypoint [[6934.8335, 11336.827, 0], 0];

_this setWaypointCombatMode "RED";

_this setWaypointBehaviour "AWARE";

_waypoint_3 = _this;

_this = _aiGroup addWaypoint [[6962.9517, 11343.79, 0], 0];

_waypoint_4 = _this;

_this = _aiGroup addWaypoint [[6909.5718, 11323.544, 0], 0];

_waypoint_5 = _this;

_this = _aiGroup addWaypoint [[6973.5161, 11344.028, 0], 0];

_waypoint_6 = _this;

_this = _aiGroup addWaypoint [[6933.4819, 11334.185, 0], 0];

_this setWaypointType "CYCLE";

_waypoint_7 = _this;

The code above has been written in bits and pieces, and i must have got SOMETHING wrong as sometimes the AI units don't spawn :(

Would adding more AI units via a second sqf file and telling them to get into the gun if its empty be possible, or even a decent solution? (I guess it'd be better to figure out WHY sometimes the AI don't spawn, but I've hit a brick wall :( )

Share this post


Link to post
Share on other sites

_staticGunner = {
private ["_gun","_group","_newUnit"];
_gun = _this select 0;
_group = _this select 1;
while {alive _gun} do
{
	if (typeName(gunner _gun) != "OBJECT") then {
		_newUnit = "RUS_Soldier_GL" createUnit [getMarkerPos "M2ai2",_group,"",1,"corporal"];
		_newUnit moveInGunner _gun;
		if (true) exitWith {};
	};
	sleep 1;
};
};

// Adding HMMWV M2 enemy, static guns, and infantry to man them
private ["_aiGroup","_aiGroup3"];
_aiGroup = createGroup east;
_aiGroup setVariable ["Mission",1,true];
_aiGroup3 = createGroup east;
_aiGroup3 setVariable ["Mission",1,true];

// Adding group using spawnvehicle
[getMarkerPos "devilsm21st",random 360,"BRDM2_INS",_aiGroup] call BIS_fnc_spawnVehicle;

//Adding vehicle via createvehicle
_veh = "HMMWV_M1151_M2_CZ_DES_EP1" createVehicle (getMarkerPos "devilsm21st");
_veh addeventhandler ["fired",
{
	(_this select 0) setvehicleammo 1;
	(_this select 0) setFuel 1;
}
];
sleep 0.5;

_newUnit = "RUS_Soldier_GL" createUnit [getMarkerPos "M2ai1",_aiGroup,"",1,"Major"];
_newUnit moveInDriver _veh;
_newUnit = "RUS_Soldier_GL" createUnit [getMarkerPos "M2ai1",_aiGroup,"",1,"corporal"];
_newUnit moveInGunner _veh;
_newUnit = "RUS_Soldier_GL" createUnit [getMarkerPos "M2ai2",_aiGroup,"",1,"corporal"];
_newUnit moveInCargo _veh;
_newUnit = "RUS_Soldier_GL" createUnit [getMarkerPos "M2ai2",_aiGroup,"",1,"corporal"];
_newUnit moveInCargo _veh;
_aiGroup setVariable ["Mission",1,true];
//set skills for the above units
{
_x setSkill ["aimingAccuracy",1];
_x setSkill ["aimingShake",1];
_x setSkill ["aimingSpeed",1];
_x setSkill ["endurance",1];
_x setSkill ["spotDistance",1];
_x setSkill ["spotTime",1];
_x setSkill ["courage",1];
_x setSkill ["reloadSpeed",1];
_x setSkill ["commanding",1];
_x setSkill ["general",1];
} forEach units _aiGroup;
//sleep 0.5;

// Reduce Damage for createvehicle spawned vehicle
_veh setVariable ["selections",[]];
_veh setVariable ["gethit",[]];
_veh addEventHandler [
"HandleDamage",
{
	_veh = _this select 0;
	_selection = _this select 1;
	_selections = _veh getVariable ["selections",[]];
	_gethit = _veh getVariable ["gethit",[]];
	if !(_selection in _selections) then {
		_selections set [count _selections,_selection];
		_gethit set [count _gethit,0];
	};
	_i = _selections find _selection;
	_olddamage = _gethit select _i;
	_damage = _olddamage + ((_this select 2) - _olddamage) * 0.25;
	_gethit set [_i,_damage];
	_damage;
}
];

// add static gun
_biggun = "DSHKM_TK_INS_EP1" createVehicle [6938.8813,11350.258,3.0517578];
_biggun addEventHandler ["HandleDamage",{false}];
_biggun setDir 120.91074;
_biggun addeventhandler ["fired",{(_this select 0) setvehicleammo 1}];
sleep 0.5;

// make AI for gun and tell them to move in as gunner
_newUnit = "RUS_Soldier_GL" createUnit [getMarkerPos "M2ai2",_aiGroup3,"",1,"corporal"];
_newUnit moveInGunner _biggun;
[_biggun,_aiGroup3] spawn _staticGunner;
// static gun ends

// add static gun2
_biggun2 = "DSHKM_TK_INS_EP1" createVehicle [6911.6792, 11406.456, -0.10166994];
_biggun2 addEventHandler ["HandleDamage",{false}];
_biggun2 setDir 196.36488;
_biggun2 addeventhandler ["fired",{(_this select 0) setvehicleammo 1}];
sleep 0.5;

// make AI for gun and tell them to move in as gunner
_newUnit = "RUS_Soldier_GL" createUnit [getMarkerPos "DevilsAI",_aiGroup3,"",1,"corporal"];
_newUnit moveInGunner _biggun2;
[_biggun2,_aiGroup3] spawn _staticGunner;
// static gun ends

// Make AI with RPGs
"TK_INS_Soldier_AT_EP1" createUnit [getMarkerPos "DevilsAI",_aiGroup3,"",1,"corporal"];
"TK_INS_Soldier_AT_EP1" createUnit [getMarkerPos "DevilsAI",_aiGroup3,"",1,"corporal"];
"INS_Soldier_AA" createUnit [getMarkerPos "DevilsAI",_aiGroup3,"",1,"corporal"];
"RUS_Soldier_GL" createUnit [getMarkerPos "DevilsAI",_aiGroup3,"",1,"corporal"];
"USMC_SoldierS_SniperH" createUnit [getMarkerPos "DevilsAI",_aiGroup3,"",1,"corporal"];
"USMC_SoldierS_SniperH" createUnit [getMarkerPos "DevilsAI",_aiGroup3,"",1,"corporal"];

// Stop them running away
_aiGroup allowFleeing 0;
_aiGroup3 allowFleeing 0;

// Add Waypoints for M2
_this = _aiGroup addWaypoint [[6934.8335, 11336.827, 0], 0];
_this setWaypointCombatMode "RED";
_this setWaypointBehaviour "AWARE";
_waypoint_3 = _this;

_this = _aiGroup addWaypoint [[6962.9517, 11343.79, 0], 0];
_waypoint_4 = _this;

_this = _aiGroup addWaypoint [[6909.5718, 11323.544, 0], 0];
_waypoint_5 = _this;

_this = _aiGroup addWaypoint [[6973.5161, 11344.028, 0], 0];
_waypoint_6 = _this;

_this = _aiGroup addWaypoint [[6933.4819, 11334.185, 0], 0];
_this setWaypointType "CYCLE";
_waypoint_7 = _this;

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
Sign in to follow this  

×