A3-Conquest.Altis / scripts / AEGIS / Random / Sector_Spawn.sqf
Sector_Spawn.sqf
Raw
// Sector_Spawn.sqf: Used to randomally generate INDEPENDENT Sectors in Villages, Towns, Cities, and Capitals on Init

// Define Globals
detailsMinorSectors = [];
detailsMajorSectors = [];

// Generate GEO Coordinates of Villages
// Geo Center of Map
_coordsMapCenter = [worldSize / 2, worldsize / 2, 0];
_searchRadius = (worldSize / 2);

// Define arrays, populate with coords
_coordsVillages = [];
{ _coordsVillages append [[(position _x select 0),(position _x select 1),0]] } forEach nearestLocations [_coordsMapCenter, ["NameVillage"], _searchRadius];
_coordsCities = [];
{ _coordsCities append [[(position _x select 0),(position _x select 1),0]] } forEach nearestLocations [_coordsMapCenter, ["NameCity"], _searchRadius];
_coordsCapitals = [];
{ _coordsCapitals append [[(position _x select 0),(position _x select 1),0]] } forEach nearestLocations [_coordsMapCenter, ["NameCityCapital"], _searchRadius];
// Sometimes you aint got many Capitals, thus one must combine them for content, unless Bohemia releases more dlc ;)
_coordsCapitalsCities = _coordsCities + _coordsCapitals;

// Arrays to hold Coords of Areas not being used, as to avoid Major or Minor sectors sharing coords
_coordsVillagesFinal = _coordsVillages;
_coordsCitiesFinal = _coordsCities;
_coordsCapitalsFinal = _coordsCapitals;
_coordsCapitalsCitiesFinal = _coordsCapitalsCities;

// Sector Array Definitions
_coordsMinorSectors = [];
_coordsMajorSectors = [];

// Count Desired Areas, Select X Random Number <= Count, Remove X Elements if Count != 1
_amountMajorSectors = [1,(count _coordsCapitalsCitiesFinal)] call BIS_fnc_randomInt;
_amountMajorSectors = 3;

// Transfer chosen coords to be used, into _coordsMajorSectors
if !((count _coordsCapitalsCitiesFinal) == 1) then {
	for [{ _i = 0 }, { _i < _amountMajorSectors }, { _i = _i + 1 }] do {
		_tempCoord = _coordsCapitalsCitiesFinal call BIS_fnc_selectRandom;
		if ((count _coordsCapitalsCitiesFinal) > 1) then {
			_coordsCapitalsCitiesFinal = _coordsCapitalsCitiesFinal - [_tempCoord];
		};
		_coordsMajorSectors = _coordsMajorSectors + [_tempCoord];
	};
};

// Count Desired Areas, Select X Random Number <= Count, Remove X Elements if Count != 1
_coordsRemainingSectors = _coordsVillagesFinal + _coordsCapitalsCitiesFinal;
_amountMinorSectors = [1,(count _coordsRemainingSectors)] call BIS_fnc_randomInt;
_amountMinorSectors = 3;

// Transfer chosen coords to be used, into _coordsMinorSectors
if !((count _coordsRemainingSectors) == 1) then {
	for [{ _i = 0 }, { _i < _amountMinorSectors }, { _i = _i + 1 }] do {
		_tempCoord = _coordsRemainingSectors call BIS_fnc_selectRandom;
		if ((count _coordsRemainingSectors) > 1) then {
			_coordsRemainingSectors = _coordsRemainingSectors - [_tempCoord];
		};
		_coordsMinorSectors = _coordsMinorSectors + [_tempCoord];
	};
};

// Set Default Sector Owner
private _defaultOwner = 2;

// Generate all Minor Sectors
private _sectorMinorCounter = 0;
{
	// Generate height & width for sector area
	_sectorCoords = [(_x select 0), (_x select 1), 0];
	_coordX = _x;
	_coordY = _x;

	// Fetch X Max
	while {(_coordX inArea nearestLocation [_coordX, ""]) == true} do { 
		_coordX = [(_coordX select 0) + 1, _coordX select 1, _coordX select 2]; 
		_coordX inArea nearestLocation [_coordX, ""]; 
	};
	 
	// Fetch Y Max
	while {(_coordY inArea nearestLocation [_coordY, ""]) == true} do { 
		_coordY = [_coordY select 0, (_coordY select 1) + 1, _coordY select 2]; 
		_coordY inArea nearestLocation [_coordY, ""]; 
	};
	
	// Calcuate 2D Squared Distance between orig coords and max X/Y
	_distance = _sectorCoords distance2D [((_coordX select 0) - 1), ((_coordY select 1) - 1), 0];
	
	// Due to DAC not generating waypoints in smaller sectors, set optimal minimum size
	if (_distance < 200) then {
		_distance = 200;
	};
	
	// Create Sector & Set Variables
	_sector = (createGroup sideLogic) createUnit ["ModuleSector_F",_sectorCoords,[],0,"NONE"];
	_areaLogic = (createGroup sideLogic) createUnit ["LocationArea_F",_sectorCoords,[],0,"NONE"];
	_sector synchronizeObjectsAdd [_areaLogic];

		//Sector settings
		_sector setVariable ["CostAir","1"];
		_sector setVariable ["CostInfantry","1"];
		_sector setVariable ["CostPlayers","1"];
		_sector setVariable ["CostTracked","1"];
		_sector setVariable ["CostWater","1"];
		_sector setVariable ["CostWheeled","1"];
		_sector setVariable ["DefaultOwner",_defaultOwner];
		_sector setVariable ["Designation"," "];
		_sector setvariable ["OnOwnerChange","[_this select 0, _this select 1, _this select 2, 0] execVM 'scripts\AEGIS\Random\Sector_Icons.sqf'"];
		_sector setVariable ["Name"," "];
		_sector setVariable ["OwnerLimit","1"];
		_sector setVariable ["ScoreReward","0"];
		_sector setVariable ["sides",[east,west,resistance]];
		
			// Initialize Sector
			[_sector] call BIS_fnc_moduleSector;
			waitUntil {
			 !isNil { _sector getVariable ["finalized",nil] } &&
				{ !( _sector getVariable ["finalized",true] ) }
			};
	
	_sectorMinorCounter = _sectorMinorCounter + 1;
	
	// Append generated markers, coords, and areas
	_sectorTrigger = (_sector getVariable "areas") select 0;
	detailsMinorSectors append [[(_sectorTrigger getVariable "markers") select 1, _sectorCoords, _distance]];
	
} forEach _coordsMinorSectors;


// Generate all Major Sectors
private _sectorMajorCounter = 0;
{
	// Generate height & width for sector area
	_sectorCoords = [(_x select 0), (_x select 1), 0];
	_coordX = _x;
	_coordY = _x;

	// Fetch X Max
	while {(_coordX inArea nearestLocation [_coordX, ""]) == true} do { 
		_coordX = [(_coordX select 0) + 1, _coordX select 1, _coordX select 2]; 
		_coordX inArea nearestLocation [_coordX, ""]; 
	};
	 
	// Fetch Y Max
	while {(_coordY inArea nearestLocation [_coordY, ""]) == true} do { 
		_coordY = [_coordY select 0, (_coordY select 1) + 1, _coordY select 2]; 
		_coordY inArea nearestLocation [_coordY, ""]; 
	};
	
	// Calcuate 2D Squared Distance between orig coords and max X/Y
	_distance = _sectorCoords distance2D [((_coordX select 0) - 1), ((_coordY select 1) - 1), 0];
	_distance = round _distance;
	
	// Due to DAC not generating waypoints in smaller sectors, set optimal minimum size
	if (_distance < 200) then {
		_distance = 200;
	};
	
	// Create Sector & Set Variables
	_sector = (createGroup sideLogic) createUnit ["ModuleSector_F",_sectorCoords,[],0,"NONE"];
	_areaLogic = (createGroup sideLogic) createUnit ["LocationArea_F",_sectorCoords,[],0,"NONE"];
	_sector synchronizeObjectsAdd [_areaLogic];

		//Sector settings
		_sector setVariable ["CostAir","1"];
		_sector setVariable ["CostInfantry","1"];
		_sector setVariable ["CostPlayers","1"];
		_sector setVariable ["CostTracked","1"];
		_sector setVariable ["CostWater","1"];
		_sector setVariable ["CostWheeled","1"];
		_sector setVariable ["DefaultOwner",_defaultOwner];
		_sector setVariable ["Designation"," "];
		_sector setvariable ["OnOwnerChange","[_this select 0, _this select 1, _this select 2, 1] execVM 'scripts\AEGIS\Random\Sector_Icons.sqf'"];
		_sector setVariable ["Name"," "];
		_sector setVariable ["OwnerLimit","1"];
		_sector setVariable ["ScoreReward","0"];
		_sector setVariable ["sides",[east,west,resistance]];
		
			// Initialize Sector
			[_sector] call BIS_fnc_moduleSector;
			waitUntil {
			 !isNil { _sector getVariable ["finalized",nil] } &&
				{ !( _sector getVariable ["finalized",true] ) }
			};
	
	_sectorMajorCounter = _sectorMajorCounter + 1;
	
	// Append generated markers, coords, and areas
	_sectorTrigger = (_sector getVariable "areas") select 0;
	detailsMajorSectors append [[(_sectorTrigger getVariable "markers") select 1, _sectorCoords, _distance]];
	
} forEach _coordsMajorSectors;