dayZ NPC (AI) Units / Troops | Add to Server.

where does your "player" variable come from ? remember you are on the server.

lemme quote some codesnippets:


Code:
// SARGE DEBUG - TODO - eventually adjust the sleep timer
//
// Traces only bandits
 
private["_ai","_magazintype","_entity_array","_humanity","_humanitylimit","_sleeptime","_detectrange"];
 
_ai = _this select 0;
 
_weapons = weapons _ai;
_weapon = _weapons select 0;
_magazinetype= getArray (configFile >> "CfgWeapons" >> _weapon >> "magazines") select 0;
 
_detectrange=300;
_humanitylimit=0;
_humanity=0;
_sleeptime=0.5;
 
 
while {alive _ai} do {
 
    //diag_log "heartbeat";
 
    _entity_array = (position _ai) nearEntities [["Bandit1_DZ","BanditW1_DZ"],_detectrange];
 
    {
 
        if(vehicle _ai != _ai) then { // NPC in a vehicle
 
            if(isPlayer _x) then {
                _humanity= _x getVariable ["humanity",0];
 
                If (_humanity < _humanitylimit && rating _x > -10000) then {
                    diag_log format["reducing rating for player: %1",name _x];
                    _x addrating -10000;
                };
            };
     
        } else { //NPC on foot
 
            if(isPlayer _x) then {
                _humanity= _x getVariable ["humanity",0];
 
                If (_humanity < _humanitylimit && rating _x > -10000) then {
                    diag_log format["reducing rating for player: %1",name _x];
                    _x addrating -10000;
                };
            };
        };
 
     
    } forEach _entity_array;
 
    // refresh ammo & fuel
 
    if(vehicle _ai != _ai) then {
        vehicle _ai setVehicleAmmo 1;
        //refuel, so they never run out of fuel
        _vehicle = vehicle _ai;
        _vehicle setFuel 1;
 
    } else {
        if !(someAmmo _ai) then {
            {_ai removeMagazine _x} forEach magazines _ai;
            _ai addMagazine _magazintype;
        };
    };
 
    sleep _sleeptime;
 
};

Thats the function i run in the vehicleInit of the generated AI. do NOT use as is, check if you understand what i am doing there and adjust accordingly.

At the end of the script, the AI is dead ... so this could be utilzed for any respawns / cleanups etc.

cheers

Sarge
 
where does your "player" variable come from ? remember you are on the server.

lemme quote some codesnippets:


Code:
// SARGE DEBUG - TODO - eventually adjust the sleep timer
//
// Traces only bandits
 
private["_ai","_magazintype","_entity_array","_humanity","_humanitylimit","_sleeptime","_detectrange"];
 
_ai = _this select 0;
 
_weapons = weapons _ai;
_weapon = _weapons select 0;
_magazinetype= getArray (configFile >> "CfgWeapons" >> _weapon >> "magazines") select 0;
 
_detectrange=300;
_humanitylimit=0;
_humanity=0;
_sleeptime=0.5;
 
 
while {alive _ai} do {
 
    //diag_log "heartbeat";
 
    _entity_array = (position _ai) nearEntities [["Bandit1_DZ","BanditW1_DZ"],_detectrange];
 
    {
 
        if(vehicle _ai != _ai) then { // NPC in a vehicle
 
            if(isPlayer _x) then {
                _humanity= _x getVariable ["humanity",0];
 
                If (_humanity < _humanitylimit && rating _x > -10000) then {
                    diag_log format["reducing rating for player: %1",name _x];
                    _x addrating -10000;
                };
            };
   
        } else { //NPC on foot
 
            if(isPlayer _x) then {
                _humanity= _x getVariable ["humanity",0];
 
                If (_humanity < _humanitylimit && rating _x > -10000) then {
                    diag_log format["reducing rating for player: %1",name _x];
                    _x addrating -10000;
                };
            };
        };
 
   
    } forEach _entity_array;
 
    // refresh ammo & fuel
 
    if(vehicle _ai != _ai) then {
        vehicle _ai setVehicleAmmo 1;
        //refuel, so they never run out of fuel
        _vehicle = vehicle _ai;
        _vehicle setFuel 1;
 
    } else {
        if !(someAmmo _ai) then {
            {_ai removeMagazine _x} forEach magazines _ai;
            _ai addMagazine _magazintype;
        };
    };
 
    sleep _sleeptime;
 
};

Thats the function i run in the vehicleInit of the generated AI. do NOT use as is, check if you understand what i am doing there and adjust accordingly.

At the end of the script, the AI is dead ... so this could be utilzed for any respawns / cleanups etc.

cheers

Sarge

the player variable is a standard variable in the mission pbo.. on the server I was using _playerObj in some places and _character in some places depending on which files I was in.

so by this logic I should be able to go into my server pbo and in some file, set the rating of players, but I couldn't get this to work. i wonder what I was doing wrong.
 
where does your "player" variable come from ? remember you are on the server.

lemme quote some codesnippets:


Code:
// SARGE DEBUG - TODO - eventually adjust the sleep timer
//
// Traces only bandits
 
private["_ai","_magazintype","_entity_array","_humanity","_humanitylimit","_sleeptime","_detectrange"];
 
_ai = _this select 0;
 
_weapons = weapons _ai;
_weapon = _weapons select 0;
_magazinetype= getArray (configFile >> "CfgWeapons" >> _weapon >> "magazines") select 0;
 
_detectrange=300;
_humanitylimit=0;
_humanity=0;
_sleeptime=0.5;
 
 
while {alive _ai} do {
 
    //diag_log "heartbeat";
 
    _entity_array = (position _ai) nearEntities [["Bandit1_DZ","BanditW1_DZ"],_detectrange];
 
    {
 
        if(vehicle _ai != _ai) then { // NPC in a vehicle
 
            if(isPlayer _x) then {
                _humanity= _x getVariable ["humanity",0];
 
                If (_humanity < _humanitylimit && rating _x > -10000) then {
                    diag_log format["reducing rating for player: %1",name _x];
                    _x addrating -10000;
                };
            };
   
        } else { //NPC on foot
 
            if(isPlayer _x) then {
                _humanity= _x getVariable ["humanity",0];
 
                If (_humanity < _humanitylimit && rating _x > -10000) then {
                    diag_log format["reducing rating for player: %1",name _x];
                    _x addrating -10000;
                };
            };
        };
 
   
    } forEach _entity_array;
 
    // refresh ammo & fuel
 
    if(vehicle _ai != _ai) then {
        vehicle _ai setVehicleAmmo 1;
        //refuel, so they never run out of fuel
        _vehicle = vehicle _ai;
        _vehicle setFuel 1;
 
    } else {
        if !(someAmmo _ai) then {
            {_ai removeMagazine _x} forEach magazines _ai;
            _ai addMagazine _magazintype;
        };
    };
 
    sleep _sleeptime;
 
};

Thats the function i run in the vehicleInit of the generated AI. do NOT use as is, check if you understand what i am doing there and adjust accordingly.

At the end of the script, the AI is dead ... so this could be utilzed for any respawns / cleanups etc.

cheers

Sarge

Basically what I want to do, is make it so that any player that has a humanity less than 2500 gets an automatic rating of -10000 until he raises his humanity back to 2500 again. I've tried setting this in playerSetup and playerSync and was unsuccesful in both. Would you mind helping me figure this out?
 
you will need sth like this in the logic that spawns your AI. If you spawn them with the editor, put this (adjusted to your paths) into the init box of the unit:

Code:
 "null = [this] execVM 'addons\SARGE\SAR_trace_banditsonly.sqf';";

if you generate your AI via scripting a typical spawn routine looks like this: (again, do not use as is, understand it and use the principles)

Code:
if(!isServer) exitWith {};
 
//adjust and cleanup
private ["_counter","_soldierlist", "_group", "_pos","_markerName","_marker","_amountOfVehicles","_hint"];
 
diag_log format["AI Heli Spawning Started"];
 
_patrol_area = _this select 0;
_patrol_area_name = _patrol_area;
 
_patrol_weapon_name="M4A1";
_patrol_magazine_name= getArray (configFile >> "CfgWeapons" >> _patrol_weapon_name >> "magazines") select 0;
 
diag_log format["SARGE DEBUG: magazintype: %1",_patrol_magazine_name];
 
// type of soldier list
 
_leader_sold_list = ["Rocket_DZ"];
_soldier_sold_list = ["Soldier1_DZ","Camo1_DZ"];
 
_soldiertype="Soldier1_DZ";
 
_marker_upsmon = _patrol_area;
_rndpos = [_patrol_area_name] call SHK_pos;
 
// create Heli and patrol the south coast
 
_groupheli = createGroup west;
 
// create the heli
 
_heli = createVehicle ["UH1H_DZ", [(_rndpos select 0) + 10, _rndpos select 1, 80], [], 0, "FLY"];
_heli setVariable ["Sarge",1,true];
_heli engineon true;
_heli allowDamage false;
[_heli] joinSilent _groupheli;
 
// create ppl in it
 
_leaderheli = _groupheli createunit [_leader_sold_list select 0, [(_rndpos select 0) + 10, _rndpos select 1, 0], [], 0.5, "NONE"];
_leaderheli addMagazine _patrol_magazine_name;
_leaderheli addWeapon _patrol_weapon_name;
_leaderheli setVehicleInit "null = [this] execVM 'addons\SARGE\SAR_trace_banditsonly.sqf';";
_leaderheli addMPEventHandler ["MPkilled", {Null = _this execVM "addons\SARGE\SAR_aikilled.sqf";}];
 
_leaderheli action ["getInPilot", _heli];
[_leaderheli] joinSilent _groupheli;
 
//Support
_man2heli = _groupheli createunit [_soldiertype, [(_rndpos select 0) - 30, _rndpos select 1, 0], [], 0.5, "NONE"];
_man2heli addMagazine _patrol_magazine_name;
_man2heli addWeapon _patrol_weapon_name;
_man2heli addMPEventHandler ["MPkilled", {Null = _this execVM "addons\SARGE\SAR_aikilled.sqf";}];
 
_man2heli action ["getInTurret", _heli,[0]];
[_man2heli] joinSilent _groupheli;
 
//Rifleman
_man3heli = _groupheli createunit [_soldiertype, [_rndpos select 0, (_rndpos select 1) + 30, 0], [], 0.5, "NONE"];
_man3heli addMagazine _patrol_magazine_name;
_man3heli addWeapon _patrol_weapon_name;
_man3heli addMPEventHandler ["MPkilled", {Null = _this execVM "addons\SARGE\SAR_aikilled.sqf";}];
 
_man3heli action ["getInTurret", _heli,[1]];
[_man3heli] joinSilent _groupheli;
 
// initialize upsmon for the group
 
_leaderheli = leader _groupheli;
 
null=[_leaderheli,_patrol_area_name,'spawned','nofollow','nowait','aware'] execVM 'addons\UPSMON\scripts\upsmon.sqf';
 
processInitCommands;
 
diag_log "SARGE - AI Heli patrol spawned";
 
If i'm understanding this correctly, i could create a mission file called myfile.sqf, and run it in the units init and from myfile.sqf I could run your previous script, which would refill their mags, and detect players?
 
Code:
private["_ai","_magazintype","_entity_array","_humanity","_humanitylimit","_sleeptime","_detectrange"];
_ai = _this select 0;
_weapons = weapons _ai;
_weapon = _weapons select 0;
_magazinetype= getArray (configFile >> "CfgWeapons" >> _weapon >> "magazines") select 0;
 
_detectrange=300;
_humanitylimit=0;
_humanity=0;
_sleeptime=0.5;
 
while {alive _ai} do {
    //diag_log "heartbeat";
    _entity_array = (position _ai) nearEntities [["Bandit1_DZ","BanditW1_DZ"],_detectrange];
    {
        if(vehicle _ai != _ai) then { // NPC in a vehicle
            if(isPlayer _x) then {
                _humanity= _x getVariable ["humanity",0];
                If (_humanity < _humanitylimit && rating _x > -10000) then {
                    diag_log format["reducing rating for player: %1",name _x];
                    _x addrating -10000;
                };
            };
        } else { //NPC on foot
            if(isPlayer _x) then {
                _humanity= _x getVariable ["humanity",0];
                If (_humanity < _humanitylimit && rating _x > -10000) then {
                    diag_log format["reducing rating for player: %1",name _x];
                    _x addrating -10000;
                };
            };
        };
 
    } forEach _entity_array;
 
    // refresh ammo
    if !(someAmmo _ai) then {
        {_ai removeMagazine _x} forEach magazines _ai;
        _ai addMagazine _magazintype;
    };
 
    sleep _sleeptime;
};

So I could run this for a basic ground unit? Could you show your original createUnit so I could see what [this] refers to in your units init?
 
How do you generate your AI ?

My schema is:

a) in server_monitor.sqf, i run 2 specific scripts. The first sets up all the definitions for area markers (you only need this if you use areas for AI to patrol, i need it for upsmon. The second calls my AI_setup script.

b) in this AI setup script, i call the different scripts that are used to generate different Ai grous (e.g. a heli group for southcoast patrol, a heli group for eascoast patrol etc.)

c) the heli group generation script is posted above (takes an areaname as parameter)

d) this heli group generation script creates the units, and defines the script that is being run on UNIT/AI init.

e) the init script was the forst i posted - thats the one that runs while the Ai unit is alive.

General: you can skip the step with your myfile.sqf. Simply find where your AI units are being defined, and add my script (adjusted) to the unit init command.
 
Code:
private["_ai","_magazintype","_entity_array","_humanity","_humanitylimit","_sleeptime","_detectrange"];
_ai = _this select 0;
_weapons = weapons _ai;
_weapon = _weapons select 0;
_magazinetype= getArray (configFile >> "CfgWeapons" >> _weapon >> "magazines") select 0;
 
_detectrange=300;
_humanitylimit=0;
_humanity=0;
_sleeptime=0.5;
 
while {alive _ai} do {
    //diag_log "heartbeat";
    _entity_array = (position _ai) nearEntities [["Bandit1_DZ","BanditW1_DZ"],_detectrange];
    {
        if(vehicle _ai != _ai) then { // NPC in a vehicle
            if(isPlayer _x) then {
                _humanity= _x getVariable ["humanity",0];
                If (_humanity < _humanitylimit && rating _x > -10000) then {
                    diag_log format["reducing rating for player: %1",name _x];
                    _x addrating -10000;
                };
            };
        } else { //NPC on foot
            if(isPlayer _x) then {
                _humanity= _x getVariable ["humanity",0];
                If (_humanity < _humanitylimit && rating _x > -10000) then {
                    diag_log format["reducing rating for player: %1",name _x];
                    _x addrating -10000;
                };
            };
        };
 
    } forEach _entity_array;
 
    // refresh ammo
    if !(someAmmo _ai) then {
        {_ai removeMagazine _x} forEach magazines _ai;
        _ai addMagazine _magazintype;
    };
 
    sleep _sleeptime;
};

So I could run this for a basic ground unit? Could you show your original createUnit so I could see what [this] refers to in your units init?

Sure you can.

In

Code:
_leaderheli setVehicleInit "null = [this] execVM 'addons\SARGE\SAR_trace_banditsonly.sqf';";

the this is being filled in with the Object that ran the init, in this case the AI unit. You access that value in the assigned sqf with _this.

recommended read: http://community.bistudio.com/wiki/this

cheers

Sarge
 
I generate them in my mission.pbo init file. I create them when the server is created and have them patrol areas.
 
Anybody figure out a way to get the AI to attack zombies all the time, whether they've focused the player first or not.. or whatever the mechanics is ;)
 
Last questions for now Sarge (thanks for all the help btw):
you use "nearEntities [["Bandit1_DZ","BanditW1_DZ"]"
is there a way to set that just to players in general, because I want to set my humanity limit higher, and you don't have those skins when you're at a positive limit still I believe.

you also use this:
getArray (configFile >> "CfgWeapons" >> _weapon >> "magazines") select 0;
is that a universal thing? cause I do removeAllWeopans and then addMagazine on my units to set their magazines.
 
Anybody figure out a way to get the AI to attack zombies all the time, whether they've focused the player first or not.. or whatever the mechanics is ;)

So you mean switch targets from a player to the Zeds ? Think thats not possible, there is no "threat level logic within the Arma2 engine. Happy to learn if its different.
 
in my simple ai tutorial thing i posted, it's the zombie_generate.sqf file fix.

I can't edit the dayz_code file on my server.. This is restricted by my provider.

@Sarge: No, I just mean "the friendlies" .. They seem to attack some zombies, then leave the rest alone..
 
in my simple ai tutorial thing i posted, it's the zombie_generate.sqf file fix.

This will not let the AI switch targets if they already spotted a player and targetted him.

check http://community.bistudio.com/wiki/knowsAbout

afaik this value can not be manipulated, otherwise we would have a way for the AI to drop aggro / switch targets.

What eventually may work (untested) is to have a function on the group leader that checks if Zeds are around, and if, order all people in the group to attack the closest zombie.
 
I can't edit the dayz_code file on my server.. This is restricted by my provider.

@Sarge: No, I just mean "the friendlies" .. They seem to attack some zombies, then leave the rest alone..
You don't have to edit it. You just go into your install folder, and then into the map that you're using (labeled as @DAYZMAP) and pull the pbo from there, open it, pull the file and put it into your scripts folder, and then add that line that I said into the very bottom of the file, and add the new line into your mission init.
 
This will not let the AI switch targets if they already spotted a player and targetted him.

check http://community.bistudio.com/wiki/knowsAbout

afaik this value can not be manipulated, otherwise we would have a way for the AI to drop aggro / switch targets.

What eventually may work (untested) is to have a function on the group leader that checks if Zeds are around, and if, order all people in the group to attack the closest zombie.
correct. he just wants his AI to be able to attack zombies.
 
I can't edit the dayz_code file on my server.. This is restricted by my provider.

@Sarge: No, I just mean "the friendlies" .. They seem to attack some zombies, then leave the rest alone..

Make sure that all of the Zeds have a negative rating. My groups choose targets randomly as well, and it might be that they target a zombie further away. If they stand in the middle of a bunch of Zombies and do not shoot at all anymore, check if they are out of ammo :)
 
Last questions for now Sarge (thanks for all the help btw):
you use "nearEntities [["Bandit1_DZ","BanditW1_DZ"]"
is there a way to set that just to players in general, because I want to set my humanity limit higher, and you don't have those skins when you're at a positive limit still I believe.

you also use this:
getArray (configFile >> "CfgWeapons" >> _weapon >> "magazines") select 0;
is that a universal thing? cause I do removeAllWeopans and then addMagazine on my units to set their magazines.
You see this question Sarge?
 
Also, 1 more question:
Could I set up an eventHandler so that when someone kills one of my AI that it gives them a survivor kill or bandit kill and reduces their humanity? so that they can become bandits from killing AI?
 
Back
Top