Announcement

Collapse
No announcement yet.

Help with optimising code

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help with optimising code

    Long post ahead, you've been warned.

    I've noticed that we do have some scripters on the forums here, and was wondering if you guys could take a look at the various bits of code I have just to have a new set of eyes look it over.

    A few of my missions sometimes have lag problems at the very end, I really don't think it is any run away scripts, but you never know.

    Also, I'd like my code to be as efficient as possible, because ArmA is a very resource intensive game, and I don't want to slow down the server/clients just to add some features.

    So anyways, I am going to post up a bunch of this code, and if anyone notices anything out of the ordinary that might cause problems, let me know!

    This is the code that detects when you can/cannot place deployables, moves the rallypoint around, and has some PLT CO only stuff. New version of it, not what is currently running in the missions.
    Code:
    //Written by beta
    //Client-side loop logic
    
     
    _x = 0;
    _sandbagFlag = false;
    _wireFlag = false;
    _truckFlag = false;
    _endFlag = false;
    _moveFlag = false;
    _rallyFlag = false;
    
    //infinite loop
    for [{_i=0}, {_i<1}, {_i=0}] do
    {
        _wireArray = nearestObjects [player, ["Wire"], 3];  //get new array of nearby wires
        _sandbagWallArray = nearestObjects [player, ["FenceWood"], 3];  //get new array of nearby sandbag walls
        _sandbagArray = nearestObjects [player, ["Fence"], 3];  //get new array of nearby sandbag
        _sandbagArray =  _sandbagArray - _wireArray;  //take out wires
        _sandbagArray =  _sandbagArray - _sandbagWallArray;  //take out sandbag walls
    	_truckArray = nearestObjects [player, ["ACE_Truck5T_Open"], 50];  //get new array of nearby trucks
    	{if (!alive _x) then {_truckArray = _truckArray - [_x];};} forEach _truckArray;
    	_rallyArray = nearestObjects [BLUFOR_Rally, ["ACE_Truck5T_Open"], 50];  //get new array of nearby trucks
    	{if (!alive _x) then {_rallyArray = _rallyArray - [_x];};} forEach _rallyArray;
    	
    	//if player is within range of a valid truck
        if (count _truckArray > 0 && !_truckFlag) then
        {
    		deployAct = player addAction ['Deploy Dialog', 'Dialogs\DeployDialog_BLUFOR.sqf', [], -5, true, true, ''];
    		_truckFlag = true;
        }
    	else
    	{
    		//if there is not a truck near the player
    		if (count _truckArray < 1) then
    		{ 
    			player removeAction deployAct;
    			_truckFlag = false;
    		};
    	   
    	   	//if there is a wire near the player and the action hasn't been added
            if (count _wireArray > 0 && !_wireFlag) then
            {
               reclaimWireAct = player addAction ["Reclaim wire", "scripts\reclaim_object.sqf", [_wireArray select 0, player], -4, true, true, ""];
               _wireFlag = true;
            }
            else
            {
               //if there is not a wire near the player
               if (count _wireArray < 1) then
               { 
                  player removeAction reclaimWireAct;
                  _wireFlag = false;
               };
            };
    		
    		//if there is a sandbag near the player and the action hasn't been added
            if (count _sandbagArray > 0 && !_sandbagFlag) then
            {
               reclaimSandbagAct = player addAction ["Reclaim sandbags", "scripts\reclaim_object.sqf", [_sandbagArray select 0, player], -4, true, true, ""];
               _sandbagFlag = true;
            }
            else
            {
               //if there is not a sandbag near the player
               if (count _sandbagArray < 1) then
               { 
                  player removeAction reclaimSandbagAct;
                  _sandbagFlag = false;
               };
            };
    	};
    	
    	//if rallypoint is within range of a valid truck
        if (count _rallyArray > 0) then
        {
    		"respawn_west" setMarkerPos getPos BLUFOR_Rally;
    		_rallyFlag = false;
        }
    	else
    	{
    		//if the rallypoint has not been moved
    	   if (!_rallyFlag) then
    	   { 
    			BLUFOR_Rally setPos getMarkerPos "BLUFOR_Base";
    			hint "Rallypoint moved back to field base.";
    			_rallyFlag = true;
    	   };
    	};
    	
    	if (player == b33) then  //if player is platoon leader
    	{ 
    	  	//if there is a truck or endmap near the player and the action hasn't been added
            if ((count _truckArray > 0 && !_endFlag) || (player distance endmap < 5 && !_endFlag)) then
            {
               endAct = player addAction ['End Mission, Send Report', 'scripts\end_mission_client.sqf', [], -15, true, true, ''];
               _endFlag = true;
            }
            else
            {
               //if there is not a truck or endmap near the player
               if (count _truckArray < 1 && player distance endmap > 5) then
               { 
                  player removeAction endAct;
                  _endFlag = false;
               };
            };
    		
    		//if there is a truck near the player and the action hasn't been added
            if (count _truckArray > 0 && !_moveFlag) then
            {
               moveAct = player addAction ['Move Rallypoint', 'scripts\move_rally_BLUFOR.sqf', [], -5, true, true, ''];
               _moveFlag = true;
            }
            else
            {
               //if there is not a truck near the player
               if (count _truckArray < 1) then
               { 
                  player removeAction moveAct;
                  _moveFlag = false;
               };
            };
    	};
           
        sleep 1;
        _x = _x + 1;
             
        //if 5 seconds has passed
        if (_x == 5) then
        {       
           if (isNil "mkr_SCT1_BLUFOR") then {[SCT1_BLUFOR, 1, "1 SECT", "ColorBlue"] execVM "scripts\ShackTac_localMarker.sqf";};
           if (isNil "mkr_SCT2_BLUFOR") then {[SCT2_BLUFOR, 1, "2 SECT", "ColorBlue"] execVM "scripts\ShackTac_localMarker.sqf";};
           if (isNil "mkr_SCT3_BLUFOR") then {[SCT3_BLUFOR, 1, "3 SECT", "ColorBlue"] execVM "scripts\ShackTac_localMarker.sqf";};
           if (isNil "mkr_SCTW_BLUFOR") then {[SCTW_BLUFOR, 0, "W SECT", "ColorBlue"] execVM "scripts\ShackTac_localMarker.sqf";};
           if (isNil "mkr_PLHQ_BLUFOR") then {[PLHQ_BLUFOR, 0, "PLT HQ", "ColorBlue"] execVM "scripts\ShackTac_localMarker.sqf";};
            
           _x  = 0;
        };
    };
    This is the code that gets the players kit, and rearms them on respawn.
    Code:
    //Written by beta
    //Waits until the player dies, then subtracts ticket and rearms them
    
    
    sleep 1;
    
    _weapons = weapons player;
    _magazines = magazines player;
    
    //infinite loop
    for [{_i=0}, {_i<1}, {_i=0}] do
    {
    	waitUntil {!alive player};  //player has died
    	waitUntil {alive player};  //player has respawned
        
    	BLUFOR_tickets = BLUFOR_tickets - 1; publicVariable "BLUFOR_tickets";  
    	hint format["%1 Tickets remaining", BLUFOR_tickets];   
      
    	removeAllWeapons player;
    	{player addMagazine _x;} forEach _magazines;
    	{player addWeapon _x;} forEach _weapons;
    	[] execVM "scripts\kitout.sqf";
    };
    This is the code that checks for a truck being alive, and removes a dialog option if it is dead.
    Code:
    //Written by beta
    //Monitors truck in order to remove action when it dies
    
    
    _vehicle = _this select 0;  //vehicle
    
    _id = _vehicle addAction ["Truck Inventory", "Dialogs\TruckInvDialog_BLUFOR.sqf", [], 0, false, false, ""];
    
    while {alive _vehicle} do {sleep 1.3756;};
    _vehicle removeAction _id;
    This is the code for a new TvT mission. It checks for 'bunkers' within 100m of various objective areas. Anyone have experience with nearestObjects? Any idea how stressful this will be on the server?
    Code:
    //Written by beta
    //Main thread for the server
    
    
    sleep 1;  //wait until mission starts
    
    //infinite loop
    for [{_i=0}, {_i<1}, {_i=0}] do
    {
    	_REDArray = nearestObjects [obj1, ["WarfareBEastBarracks"], 100];  //get new array of nearby bunkers
    	_BLUArray = nearestObjects [obj1, ["WarfareBWestBarracks"], 100];  //get new array of nearby bunkers
    	
    	if (count _REDArray > 0) then {obj1_REDFOR = true;} else {obj1_REDFOR = false;};
    	if (count _BLUArray > 0) then {obj1_BLUFOR = true;} else {obj1_REDFOR = false;};
    	if (obj1_REDFOR && obj2_REDFOR && obj3_REDFOR) then 
    	{
    		REDFOR_wins = true;
    		publicVariable "BLUFOR_wins";
    		publicVariable "obj1_BLUFOR";
    		publicVariable "obj2_BLUFOR";
    		publicVariable "obj3_BLUFOR";
    		publicVariable "obj1_REDFOR";
    		publicVariable "obj2_REDFOR";
    		publicVariable "obj3_REDFOR";
    	};
    	if (obj1_BLUFOR && obj2_BLUFOR && obj3_BLUFOR) then 
    	{
    		BLUFOR_wins = true;
    		publicVariable "BLUFOR_wins";
    		publicVariable "obj1_BLUFOR";
    		publicVariable "obj2_BLUFOR";
    		publicVariable "obj3_BLUFOR";
    		publicVariable "obj1_REDFOR";
    		publicVariable "obj2_REDFOR";
    		publicVariable "obj3_REDFOR";
    	};
    	
    	sleep 5.293;
    	
    	_REDArray = nearestObjects [obj2, ["WarfareBEastBarracks"], 100];  //get new array of nearby bunkers
    	_BLUArray = nearestObjects [obj2, ["WarfareBWestBarracks"], 100];  //get new array of nearby bunkers
    	
    	if (count _REDArray > 0) then {obj2_REDFOR = true;} else {obj2_REDFOR = false;};
    	if (count _BLUArray > 0) then {obj2_BLUFOR = true;} else {obj2_REDFOR = false;};
    	if (obj1_REDFOR && obj2_REDFOR && obj3_REDFOR) then 
    	{
    		REDFOR_wins = true;
    		publicVariable "BLUFOR_wins";
    		publicVariable "obj1_BLUFOR";
    		publicVariable "obj2_BLUFOR";
    		publicVariable "obj3_BLUFOR";
    		publicVariable "obj1_REDFOR";
    		publicVariable "obj2_REDFOR";
    		publicVariable "obj3_REDFOR";
    	};
    	if (obj1_BLUFOR && obj2_BLUFOR && obj3_BLUFOR) then 
    	{
    		BLUFOR_wins = true;
    		publicVariable "BLUFOR_wins";
    		publicVariable "obj1_BLUFOR";
    		publicVariable "obj2_BLUFOR";
    		publicVariable "obj3_BLUFOR";
    		publicVariable "obj1_REDFOR";
    		publicVariable "obj2_REDFOR";
    		publicVariable "obj3_REDFOR";
    	};
    	
    	sleep 5.293;
    	
    	_REDArray = nearestObjects [obj3, ["WarfareBEastBarracks"], 100];  //get new array of nearby bunkers
    	_BLUArray = nearestObjects [obj3, ["WarfareBWestBarracks"], 100];  //get new array of nearby bunkers
    	
    	if (count _REDArray > 0) then {obj3_REDFOR = true;} else {obj3_REDFOR = false;};
    	if (count _BLUArray > 0) then {obj3_BLUFOR = true;} else {obj3_REDFOR = false;};
    	if (obj1_REDFOR && obj2_REDFOR && obj3_REDFOR) then 
    	{
    		REDFOR_wins = true;
    		publicVariable "BLUFOR_wins";
    		publicVariable "obj1_BLUFOR";
    		publicVariable "obj2_BLUFOR";
    		publicVariable "obj3_BLUFOR";
    		publicVariable "obj1_REDFOR";
    		publicVariable "obj2_REDFOR";
    		publicVariable "obj3_REDFOR";
    	};
    	if (obj1_BLUFOR && obj2_BLUFOR && obj3_BLUFOR) then 
    	{
    		BLUFOR_wins = true;
    		publicVariable "BLUFOR_wins";
    		publicVariable "obj1_BLUFOR";
    		publicVariable "obj2_BLUFOR";
    		publicVariable "obj3_BLUFOR";
    		publicVariable "obj1_REDFOR";
    		publicVariable "obj2_REDFOR";
    		publicVariable "obj3_REDFOR";
    	};
    	
    	sleep 5.293;
    };


    Well, thanks for taking a look, if you notice anything, post! Always looking to have better code ...

  • #2
    Re: Help with optimising code

    wasn't able to skim the long first script yet, but just thought I'd mention, it think says in the wiki NearestObjects only works up to 50m ... I have no idea of the wiki's reliability however. I wouldn't worry at all about the server stress of it, since you're only doing it once every ... 15.879 seconds (hehe, you're pretty accurate with your sleeps :)) ... I think you know what you're doing. As long as you're keeping scripts that don't need to be run on the server, only running on the client ... and vice versa... but I imagine you're familiar with all that garbage.

    the three shorter scripts look good though for sure, to me (thank goodness you use reader-friendly syntax ... even some of the "genius" guys like Kegetys use a "byte-saving" syntax and it is HORRIBLE to read!)

    Comment

    Connect

    Collapse

    TeamSpeak 3 Server

    Collapse

    Advertisement

    Collapse

    Twitter Feed

    Collapse

    Working...
    X