JacobJ 10 Posted March 8, 2012 (edited) Hey all I got this civilian traffic script which I have made from bits and pieces of code provided here on the forum. It all works, but sometimes it just doesnt. I have narrowed the error down to the coloured code bit. The problem is, that if I put in a to big number in the _MindistanceToNextPos the while-loop goes crazy and the cars doesnt move. It just hangs in that loop. If I put in another lower number, like 100, instead of 400 it runs fine. But why does it hang there? At the moment I see no other option, than removing the distance condition and the whole loop and just satisfy with a random distance to the next waypoint. The reason I would like this, is that it would make the cars drive a bit more realisticly, not stopping all the time and also very much because I think it would help in performance, because the script would be executed a lot fewer times. Here is the script: hunhund = if (count _this >= 1) then {_this select 1} else {false}; if isserver then { SHK_cars = []; createCenter CIVILIAN; call compile preprocessfile "SHK_pos\shk_pos_init.sqf"; sleep 5; hint "GO"; //Script can be improved by using the _pos of the car to generate the new position. To prevent the car from leaving the city, a _car distance marker can be put in to prevent that. //It is essential, that the _spawnrange and _spawnroads is set to as low numbers as possible. That makes the cars being generated a lot quicker and after that, the cars can use the larger //number, which can be defined in the _range and _DisToRoads parameters. private ["_cartypes","_count","_distance","_areacenter","_pos","_cartype","_car","_trg","_cnd","_act","_mrk","_side","_knowsAbout","_range","_onroads","_DisToRoads","_MindistanceToNextPos","_spawnrange","_spawnroads","_DisToRoadsCenter","_rangeCenter"]; _cartypes = ["car_hatchback","car_sedan","datsun1_civil_1_open","datsun1_civil_2_covered","datsun1_civil_3_open","hilux1_civil_1_open","hilux1_civil_2_covered","hilux1_civil_3_open","Ikarus","Lada1","Lada2","LadaLM","MMT_Civ","Skoda","SkodaBlue","SkodaGreen","SkodaRed","Tractor","TT650_Civ","UralCivil","UralCivil2","V3S_Civ","VWGolf","hilux1_civil_3_open_EP1","Ikarus_TK_CIV_EP1","Lada1_TK_CIV_EP1","Lada2_TK_CIV_EP1","LandRover_TK_CIV_EP1","Old_bike_TK_CIV_EP1","Old_moto_TK_Civ_EP1","S1203_TK_CIV_EP1","SUV_TK_CIV_EP1","TT650_TK_CIV_EP1","UAZ_Unarmed_TK_CIV_EP1","Ural_TK_CIV_EP1","V3S_Open_TK_CIV_EP1","Volha_1_TK_CIV_EP1","Volha_2_TK_CIV_EP1","VolhaLimo_TK_CIV_EP1"]; _civtypes = ["TK_CIV_Takistani01_EP1", "TK_CIV_Takistani02_EP1", "TK_CIV_Takistani03_EP1", "TK_CIV_Takistani04_EP1", "TK_CIV_Takistani05_EP1", "TK_CIV_Takistani06_EP1"]; _count = _this select 0; _distance = 50; _areacenter = getMarkerPos "CIVcarMarker"; _side = WEST; _knowsAbout = 0.5; _spawnrange = random 200; _spawnroads = 100; _range = random 600; _rangeCenter = random 200; _DisToRoadsCenter = 100; _DisToRoads = 100; _MindistanceToNextPos = 400; _onroads = 1; for "_i" from 0 to (_count - 1) do { _pos = [_areacenter, _spawnrange, random 359, false, [_onroads, _spawnroads]] call SHK_pos; _cartype = _cartypes select (floor(random(count _cartypes))); _car = (_cartypes select (floor(random(count _cartypes)))) createVehicle (_pos); _car setDir (random 360); _civtype = _civtypes select (floor(random(count _civtypes))); _civ = (creategroup CIVILIAN) createUnit [_civtype,_pos,[],0,"NONE"]; _civ moveInDriver _car; _civ setSpeedMode "NORMAL"; _civ setBehaviour "SAFE"; SHK_cars set [_i,_car]; /* _trg = createTrigger ["EmptyDetector",_pos]; _trg setTriggerArea [_distance,_distance,0,false]; _trg setTriggerActivation ["WEST","PRESENT",true]; _cnd = format ["this && alive (SHK_cars select %1)",_i]; _act = format ["%1 spawn SHK_fncCivBomb",_i]; _trg setTriggerStatements [_cnd,_act,""]; _trg attachTo [_car]; */ if (hunhund) then { _mrk = createMarker [format ["CIVcar_%1",_i],_pos]; _mrk setMarkerShape "Icon"; _mrk setMarkerType "dot"; _mrk setMarkerColor "ColorRed"; _mrk setMarkerText format ["CIVcar_%1", (_i + 1)]; }; [_car,_mrk,_areacenter,_side,_knowsAbout,_civ,_range,_onroads,_DisToRoads,_MindistanceToNextPos,_DisToRoadsCenter,_rangeCenter,_i,_mrk1] spawn { private ["_car","_mrk","_areacenter","_pos","_i","_trg","_side","_knowsAbout","_areaway","_carway","_mrk1"]; _car = _this select 0; _civ = _this select 5; _range = _this select 6; _onroads = _this select 7; _DisToRoads = _this select 8; _MindistanceToNextPos = _this select 9; _DisToRoadsCenter = _this select 10; _rangeCenter = _this select 11; _i = _this select 12; if (hunhund) then { _mrk = _this select 1; //_mrk1 = _this select 13 _mrk1 = createMarker [format ["WhereToCar_%1",_i], getpos _civ]; _mrk1 setMarkerShape "Icon"; _mrk1 setMarkerType "dot"; _mrk1 setMarkerColor "ColorBlue"; _mrk1 setMarkerText format ["W%1", (_i + 1)]; }; _areacenter = _this select 2; _side = _this select 3; _knowsAbout = _this select 4; areaway = 0; loopcount = 0; while {alive _car AND alive _civ} do { if (hunhund) then { _mrk setMarkerPos getpos _car; }; // Drive around if (alive _car AND alive _civ) then { if (unitReady _civ) then { _cardir = getDir _car; areaway = areaway + 1; //hint str format ["Car%1 - Total %2", _i, areaway] if (_civ distance _areacenter > 3200) then { _pos = [_areacenter, _rangeCenter, random 360, false, [_onroads, _DisToRoadsCenter]] call SHK_pos; while {(_civ distance _pos) < _MindistanceToNextPos} do { _pos = [_areacenter, _rangeCenter, random 360, false, [_onroads, _DisToRoadsCenter]] call SHK_pos; sleep 0.5; }; _civ move _pos; _mrk1 setMarkerPos _pos; } else { //Lav det om igen, så det kun er normal random der er herunder. [color="#A52A2A"]_pos = [_civ, _range, (_cardir + ((random 20) - (random 20))), false, [_onroads, _DisToRoads]] call SHK_pos; _relative = _civ distance _pos; while {_relative < _MindistanceToNextPos} do { _pos = [_civ, _range, (_cardir + ((random 20) - (random 20))), false, [_onroads, _DisToRoads]] call SHK_pos; sleep 0.5; _relative = _civ distance _pos; loopcount = loopcount + 1; hint str loopcount; }; _civ move _pos; _mrk1 setMarkerPos _pos; };[/color] }; }; sleep 0.5; }; if (hunhund) then { deleteMarker _mrk; deleteMarker _mrk1; }; }; }; }; hint "CIVcarTraffic.sqf - DONE"; Edited March 13, 2012 by JacobJ Share this post Link to post Share on other sites
JacobJ 10 Posted March 14, 2012 I have now removed those while loops and now it is working, but I am not happy with this. I need it to check if the next waypoint is closer than a certain distance or else it must generate a new one. But maybe my logic is wrong? It has to generate that position anyways, wouldnt it be easier on the server if it just satisfied with that position and used that instead of generating a new one? But this isnt looking very good, if they drew longer it would be nicer to look at. How can I put in a condition that would run the _pos code again to get a position further away and lets say 200 meters? Simpler script, but not what I am looking for: hunhund = if (count _this >= 1) then {_this select 1} else {false}; if isserver then { SHK_cars = []; createCenter CIVILIAN; call compile preprocessfile "SHK_pos\shk_pos_init.sqf"; //Script can be improved by using the _pos of the car to generate the new position. To prevent the car from leaving the city, a _car distance marker can be put in to prevent that. //It is essential, that the _spawnrange and _spawnroads is set to as low numbers as possible. That makes the cars being generated a lot quicker and after that, the cars can use the larger //number, which can be defined in the _range and _DisToRoads parameters. private ["_cartypes","_count","_distance","_areacenter","_pos","_cartype","_car","_trg","_cnd","_act","_mrk","_side","_knowsAbout","_range","_onroads","_DisToRoads","_MindistanceToNextPos","_spawnrange","_spawnroads","_DisToRoadsCenter","_rangeCenter"]; _cartypes = ["car_hatchback","car_sedan","datsun1_civil_1_open","datsun1_civil_2_covered","datsun1_civil_3_open","hilux1_civil_1_open","hilux1_civil_2_covered","hilux1_civil_3_open","Ikarus","Lada1","Lada2","LadaLM","MMT_Civ","Skoda","SkodaBlue","SkodaGreen","SkodaRed","Tractor","TT650_Civ","UralCivil","UralCivil2","V3S_Civ","VWGolf","hilux1_civil_3_open_EP1","Ikarus_TK_CIV_EP1","Lada1_TK_CIV_EP1","Lada2_TK_CIV_EP1","LandRover_TK_CIV_EP1","Old_bike_TK_CIV_EP1","Old_moto_TK_Civ_EP1","S1203_TK_CIV_EP1","SUV_TK_CIV_EP1","TT650_TK_CIV_EP1","UAZ_Unarmed_TK_CIV_EP1","Ural_TK_CIV_EP1","V3S_Open_TK_CIV_EP1","Volha_1_TK_CIV_EP1","Volha_2_TK_CIV_EP1","VolhaLimo_TK_CIV_EP1"]; _civtypes = ["TK_CIV_Takistani01_EP1", "TK_CIV_Takistani02_EP1", "TK_CIV_Takistani03_EP1", "TK_CIV_Takistani04_EP1", "TK_CIV_Takistani05_EP1", "TK_CIV_Takistani06_EP1"]; _count = _this select 0; _distance = 50; _areacenter = getMarkerPos "CIVcarMarker"; _side = WEST; _knowsAbout = 0.5; _spawnrange = random 200; _spawnroads = 100; _range = random 500; _DisToRoads = 100; _MindistanceToNextPos = 100; _onroads = 1; for "_i" from 0 to (_count - 1) do { _pos = [_areacenter, _spawnrange, random 359, false, [_onroads, _spawnroads]] call SHK_pos; _cartype = _cartypes select (floor(random(count _cartypes))); _car = (_cartypes select (floor(random(count _cartypes)))) createVehicle (_pos); _car setDir (random 360); _civtype = _civtypes select (floor(random(count _civtypes))); _civ = (creategroup CIVILIAN) createUnit [_civtype,_pos,[],0,"NONE"]; _civ moveInDriver _car; _civ setSpeedMode "NORMAL"; _civ setBehaviour "SAFE"; SHK_cars set [_i,_car]; /* _trg = createTrigger ["EmptyDetector",_pos]; _trg setTriggerArea [_distance,_distance,0,false]; _trg setTriggerActivation ["WEST","PRESENT",true]; _cnd = format ["this && alive (SHK_cars select %1)",_i]; _act = format ["%1 spawn SHK_fncCivBomb",_i]; _trg setTriggerStatements [_cnd,_act,""]; _trg attachTo [_car]; */ if (hunhund) then { _mrk = createMarker [format ["CIVcar_%1",_i],_pos]; _mrk setMarkerShape "Icon"; _mrk setMarkerType "dot"; _mrk setMarkerColor "ColorRed"; _mrk setMarkerText format ["CIVcar_%1", (_i + 1)]; }; [_car,_mrk,_areacenter,_side,_knowsAbout,_civ,_range,_onroads,_DisToRoads,_i,_mrk1] spawn { private ["_car","_mrk","_areacenter","_pos","_i","_trg","_side","_knowsAbout","_areaway","_carway","_mrk1"]; _car = _this select 0; _civ = _this select 5; _range = _this select 6; _onroads = _this select 7; _DisToRoads = _this select 8; _i = _this select 9; if (hunhund) then { _mrk = _this select 1; //_mrk1 = _this select 13 _mrk1 = createMarker [format ["WhereToCar_%1",_i], getpos _civ]; _mrk1 setMarkerShape "Icon"; _mrk1 setMarkerType "dot"; _mrk1 setMarkerColor "ColorBlue"; _mrk1 setMarkerText format ["W%1", (_i + 1)]; }; _areacenter = _this select 2; _side = _this select 3; _knowsAbout = _this select 4; areaway = 0; [color="#800000"] while {alive _car AND alive _civ} do { if (hunhund) then { _mrk setMarkerPos getpos _car; }; // Drive around if (alive _car AND alive _civ) then { if (unitReady _civ) then { _cardir = getDir _car; areaway = areaway + 1; //hint str format ["Car%1 - Total %2", _i, areaway] _pos = [_civ, _range, (_cardir + ((random 20) - (random 20))), false, [_onroads, _DisToRoads]] call SHK_pos; if (_civ distance _areacenter > 3200) then { _pos = [_areacenter, _range, random 360, false, [_onroads, _DisToRoads]] call SHK_pos; }; _civ move _pos; _mrk1 setMarkerPos _pos; }; }; sleep 2; };[/color] if (hunhund) then { deleteMarker _mrk; deleteMarker _mrk1; }; }; }; }; hint "CIVcarTraffic.sqf - DONE"; ---------- Post added at 04:18 PM ---------- Previous post was at 03:27 PM ---------- Okay I admit that I am not the sharpest knife in the drawer, when it comes to programming arma 2 stuff, but this is just silly.. I put in the _MinDistanceToNextPos into the _range parameter that is send to the SHK_pos script. By doing that I dont need loops like that to check the distance, because the distance to the next pos will be atleast the _MinDistanceToNextPos variable. Here is the code and I have added a distance hint to show how far away car1 is from W1. hunhund = if (count _this >= 1) then {_this select 1} else {false}; if isserver then { SHK_cars = []; createCenter CIVILIAN; call compile preprocessfile "SHK_pos\shk_pos_init.sqf"; //Script can be improved by using the _pos of the car to generate the new position. To prevent the car from leaving the city, a _car distance marker can be put in to prevent that. //It is essential, that the _spawnrange and _spawnroads is set to as low numbers as possible. That makes the cars being generated a lot quicker and after that, the cars can use the larger //number, which can be defined in the _range and _DisToRoads parameters. private ["_cartypes","_count","_distance","_areacenter","_pos","_cartype","_car","_trg","_cnd","_act","_mrk","_side","_knowsAbout","_range","_onroads","_DisToRoads","_MindistanceToNextPos","_spawnrange","_spawnroads","_DisToRoadsCenter","_rangeCenter"]; _cartypes = ["car_hatchback","car_sedan","datsun1_civil_1_open","datsun1_civil_2_covered","datsun1_civil_3_open","hilux1_civil_1_open","hilux1_civil_2_covered","hilux1_civil_3_open","Ikarus","Lada1","Lada2","LadaLM","MMT_Civ","Skoda","SkodaBlue","SkodaGreen","SkodaRed","Tractor","TT650_Civ","UralCivil","UralCivil2","V3S_Civ","VWGolf","hilux1_civil_3_open_EP1","Ikarus_TK_CIV_EP1","Lada1_TK_CIV_EP1","Lada2_TK_CIV_EP1","LandRover_TK_CIV_EP1","Old_bike_TK_CIV_EP1","Old_moto_TK_Civ_EP1","S1203_TK_CIV_EP1","SUV_TK_CIV_EP1","TT650_TK_CIV_EP1","UAZ_Unarmed_TK_CIV_EP1","Ural_TK_CIV_EP1","V3S_Open_TK_CIV_EP1","Volha_1_TK_CIV_EP1","Volha_2_TK_CIV_EP1","VolhaLimo_TK_CIV_EP1"]; _civtypes = ["TK_CIV_Takistani01_EP1", "TK_CIV_Takistani02_EP1", "TK_CIV_Takistani03_EP1", "TK_CIV_Takistani04_EP1", "TK_CIV_Takistani05_EP1", "TK_CIV_Takistani06_EP1"]; _count = _this select 0; _distance = 50; _areacenter = getMarkerPos "CIVcarMarker"; _side = WEST; _knowsAbout = 0.5; _spawnrange = random 200; _spawnroads = 100; _range = random 500; _DisToRoads = 100; _MindistanceToNextPos = 10; _onroads = 1; for "_i" from 0 to (_count - 1) do { _pos = [_areacenter, _spawnrange, random 359, false, [_onroads, _spawnroads]] call SHK_pos; _cartype = _cartypes select (floor(random(count _cartypes))); _car = (_cartypes select (floor(random(count _cartypes)))) createVehicle (_pos); _car setDir (random 360); _civtype = _civtypes select (floor(random(count _civtypes))); _civ = (creategroup CIVILIAN) createUnit [_civtype,_pos,[],0,"NONE"]; _civ moveInDriver _car; _civ setSpeedMode "NORMAL"; _civ setBehaviour "SAFE"; SHK_cars set [_i,_car]; /* _trg = createTrigger ["EmptyDetector",_pos]; _trg setTriggerArea [_distance,_distance,0,false]; _trg setTriggerActivation ["WEST","PRESENT",true]; _cnd = format ["this && alive (SHK_cars select %1)",_i]; _act = format ["%1 spawn SHK_fncCivBomb",_i]; _trg setTriggerStatements [_cnd,_act,""]; _trg attachTo [_car]; */ if (hunhund) then { _mrk = createMarker [format ["CIVcar_%1",_i],_pos]; _mrk setMarkerShape "Icon"; _mrk setMarkerType "dot"; _mrk setMarkerColor "ColorRed"; _mrk setMarkerText format ["CIVcar_%1", (_i + 1)]; }; [_car,_mrk,_areacenter,_side,_knowsAbout,_civ,_range,_onroads,_DisToRoads,_i,_mrk1,_MindistanceToNextPos] spawn { private ["_car","_mrk","_areacenter","_pos","_i","_trg","_side","_knowsAbout","_areaway","_carway","_mrk1"]; _car = _this select 0; _civ = _this select 5; _range = _this select 6; _onroads = _this select 7; _DisToRoads = _this select 8; _i = _this select 9; _MindistanceToNextPos = _this select 11; if (hunhund) then { _mrk = _this select 1; //_mrk1 = _this select 13 _mrk1 = createMarker [format ["W%1",_i], getpos _civ]; _mrk1 setMarkerShape "Icon"; _mrk1 setMarkerType "dot"; _mrk1 setMarkerColor "ColorBlue"; _mrk1 setMarkerText format ["W%1", (_i + 1)]; }; _areacenter = _this select 2; _side = _this select 3; _knowsAbout = _this select 4; areaway = 0; [color="#800000"] if (hunhund) then { [_civ] spawn { private ["_civ"]; _civ = _this select 0; _car1 = SHK_cars select 0; while {alive _civ} do { afstand = _car1 distance (getmarkerpos "W0"); hint str afstand; sleep 0.5; }; }; }; while {alive _car AND alive _civ} do { if (hunhund) then { _mrk setMarkerPos getpos _car; }; // Drive around if (alive _car AND alive _civ) then { if (unitReady _civ) then { _cardir = getDir _car; areaway = areaway + 1; //hint str format ["Car%1 - Total %2", _i, areaway] _pos = [_civ, [color="#FF0000"](_MindistanceToNextPos + _range)[/color], (_cardir + ((random 20) - (random 20))), false, [_onroads, _DisToRoads]] call SHK_pos; if ((_civ distance _areacenter) > 3200) then { _pos = [_areacenter, _range, random 360, false, [_onroads, _DisToRoads]] call SHK_pos; }; _civ move _pos; _mrk1 setMarkerPos _pos; }; }; sleep 2; };[/color] if (hunhund) then { deleteMarker _mrk; deleteMarker _mrk1; }; }; }; }; hint "CIVcarTraffic.sqf - DONE"; Share this post Link to post Share on other sites