Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
  • 0
A.o.D.

More efficient scripting

Question

So I've made one map with ACS scripting, with some help from some very nice people here and elsewhere.  Now I want to learn how to be more efficient with my code.

 

Take for instance, this mechanism where I move some ceilings, raise some floors, and turn on some lights.

 

 

that sequence of events if performed by the following script.  What I'd like to know is there a more efficient way of doing this, as this requires dozens of tagged sectors and hundreds of lines of script, but performs a relatively simple string of events.  Obviously, the script works, so it's not a major crisis, but I would like to expand my understanding of how this all works.

script 3 (int compswnumber)
{
	if (compswnumber == 1)
	{
		compsw1 = true;

		Floor_LowerByValue(73, 8, 16);

		Light_ChangeToValue(68, 144);

		Light_ChangeToValue(69, 96);
	}

	if (compswnumber == 2)
	{
		compsw2 = true;

		Floor_LowerByValue(70, 8, 16);

		Light_ChangeToValue(71, 144);

		Light_ChangeToValue(72, 96);
	}

	if (compswnumber == 3)
	{
		compsw3 = true;

		Floor_LowerByValue(74, 8, 16);

		Light_ChangeToValue(75, 144);

		Light_ChangeToValue(76, 96);
	}

	if (compswnumber == 4)
	{
		compsw4 = true;

		Floor_LowerByValue(77, 8, 16);

		Light_ChangeToValue(78, 144);

		Light_ChangeToValue(79, 96);
	}

	if (compswnumber == 5)
	{
		compsw5 = true;

		Floor_LowerByValue(80, 8, 16);

		Light_ChangeToValue(81, 144);

		Light_ChangeToValue(82, 96);
	}

	if (compswnumber == 6)
	{
		compsw6 = true;

		Floor_LowerByValue(83, 8, 16);

		Light_ChangeToValue(84, 144);

		Light_ChangeToValue(85, 96);
	}

	if (compsw1 == true && compsw2 == true && compsw3 == true && compsw4 == true && compsw5 == true && compsw6 == true)
	{
		//step 1
		Floor_RaiseByValue(86, 8, 8);

		Light_changetoValue(88, 128);

		Light_ChangeToValue(89, 112);

		Light_ChangeToValue(90, 96);

		Light_ChangeToValue(91, 80);

		Delay(4);

		Light_ChangeToValue(87, 32);

		Delay(20);

		Light_changetoValue(88, 128);

		Light_ChangeToValue(89, 112);

		Light_ChangeToValue(90, 96);

		Light_ChangeToValue(91, 80);

		Delay(4);

		Light_ChangeToValue(87, 32);

		Delay(10);

		Light_changetoValue(88, 128);

		Light_ChangeToValue(89, 112);

		Light_ChangeToValue(90, 96);

		Light_ChangeToValue(91, 80);

		Delay(4);

		Light_ChangeToValue(87, 32);

		Delay(8);

		Light_changetoValue(88, 128);

		Light_ChangeToValue(89, 112);

		Light_ChangeToValue(90, 96);

		Light_ChangeToValue(91, 80);

		Delay(4);

		Light_ChangeToValue(87, 32);

		Delay(4);

		Light_changetoValue(88, 128);

		Light_ChangeToValue(89, 112);

		Light_ChangeToValue(90, 96);

		Light_ChangeToValue(91, 80);

		Delay(4);

		Light_ChangeToValue(87, 32);

		Delay(3);

		Light_changetoValue(88, 128);

		Light_ChangeToValue(89, 112);

		Light_ChangeToValue(90, 96);

		Light_ChangeToValue(91, 80);

		Delay(4);

		Light_ChangeToValue(87, 32);

		Delay(2);

		Light_changetoValue(88, 128);

		Light_ChangeToValue(89, 112);

		Light_ChangeToValue(90, 96);

		Light_ChangeToValue(91, 80);

		Delay(4);

		Light_ChangeToValue(87, 32);

		Delay(1);

		Light_changetoValue(88, 128);

		Light_ChangeToValue(89, 112);

		Light_ChangeToValue(90, 96);

		Light_ChangeToValue(91, 80);

		//step 2
		Floor_RaiseByValue(92, 8, 8);

		Light_changetoValue(93, 128);

		Light_ChangeToValue(94, 112);

		Light_ChangeToValue(95, 96);

		Delay(4);

		Light_ChangeToValue(96, 32);

		Delay(20);

		Light_changetoValue(93, 128);

		Light_ChangeToValue(94, 112);

		Light_ChangeToValue(95, 96);

		Delay(4);

		Light_ChangeToValue(96, 32);

		Delay(10);

		Light_changetoValue(93, 128);

		Light_ChangeToValue(94, 112);

		Light_ChangeToValue(95, 96);

		Delay(4);

		Light_ChangeToValue(96, 32);

		Delay(8);

		Light_changetoValue(93, 128);

		Light_ChangeToValue(94, 112);

		Light_ChangeToValue(95, 96);

		Delay(4);

		Light_ChangeToValue(96, 32);

		Delay(4);

		Light_changetoValue(93, 128);

		Light_ChangeToValue(94, 112);

		Light_ChangeToValue(95, 96);

		Delay(4);

		Light_ChangeToValue(96, 32);

		Delay(3);

		Light_changetoValue(93, 128);

		Light_ChangeToValue(94, 112);

		Light_ChangeToValue(95, 96);

		Delay(4);

		Light_ChangeToValue(96, 32);

		Delay(2);

		Light_changetoValue(93, 128);

		Light_ChangeToValue(94, 112);

		Light_ChangeToValue(95, 96);

		Delay(4);

		Light_ChangeToValue(96, 32);

		Delay(1);

		Light_changetoValue(93, 128);

		Light_ChangeToValue(94, 112);

		Light_ChangeToValue(95, 96);

		//step 3
		Floor_RaiseByValue(97, 8, 8);

		Light_changetoValue(98, 128);

		Light_ChangeToValue(99, 112);

		Light_ChangeToValue(100, 96);

		Delay(4);

		Light_ChangeToValue(101, 32);

		Delay(20);

		Light_changetoValue(98, 128);

		Light_ChangeToValue(99, 112);

		Light_ChangeToValue(100, 96);

		Delay(4);

		Light_ChangeToValue(101, 32);

		Delay(10);

		Light_changetoValue(98, 128);

		Light_ChangeToValue(99, 112);

		Light_ChangeToValue(100, 96);

		Delay(4);

		Light_ChangeToValue(101, 32);

		Delay(8);

		Light_changetoValue(98, 128);

		Light_ChangeToValue(99, 112);

		Light_ChangeToValue(100, 96);

		Delay(4);

		Light_ChangeToValue(101, 32);

		Delay(4);

		Light_changetoValue(98, 128);

		Light_ChangeToValue(99, 112);

		Light_ChangeToValue(100, 96);

		Delay(4);

		Light_ChangeToValue(101, 32);

		Delay(3);

		Light_changetoValue(98, 128);

		Light_ChangeToValue(99, 112);

		Light_ChangeToValue(100, 96);

		Delay(4);

		Light_ChangeToValue(101, 32);

		Delay(2);

		Light_changetoValue(98, 128);

		Light_ChangeToValue(99, 112);

		Light_ChangeToValue(100, 96);

		Delay(4);

		Light_ChangeToValue(101, 32);

		Delay(1);

		Light_changetoValue(98, 128);

		Light_ChangeToValue(99, 112);

		Light_ChangeToValue(100, 96);

		//step 4
		Floor_RaiseByValue(102, 8, 8);

		Light_ChangeToValue(103, 128);

		Light_ChangeToValue(104, 112);

		Light_ChangeToValue(105, 96);

		Delay(4);

		Light_ChangeToValue(106, 32);

		Delay(20);

		Light_ChangeToValue(103, 128);

		Light_ChangeToValue(104, 112);

		Light_ChangeToValue(105, 96);

		Delay(4);

		Light_ChangeToValue(106, 32);

		Delay(10);

		Light_ChangeToValue(103, 128);

		Light_ChangeToValue(104, 112);

		Light_ChangeToValue(105, 96);

		Delay(4);

		Light_ChangeToValue(106, 32);

		Delay(8);

		Light_ChangeToValue(103, 128);

		Light_ChangeToValue(104, 112);

		Light_ChangeToValue(105, 96);

		Delay(4);

		Light_ChangeToValue(106, 32);

		Delay(4);

		Light_ChangeToValue(103, 128);

		Light_ChangeToValue(104, 112);

		Light_ChangeToValue(105, 96);

		Delay(4);

		Light_ChangeToValue(106, 32);

		Delay(3);

		Light_ChangeToValue(103, 128);

		Light_ChangeToValue(104, 112);

		Light_ChangeToValue(105, 96);

		Delay(4);

		Light_ChangeToValue(106, 32);

		Delay(2);

		Light_ChangeToValue(103, 128);

		Light_ChangeToValue(104, 112);

		Light_ChangeToValue(105, 96);

		Delay(4);

		Light_ChangeToValue(106, 32);

		Delay(1);

		Light_ChangeToValue(103, 128);

		Light_ChangeToValue(104, 112);

		Light_ChangeToValue(105, 96);

		//step 5
		Floor_RaiseByValue(107, 8, 8);

		Light_ChangeToValue(108, 128);

		Light_ChangeToValue(109, 112);

		Light_ChangeToValue(110, 96);

		Delay(4);

		Light_ChangeToValue(111, 32);

		Delay(20);

		Light_ChangeToValue(108, 128);

		Light_ChangeToValue(109, 112);

		Light_ChangeToValue(110, 96);

		Delay(4);

		Light_ChangeToValue(111, 32);

		Delay(10);

		Light_ChangeToValue(108, 128);

		Light_ChangeToValue(109, 112);

		Light_ChangeToValue(110, 96);

		Delay(4);

		Light_ChangeToValue(111, 32);

		Delay(8);

		Light_ChangeToValue(108, 128);

		Light_ChangeToValue(109, 112);

		Light_ChangeToValue(110, 96);

		Delay(4);

		Light_ChangeToValue(111, 32);

		Delay(4);

		Light_ChangeToValue(108, 128);

		Light_ChangeToValue(109, 112);

		Light_ChangeToValue(110, 96);

		Delay(4);

		Light_ChangeToValue(111, 32);

		Delay(3);

		Light_ChangeToValue(108, 128);

		Light_ChangeToValue(109, 112);

		Light_ChangeToValue(110, 96);

		Delay(4);

		Light_ChangeToValue(111, 32);

		Delay(2);

		Light_ChangeToValue(108, 128);

		Light_ChangeToValue(109, 112);

		Light_ChangeToValue(110, 96);

		Delay(4);

		Light_ChangeToValue(111, 32);

		Delay(1);

		Light_ChangeToValue(108, 128);

		Light_ChangeToValue(109, 112);

		Light_ChangeToValue(110, 96);

		//step 6
		Floor_RaiseByValue(112, 8, 8);

		Light_ChangeToValue(113, 128);

		Light_ChangeToValue(114, 112);

		Light_ChangeToValue(115, 96);

		Delay(4);

		Light_ChangeToValue(116, 32);

		Delay(20);

		Light_ChangeToValue(113, 128);

		Light_ChangeToValue(114, 112);

		Light_ChangeToValue(115, 96);

		Delay(4);

		Light_ChangeToValue(116, 32);

		Delay(10);

		Light_ChangeToValue(113, 128);

		Light_ChangeToValue(114, 112);

		Light_ChangeToValue(115, 96);

		Delay(4);

		Light_ChangeToValue(116, 32);

		Delay(8);

		Light_ChangeToValue(113, 128);

		Light_ChangeToValue(114, 112);

		Light_ChangeToValue(115, 96);

		Delay(4);

		Light_ChangeToValue(116, 32);

		Delay(4);

		Light_ChangeToValue(113, 128);

		Light_ChangeToValue(114, 112);

		Light_ChangeToValue(115, 96);

		Delay(4);

		Light_ChangeToValue(116, 32);

		Delay(3);

		Light_ChangeToValue(113, 128);

		Light_ChangeToValue(114, 112);

		Light_ChangeToValue(115, 96);

		Delay(4);

		Light_ChangeToValue(116, 32);

		Delay(2);

		Light_ChangeToValue(113, 128);

		Light_ChangeToValue(114, 112);

		Light_ChangeToValue(115, 96);

		Delay(4);

		Light_ChangeToValue(116, 32);

		Delay(1);

		Light_ChangeToValue(113, 128);

		Light_ChangeToValue(114, 112);

		Light_ChangeToValue(115, 96);

		//step 7
		Floor_RaiseByValue(117, 8, 8);

		Light_ChangeToValue(118, 128);

		Light_ChangeToValue(119, 112);

		Light_ChangeToValue(120, 96);

		Delay(4);

		Light_ChangeToValue(121, 32);

		Delay(20);

		Light_ChangeToValue(118, 128);

		Light_ChangeToValue(119, 112);

		Light_ChangeToValue(120, 96);

		Delay(4);

		Light_ChangeToValue(121, 32);

		Delay(10);

		Light_ChangeToValue(118, 128);

		Light_ChangeToValue(119, 112);

		Light_ChangeToValue(120, 96);

		Delay(4);

		Light_ChangeToValue(121, 32);

		Delay(8);

		Light_ChangeToValue(118, 128);

		Light_ChangeToValue(119, 112);

		Light_ChangeToValue(120, 96);

		Delay(4);

		Light_ChangeToValue(121, 32);

		Delay(4);

		Light_ChangeToValue(118, 128);

		Light_ChangeToValue(119, 112);

		Light_ChangeToValue(120, 96);

		Delay(4);

		Light_ChangeToValue(121, 32);

		Delay(3);

		Light_ChangeToValue(118, 128);

		Light_ChangeToValue(119, 112);

		Light_ChangeToValue(120, 96);

		Delay(4);

		Light_ChangeToValue(121, 32);

		Delay(2);

		Light_ChangeToValue(118, 128);

		Light_ChangeToValue(119, 112);

		Light_ChangeToValue(120, 96);

		Delay(4);

		Light_ChangeToValue(121, 32);

		Delay(1);

		Light_ChangeToValue(118, 128);

		Light_ChangeToValue(119, 112);

		Light_ChangeToValue(120, 96);

		//step 8
		Light_ChangeToValue(117, 128);

		Delay(4);

		Light_ChangeToValue(117, 32);

		Delay(20);

		Light_ChangeToValue(117, 128);

		Delay(4);

		Light_ChangeToValue(117, 32);

		Delay(10);

		Light_ChangeToValue(117, 128);

		Delay(4);

		Light_ChangeToValue(117, 32);

		Delay(8);

		Light_ChangeToValue(117, 128);

		Delay(4);

		Light_ChangeToValue(117, 32);

		Delay(4);

		Light_ChangeToValue(117, 128);

		Delay(4);

		Light_ChangeToValue(117, 32);

		Delay(3);

		Light_ChangeToValue(117, 128);

		Delay(4);

		Light_ChangeToValue(117, 32);

		Delay(2);

		Light_ChangeToValue(117, 128);

		Delay(4);

		Light_ChangeToValue(117, 32);

		Delay(1);

		Light_ChangeToValue(117, 128);
	}

}

 

Share this post


Link to post

10 answers to this question

Recommended Posts

  • 0

Holy crap, first of all. Yes, this can be greatly improved.

 

Instead of identifying each switch individually, you can use an int value to increment by one each switch press instead of checking every switch as a separate boolean.

 

Instead of running this whole thing as one script, first write a function for the switches that takes in the needed tags since it looks like the values are the same and there are no delays.

 

Separate the steps script into a new OPEN script. Make it loop until the int value from above is reached.

 

Functions can't have delays, so what I would do is write a function for the step light changes for when there's 3 changes at once and use this between delays but keep the single line light changes.

 

That should greatly reduce your line count. I can't provide a re-written example right now (on mobile) but if nobody else has by later today I'll see what I can come up with.

Share this post


Link to post
  • 1

Adding to what @Nevander said, if I have a bunch of repeated calls to the same action specials like you have above, and I know in advance how many there are, I would use one or more array structure (possibly complex) to hold the data and then loop over that array length, passing the value(s) from the array(s) to the appropriate script call on each loop.

 

Your repeated calls to this:

 

Delay(4);
Light_ChangeToValue(117, 32);

Delay(2);
Light_ChangeToValue(117, 128);

could easily be reduced to a loop over big data arrays (assume you have 20 items to change for my example). You'd need to define the data up front:

int _delays[20] = {... ,4,2, ...};    //would be all 20 delay values, comma separated
int _sectorTags[20] = {... ,117,117, ...};
int _lightToVals[20] = {... ,32,128, ...};

and once you have defined all your data, simply loop over it like so:

for (int a = 0;a < 20; a++){
        Delay(_delays[a]);      //zero-indexed array counting!
        Light_ChangeToValue(_sectorTags[a], _lightToVals[a]);
}

 

Obviously, this will only work if each step has the same statements (so here, always a delay of a certain value followed by a light level change, though of course the sector tags etc. are different on each loop)

 

 

Here is a big script I did ages ago (the WAD is still a WiP!) that does what I am talking about.  The stuff about the teleport array specifically:

Spoiler

 


#include "zcommon.acs"
world int 1:lookupDeadThings[];
/*
Here, we are testing for a global array of TIDs of imps to kill.
This defaults to empty, but if one or more imps are killed in the secret level,
this array is populated with TIDs.

If it is so populated, the killSecretThings() method is called and the global
array used to kill off correspondsingly TID flagged imps in THIS map.
*/


#define TELEPORT_ARRAY_LENGTH 12
int randomIndexAssigned[12] = {0,0,0,0,0,0,0,0,0,0,0,0};    //store the teleTargetSector ID index as they are chosen

//int telePlatforms[12] = {63,64,65,66,67,68,69,70,71,72,73,74};    //sector tags of departure platfoms
//int randomTelePlatforms[12] = {0,0,0,0,0,0,0,0,0,0,0,0};    //store the telePlatforms item at the randomised index ID index as they are chosen

int _currentPlatformToLower = -1;

/*
Print a message on finding each exit switch:
These will be used in the finding order.
 */
str switchFindMsg[12] = {
    "OK, let's find the others...",
    "Right, that's 2",
    "And the third",
    "How many of these damn things are there?",
    "Is that five now?",
    "What kind of gate IS this?",
    "Thats seven, I think",
    "And eight",
    "Nine. Should be nearly done now",
    "Ten? How many of those damn pyramids were there?",
    "Eleven. I think just one more to go.",
    "That damn exit should be open now!"
};

/*
The sector tags of teleport targets from main hub area:
*/
int teleTargetSectors[12] = {1,2,37,4,5,6,7,8,9,10,11,12};

/*
The sector tags of the lights in the exit area.
*/
int exitLights[12] = {47,48,49,50,51,52,53,54,55,56,57,58};

/*
runtime tracker for tally of completed sections:
*/
int exitTasksCompleted[12] = {0,0,0,0,0,0,0,0,0,0,0,0};

/*
boolean indicating whether all tasks are done (may be redundent):
*/
bool openExit = false;


script 1 ENTER {
    /* start wind sound at well of dead souls */
    startWindSound(20);
    randomizeTeleportDestinations();

    /* rotate the demon floor textures to align with corridor*/
    rotateTextures(37, 270, 0);

    /* init  */
    _currentPlatformToLower = 0;    //this is set dynamically in script 2

     log(s:"triggering kill script with world scope data");
    killSecretThings();
}

/*
Triggered by walking over a linedef at the top of the pyramids. Each linedef cross passes an argument 0 to 11 (depending on which
pyramid) and this is then used as the index of the randomized target array 'randomIndexAssigned' set up in the ENTER script
*/
script 2 (int target,int source){
    log(s:"TELEPORTING OUT: script 2: plat to lower:");
    log(i:source);
    _currentPlatformToLower = source;    //now we can use this on clicking the buttons!
    Teleport(0,randomIndexAssigned[target],0);
}

/****
EXIT LIGHT HANDLING:
--------------------
Handler script for each segment button. This in turn will switch the
exit lights on and increment the switched count. 0 to 11, used as index
for  setExitSegmentDone exitLights array.
When we get here we have already set the transient sector tag of our departure platform,
so we can use it in the floor lower call:
*/
script 3 (int exitLightIndex){
    log(s:"CLICKING TRIGGER\nLight Index:");
    log(i:exitLightIndex);
    setExitSegmentDone(exitLightIndex);

    /* also set any area-specific things: */
    switch(exitLightIndex){
        case 0:
            log(s:"Spawn demons in the arenas...");
            spawnStuff(0);    //use the arg to determine which area we are spawning into (if I use elsewhere)
        break;

        case 1:    //floating switch. Need to set the CONTROL SECTOR texture to switched:
            SetLineTexture(1,SIDE_FRONT, TEXTURE_MIDDLE,"SW2MET2");
        break;
        case 2:
            Door_Open(38,12,0);
        break;
    }
    log(s:"Now we lower the platform. Platform tag:");
    log(i:_currentPlatformToLower);
    /* we have activated the switch - raise the departure platform by tag stored in _currentPlatformToLower */
    Floor_RaiseByValue(_currentPlatformToLower, 12, 56);

}


/* TEST teleport to fake floor */
script 5 (void){
    Teleport(10,0,0);
}

/*
handler for other triggered stuff per area:
*/
script 6 (int areaIndex){
    if(exitTasksCompleted[areaIndex]){    //1 is truthy
        switch(areaIndex){
            case 0:
                /*
                spawn a BoH as you go past the hall intersection
                */
                Spawn("BaronOfHell",    -1408<<16,    -2176<<16,    144<<16,0,64);
                Spawn("Fatso",            -864<<16,    -2496<<16,    144<<16,0,64);
                Spawn("Hellknight",        -1856<<16,    -2144<<16,    144<<16,0,64);
            break;
        }
    }
}

/*
open exit door if all switches are triggered, otherwise - er -
don't...

ceiling_raisebyvalue(sectorID, speed, dist);
floor_lowerbyvalue(sectorID, speed, dist);

}


*/
script 7(void){
    /* iterate over sector ID array. I need an array length */
    openExit = TRUE;
    for (int a = 0;a < TELEPORT_ARRAY_LENGTH; a++){
        /* test if any unpressed buttons: */
        log(i:exitTasksCompleted[a]);
        if(!(exitTasksCompleted[a])){
            openExit = FALSE;
            break;
        }
     }
    log(s:"Exit open status: ",b:openExit);
    if(openExit){
        ceiling_raisebyvalue(83, 12, 8);
        floor_lowerbyvalue(83, 12, 8);
        TagWait(83);
        ceiling_lowerbyvalue(83, 8, 16);
        floor_raisebyvalue(83, 8, 16);
        TagWait(83);
        ceiling_raisebyvalue(83, 4, 64);
        floor_lowerbyvalue(83, 4, 56);
        /* suppress retriggering: */
        ClearLineSpecial();
        log(s:"Open exit");
    }
    else{
        log(s:"need to find all switches");
        ceiling_raisebyvalue(83, 12, 8);
        floor_lowerbyvalue(83, 12, 8);
        TagWait(83);
        ceiling_lowerbyvalue(83, 48, 16);
        floor_raisebyvalue(83, 48, 16);
        TagWait(83);
        ceiling_raisebyvalue(83, 12, 8);
        floor_lowerbyvalue(83, 12, 8);
    }
}

/*
called from MAP31:
*/
function void killSecretThings(void){
    for(int a=0;a<3;a++){
        log(s:"thing to destroy ", i:lookupDeadThings[a]);
        if(lookupDeadThings[a]> 0){
            Thing_Destroy(lookupDeadThings[a],0,0);
        }

    }
}

/*
start localised wind sound at specified thing(s) tag:
*/
function void startWindSound(int thingID){
    PlaySound ( thingID, "WINDHOWL", 0, 0.5, true, ATTN_NORM);
}

/* rotation is clockwise in degrees */
function void rotateTextures(int sectorId, int floorAngle, int ceilingAngle){
    Sector_SetRotation(sectorId, floorAngle, ceilingAngle);
}

/*
This randomizes the teleport hub targets, so each new playthrough has different teleport destinations linked to the pyramids. The
assigned targets persist over saves though.
Also need to mirror this randomization for the departur platforms as well.
*/
function void randomizeTeleportDestinations(void){
    //int teleTargetSectors[12] = {1,2,37,4,5,6,7,8,9,10,11,12};
    int assigned = 0;

    /* iterate over sector ID array. I need an array length */
    for (int a = 0;a < TELEPORT_ARRAY_LENGTH; a++){
        int entryDone = 0;
        while(!(entryDone)){
            /*     if it is not already in place, proceed to get it. So we need to get a RANDOM index of the input and put it at the CURRENT index of the output: */
            int rnd = Random(0,11);

            /* test if this random index has been used already: */
            if(!(randomIndexAssigned[rnd])){
                /* if it has not, assign it to the target sector randomized ordr array and exit the entryDone loop: */
                randomIndexAssigned[rnd] = teleTargetSectors[a];
                // randomTelePlatforms[rnd] = telePlatforms[a]; ?
                entryDone = 1;
            }
        }
     }
}

/*
key these to the INDEX of the platform being done
Triggered on actioning the switches in each segment.
When all are done, the exit itself will lower/open
Logic:
 - each switch is S1
 - each passes a unique index number
 - on passing the index number, the exitTasksCompleted array is updated
*/
function void setExitSegmentDone(int exitLightIndex){
    /* flag the array index as true. The switches are S1, so this is probably redundant:  */
    exitTasksCompleted[exitLightIndex] = 1;

    log(i:exitLightIndex);
    log(i:exitLights[exitLightIndex]);


    /* and now perform the action to set the activated light to max brightness: */
    Light_ChangeToValue(exitLights[exitLightIndex],255);

    /* finally, check whether ALL lights are on. If so,  open the exit... */
    openExit = true;
    int numFound = -1;    //start at -1 because zero-indexed.
    for(int n=0; n<TELEPORT_ARRAY_LENGTH; n++){
        if(!exitTasksCompleted[n] == 0){
            openExit = false;
            numFound++;
        }
    }
    log(s:"Number of switches currently found:");
    log(i:numFound);
    print(s:switchFindMsg[numFound]);

    if(openExit){
        log(s:"Open exit...");
    };
}

/*
'areaNum' is an arbitrary integer identifying each main area. It is not a sector tag as such, just an index.
Therefore, I can use it to key an array of spawn data (not sure whether this will work though).
*/
function void spawnStuff(int areaNum){
    switch(areaNum){
        case 0:
            log(s:"Spawning for area 0");
            int start[3] = {0, -3072, 144};
            int dim[2] = {5,5};    //cols, rows

            int counter = 7;
            int x_pos = -1;
            int y_pos = -1;
            int y_start = 0;

            //x
            for(int a=0; a< dim[0]; a++){
                x_pos = start[0] + (a*128);
                log(s:"row:",i:a,s:"x-pos", i:x_pos);
                //y
                for(int b=0; b<dim[1]; b++){
                    y_pos = start[1] + (b*128);
                    log(s:"col: ", i:b, s:"y-pos: ",i:y_pos);
                    Spawn("DoomImp",        x_pos<<16,        y_pos<<16,    start[2]<<16,0,64);
                    Spawn("ShotgunGuy",        (x_pos+64)<<16,    y_pos<<16,    start[2]<<16,0,64);
                }
            }
        break;


        default:
            log(s:"no action");
    }
}

 

 

 


 

Edited by smeghammer

Share this post


Link to post
  • 1

That all looks very good, and is probably a perfectly succinct answer.

 

But let's pretend for a moment that I have absolutely no idea what I'm doing here.   Mostly because that's the exact truth.  Every bit of code I've ever used has been cargo culted and cut and pasted and mangled into place to make it do an approximation of what I've envisioned.  You're literally looking at examples from the zdoomwiki and Chubzdoomer that have been reworked.  Please talk to me like I am very, very dumb.

Share this post


Link to post
  • 0

You are probably best off starting here.

 

It's introductory tutorials for ACS. Look particularly at the count controlled loops bit. I'd also suggest building some dummy levels to test out how ACS works and fits with the lines, things and sector tags - it's what I did (and still do).

 

Even just doing the examples presented there as-is will likely help a lot. If you have not done any coding in other contexts, then getting your head around programming logic will be quite a steep learning curve, but ultimately very satisfying.

 

I have always likened efficient code to being lazy (i.e you write as little as possible to do the task at hand) and converting a load of repeated steps that do essentially the same thing to a single task that is repeated inside a loop structure is - essentially - being lazy (because you write much less code)....  

 

loops are fundamental to any programming language and ACS is no different. While you can write perfectly functional code as a bunch of statements one after another, it is almost always better from a speed of execution, memory use, ease of reading and re-usability to figure out which of the repeated lines of code are actually doing the same thing and pull them out into a loop that goes around the number of times you want to repeat the steps.

 

I made a mickey mouse example that does the same raise/lower routine from each switch, but one calls a script that is a bunch of sequential statements, and the other calls a script that loops over arrays holding the same up/down/speed data to use.

 

Check it out in slade.


 

 

 

Share this post


Link to post
  • 0

Okay, here's where I am so far.  First I tried to get rid of the six different BOOL calls.  I started by declaring int compswitch = 0 and replacing all of the bool = true with compswitch ++; functions.  I then replaced the lengthy if statement with "if (compswitch == 6)".  It compiled and looked good.   But it didn't work. 

 

Why doesn't it work?  I know, I'll put print functions in every step to verify that int compswitch is incrementing!   I do that, and every button I push prints the number "1".  A braincell kicks in and I realize that since I put int compswitch = 0; at the beginning of the script, every time I push a button the integer gets reset to 0.  Okay, so I put it at the beginning of the script pile outside of the script I'm working in.  All of a sudden everything works again.  The stairs rise and light up, and the integer increments with every button push.  Hooray!

 

Okay, so now the redundant BOOL functions are gone, and replaced with a single integer call.

 

Now.. I just have to make my brain understand loops, which I'm bouncing pretty hard off of so far.  Here's where I am with trying to read your example:

 

You mention 20 variables in your script.  I have 14 delays at each stage.  3 sectors that turn on lights and 1 sector for turning off the lights for those 3 sectors.  How do I make all that mesh?

Edited by A.o.D.

Share this post


Link to post
  • 0

I have replaced the entire //step 1 section of the script with the following:

 

{
		//step 1
		int _sectortags1[15] = {88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88};

		int _sectortags2[15] = {89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89};

		int _sectortags3[15] = {90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90};

		int _sectortags4[15] = {91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91};

		int _lightvals1[15] = {128, 32, 128, 32, 128, 32, 128, 32, 128, 32, 128, 32, 128, 32, 128};

		int _lightvals2[15] = {112, 32, 112, 32, 112, 32, 112, 32, 112, 32, 112, 32, 112, 32, 112};

		int _lightvals3[15] = {96, 32, 96, 32, 96, 32, 96, 32, 96, 32, 96, 32, 96, 32, 96};

		int _lightvals4[15] = {80, 32, 80, 32, 80, 32, 80, 32, 80, 32, 80, 32, 80, 32, 80};

		int _delays[15] = {4, 20, 4, 10, 4, 8, 4, 4, 4, 3, 4, 2, 4, 1, 1};


		Floor_RaiseByValue(86, 8, 8);

		for (int a = 0; a < 15; a++)
		{
			Light_ChangeToValue(_sectortags1[a], _lightvals1[a]);
			Light_ChangeToValue(_sectortags2[a], _lightvals2[a]);
			Light_ChangeToValue(_sectortags3[a], _lightvals3[a]);
			Light_ChangeToValue(_sectortags4[a], _lightvals4[a]);
			Delay(_delays[a]);
		}

and it works!  It does everything my original, much longer script did, and in the exact same timing.  I have reduced the number of lines of script to accomplish this by about 2/3rds.  What I want to know now is if this is good?  Can it be better?

Share this post


Link to post
  • 0

@A.o.D., I was just writing a big arse reply. I'll stop that now...

 

Glad you got it resolved!. I'd say that's a pretty good refactoring... If you do something similar with the other blocks, that should also reduce the size further, and also might show that some parts could be moved to functions, but be careful, as certain built-in calls (like Delay() iirc) can't be used in functions.

 

The only other thing I might suggest is using multidimensional arrays as your data store. Although that could in theory reduce you data to a single line, it's definitely more confusing to read, so I'd probably stick with the flat arrays you currently have.

 

Nice one!

 

 

 

Share this post


Link to post
  • 0

Thank you for the pointers.  I'll be sure to include you in the credits when this thing finally releases.

Share this post


Link to post
  • 0
Just now, A.o.D. said:

Thank you for the pointers.  I'll be sure to include you in the credits when this thing finally releases.

You ae very welcome. Glad I could assist.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×