Jump to content
Sign in to follow this  
joikd

Scripting Issue--four days without success--please help!

Recommended Posts

Here is the original (working) code:

_isAir = vehicle player iskindof "Air";
_inVehicle = (vehicle player != player);
_fastRun = _this select 0;
_dateNow = (DateToNumber date);
_age = -1;
if (!_inVehicle) then {
_position = getPosATL player;
//waitUntil{_position nearObjectsReady 200};
_nearby = _position nearObjects ["building",200]; //nearestObjects [player, ["building"], 200];
_tooManyZs = {alive _x} count (_position nearEntities ["zZombie_Base",400]) > dayz_maxLocalZombies;
{
	_type = typeOf _x;
	_config = 		configFile >> "CfgBuildingLoot" >> _type;
	_canLoot = 		isClass (_config);		
	if (_canLoot) then {			
		_dis = _x distance player;
		if ((_dis < 120) and (_dis > 30)) then {
			_looted = (_x getVariable ["looted",-0.1]);
			_cleared = (_x getVariable ["cleared",true]);
			/*
			if(isServer) then {
				_dateNow = (DateToNumber date);
				_age = (_dateNow - _looted) * 525948;
			} else {
				_dateNow = serverTime;
				_age = (_dateNow * 60) - _looted;
			};
			*/
			_dateNow = (DateToNumber date);
			_age = (_dateNow - _looted) * 525948;
			//diag_log ("SPAWN LOOT: " + _type + " Building is " + str(_age) + " old" );
			if ((_age > 10) and (!_cleared) and !_inVehicle) then {
				_nearByObj = nearestObjects [(getPosATL _x), ["WeaponHolder","WeaponHolderBase"],((sizeOf _type)+5)];
				{deleteVehicle _x} forEach _nearByObj;
				_x setVariable ["cleared",true,true];
				_x setVariable ["looted",_dateNow,true];
			};
			if ((_age > 10) and (_cleared) and !_inVehicle) then {
				//Register
				_x setVariable ["looted",_dateNow,true];
				//cleanup
				_handle = [_x,_fastRun] spawn building_spawnLoot;
				waitUntil{scriptDone _handle};
			};
		};
		if ((time - dayz_spawnWait) > dayz_spawnDelay) then {
			if (dayz_spawnZombies < dayz_maxLocalZombies) then {
				if (!_tooManyZs) then {
					private["_zombied"];
					_zombied = (_x getVariable ["zombieSpawn",-0.1]);
					_dateNow = (DateToNumber date);
					_age = (_dateNow - _zombied) * 525948;
					if (_age > 5) then {
						_bPos = getPosATL _x;
						_zombiesNum = {alive _x} count (_bPos nearEntities ["zZombie_Base",(((sizeOf _type) * 2) + 10)]);
						if (_zombiesNum == 0) then {
							//Randomize Zombies
							_x setVariable ["zombieSpawn",_dateNow,true];
							_handle = [_x,_fastRun] spawn building_spawnZombies;
							waitUntil{scriptDone _handle};
						//} else {
							//_x setVariable ["zombieSpawn",_dateNow,true];
						};
					};
				};
			} else {
				dayz_spawnWait = time;
				dayz_spawnZombies = 0;
			};
		};
	};
	if (!_fastRun) then {
		sleep 0.1;
	};
} forEach _nearby;
};

I want it to first check the range at 500 (if not true), then 400 (if not true), and so on. So, I have tried many, many variations of the code below without success (other than sometimes only 200 will work). Any help would be greatly appreciated!

private ["_position";"_nearby"];
_inVehicle = vehicle player isKindOf "player";
_fastRun = _this select 0;
_position = getPosATL player;
_500m = count (_position nearObjects ["building",500]) < 11;
_400m = count (_position nearObjects ["building",400]) < 11;
_300m = count (_position nearObjects ["building",300]) < 11;
if (_500m) then {
_nearby = _position nearObjects ["building",500];
} else {
if (_400m) then {
_nearby = _position nearObjects ["building",400];
} else {
	if (_300m) then {
		_nearby = _position nearObjects ["building",300];
	} else {			
			_nearby = _position nearObjects ["building",200];
		};
	};
};

{
_type = typeOf _x;
_config = configFile >> "CfgBuildingLoot" >> _type;
_canLoot = isClass (_config);		
if (_canLoot) then {			
	_dis = _x distance player;					
	_bPos = getPosATL _x;
	_zombiesNum = {alive _x} count (_bPos nearEntities ["zZombie_Base",(((sizeOf _type) * 2) + 10)]);
	if (_zombiesNum == 0) then {
		[_x,_fastRun] call building_spawnZombies;
	};				
};
} forEach _nearby;

Edited by joikd

Share this post


Link to post
Share on other sites

The problem seems to be the use of if-then-else.

This does not work:

private ["_position","_nearby"];
_inVehicle = vehicle player isKindOf "player";
_fastRun = _this select 0;
_position = getPosATL player;
/*if (count (_position nearObjects ["building",500]) > 0) then {
_nearby = _position nearObjects ["building",500];
};*/
[color="#FF0000"]if (count (_position nearObjects ["building",500]) < 11) then {
_nearby = _position nearObjects ["building",500];
} else {
if (count (_position nearObjects ["building",400]) < 11) then {
	_nearby = _position nearObjects ["building",400];
} else {
	if (count (_position nearObjects ["building",300]) < 11) then {
		_nearby = _position nearObjects ["building",300];
	} else {			
		_nearby = _position nearObjects ["building",200];
	};
};
};[/color]	
/*switch (true) do {
   case ({_x} count nearestObjects [_position, ["building"], 500] < 11 :
   {
       _nearby = _position nearObjects ["building",500];
   };
   case ({_x} count nearestObjects [_position, ["building"], 400] < 11 :
{
       _nearby = _position nearObjects ["building",400];
};
case ({_x} count nearestObjects [_position, ["building"], 300] < 11 :
{
       _nearby = _position nearObjects ["building",300];
};
   default
   {
	_nearby = _position nearObjects ["building",200];
   };
};*/
{
_type = typeOf _x;
_config = configFile >> "CfgBuildingLoot" >> _type;
_canLoot = isClass (_config);		
if (_canLoot) then {			
	_dis = _x distance player;					
	_bPos = getPosATL _x;
	_zombiesNum = {alive _x} count (_bPos nearEntities ["zZombie_Base",(((sizeOf _type) * 2) + 10)]);
	if (_zombiesNum == 0) then {
		[_x,_fastRun] call building_spawnZombies;
	};				
};
} forEach _nearby;

But, this does:

private ["_position","_nearby"];
_inVehicle = vehicle player isKindOf "player";
_fastRun = _this select 0;
_position = getPosATL player;
[color="#FF0000"]if (count (_position nearObjects ["building",500]) > 0) then {
_nearby = _position nearObjects ["building",500];
};[/color]
/*if (count (_position nearObjects ["building",500]) < 11) then {
_nearby = _position nearObjects ["building",500];
} else {
if (count (_position nearObjects ["building",400]) < 11) then {
	_nearby = _position nearObjects ["building",400];
} else {
	if (count (_position nearObjects ["building",300]) < 11) then {
		_nearby = _position nearObjects ["building",300];
	} else {			
		_nearby = _position nearObjects ["building",200];
	};
};
};*/	
/*switch (true) do {
   case ({_x} count nearestObjects [_position, ["building"], 500] < 11 :
   {
       _nearby = _position nearObjects ["building",500];
   };
   case ({_x} count nearestObjects [_position, ["building"], 400] < 11 :
{
       _nearby = _position nearObjects ["building",400];
};
case ({_x} count nearestObjects [_position, ["building"], 300] < 11 :
{
       _nearby = _position nearObjects ["building",300];
};
   default
   {
	_nearby = _position nearObjects ["building",200];
   };
};*/
{
_type = typeOf _x;
_config = configFile >> "CfgBuildingLoot" >> _type;
_canLoot = isClass (_config);		
if (_canLoot) then {			
	_dis = _x distance player;					
	_bPos = getPosATL _x;
	_zombiesNum = {alive _x} count (_bPos nearEntities ["zZombie_Base",(((sizeOf _type) * 2) + 10)]);
	if (_zombiesNum == 0) then {
		[_x,_fastRun] call building_spawnZombies;
	};				
};
} forEach _nearby;

Any ideas?

Edited by joikd

Share this post


Link to post
Share on other sites

What type of error are you getting, if any? And how are you initializing the code in-game?

Share this post


Link to post
Share on other sites

I recommend you use a switch-statement instead of if-else-if-else-if-else etc. Makes things alot more readable and is generally alot better.

Also, I believe you're using else if wrong.

It would be something like this:

if (CONDITION) then {
// CODE
}else if (CONDITION) then {
//CODE
};

instead of

if (CONDITION) then {
//CODE
} else {
if (CONDITION) then {
//CODE
};
};

//Edit:

switch-statement

Syntax would be like this:

switch (CONDITION) do {
case X: //CODE
case Y: //CODE
case Z: //CODE
};

Share this post


Link to post
Share on other sites

Thanks for the replies.

@JSF I'm not getting any errors (assuming you mean when using the command line switch). This code is from DayZ--not mine. I'm just trying to edit the already working code.

@tryteyker How does switch work if more than one case is true? For my code above, there may be less than 11 buildings in the first three cases, which would make them all true. That's why I placed the 500 check first. If true, assign _nearby, but STOP. It keeps going all the way to the end like the "else" has no meaning, and was "and" instead, so I end up with the 200 _nearby. The preceding are ignored, or passed on. Doesn't the order matter?

Edited by joikd

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×