cyrq

server_spawnVehicles.sqf

Mar 23rd, 2014
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.45 KB | None | 0 0
  1. /*
  2. server_spawnVehicles.sqf
  3. Author: cyrq (cyrq1337@gmail.com).
  4. Made for DayZed: http://dayzed.eu
  5.  
  6. Thanks to DayZ Epoch for the positioning code: http://epochmod.com/
  7.  
  8. You can use, modify and distribute this without any permissions.
  9. Give credit where credit is due.
  10.  
  11. Description: Spawns Vehicles with random Damage, Loot and Position around the map.
  12. Uses the 999 Call to insert them into the DB.
  13. */
  14. private ["_point","_position","_lootTable","_itemTypes","_weights","_cntWeights","_itemType","_array","_totaldam","_dam","_selection","_data","_temp","_result","_status","_hitpoints","_fuel","_key","_cars","_vehicle","_isAir","_isShip","_dir","_isTooMany","_veh","_num","_allCfgLoots","_roll","_chance","_maxCars","_maxMBikes","_maxBikes","_maxHelis","_maxPlanes","_maxShips","_maxVehicles","_mapArea","_roadList","_buildingList","_findCars","_findMBikes","_findBikes","_findHelis","_findPlanes","_findShips","_allVehicles","_freeSlots","_index","_uniqueID","_typeObj","_posObj","_dirObj","_freeSlotCounter","_Cars","_MBikes","_Bikes","_Helis","_Planes","_Ships","_vehiclePool"];
  15.  
  16. //Make sure it runs via Server
  17. if (!isDedicated) exitWith {};
  18.  
  19. diag_log format ["DAYZED VEHICLE SPAWNER: Initializing..."];
  20.  
  21. //% Chance to spawn vehicles
  22. _roll = round(random 100);
  23. _chance = if (_roll <= 40) then {true} else {false};
  24.  
  25. //Exit if roll less then 40
  26. if (!_chance) exitWith { diag_log format ["DAYZED VEHICLE SPAWNER: Chance was %1. Needed <= 40. Not spawning any vehicles!",_roll];};
  27.  
  28. //Basic Defines - Limits and Values
  29. _maxCars = 20;
  30. _maxMBikes = 5;
  31. _maxBikes = 15;
  32. _maxHelis = 2;
  33. _maxPlanes = 2;
  34. _maxShips = 6;
  35. _maxVehicles = 50;
  36.  
  37. //Center - Novy
  38. _point = getMarkerpos "center";
  39. //Not completly sure about this one, but let's leave it @ 6500 for now
  40. _mapArea = 6500;
  41. //Let's get all the roads in Chernarus
  42. _roadList = _point nearRoads _mapArea;
  43. //Let's get all the buildings in Chernarus. Very demanding, but runs only @ server start
  44. _buildingList = [];
  45. {
  46. if (isClass (configFile >> "CfgBuildingLoot" >> (typeOf _x))) then {
  47.     _buildingList set [count _buildingList,_x];
  48. };
  49. } forEach (_point nearObjects ["Building",_mapArea]);
  50.  
  51. //Let's find all Vehicle types on the Map. Pretty shitty method but I'm a n00b.
  52. _findCars = (_point) nearObjects ["Car",20000];
  53. _findMBikes = (_point) nearObjects ["Motorcycle",20000];
  54. _findBikes = (_point) nearObjects ["Bicycle",20000];
  55. _findHelis = (_point) nearObjects ["Helicopter",20000];
  56. _findPlanes = (_point) nearObjects ["Plane",20000];
  57. _findShips = (_point) nearObjects ["Ship",20000];
  58. //Let's sum it up
  59. _allVehicles = count (_findCars + _findMBikes + _findBikes + _findHelis + _findPlanes + _findShips);
  60. //Simple math to find out how many vehicles we can spawn
  61. _freeSlots = (_maxVehicles - _allVehicles);
  62. //Don't always spawn the max ammount of available slots
  63. _freeSlotCounter = round (random _freeSlots);
  64.  
  65. //Available Vehicles
  66. _Cars = ["ATV_CZ_EP1","Skoda","SkodaBlue","SkodaGreen","SkodaRed","Lada1","Lada2","Lada1_TK_CIV_EP1","LadaLM","Volha_1_TK_CIV_EP1","Volha_2_TK_CIV_EP1","VolhaLimo_TK_CIV_EP1","car_hatchback","car_sedan","S1203_TK_CIV_EP1","hilux1_civil_1_open","hilux1_civil_3_open","hilux1_civil_2_covered","UAZ_RU","UAZ_Unarmed_TK_CIV_EP1","UAZ_Unarmed_UN_EP1","UAZ_CDF","UAZ_Unarmed_TK_EP1","HMMWV_DZ","SUV_DZ","SUV_DZed","BAF_Offroad_W","LandRover_TK_CIV_EP1","BTR40_DZed","UAZ_MG_CDF_DZed","Offroad_DSHKM_Gue_DZed","Ural_CDF","UralCivil","UralCivil2","Ural_UN_EP1","Kamaz_DZed","V3S_Civ","VWGolf","Ikarus","Ikarus_TK_CIV_EP1","Tractor","tractorOld"];
  67. _MBikes = ["M1030","Old_moto_TK_Civ_EP1","TT650_Civ","TT650_TK_EP1"];
  68. _Bikes = ["Old_bike_TK_CIV_EP1","MMT_Civ"];
  69. _Helis = ["UH1H_DZ","UH1H_DZ2","MH6J_DZ","Mi17_DZ","Mi17_Civilian"];
  70. _Planes = ["AN2_DZ"];
  71. _Ships = ["Fishing_Boat","PBX","Smallboat_1"];
  72. _vehiclePool = _Cars + _MBikes + _Bikes + _Helis + _Planes + _Ships;
  73.  
  74. //Show how many vehicles on map
  75. if (_allVehicles > 0) then {
  76.     diag_log format ["DAYZED VEHICLE SPAWNER: Vehicles found: %1 || Cars: %2 || MBikes: %3 || Bikes: %4 || Helis: %5 || Planes: %6 || Ships: %7",_allVehicles, count _findCars, count _findMBikes, count _findBikes, count _findHelis, count _findPlanes, count _findShips];
  77. };
  78.  
  79. //If all vehicles exceeds _maxVehicles exit script
  80. if (_allVehicles >= _maxVehicles) exitWith {
  81.     diag_log format ["DAYZED VEHICLE SPAWNER: Vehicles Limit of %1 reached! Not Spawning!",_maxVehicles];
  82. };
  83.  
  84. //Check if limits reached
  85. if ((count _findCars) >= _maxCars) then {
  86.     diag_log format ["DAYZED VEHICLE SPAWNER: Max Car Count (%1) Reached! [%2] removed from pool!",_maxCars,_Cars];
  87.     _vehiclePool = _vehiclePool - _Cars;
  88. };
  89.  
  90. if ((count _findMBikes) >= _maxMBikes) then {
  91.     diag_log format ["DAYZED VEHICLE SPAWNER: Max MBikes Count (%1) Reached! [%2] removed from pool!",_maxMBikes,_MBikes];
  92.     _vehiclePool = _vehiclePool - _MBikes;
  93. };
  94.  
  95. if ((count _findBikes) >= _maxBikes) then {
  96.     diag_log format ["DAYZED VEHICLE SPAWNER: Max Bikes Count (%1) Reached! [%2] removed from pool!",_maxBikes,_Bikes];
  97.     _vehiclePool = _vehiclePool - _Bikes;
  98. };
  99.  
  100. if ((count _findHelis) >= _maxHelis) then {
  101.     diag_log format ["DAYZED VEHICLE SPAWNER: Max Heli Count (%1) Reached! [%2] removed from pool!",_maxHelis,_Helis];
  102.     _vehiclePool = _vehiclePool - _Helis;
  103. };
  104.  
  105. if ((count _findPlanes) >= _maxPlanes) then {
  106.     diag_log format ["DAYZED VEHICLE SPAWNER: Max Plane Count (%1) Reached! [%2] removed from pool!",_maxPlanes,_Planes];
  107.     _vehiclePool = _vehiclePool - _Planes;
  108. };
  109.  
  110. if ((count _findShips) >= _maxShips) then {
  111.     diag_log format ["DAYZED VEHICLE SPAWNER: Max Ship Count (%1) Reached! [%2] removed from pool!",_maxShips,_Ships];
  112.     _vehiclePool = _vehiclePool - _Ships;
  113. };
  114.  
  115. //If Free slot's available then spawn
  116. if (_freeSlots > 0) then {
  117. diag_log format ["DAYZED VEHICLE SPAWNER: %1 Free Vehicle Slots Available! Spawning %2 Vehicles!!",_freeSlots, _freeSlotCounter];
  118.     while {_freeSlotCounter > 0} do {
  119.             if (_freeSlotCounter > 0 ) then {
  120.                 diag_log format ["DAYZED VEHICLE SPAWNER: %1 Free Vehicle Slots Left. Continuing...",_freeSlotCounter];
  121.             } else {
  122.                 diag_log format ["DAYZED VEHICLE SPAWNER: %1 Free Vehicle Slots Left. Stopping...",_freeSlotCounter];
  123.             };
  124.             waitUntil{!isNil "BIS_fnc_selectRandom"};
  125.             _vehicle = _vehiclePool call BIS_fnc_selectRandom;
  126.             //Check if selected vehicle by BIS_fnc_selectRandom is a Plane or Heli.
  127.             //This is important so we won't have more then allowed Planes/Helis on the Map in one script run.
  128.             //Other classes are not crucial since it's nearly impossible to exceed their limit in one run.
  129.             //Since nearObjects is pretty demanding we'll focus only on those two.
  130.             if (_vehicle isKindOf "Helicopter") then {
  131.                 if ((count ((_point) nearObjects ["Helicopter",20000])) >= _maxHelis) then {
  132.                     _vehiclePool = _vehiclePool - _Helis;
  133.                     waitUntil{!isNil "BIS_fnc_selectRandom"};
  134.                     _vehicle = _vehiclePool call BIS_fnc_selectRandom;
  135.                     diag_log format ["DAYZED VEHICLE SPAWNER: Max Heli Count (%1) Reached! [%2] removed from pool!",_maxHelis,_Helis];
  136.                 };
  137.             };
  138.             if (_vehicle isKindOf "Plane") then {
  139.                 if ((count ((_point) nearObjects ["Plane",20000])) >= _maxPlanes) then {   
  140.                     _vehiclePool = _vehiclePool - _Planes;
  141.                     waitUntil{!isNil "BIS_fnc_selectRandom"};
  142.                     _vehicle = _vehiclePool call BIS_fnc_selectRandom;
  143.                     diag_log format ["DAYZED VEHICLE SPAWNER: Max Plane Count (%1) Reached! [%2] removed from pool!",_maxPlanes,_Planes];
  144.                 };
  145.             };             
  146.             _isAir = _vehicle isKindOf "Air";
  147.             _isShip = _vehicle isKindOf "Ship";
  148.             //If vehicle is a Ship or Heli/Plane don't spawn it in ridiculous positions
  149.             if (_isShip OR _isAir) then {
  150.                 if (_isShip) then {
  151.                     //Spawn Boats anywhere around coast on water
  152.                     waitUntil{!isNil "BIS_fnc_findSafePos"};
  153.                     _position = [_point,0,_mapArea,10,1,2000,1] call BIS_fnc_findSafePos;
  154.                     //diag_log("DEBUG: Spawning Boat near coast " + str(_position));
  155.                 } else {
  156.                     //Spawn air anywhere that is flat
  157.                     waitUntil{!isNil "BIS_fnc_findSafePos"};
  158.                     _position = [_point,0,_mapArea,10,0,2000,0] call BIS_fnc_findSafePos;
  159.                     //diag_log("DAYZED VEHICLE SPAWNER: Spawning Heli/Plane anywhere flat " + str(_position));
  160.                 };
  161.             } else {
  162.                 //Spawn Cars around buildings and 50% near roads
  163.                 if((random 1) > 0.5) then {
  164.                     waitUntil{!isNil "BIS_fnc_selectRandom"};
  165.                     _position = _roadList call BIS_fnc_selectRandom;
  166.                     _position = _position modelToWorld [0,0,0];
  167.                     waitUntil{!isNil "BIS_fnc_findSafePos"};
  168.                     _position = [_position,0,10,10,0,2000,0] call BIS_fnc_findSafePos;
  169.                     //diag_log("DAYZED VEHICLE SPAWNER: Spawning Vehicle near road " + str(_position));
  170.                 } else {
  171.                     waitUntil{!isNil "BIS_fnc_selectRandom"};
  172.                     _position = _buildingList call BIS_fnc_selectRandom;
  173.                     _position = _position modelToWorld [0,0,0];
  174.                     waitUntil{!isNil "BIS_fnc_findSafePos"};
  175.                     _position = [_position,0,40,5,0,2000,0] call BIS_fnc_findSafePos;
  176.                     //diag_log("DAYZED VEHICLE SPAWNER: Spawning Vehicle around building " + str(_position));
  177.  
  178.                 };
  179.             };
  180.             //Sometimes BIS_fnc_findSafePos fails to find height so only proceed when 2 values present
  181.             if ((count _position) == 2) then {
  182.                 //Check if something is around
  183.                 _isTooMany = (_position) nearObjects ["AllVehicles",50];
  184.                 //If another Vehicle is in 50m raddious then don't spawn
  185.                 if ((count _isTooMany) > 0) exitWith { diag_log ("DAYZED VEHICLE SPAWNER: Too many vehicles at " + str(_position));};
  186.  
  187.                 //Create Vehicle
  188.                 _veh = createVehicle [_vehicle, _position, [], 0, "CAN_COLLIDE"];
  189.                 //Set it's position and direction
  190.                 _dir = round(random 180);
  191.                 _veh setdir _dir;
  192.                 _veh setpos _position; 
  193.                 //Clear Defaul cargo defined in ArmA config Files
  194.                 clearWeaponCargoGlobal _veh;
  195.                 clearMagazineCargoGlobal _veh;     
  196.                 //Add MPEventHandler for damage
  197.                 _veh addMPEventHandler ["MPKilled",{_this call vehicle_handleServerKilled;}];
  198.                 //Create an unique ID
  199.                 _uniqueID = str(round(random 999999));
  200.                 _veh setVariable ["ObjectID", _uniqueID, true];
  201.                 _veh setVariable ["ObjectUID", _uniqueID, true];
  202.                 //Set lastUpdate time for server
  203.                 _veh setVariable ["lastUpdate",time,true];
  204.                 //Add it to Object Monitor
  205.                 if(!isNil "dayz_serverObjectMonitor")then {
  206.                     dayz_serverObjectMonitor set [count dayz_serverObjectMonitor, _veh];
  207.                 };
  208.            
  209.             //Add Loot to vehicle (0-3)
  210.             _num = floor(random 4);
  211.             _allCfgLoots = ["policeman","civilian","viralloot","food","generic","medical","trash","hunter","CastleLoot","Church","crashMED","Farm","Hangar","Hangar","hospital","hunter","Industrial","military","militaryEAST","militaryWEST","Residential","Supermarket"];
  212.             for "_x" from 1 to _num do {
  213.                 waitUntil{!isNil "BIS_fnc_selectRandom"};
  214.                 _lootTable = _allCfgLoots call BIS_fnc_selectRandom;
  215.                 _itemTypes = [];
  216.                 { _itemTypes set [count _itemTypes, _x select 0]
  217.                 } foreach getArray (configFile >> "cfgLoot" >> _lootTable);
  218.                 _index = dayz_CLBase find _lootTable;
  219.                 _weights = dayz_CLChances select _index;
  220.                 _cntWeights = count _weights;
  221.                 _index = floor(random _cntWeights);
  222.                 _index = _weights select _index;
  223.                 _itemType = _itemTypes select _index;
  224.                 _veh addMagazineCargoGlobal [_itemType,1];
  225.             };
  226.            
  227.             //If Vehicle is not a bicycle generate damage and fuel and add after it's published to HIVE
  228.             _fuel = 0;
  229.             if (getNumber(configFile >> "CfgVehicles" >> _vehicle >> "isBicycle") != 1) then {
  230.             //Create randomly damaged parts
  231.             _array = [];
  232.             _hitpoints = _veh call vehicle_getHitpoints;
  233.             //Generate damage on all parts
  234.             {   _dam = random(100) / 100;
  235.                 _selection = getText(configFile >> "cfgVehicles" >> (typeOf _veh)  >> "HitPoints" >> _x >> "name");
  236.                     if (_dam > 0) then {
  237.                         _array set [count _array,[_selection,_dam]];
  238.                     };
  239.                 } forEach _hitpoints;
  240.             //Generate fuel
  241.             _fuel = (random(100)) / 100;
  242.             };
  243.        
  244.             //Everything is ready so let's publish to HIVE 
  245.             _typeObj = typeOf _veh;
  246.             _posObj = getPos _veh;
  247.             _dirObj = getDir _veh;
  248.  
  249.             //The Hive 999 call to publish the object
  250.             /* ///START/// */
  251.             _key = format["CHILD:999:select `id` from `vehicle` where `class_name` = '?' LIMIT 1:[""%1""]:", _typeObj];
  252.             _data = "HiveEXT" callExtension _key;            
  253.             _result = call compile format ["%1", _data];
  254.             _status = _result select 0;
  255.             if (_status == "CustomStreamStart") then {
  256.                 "HiveEXT" callExtension _key;
  257.                 _temp = _result select 1;
  258.                 if (_temp == 0) then {
  259.                     _data = "HiveEXT" callExtension format ["CHILD:999:Insert into vehicle
  260.                     (class_name, damage_min, damage_max, fuel_min, fuel_max, limit_min, limit_max, parts, inventory)
  261.                     values
  262.                     ('?',0,0,1.0,1.0,0,99,'',''):[""%1""]:", _typeObj];
  263.                 }; 
  264.             };
  265.            
  266.             _data = "HiveEXT" callExtension format ["CHILD:999:Insert into world_vehicle
  267.             (vehicle_id, world_id, worldspace, chance)
  268.             values
  269.             ((SELECT `id` FROM `vehicle` where `class_name` = '?' LIMIT 1), 1, '%3',1):[""%1"", ""%2""]:", _typeObj, worldName, [_dirObj, _posObj]];
  270.  
  271.             _data = "HiveEXT" callExtension format["CHILD:999:Insert into instance_vehicle
  272.             (world_vehicle_id, instance_id, worldspace, inventory, parts, fuel, damage)
  273.             values
  274.             ((SELECT `id` FROM `world_vehicle` where `vehicle_id` = (SELECT `id` FROM `vehicle` where `class_name` = '%1' LIMIT 1) LIMIT 1), %3, '%2','[[[],[]],[[],[]],[[],[]]]','[]',1,0):[]:", _typeObj, [_dirObj, _posObj], dayZ_instance];
  275.  
  276.             _key = format["CHILD:999:SELECT `id` FROM `instance_vehicle` ORDER BY `id` DESC LIMIT 1:[]:"];
  277.             _data = "HiveEXT" callExtension _key;
  278.  
  279.             _result = call compile format ["%1", _data];
  280.             _status = _result select 0;
  281.             if (_status == "CustomStreamStart") then {
  282.                 _temp = _result select 1;
  283.                 if (_temp == 1) then {
  284.                     _data = "HiveEXT" callExtension _key;
  285.                     _result = call compile format ["%1", _data];
  286.                     _status = _result select 0;
  287.                 }; 
  288.             };
  289.             /* ///END/// */
  290.             //After it's published Set Variables/Damage and Fuel
  291.             _veh setVariable ["lastUpdate",time];
  292.             _veh setVariable ["ObjectID", str(_status), true];
  293.             _veh setVariable ["CharacterID", "1337", true];
  294.             //Set 0 damage
  295.             _veh setDamage 0;
  296.             // Set Hits after ObjectID is set
  297.             {
  298.             _selection = _x select 0;
  299.             _dam = _x select 1;
  300.             //Don't do partial damage to parts cuz in dumb as fuck. If damage roll > 0.66 then destroy it completely...
  301.             if (_dam > 0.66) then {_dam = 1};
  302.             //...and don't destory Engines and FuelTanks completely
  303.             if (_selection in dayZ_explosiveParts and _dam > 0.8) then {_dam = 0.8};
  304.             PVCDZ_veh_SH = [_veh,_selection,_dam];
  305.             publicVariable "PVCDZ_veh_SH";
  306.             if (local _veh) then {
  307.             PVCDZ_veh_SH call fnc_veh_handleDam;
  308.             };
  309.             } forEach _array;
  310.             //Set Fuel
  311.             _veh setFuel _fuel;
  312.             //Set Velocity
  313.             _veh setvelocity [0,0,1];
  314.             //Reset EH
  315.             _veh call fnc_veh_ResetEH;
  316.             //Mounted Slots
  317.             _veh setVehicleInit format['
  318.             this setVehicleVarname "%1";
  319.             %1 = this;
  320.             mounted_actions_init = if (isNil "mounted_actions_init") then { [] } else {mounted_actions_init};
  321.             mounted_actions_init = mounted_actions_init + [%1];
  322.             [%1] call mounted_add_actions;
  323.             ',_vehicle];
  324.             processInitCommands;
  325.             //Update the object via server
  326.             [_veh,"all"] spawn server_updateObject;
  327.             diag_log format ["DAYZED VEHICLE SPAWNER: Spawned %1 near %2 with ObjectID %3",_typeObj,((getPosATL _veh) call fa_coor2str),_status];
  328.         } else {
  329.         diag_log format ["DAYZED VEHICLE SPAWNER: BIS_fnc_findSafePos did not generate Height for %1. Skipping....",_vehicle];
  330.         };
  331.         _freeSlotCounter = _freeSlotCounter - 1;
  332.     };
  333.     sleep 1.5;
  334. };
  335.  
  336. diag_log format ["DAYZED VEHICLE SPAWNER: Finished. Exiting..."];
Add Comment
Please, Sign In to add comment