Spawn wrecks dynamically

Johnkok

Well-Known Member
Hi,

Is it possible to have a wreck spawned dynamically? What I mean by this is to have a wreck spawn in a certain location under a certain condition. I am looking to have the wreck spawn when I need it to rather than a random time under chance.

So for example, if I walk into an certain area it spawns a wreck?
 
you'd need to set up some triggers similar to dome protection via the mission sqm

Thanks for the reply Player2. The trigger system I understand, it is the wreck spawning system I don't quite get. I am not a coder, although I have started learning, I don't know which bits to use from the spawnwrecks file in the trigger.
 
I'm sure there is an easier way to do it and I don't know if this would work but you can certainly give it a shot, just make a trigger with that executes spawnWreck.sqf and put this into spawnWreck.sqf.

Now BEFORE YOU DO THIS I have to say I threw this together in five minutes by hacking up server_spawnCrashSite.sqf so I doubt it will work, please backup all of your database and mission file before you try it. I doubt it could mess anything up but better safe than sorry eh? Also I left all the variables in private that I took out so if you wanted to make it cleaner you could go through and find what I took out.
Code:
private ["_guaranteedLoot","_randomizedLoot","_spawnOnStart","_frequency","_variance","_spawnChance","_spawnMarker","_spawnRadius","_spawnFire","_fadeFire","_timeAdjust","_timeToSpawn","_crashModel","_lootTable","_crashName","_debugarea","_crash","_width","_length","_position","_y","_crashAngle","_itemTypes","_index","_weights","_cntWeights","_itemType","_lootpos","_angle","_radius","_item","_clutter","_config","_newHeight"];
 
_guaranteedLoot = _this select 0;
_randomizedLoot = _this select 1;
_spawnOnStart = _this select 2;
_frequency = _this select 3;
_variance = _this select 4;
_spawnChance = _this select 5;
_spawnMarker = _this select 6;
_spawnRadius = _this select 7;
_spawnFire = _this select 8;
_fadeFire = _this select 9;
 
diag_log("CRASHSPAWNER: Starting spawn logic for Crash Spawner");
 
while {true} do {
    //Selecting random crash type
    _crashModel = ["UH60Wreck_DZ","UH1Wreck_DZ","Mi8Wreck_DZ"] call BIS_fnc_selectRandom;
 
    //selecting loottable
    //Random lootables?
    //if (_crashModel == "Mi8Wreck_DZ") then {_lootTable = ["MilitaryEAST","HeliCrashEAST"] call BIS_fnc_selectRandom;}
    //else {_lootTable = ["MilitaryWEST","HeliCrashWEST"] call BIS_fnc_selectRandom;};
 
    //or just helicrash loottable
    if (_crashModel == "Mi8Wreck_DZ") then {_lootTable = "HeliCrashEAST";}
    else {_lootTable = "HeliCrashWEST";};
 
    _crashName = getText (configFile >> "CfgVehicles" >> _crashModel >> "displayName");
 
    //diag_log(format["CRASHSPAWNER: %1%2 chance to spawn '%3' with loot table '%4' at %5", round(_spawnChance * 100), '%', _crashName, _lootTable, _timeToSpawn]);
 
    // Percentage roll
    if (random 100 <= 50) then {//50=chance to spawn -Spectre
        _debugarea = getMarkerPos "markername";//changemarkername to... you guesed it, your makers name -Spectre
        _crash = _crashModel createVehicleLocal _debugarea;
        sleep 0.01;
        _width = ((boundingBox _crash) select 0) select 0;
        _length = ((boundingBox _crash) select 0) select 1;
 
        _position = _debugarea;
        for "_y" from 1 to 10 do {
            _position = getMarkerPos "markername" call BIS_fnc_findSafePos;
            switch (true) do {
                default { 
                    deleteVehicle _crash;
                    _position set [2,0];
                    _crash = _crashModel createVehicleLocal _position;
                    sleep 0.001;
                    _position = getPosATL _crash;
                    _y = 11; //break;
                };
            };
        };
 
        deleteVehicle _crash; // delete local vehicle used for get width and length
 
        if (count _position >= 2) then {
            diag_log format["CRASHSPAWNER: Spawning '%1' with loot table '%2' at %3", _crashName, _lootTable, _position call fa_coor2str];
 
            _position set [2,0];
            _crash = createVehicle ["ClutterCutter_small_2_EP1", _position, [], 0, "CAN_COLLIDE"]; // used to get modelToWorld
            _crashAngle = random 360;
            _crash setDir _crashAngle;
            _crash setPos _position;
 
            _itemTypes = [] + getArray (configFile >> "CfgBuildingLoot" >> _lootTable >> "lootType");
            _index = dayz_CBLBase find _lootTable;
            _weights = dayz_CBLChances select _index;
            _cntWeights = count _weights;
 
            for "_x" from (round(random _randomizedLoot) + _guaranteedLoot) to 1 step -1  do {
                //create loot
                _index = floor(random _cntWeights);
                _index = _weights select _index;
                _itemType = _itemTypes select _index;
 
                _lootpos = [];
                for [{_y = 0}, {_y < 10 && ((count _lootpos) == 0)}, {_y = _y + 1}] do {
                    _angle = random 360;
                    _radius = 0.9 + random 0.6 ;//* (1 + random 0.3);
                    _lootpos = _crash modelToWorld [_radius*_width*sin(_angle), _radius*_length*cos(_angle), 0];
                    _lootpos = _lootpos findEmptyPosition [0,_length/3, "WeaponHolder"];
                };
                if ((count _lootpos) >= 2) then {
                    _lootpos set [2,0];
                    _item = [_itemType select 0, _itemType select 1, _lootpos, 1] call spawn_loot;
                    _item setVariable ["permaLoot",true];
                 
                    if (dayz_spawnCrashSite_clutterCutter == 1) then { // shift loot upward to 5cm
                        _lootpos set [2,0.05];
                        _item setPosATL _lootpos;
                    }
                    else { if (dayz_spawnCrashSite_clutterCutter >= 2) then { // cutterclutter
                        _clutter = createVehicle ["ClutterCutter_small_2_EP1", _lootpos, [], 0, "CAN_COLLIDE"];
                        _clutter setPos _lootpos;
                        if (dayz_spawnCrashSite_clutterCutter == 3) then { // debug
                            createVehicle ["Sign_sphere100cm_EP1", [_lootpos select 0, _lootpos select 1, 0.30], [], 0, "CAN_COLLIDE"];                 
                        };
                    };};
                 
                    //diag_log(format["CRASHSPAWNER: Loot spawn at '%1 - %3' with loot table '%2'", _crashName, str(_itemType),_lootpos]);
                    sleep 0.001;
                };
            }; // loot loop
         
            _crash = createVehicle [_crashModel,_position, [], 0, "CAN_COLLIDE"];
            _crash setDir _crashAngle;
 
            // Using "custom" wrecks (using the destruction model of a vehicle vs. a prepared wreck model) will result
            // in the model spawning halfway in the ground.  To combat this, an OPTIONAL configuration can be tied to
            // the CfgVehicles class you've created for the custom wreck to define how high above the ground it should
            // spawn.  This is optional.
            _config = configFile >> "CfgVehicles" >> _crashModel >> "heightAdjustment";
            _newHeight = 0;
            if ( isNumber(_config)) then {
                _newHeight = getNumber(_config);
            };
 
            // Must setPos after a setDir otherwise the wreck won't level itself with the terrain
            _crash setPos [(_position select 0), (_position select 1), _newHeight];
 
            // I don't think this is needed (you can't get "in" a crash), but it was in the original DayZ Crash logic
            dayz_serverObjectMonitor set [count dayz_serverObjectMonitor,_crash];
            _crash setVariable ["ObjectID",1,true];
 
            if (_spawnFire) then {
                PVDZ_obj_Fire = [_crash,2,time,false,_fadeFire];
                publicVariable "PVDZ_obj_Fire";
                _crash setvariable ["fadeFire",_fadeFire,true];
            };
            _spawnOnStart = _spawnOnStart -1;
        };
    } else {
        //diag_log(format["CRASHSPAWNER: %1%2 chance to spawn '%3' with loot table '%4' at %5 FAILED (chance)", round(_spawnChance * 100), '%', _crashName, _lootTable, _timeToSpawn]);
    };
};
You also need to change any instance of markername to a marker that you named and placed where you want the wreck to spawn.
Let me know if it works, I will probably mess with it later and see if I can come up with something solid for you, I'm just too tired right now. Good luck!
 
I'm sure there is an easier way to do it and I don't know if this would work but you can certainly give it a shot, just make a trigger with that executes spawnWreck.sqf and put this into spawnWreck.sqf.

Now BEFORE YOU DO THIS I have to say I threw this together in five minutes by hacking up server_spawnCrashSite.sqf so I doubt it will work, please backup all of your database and mission file before you try it. I doubt it could mess anything up but better safe than sorry eh? Also I left all the variables in private that I took out so if you wanted to make it cleaner you could go through and find what I took out.
Code:
private ["_guaranteedLoot","_randomizedLoot","_spawnOnStart","_frequency","_variance","_spawnChance","_spawnMarker","_spawnRadius","_spawnFire","_fadeFire","_timeAdjust","_timeToSpawn","_crashModel","_lootTable","_crashName","_debugarea","_crash","_width","_length","_position","_y","_crashAngle","_itemTypes","_index","_weights","_cntWeights","_itemType","_lootpos","_angle","_radius","_item","_clutter","_config","_newHeight"];
 
_guaranteedLoot = _this select 0;
_randomizedLoot = _this select 1;
_spawnOnStart = _this select 2;
_frequency = _this select 3;
_variance = _this select 4;
_spawnChance = _this select 5;
_spawnMarker = _this select 6;
_spawnRadius = _this select 7;
_spawnFire = _this select 8;
_fadeFire = _this select 9;
 
diag_log("CRASHSPAWNER: Starting spawn logic for Crash Spawner");
 
while {true} do {
    //Selecting random crash type
    _crashModel = ["UH60Wreck_DZ","UH1Wreck_DZ","Mi8Wreck_DZ"] call BIS_fnc_selectRandom;
 
    //selecting loottable
    //Random lootables?
    //if (_crashModel == "Mi8Wreck_DZ") then {_lootTable = ["MilitaryEAST","HeliCrashEAST"] call BIS_fnc_selectRandom;}
    //else {_lootTable = ["MilitaryWEST","HeliCrashWEST"] call BIS_fnc_selectRandom;};
 
    //or just helicrash loottable
    if (_crashModel == "Mi8Wreck_DZ") then {_lootTable = "HeliCrashEAST";}
    else {_lootTable = "HeliCrashWEST";};
 
    _crashName = getText (configFile >> "CfgVehicles" >> _crashModel >> "displayName");
 
    //diag_log(format["CRASHSPAWNER: %1%2 chance to spawn '%3' with loot table '%4' at %5", round(_spawnChance * 100), '%', _crashName, _lootTable, _timeToSpawn]);
 
    // Percentage roll
    if (random 100 <= 50) then {//50=chance to spawn -Spectre
        _debugarea = getMarkerPos "markername";//changemarkername to... you guesed it, your makers name -Spectre
        _crash = _crashModel createVehicleLocal _debugarea;
        sleep 0.01;
        _width = ((boundingBox _crash) select 0) select 0;
        _length = ((boundingBox _crash) select 0) select 1;
 
        _position = _debugarea;
        for "_y" from 1 to 10 do {
            _position = getMarkerPos "markername" call BIS_fnc_findSafePos;
            switch (true) do {
                default {
                    deleteVehicle _crash;
                    _position set [2,0];
                    _crash = _crashModel createVehicleLocal _position;
                    sleep 0.001;
                    _position = getPosATL _crash;
                    _y = 11; //break;
                };
            };
        };
 
        deleteVehicle _crash; // delete local vehicle used for get width and length
 
        if (count _position >= 2) then {
            diag_log format["CRASHSPAWNER: Spawning '%1' with loot table '%2' at %3", _crashName, _lootTable, _position call fa_coor2str];
 
            _position set [2,0];
            _crash = createVehicle ["ClutterCutter_small_2_EP1", _position, [], 0, "CAN_COLLIDE"]; // used to get modelToWorld
            _crashAngle = random 360;
            _crash setDir _crashAngle;
            _crash setPos _position;
 
            _itemTypes = [] + getArray (configFile >> "CfgBuildingLoot" >> _lootTable >> "lootType");
            _index = dayz_CBLBase find _lootTable;
            _weights = dayz_CBLChances select _index;
            _cntWeights = count _weights;
 
            for "_x" from (round(random _randomizedLoot) + _guaranteedLoot) to 1 step -1  do {
                //create loot
                _index = floor(random _cntWeights);
                _index = _weights select _index;
                _itemType = _itemTypes select _index;
 
                _lootpos = [];
                for [{_y = 0}, {_y < 10 && ((count _lootpos) == 0)}, {_y = _y + 1}] do {
                    _angle = random 360;
                    _radius = 0.9 + random 0.6 ;//* (1 + random 0.3);
                    _lootpos = _crash modelToWorld [_radius*_width*sin(_angle), _radius*_length*cos(_angle), 0];
                    _lootpos = _lootpos findEmptyPosition [0,_length/3, "WeaponHolder"];
                };
                if ((count _lootpos) >= 2) then {
                    _lootpos set [2,0];
                    _item = [_itemType select 0, _itemType select 1, _lootpos, 1] call spawn_loot;
                    _item setVariable ["permaLoot",true];
               
                    if (dayz_spawnCrashSite_clutterCutter == 1) then { // shift loot upward to 5cm
                        _lootpos set [2,0.05];
                        _item setPosATL _lootpos;
                    }
                    else { if (dayz_spawnCrashSite_clutterCutter >= 2) then { // cutterclutter
                        _clutter = createVehicle ["ClutterCutter_small_2_EP1", _lootpos, [], 0, "CAN_COLLIDE"];
                        _clutter setPos _lootpos;
                        if (dayz_spawnCrashSite_clutterCutter == 3) then { // debug
                            createVehicle ["Sign_sphere100cm_EP1", [_lootpos select 0, _lootpos select 1, 0.30], [], 0, "CAN_COLLIDE"];               
                        };
                    };};
               
                    //diag_log(format["CRASHSPAWNER: Loot spawn at '%1 - %3' with loot table '%2'", _crashName, str(_itemType),_lootpos]);
                    sleep 0.001;
                };
            }; // loot loop
       
            _crash = createVehicle [_crashModel,_position, [], 0, "CAN_COLLIDE"];
            _crash setDir _crashAngle;
 
            // Using "custom" wrecks (using the destruction model of a vehicle vs. a prepared wreck model) will result
            // in the model spawning halfway in the ground.  To combat this, an OPTIONAL configuration can be tied to
            // the CfgVehicles class you've created for the custom wreck to define how high above the ground it should
            // spawn.  This is optional.
            _config = configFile >> "CfgVehicles" >> _crashModel >> "heightAdjustment";
            _newHeight = 0;
            if ( isNumber(_config)) then {
                _newHeight = getNumber(_config);
            };
 
            // Must setPos after a setDir otherwise the wreck won't level itself with the terrain
            _crash setPos [(_position select 0), (_position select 1), _newHeight];
 
            // I don't think this is needed (you can't get "in" a crash), but it was in the original DayZ Crash logic
            dayz_serverObjectMonitor set [count dayz_serverObjectMonitor,_crash];
            _crash setVariable ["ObjectID",1,true];
 
            if (_spawnFire) then {
                PVDZ_obj_Fire = [_crash,2,time,false,_fadeFire];
                publicVariable "PVDZ_obj_Fire";
                _crash setvariable ["fadeFire",_fadeFire,true];
            };
            _spawnOnStart = _spawnOnStart -1;
        };
    } else {
        //diag_log(format["CRASHSPAWNER: %1%2 chance to spawn '%3' with loot table '%4' at %5 FAILED (chance)", round(_spawnChance * 100), '%', _crashName, _lootTable, _timeToSpawn]);
    };
};
You also need to change any instance of markername to a marker that you named and placed where you want the wreck to spawn.
Let me know if it works, I will probably mess with it later and see if I can come up with something solid for you, I'm just too tired right now. Good luck!

Wow, cool, thank you so much. I will have to try this next week as I am just about to leave for Germany. I am off to Gamescon 2013 in Cologne. I will get back to you ASAP. Thanks again
 
So I have given this a try. So far I have got the trigger working fine in regards to text and area. But I have no wreck spawning or anything in the report. I also have no errors so that is good.

I think I need to test it with a proven something.sqf file to make sure the init statement is working. Will update this post soon.
 
So I have given this a try. So far I have got the trigger working fine in regards to text and area. But I have no wreck spawning or anything in the report. I also have no errors so that is good.

I think I need to test it with a proven something.sqf file to make sure the init statement is working. Will update this post soon.

I have tested the trigger with a working executable sqf. It is fine. It is the spawnWrecks.sqf that is doing nothing - so far.
 
The code I provided was not intended to be used with a trigger if that is what you are doing. For your needs I'd suggest a modification of the animated crashsites script to be used with a trigger. I'm busy right now but tomorrow I'll see if I can make one for you.
 
The code I provided was not intended to be used with a trigger if that is what you are doing. For your needs I'd suggest a modification of the animated crashsites script to be used with a trigger. I'm busy right now but tomorrow I'll see if I can make one for you.

That would be awesome if you could. If you are too busy though and it's a hassle then no worries, I was going to start chopping it up and see how it goes.
 
Back
Top