DS_Stack issues

O

oziphantom

Guest
lets start with the code
Code:
function getValidMoveSquares(startPos, distance, unitType, team)
{
    var moveData;
    for (var i = 0; i < distance*2+1; ++i)
    {
        for (var j=0; j< distance*2+1; ++j)
        {
            moveData[i][j] = 255;
        }
    }
    var openPositions = ds_stack_create();
    var dummy = array_create(2);
    dummy[0] = startPos[0]
    dummy[1] = startPos[1]
    ds_stack_push(openPositions, dummy);
    var travel = 0;
    do
    {
        var newOpenPositions = ds_stack_create();
        if(travel < distance) // this need to be inside the loop
        {
            show_debug_message("looking at distance : "+string(travel));
            show_debug_message("num points: "+string(ds_stack_size(openPositions)));
            while(ds_stack_size(openPositions) > 0)
            {
                var testPos = ds_stack_pop(openPositions);
                show_debug_message("test pos : "+string(testPos));
                var offsetTestPos1;
                offsetTestPos1[0] = testPos[0]
                offsetTestPos1[1] = testPos[1]-1
                if( canMoveTo(offsetTestPos1,unitType,team) )
                {
                    if (updateDistanceAt(moveData,offsetTestPos1,startPos,travel))
                    {
                        show_debug_message("adding pos : "+string(offsetTestPos1));
                        var dummy = array_create(2);
                        array_set(dummy,0, offsetTestPos1[0]);
                        array_set(dummy,1, offsetTestPos1[1]);
                        ds_stack_push(newOpenPositions, dummy);
                        show_debug_message(" dummy : " + string(dummy));
                        show_debug_message("num points: "+string(ds_stack_size(newOpenPositions)));
                    }
                }
                var offsetTestPos2;
                offsetTestPos2[0] = testPos[0]-1
                offsetTestPos2[1] = testPos[1]
                if( canMoveTo(offsetTestPos2,unitType,team) )
                {
                    if (updateDistanceAt(moveData,offsetTestPos2,startPos,travel))
                    {
                        show_debug_message("adding pos : "+string(offsetTestPos2));
                        var dummy = array_create(2);
                        array_set(dummy,0, offsetTestPos2[0]);
                        array_set(dummy,1, offsetTestPos2[1]);
                        ds_stack_push(newOpenPositions, dummy);
                        show_debug_message("num points: "+string(ds_stack_size(newOpenPositions)));
                    }
                }
                var offsetTestPos3;
                offsetTestPos3[0] = testPos[0]
                offsetTestPos3[1] = testPos[1]+1
                if( canMoveTo(offsetTestPos3,unitType,team) )
                {
                    if (updateDistanceAt(moveData,offsetTestPos3,startPos,travel))
                    {
                        show_debug_message("adding pos : "+string(offsetTestPos3));
                        var dummy = array_create(2);
                        array_set(dummy,0, offsetTestPos3[0]);
                        array_set(dummy,1, offsetTestPos3[1]);
                        ds_stack_push(newOpenPositions, dummy);
                        show_debug_message("num points: "+string(ds_stack_size(newOpenPositions)));
                    }
                }
                var offsetTestPos4;
                offsetTestPos4[0] = testPos[0]+1
                offsetTestPos4[1] = testPos[1]
                if( canMoveTo(offsetTestPos4,unitType,team) )
                {
                    if (updateDistanceAt(moveData,offsetTestPos4,startPos,travel))
                    {
                        show_debug_message("adding pos : "+string(offsetTestPos4));
                        var dummy = array_create(2);
                        array_set(dummy,0, offsetTestPos4[0]);
                        array_set(dummy,1, offsetTestPos4[1]);
                        ds_stack_push(newOpenPositions, dummy);
                        show_debug_message("num points: "+string(ds_stack_size(newOpenPositions)));
                    }
                }
                array_delete(dummy,0,2)
            } //while(ds_stack_size(newOpenPositions) > 0)
            show_debug_message("new num points: "+string(ds_stack_size(newOpenPositions)));
            ds_stack_destroy(openPositions);
            openPositions = newOpenPositions;
            show_debug_message("num points: "+string(ds_stack_size(openPositions)));
            travel += 1;
        } //if(travel < distance) 
        else
        {
            ds_stack_clear(openPositions); // we are over movement, we don't care about them any more
        }
    } until( ds_stack_size(openPositions) == 0 )
    ds_stack_destroy(openPositions);
    return moveData;
}
Been having a lot of fun with this as you can probably tell.
apparently if you use array_create you then can't use array[number] on it, as its "not an array" ;)
but all the cloning and copying works up until it tries to go again.
Here is a print out of what happens when it runs
Code:
looking at distance : 0
num points: 1
test pos : [ 9,5 ]
adding pos : [ 9,4 ]
 dummy : [ 9,4 ]
num points: 1
adding pos : [ 8,5 ]
num points: 2
adding pos : [ 9,6 ]
num points: 3
adding pos : [ 10,5 ]
num points: 4
new num points: 4
num points: 4
looking at distance : 1
num points: 4
test pos : [  ]
The first pass it adds points, which are custom copies of the point so they don't trash each other, even when they are in scope { } and a new var. So they should hold their values, but after I swap the stacks over, it still has 4 points in it, just now the point is [ ] and not its value . If you just print a stack you get the ID number and not an meaningful data, the debugger also can't "see inside of them".

So what do I need to do to make the stack holds the values?
 
O

oziphantom

Guest
Always the way, you spend all night on it, you sleep on it, you look at it again. THEN THE SECOND YOU POST ABOUT IT...
Code:
           ds_stack_push(newOpenPositions, dummy);
                        show_debug_message("num points: "+string(ds_stack_size(newOpenPositions)));
                    }
                }
                array_delete(dummy,0,2); <- MAGICALLY DELETES THE THING THAT WAS JUST ADDED
            } //while(ds_stack_size(newOpenPositions) > 0)
            show_debug_message("new num points: "+string(ds_stack_size(newOpenPositions)));
            ds_stack_destroy(openPositions);
            openPositions = newOpenPositions;
            show_debug_message("num points: "+string(ds_stack_size(openPositions)));
            travel += 1;
and it should be
array_delete(testPos,0,2);

sigh.
 

Nidoking

Member
apparently if you use array_create you then can't use array[number] on it, as its "not an array"
I don't believe this is true. I do notice that you've reused the "dummy" variable multiple times in the same scope, which is probably causing some confusion for the compiler. You can use the same name multiple times if the scopes don't intersect, but there's a var dummy declaration that covers the entire function here. I also don't see the need to array_create when you're building an array of two known amounts. Can't you just assign [thing, thing] to the variable to get the same effect?
 
O

oziphantom

Guest
just did a test
Code:
var dummy = array_create(2);
array_set(dummy,0, offsetTestPos1[0]);
array_set(dummy,1, offsetTestPos1[1]);
ds_stack_push(newOpenPositions, dummy);
is the same as doing
Code:
ds_stack_push(newOpenPositions, [offsetTestPos1[0],offsetTestPos1[1]]);
doing ds_stack_push(newOpenPositions, offsetTestPos1) was causing it to fail as offsetTestPos1 was changed on other loops all of the ones added to the stack where changing to match so my stack made a small amount of values rather than a full search.

I just did an isolated test, re the array[number] and it worked, so it might have been a overlapping scope issue.

Thanks for your help :)
 
Top