Jump to content
Fiddi

Way to spawn stuff in the middle of road?

Recommended Posts

Hi,

 

I've written an ambient roadblock script but problem is the roadblock doesn't always spawn in the middle of a selected road. Most times it spawns offset to the right. I wonder if there's a better way than I currently have done to spawn roadblocks on the fly.

The gate is the "centerpoint" for the other objects.

 

This is what I have:

private ["_vehicleTypes", "_unitTypes"];
params [
	["_road", objNull, [objNull]],
	["_playerUnit", objNull, [objNull]],
	["_maxDist", 2500, [0]]
];

_connectedRoads = roadsConnectedTo _road;

_connectedRoads = _connectedRoads apply {[_playerUnit distance _x, _x]};
_connectedRoads sort true;
_nearestRoad = (_connectedRoads select 0) select 1;

_spawnDir = _road getDir _nearestRoad;
_spawnPos = getPos _road;

_gate = "Land_BarGate_F" createVehicle (_road getPos [4.5, _spawnDir]);
_gate setDir _spawnDir;
_gate setPos (_road getPos [4.5, _spawnDir]);

_sandbag = "Land_BagFence_01_round_green_F" createVehicle (_gate modelToWorld [4, -2, 0]);
_sandbag setDir ((_spawnDir + 160) % 360);

_enemySide = selectRandom [east, resistance];
if (_enemySide isEqualTo east) then {
	_vehicleTypes = [
		"O_T_APC_Tracked_02_AA_ghex_F","O_T_APC_Tracked_02_cannon_ghex_F","O_T_APC_Wheeled_02_rcws_ghex_F",
		"O_T_MBT_02_cannon_ghex_F","O_T_MRAP_02_gmg_ghex_F","O_T_MRAP_02_hmg_ghex_F","O_T_LSV_02_armed_F"
	];
	_unitTypes = OPFUnits;
} else {
	_vehicleTypes = [INDCars select 1];
	_unitTypes = INDUnits;
};
_skills = (paramsArray select 6) / 10;

_vehicle = selectRandom _vehicleTypes createVehicle (_gate modelToWorld [-5, -4.5, 0]);
_vehicle setDir ((_spawnDir + 10) % 360);
createVehicleCrew _vehicle;
{[_x, _skills] call FRED_fnc_setAISkill} count crew _vehicle;
_group = group driver _vehicle;

_unit1 = _group createUnit [selectRandom _unitTypes, _gate modelToWorld [4, -3, 0], [], 0, "NONE"];
_unit1 setDir ((_spawnDir + 350) % 360);
[_unit1, _skills] call FRED_fnc_setAISkill;

_unit2 = _group createUnit [selectRandom _unitTypes, _gate modelToWorld [-2.5, -2.5, 0], [], 0, "NONE"];
_unit2 setDir ((_spawnDir + 15) % 360);
[_unit2, _skills] call FRED_fnc_setAISkill;

_unit3 = _group createUnit [selectRandom _unitTypes, _gate modelToWorld [3, -7.5, 0], [], 0, "NONE"];
_unit3 setDir ((_spawnDir + 340) % 360);
[_unit3, _skills] call FRED_fnc_setAISkill;

_unit4 = _group createUnit [selectRandom _unitTypes, _gate modelToWorld [-3, -7.5, 0], [], 0, "NONE"];
_unit4 setDir ((_spawnDir + 10) % 360);
[_unit4, _skills] call FRED_fnc_setAISkill;

doStop [_unit1, _unit2, _unit3, _unit4];

_roadblock = [_gate, _sandbag, _vehicle, _unit1, _unit2, _unit3, _unit4];

{
	_x setDamage 0;
} count _roadblock;

[_roadblock, _maxDist] spawn {
	params [
		["_roadblock", [], [[]]],
		["_maxDist", 2500, [0]]
	];
	_gate = _roadblock select 0;
	_dead = false;
	waitUntil {
		Sleep 5;
		if ({alive _x} count ((_roadblock select [3, 4]) + (crew (_roadblock select 2))) < 1) then {
			_dead = true;
		};
		{_x distance _gate < _maxDist} count allPlayers < 1 OR 
		{alive _x} count ((_roadblock select [3, 4]) + (crew (_roadblock select 2))) < 1
	};
	ARroadblocks = ARroadblocks - [_roadblock];
	if _dead then {Sleep 120;};
	deleteVehicle _gate;
	deleteVehicle (_roadblock select 1);
	_vehicle = _roadblock select 2;
	{
		if (alive _x) then {
			deleteVehicle _x;
		};
		true
	} count ((_roadblock select [3, 4]) + (crew _vehicle));
	if (alive _vehicle) then {
		deleteVehicle _vehicle
	};
	
};

_roadblock 

 

Thanks.

Share this post


Link to post
Share on other sites

setPos the bagfence too

 

Tried that before, but then it was flying 1-2m above the ground.

However, the initial problem persist, everything spawns perfect in relation to each other but since the gate is offset the whole roadblock is offset.. Everything else works as it should.

Share this post


Link to post
Share on other sites

Tried that before, but then it was flying 1-2m above the ground.

However, the initial problem persist, everything spawns perfect in relation to each other but since the gate is offset the whole roadblock is offset.. Everything else works as it should.

The gate's object center is probably offset from the visual center. Try adjusting the roadblocks position accordingly.

Pseudo code:

_spawnPos = getPos _Road getPos [offset, spawndir+90];

Share this post


Link to post
Share on other sites

Thanks for the help! I think I've figured it out, the roadblock is most times in the middle, however sometimes the gates or sandbags rotation can be a bit wonky but mostly works fine.

 

private ["_vehicleTypes", "_unitTypes"];
params [
	["_road", objNull, [objNull]],
	["_playerUnit", objNull, [objNull]],
	["_maxDist", 2500, [0]]
];

_connectedRoads = roadsConnectedTo _road;

_connectedRoads = _connectedRoads apply {[_playerUnit distance _x, _x]};
_connectedRoads sort true;
_nearestRoad = (_connectedRoads select 0) select 1;

_spawnDir = _road getDir _nearestRoad;
_spawnPos = getPos _road;

_gate = createVehicle ["Land_BarGate_F", _road getPos [5.5, _spawnDir], [], 0, "CAN_COLLIDE"]; 
_gate setDir _spawnDir;
_gate setPos [(getPos _gate) select 0, (getPos _gate) select 1, 0];

_gate2 = createVehicle ["Land_BarGate_F", _road getPos [-5.5, _spawnDir], [], 0, "CAN_COLLIDE"];
_gate2 setDir ((_spawnDir + 180) % 360);
_gate2 setPos [(getPos _gate2) select 0, (getPos _gate2) select 1, 0];

_sandbag = createVehicle ["Land_BagFence_01_round_green_F", _gate modelToWorld [4, -2, 0], [], 0, "CAN_COLLIDE"];
_sandbag setDir ((_spawnDir + 160) % 360);
_sandbag setPos [(getPos _sandbag) select 0, (getPos _sandbag) select 1, 0];
_sandbag enableSimulationGlobal false;

_sandbag2 = createVehicle ["Land_BagFence_01_round_green_F", _gate2 modelToWorld [-4, -2, 0], [], 0, "CAN_COLLIDE"];
_sandbag2 setDir ((_spawnDir + 20) % 360);
_sandbag2 enableSimulationGlobal false;
_sandbag2 setPos [(getPos _sandbag2) select 0, (getPos _sandbag2) select 1, 0];

_enemySide = selectRandom [east, resistance];
if (_enemySide isEqualTo east) then {
	_vehicleTypes = [
		"O_T_APC_Tracked_02_AA_ghex_F","O_T_APC_Tracked_02_cannon_ghex_F","O_T_APC_Wheeled_02_rcws_ghex_F",
		"O_T_MBT_02_cannon_ghex_F","O_T_MRAP_02_gmg_ghex_F","O_T_MRAP_02_hmg_ghex_F","O_T_LSV_02_armed_F"
	];
	_unitTypes = OPFUnits;
} else {
	_vehicleTypes = [INDCars select 1];
	_unitTypes = INDUnits;
};
_skills = (paramsArray select 6) / 10;

_vehicle = createVehicle [selectRandom _vehicleTypes, _gate modelToWorld [-4, -4.5, 0], [], 0, "CAN_COLLIDE"];
_vehicle setDir ((_spawnDir + 10) % 360);
_vehicle setPos [(getPos _vehicle) select 0, (getPos _vehicle) select 1, 0];

_vehicle addEventHandler ["GetIn", {
	if ((_this select 2) in playableUnits) then {
		(_this select 0) setVariable ["playerHaveEntered", true, true];
		(_this select 0) removeEventHandler ["GetIn", _thisEventHandler];
	};
}];

createVehicleCrew _vehicle;
{[_x, _skills] call FRED_fnc_setAISkill} count crew _vehicle;
_group = group driver _vehicle;

_unit1 = _group createUnit [selectRandom _unitTypes, _gate modelToWorld [-0.5, -2.5, 0], [], 0, "CAN_COLLIDE"];
_unit1 setDir ((_spawnDir + 15) % 360);
_unit1 setPos [(getPos _unit1) select 0, (getPos _unit1) select 1, 0];
[_unit1, _skills] call FRED_fnc_setAISkill;

_unit2 = _group createUnit [selectRandom _unitTypes, _gate modelToWorld [4, -3, 0], [], 0, "CAN_COLLIDE"];
_unit2 setDir ((_spawnDir + 330) % 360);
_unit2 setPos [(getPos _unit2) select 0, (getPos _unit2) select 1, 0];
[_unit2, _skills] call FRED_fnc_setAISkill;

_unit3 = _group createUnit [selectRandom _unitTypes, _gate modelToWorld [4, -8, 0], [], 0, "CAN_COLLIDE"];
_unit3 setDir ((_spawnDir + 160) % 360);
_unit3 setPos [(getPos _unit3) select 0, (getPos _unit3) select 1, 0];
[_unit3, _skills] call FRED_fnc_setAISkill;

_unit4 = _group createUnit [selectRandom _unitTypes, _gate modelToWorld [-0.5, -8.5, 0], [], 0, "CAN_COLLIDE"];
_unit4 setDir ((_spawnDir + 150) % 360);
_unit4 setPos [(getPos _unit4) select 0, (getPos _unit4) select 1, 0];
[_unit4, _skills] call FRED_fnc_setAISkill;

doStop [_unit1, _unit2, _unit3, _unit4];

_roadblock = [_gate, _gate2, _sandbag, _sandbag2, _vehicle, _group];

[_road, _roadblock, _maxDist] spawn {
	params [
		["_road", objNull, [objNull]],
		["_roadblock", [], [[]]],
		["_maxDist", 2500, [0]]
	];
	_dead = false;
	
	waitUntil {
		Sleep 5;
		if ({alive _x} count units (_roadblock select 5) < 1) then {
			_dead = true;
		};
		{_x distance _road < _maxDist} count allPlayers < 1 OR 
		_dead
	};
	
	ARroadblocks = ARroadblocks - [_roadblock];
	
	if _dead exitWith {
		Sleep 120;
		{deleteVehicle _x;} count (_roadblock select [0, 4]);
		if (alive _vehicle AND !(_vehicle getVariable ["PlayerHaveEntered", false])) then {deleteVehicle _vehicle};
	};
	
	{deleteVehicle _x;} count (_roadblock select [0, 4]);
	{deleteVehicle _x;} count units (_roadblock select 5);
	
	_vehicle = _roadblock select 4;
	if (alive _vehicle AND !(_vehicle getVariable ["PlayerHaveEntered", false])) then {deleteVehicle _vehicle};
};

_roadblock 

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

×