Update spawn.sqf

Replace switch do block with call using if and exitWith statements. Magazine class items are the most commonly spawned item so it is placed at the top. Switch to the more efficient form of createVehicle. Set more local variables at the top to make the file more readable. Prevent the generator from spawning halfway into the floor by raising the z coordinate. Lower the backpack spawns to prevent them from floating. Add replacement items if dayz_toolBreaking and dayz_knifeDulling are disabled.
This commit is contained in:
worldwidesorrow
2020-01-09 17:07:46 -06:00
committed by GitHub
parent 292ce61378
commit 10889a1541

View File

@@ -12,76 +12,29 @@ Author:
Foxy
*/
#include "\z\addons\dayz_code\util\debug.hpp"
#include "\z\addons\dayz_code\util\vector.hpp"
#include "Loot.hpp"
//Maximum number of magazines spawned along with weapons
#define MAX_WEAPON_MAGAZINES 2
//If defined spawns random (but applicable) magazines along with weapons instead of their primary type.
//#define COMPLEX_WEAPON_MAGAZINES
//If defined calculates better placement for backpacks
//#define COMPLEX_BACKPACK_POSITION
#ifdef SERVER
#define INCREMENT_WEAPON_HOLDERS()
#define INCREMENT_WEAPON_HOLDERS
#else
#define INCREMENT_WEAPON_HOLDERS() dayz_currentWeaponHolders = dayz_currentWeaponHolders + 1
#define INCREMENT_WEAPON_HOLDERS dayz_currentWeaponHolders = dayz_currentWeaponHolders + 1;
#endif
private
[
"_lootInfo",
"_vehicle",
"_spawnCount",
"_magazines"
];
private ["_item","_isWater","_type","_lootInfo","_vehicle","_spawnCount","_magazines"];
_lootInfo = _this select 0;
_pos = _this select 1;
_type = _lootInfo select 0;
_item = _lootInfo select 1;
_isWater = surfaceIsWater _pos;
_vehicle = objNull;
//Switch on type of loot
switch (_lootInfo select 0) do
{
//Spawn a single weapon with [0,MAX_WEAPON_MAGAZINES] magazines.
case Loot_WEAPON:
{
_vehicle = createVehicle ["WeaponHolder", _this select 1, [], 0, "CAN_COLLIDE"];
_vehicle addWeaponCargoGlobal [_lootInfo select 1, 1];
Debug_Assert(typeName (_lootInfo select 1) == typeName "" && { (_lootInfo select 1) != "" });
//Debug_Log(String_Format2("DEBUG: Loot_Spawn Weapon: %1 Position: %2", _lootInfo select 1, _this select 1));
if (surfaceIsWater (_this select 1)) then {
_vehicle setPos (_this select 1);
} else {
_vehicle setPosATL (_this select 1);
};
INCREMENT_WEAPON_HOLDERS();
_magazines = getArray (configFile >> "CfgWeapons" >> _lootInfo select 1 >> "magazines");
if (count _magazines > 0 && {getNumber (configFile >> "CfgWeapons" >> _lootInfo select 1 >> "melee") != 1}) then
{
#ifdef COMPLEX_WEAPON_MAGAZINES
for "_i" from 1 to (floor random (MAX_WEAPON_MAGAZINES + 1)) do
{
_vehicle addMagazineCargoGlobal [_magazines select floor random count _magazines, 1];
};
#else
_vehicle addMagazineCargoGlobal [_magazines select 0, floor random (MAX_WEAPON_MAGAZINES + 1)];
#endif
};
};
call {
//Spawn a single magazine
case Loot_MAGAZINE:
{
private "_item";
_item = _lootInfo select 1;
if (_type == Loot_MAGAZINE) exitWith {
if (dayz_classicBloodBagSystem && _item in dayz_typedBags) then {
if (_item in ["bloodTester","bloodBagAPOS","bloodBagABPOS"]) then { // reduce ItemBloodBag output slightly since typed bags spawn in bulk
_item = ["ItemBandage","ItemPainkiller","ItemMorphine","ItemHeatPack","ItemAntibacterialWipe"] call BIS_fnc_selectRandom;
@@ -89,102 +42,121 @@ switch (_lootInfo select 0) do
_item = "ItemBloodbag";
};
};
_vehicle = createVehicle ["WeaponHolder", _this select 1, [], 0, "CAN_COLLIDE"];
_vehicle addMagazineCargoGlobal [_item, 1];
if (surfaceIsWater (_this select 1)) then {
_vehicle setPos (_this select 1);
} else {
_vehicle setPosATL (_this select 1);
if (!dayz_toolBreaking) then {
if (_item == "equip_lever") then { // The levers are only used if tool breaking is turned on.
_item = ["equip_hose","ItemPlank","equip_nails","ItemLog","equip_brick"] call BIS_fnc_selectRandom;
};
};
if (!dayz_knifeDulling) then {
if (_item == "equip_brick") then { // Bricks are only used to sharpen tools
_item = ["equip_hose","ItemPlank","equip_nails","ItemLog"] call BIS_fnc_selectRandom;
};
};
_vehicle = "WeaponHolder" createVehicle [0,0,0];
_vehicle addMagazineCargoGlobal [_item, 1];
// Fix generator spawning half way in the floor by raising z coordinate by .3 meters.
if (_item == "ItemGenerator") then {
_pos set [2, ((_pos select 2) + .3)];
};
if (_isWater) then {
_vehicle setPos (_pos);
} else {
_vehicle setPosATL (_pos);
};
INCREMENT_WEAPON_HOLDERS
};
//Spawn a single weapon with [0,MAX_WEAPON_MAGAZINES] magazines.
if (_type == Loot_WEAPON) exitWith {
_vehicle = "WeaponHolder" createVehicle [0,0,0];
_vehicle addWeaponCargoGlobal [_item, 1];
if (_isWater) then {
_vehicle setPos (_pos);
} else {
_vehicle setPosATL (_pos);
};
INCREMENT_WEAPON_HOLDERS
_magazines = getArray (configFile >> "CfgWeapons" >> _item >> "magazines");
if (count _magazines > 0 && {getNumber (configFile >> "CfgWeapons" >> _item >> "melee") != 1}) then
{
_vehicle addMagazineCargoGlobal [_magazines select 0, floor random (MAX_WEAPON_MAGAZINES + 1)];
};
INCREMENT_WEAPON_HOLDERS();
};
//Spawn backpack
case Loot_BACKPACK:
{
#ifdef COMPLEX_BACKPACK_POSITION
private ["_b", "_p", "_d"];
_vehicle = createVehicle [_lootInfo select 1, [-10000,0,0], [], 0, "CAN_COLLIDE"];
if (_type == Loot_BACKPACK) exitWith {
_vehicle = _item createVehicle [0,0,0];
_b = boundingBox _vehicle;
_b = ((_b select 1) select 1) - ((_b select 0) select 1);
// Fix floating backpacks by lowering z coordinate by .15 meters.
_pos set [2, ((_pos select 2) - .15)];
_d = Vector_Rotate2D(Vector_NORTH, random 360);
_p = Vector_Subtract(_this select 1, Vector_Multiply_Fast(_d, _b));
_p = Vector_SetZ(_p, Vector_Z(_p) + Vector_Z(getPosATL _vehicle));
_vehicle setVectorDirAndUp [Vector_DOWN, _d];
if (surfaceIsWater _p) then {
_vehicle setPos _p;
if (_isWater) then {
_vehicle setPos (_pos);
} else {
_vehicle setPosATL _p;
_vehicle setPosATL (_pos);
};
#else
_vehicle = createVehicle [_lootInfo select 1, _this select 1, [], 0, "CAN_COLLIDE"];
if (surfaceIsWater (_this select 1)) then {
_vehicle setPos (_this select 1);
} else {
_vehicle setPosATL (_this select 1);
};
#endif
};
//Spawn multiple items from a given group. All but weapons and magazines are ignored.
case Loot_PILE:
{
if (_type == Loot_PILE) exitWith {
_spawnCount = (_lootInfo select 2) + floor random ((_lootInfo select 3) - (_lootInfo select 2) + 1);
_vehicle = createVehicle ["WeaponHolder", _this select 1, [], 0, "CAN_COLLIDE"];
Loot_InsertCargo(_vehicle, _lootInfo select 1, _spawnCount);
if (surfaceIsWater (_this select 1)) then {
_vehicle setPos (_this select 1);
_vehicle = "WeaponHolder" createVehicle [0,0,0];
Loot_InsertCargo(_vehicle, _item, _spawnCount);
if (_isWater) then {
_vehicle setPos (_pos);
} else {
_vehicle setPosATL (_this select 1);
_vehicle setPosATL (_pos);
};
INCREMENT_WEAPON_HOLDERS();
INCREMENT_WEAPON_HOLDERS
};
//Spawn a vehicle
case Loot_VEHICLE:
{
_vehicle = createVehicle [_lootInfo select 1, _this select 1, [], 0, "CAN_COLLIDE"];
if (_type == Loot_VEHICLE) exitWith {
_vehicle = _item createVehicle [0,0,0];
_vehicle setDir random 360;
if (surfaceIsWater (_this select 1)) then {
_vehicle setPos (_this select 1);
if (_isWater) then {
_vehicle setPos (_pos);
} else {
_vehicle setPosATL (_this select 1);
_vehicle setPosATL (_pos);
};
};
//Spawn a container and populate it with loot from a given group
case Loot_CONTAINER:
{
_vehicle = createVehicle [_lootInfo select 1, _this select 1, [], 0, "CAN_COLLIDE"];
INCREMENT_WEAPON_HOLDERS();
if (_type == Loot_CONTAINER) exitWith {
_vehicle = _item createVehicle [0,0,0];
//Number of items to spawn
_spawnCount = (_lootInfo select 3) + floor random ((_lootInfo select 4) - (_lootInfo select 3) + 1);
Loot_InsertCargo(_vehicle, _lootInfo select 2, _spawnCount);
Loot_InsertCargo(_vehicle, _item, _spawnCount);
_vehicle setDir random 360;
if (surfaceIsWater (_this select 1)) then {
_vehicle setPos (_this select 1);
if (_isWater) then {
_vehicle setPos (_pos);
} else {
_vehicle setPosATL (_this select 1);
_vehicle setPosATL (_pos);
};
INCREMENT_WEAPON_HOLDERS
};
//Call a function which is assumed to return an object reference.
case Loot_CUSTOM:
{
_vehicle = call (_lootInfo select 1);
if (_type == Loot_CUSTOM) exitWith {
_vehicle = call (_item);
if ((typeName _vehicle) != "OBJECT") exitWith {};
if (!isNull _vehicle) then {
if (surfaceIsWater (_this select 1)) then {
_vehicle setPos (_this select 1);
if (_isWater) then {
_vehicle setPos (_pos);
} else {
_vehicle setPosATL (_this select 1);
_vehicle setPosATL (_pos);
};
};
};