Simple AI Tutorial (no rMod or DayZ_Factions)

If somebody wants to take a stab at having zombies attack AI, then by all means pitch in - i'm currently looking through all of the dayz_code.pbo and I have some ideas, but I haven't implemented it yet.

Here's a first step though - have the zeds go after your ai gunfire like they would a player.

Add this code in your add_unit_server.sqf (I have mulitple copies of this file to generate different unit types).

Code:
// Alert zombies if a weapon is fired
_aiunit addEventHandler ["Fired", {[(_this Select 0),100,true,(getPosATL (_this Select 0))] execvm "units\NPC_AlertZombies.sqf";}];
 
//_aiunit addEventHandler ["Fired", {diag_log format ["AIUNIT: Alert - unit is shooting shit - %1",_this];}]; // for testing eventhandler
diag_log format ["AIUNIT: Creating MPEvent Handler for %1 ",_aiunit];

It should be within the "for [{ x=1 },{ x < _numunits+1 },{ x = x + 1; }] do " loop near the top.

Then put this sqf file in your units\ directory:

Code:
//NPC_AlertZombies.sqf
 
private["_unit","_distance","_i","_listTalk","_zombie","_targets","_pos"];
//Alert Zed's to noise of shot
_unit =_this select 0;
_distance = _this select 1;
_doRun =_this select 2;
_pos =_this select 3;
_listTalk = _pos nearEntities ["zZombie_Base",_distance * 2];
 
 
// post to the RPT file if an npc alerts a zed
diag_log format ["NPC_AlertZombies: %1, %2, %3, %4",_unit,_distance,_dorun,_pos];
 
 
//hint str(_listTalk);
 
{
_zombie = _x;
if (_doRun) then {
_targets = _zombie getVariable ["targets",[]];
if (!(_unit in _targets)) then {
_targets set [count _targets,_unit];
_zombie setVariable ["targets",_targets,true];
};
} else {
_zombie setVariable ["myDest",_pos,true];
};
} forEach _listTalk;

That's pretty much the same code that alerts the zombies to a player gunshot, but the dayz code passes some ranges based upon the fired weapon, which I'm sure could be added pretty easily - baby steps :)
 
Fantastic work Sycosis and of course Axeman, ButtFace and 3djoker :)

Time to play about with the options, I'd like to have fixed points so they don't patrol, eg add a sniper to a deerstand who shoots anyone who walks/drives past - Would i set the patrol radius to zero or would i need to edit the "MOVE" command ?

How hard would it be to add a new group ? eg add a 4th group so i have 1:
Sniper, 2: Gunner, 3: Militiaman,4: New group
It might be a little complicated to follow the case scheme in the add_unit_server.sqf but if you can manage, it won't be hard at all to add another type of troop. Just copy case 3, where ever there is 1, paste it between 3 and 4, name it 4, and then rename 4 to 5. and at the top of the config add another set of variables (eg: _resistanceTrooptypeRifle) and replace the static variables that are in the new case 4 with these variables.
 
If somebody wants to take a stab at having zombies attack AI, then by all means pitch in - i'm currently looking through all of the dayz_code.pbo and I have some ideas, but I haven't implemented it yet.

Here's a first step though - have the zeds go after your ai gunfire like they would a player.

Add this code in your add_unit_server.sqf (I have mulitple copies of this file to generate different unit types).

Code:
// Alert zombies if a weapon is fired
_aiunit addEventHandler ["Fired", {[(_this Select 0),100,true,(getPosATL (_this Select 0))] execvm "units\NPC_AlertZombies.sqf";}];
 
//_aiunit addEventHandler ["Fired", {diag_log format ["AIUNIT: Alert - unit is shooting shit - %1",_this];}]; // for testing eventhandler
diag_log format ["AIUNIT: Creating MPEvent Handler for %1 ",_aiunit];

It should be within the "for [{ x=1 },{ x < _numunits+1 },{ x = x + 1; }] do " loop near the top.

Then put this sqf file in your units\ directory:

Code:
//NPC_AlertZombies.sqf
 
private["_unit","_distance","_i","_listTalk","_zombie","_targets","_pos"];
//Alert Zed's to noise of shot
_unit =_this select 0;
_distance = _this select 1;
_doRun =_this select 2;
_pos =_this select 3;
_listTalk = _pos nearEntities ["zZombie_Base",_distance * 2];
 
 
// post to the RPT file if an npc alerts a zed
diag_log format ["NPC_AlertZombies: %1, %2, %3, %4",_unit,_distance,_dorun,_pos];
 
 
//hint str(_listTalk);
 
{
_zombie = _x;
if (_doRun) then {
_targets = _zombie getVariable ["targets",[]];
if (!(_unit in _targets)) then {
_targets set [count _targets,_unit];
_zombie setVariable ["targets",_targets,true];
};
} else {
_zombie setVariable ["myDest",_pos,true];
};
} forEach _listTalk;

That's pretty much the same code that alerts the zombies to a player gunshot, but the dayz code passes some ranges based upon the fired weapon, which I'm sure could be added pretty easily - baby steps :)
I've been working with setting the zombies to another group using joinSilent so that they will gain the friendship status' of that group. I haven't been able to get it working yet though.
 
on the first page you say 0 is resistance and 1 is east for faction in the init.sqf.
so i add a group of resistance which to me are the friendly guys (judging by previous loadouts), and set this in add_unit
RESISTANCE setFriend [WEST,.8];
yet on spawn, i get shot. if i do
EAST setFriend [WEST,.8]; and spawn east guys who get the SVD and MK48 loadout, they leave me alone.
RESISTANCE is definitely 0, and .8 should make you friendly. So you should be good to go. Hmm.. If you want to post your init, add_unit, and set_unit, I will take a look, but by all rights you should be good to go with what you have.
 
RESISTANCE is definitely 0, and .8 should make you friendly. So you should be good to go. Hmm.. If you want to post your init, add_unit, and set_unit, I will take a look, but by all rights you should be good to go with what you have.

i got it working by setting my (survivor) friendship with resistance to 0.8 too, didn't know if it had to work both ways but either way they dont fire at me now :)
 
It might be a little complicated to follow the case scheme in the add_unit_server.sqf but if you can manage, it won't be hard at all to add another type of troop. Just copy case 3, where ever there is 1, paste it between 3 and 4, name it 4, and then rename 4 to 5. and at the top of the config add another set of variables (eg: _resistanceTrooptypeRifle) and replace the static variables that are in the new case 4 with these variables.
If all the code for them is inside add_unit_server.sqf i'll try to figure it out :)
Thanks

Did you ever get round to making them respawn ? My players are currently hunting them down and then they don't respawn until server restart.
 
hmm i cant seem to get this to work my server just keeps popping up with something went wrong disconnect and try again i will post my init file after this but i copied all the files over to a script file in my cherno mpmission folder in scripts folder and added the line at end of the zombie generate what am i doing wrong lol
 
/*
INITILIZATION
*/
startLoadingScreen ["","RscDisplayLoadCustom"];
cutText ["","BLACK OUT"];
enableSaving [false, false];

//REALLY IMPORTANT VALUES
dayZ_instance =1;//The instance
dayzHiveRequest = [];
initialized = false;
dayz_previousID = 0;

//disable greeting menu
player setVariable ["BIS_noCoreConversations", true];
//disable radio messages to be heard and shown in the left lower corner of the screen
enableRadio false;

//Load in compiled functions
call compile preprocessFileLineNumbers "\z\addons\dayz_code\init\variables.sqf";//Initilize the Variables (IMPORTANT: Must happen very early)
progressLoadingScreen 0.1;
call compile preprocessFileLineNumbers "\z\addons\dayz_code\init\publicEH.sqf";//Initilize the publicVariable event handlers
progressLoadingScreen 0.2;
call compile preprocessFileLineNumbers "\z\addons\dayz_code\medical\setup_functions_med.sqf";//Functions used by CLIENT for medical
progressLoadingScreen 0.4;
call compile preprocessFileLineNumbers "\z\addons\dayz_code\init\compiles.sqf";//Compile regular functions
progressLoadingScreen 1.0;

"filmic" setToneMappingParams [0.153, 0.357, 0.231, 0.1573, 0.011, 3.750, 6, 4]; setToneMapping "Filmic";

if ((!isServer) && (isNull player) ) then
{
waitUntil {!isNull player};
waitUntil {time > 3};
};

if ((!isServer) && (player != player)) then
{
waitUntil {player == player};
waitUntil {time > 3};
};

zombie_generate = compile preprocessFileLineNumbers "scripts\zombie_generate.sqf";

if (isServer) then {
//
_factions = [] execVM "scripts\set_unit_faction.sqf";
//diag_log format["Factions executed: %1",_factions];
//Array to pass to .sqf: [[<worldspace>],radius of waypoints (in metres),number of waypoints,number of ai units, group type [0:random unit, 1:sniper, 2:gunner, 3:militiaman, 4:squad], faction [0:hates everyone except survivors, 1:hates everyone]]
_aispawn = [[10194.8,2115.37,0.001],100,5,2,4,0] execVM "scripts\add_unit_server.sqf";//Fort des Dunes
};

if (isServer) then {
_serverMonitor =[] execVM "\z\addons\dayz_code\system\server_monitor.sqf";
};

if (!isDedicated) then {
//Conduct map operations
0 fadeSound 0;
waitUntil {!isNil "dayz_loadScreenMsg"};
dayz_loadScreenMsg = (localize "STR_AUTHENTICATING");

//Run the player monitor
_id = player addEventHandler ["Respawn", {_id = [] spawn player_death;}];
_playerMonitor =[] execVM "\z\addons\dayz_code\system\player_monitor.sqf";
};
 
I was getting the same error as above, only mine was also giving me the message Script scripts\zombie_generate.sqf not found. Quadruple checked file paths and spelling, couldn't get it. Even removing the code from the init.sqf didn't fix the "Something went wrong" problem, I had to reup from the deploy folder :-/
 
you had to reup from the deploy folder? so you actually got passed the something went wrong screen did you? please could you tell me how as once im passed that i can get anything else that dont work working. i have got custom bases for players on my map as well as a few army last stand bases and would love to have a few army survivers that have gone insane and shoot everyone (making the AI completely hostile)
 
You have if (isServer) then { listed twice :)

I have AI all over and they work perfectly :) maybe too perfect
 
you had to reup from the deploy folder? so you actually got passed the something went wrong screen did you? please could you tell me how as once im passed that i can get anything else that dont work working. i have got custom bases for players on my map as well as a few army last stand bases and would love to have a few army survivers that have gone insane and shoot everyone (making the AI completely hostile)
take the mission file from the deploy folder (hive\deploy\mpmissions\dayz1_chernarus.pbo) and copy it to your root mpmissions folder (copy and replace when prompted) fixed it for me. I'm going to work on the AI issue again when I get home. If you no longer have a deploy folder, just run the CP again and rebuild. You don't need the whole deploy folder, just the mission file.

I forget, not everyone is running reality or has access to the delpoy folder. You may have to get in contact with your hosting provide for the mission file
 
You have if (isServer) then { listed twice :)

I have AI all over and they work perfectly :) maybe too perfect
Code:
zombie_generate = compile preprocessFileLineNumbers "scripts\zombie_generate.sqf";
 
if (isServer) then {
//
_factions = [] execVM "scripts\set_unit_faction.sqf";
//diag_log format["Factions executed: %1",_factions];
//Array to pass to .sqf: [[<worldspace>],radius of waypoints (in metres),number of waypoints,number of ai units, group type [0:random unit, 1:sniper, 2:gunner, 3:militiaman, 4:squad], faction [0:hates everyone except survivors, 1:hates everyone]]
_aispawn = [[10194.8,2115.37,0.001],100,5,2,4,0] execVM "scripts\add_unit_server.sqf";//Fort des Dunes
};
 
//if (isServer) then {
_serverMonitor =[] execVM "\z\addons\dayz_code\system\server_monitor.sqf";
};
[/quote]

Did I comment out the correct line?
 
Did I comment out the correct line?
Move the line that starts _serverMonitor =[] execVM "\z\addons\dayz_code\system\server_monitor.sqf";
Up to the ai spawn lines, so it looks like
Code:
if (isServer) then {
//
_factions = [] execVM "scripts\set_unit_faction.sqf";
//diag_log format["Factions executed: %1",_factions];
//Array to pass to .sqf: [[<worldspace>],radius of waypoints (in metres),number of waypoints,number of ai units, group type [0:random unit, 1:sniper, 2:gunner, 3:militiaman, 4:squad], faction [0:hates everyone except survivors, 1:hates everyone]]
_aispawn = [[10194.8,2115.37,0] execVM "scripts\add_unit_server.sqf";//Fort des Dunes
 
_serverMonitor =[] execVM "\z\addons\dayz_code\system\server_monitor.sqf";
 
};
Also notice i changed your worldspace, you had the format slightly wrong, Try it and it should work :)
 
thanks richie that was the one i always forget about that you would think by now it would be a straight reaction lol one more little query tho they are only spawning in as civillians with the normal sort of gear aka lee enfield and such is there a way to change them into military with more military weapons? im just testing with one group at the moment untill its sorted
 
Move the line that starts _serverMonitor =[] execVM "\z\addons\dayz_code\system\server_monitor.sqf";
Up to the ai spawn lines, so it looks like

Also notice i changed your worldspace, you had the format slightly wrong, Try it and it should work :)

I'll give it a shot at home! Thanks :)
 
ye just start a fresh with them and do it like that how richie said and they work perfectly fine gunhaver and when changing there uniforms/skins use the BAF ones for the military they the only ones that seem to work only some of the others do. im also wondering if they can have backpacks with weapons or ammo in and be able to use it as it would make it so they have alot more ammo or a different gun with ammo when there one runs out anyone have any ideas and also how to put the backpack code in and where
 
Back
Top