• Hey! Guest! The 40th (!!!) GMC Jam will take place between February 25th, 12:00 UTC to March 1st 12:00 UTC. Why not join in this very special anniversary jam! Click here to find out more!

GMS 2.3+ Return the grid which has the lowest value around 1 chosen grid

Hello everyone,
I have a little question.
I want to return the lowest value around a point.

So i did this code :

GML:
for(var xx = 0; xx <= size; xx++) {
    for(var yy = 0; yy <= size; yy++) {
        if world[xx][yy].elevation > 95 {
            var a = world[xx-1][yy].elevation
            var b = world[xx+1][yy].elevation
            var c = world[xx][yy-1].elevation
            var d = world[xx][yy+1].elevation
            var min_ = min(a,b,c,d)
            }}
 
            }
But now i want to have the "id" of the grid which has the lowest value.


Can you help me?
 

Simon Gust

Member
How are the grid ids saved? Or do you want the value together with it's position sorted?

If so, you could make a list with a, b, c, d empowered together with the positions.
Code:
var list = ds_list_create();

for(var xx = 0; xx <= size; xx++) {
for(var yy = 0; yy <= size; yy++) {
    
    if world[xx][yy].elevation > 95 {
        
        var a = world[xx-1][yy].elevation;
        var b = world[xx+1][yy].elevation;
        var c = world[xx][yy-1].elevation;
        var d = world[xx][yy+1].elevation;
        
        var tailbits = xx << 16 | yy;
        ds_list_add(list, a << 32 | tailbits);
        ds_list_add(list, b << 32 | tailbits); 
        ds_list_add(list, c << 32 | tailbits);
        ds_list_add(list, d << 32 | tailbits);
        
    }
}}

ds_list_sort(list, true);

var lowest = list[| 0];
var val = lowest >> 32;
var xx = lowest >> 16 & 65535;
var yy = lowest >> 65535;
now, it should go over the array, and for every cell with greater elevation than 95, it adds the neighbour values to the list. At the end the list is sorted for the lowest value over the whole grid.
I think that is what you meant right?

If you need to save an id, it works the same way. If you empower the elevation values by bitshifting them above the other data you can reliably connect the elevation and the id or position or whatever.
 
Top