Page 1 of 1

[solved]Colonizing Mexes with java wrapper

Posted: 23 May 2014, 00:27
by Bla
This is a (dysfunctional) method that should return the closest AIFloat3 with a metal spot on it that is not already taken (assuming no enemies). I've been debugging it for like 3 days, but I still haven't found the cause.
locs is an arrayList of all the metal locations. manage.getAllUnitPos() is a method that returns all the locations of my units.
This method is supposed to traverse through all the locations, then see if the location is shorter than the current location, and if it is already occupied.

Code: Select all

public AIFloat3 getClosestMetalSpot(Unit uni){
            AIFloat3 ans = locs.get(0);
            float minDistance = getDistancebetween(uni.getPos(), locs.get(0));
            for(AIFloat3 loc: locs){
                boolean collision = false;
                if(getDistancebetween(uni.getPos(), loc)<minDistance){
                    for(AIFloat3 occuLoc:manage.getAllUnitPos()){
                        if(loc.equals(occuLoc)){
                            collision = true;
                        }
                    }
                    if(!collision){
                        ans = loc;
                        minDistance = getDistancebetween(uni.getPos(), loc);
                    }
                }

            }
            return ans;
        }

Re: Colonizing Mexes with java wrapper

Posted: 23 May 2014, 01:10
by Petah
Although not an exact answer to your question, you can have a look at how my AI planned and captured metal spots:

https://github.com/Petah/BAI/blob/maste ... #L138-L157
https://github.com/Petah/BAI/tree/maste ... /map/metal

Re: Colonizing Mexes with java wrapper

Posted: 23 May 2014, 01:36
by abma
what does it currently return?

minDistance is initialized without checking if its already taken...


imo you should initialize minDistance with Double.POSITIVE_INFINITY or sth. like that.

(the same for ans, i guess you should initialize it with -1, -1, -1 or sth. invalid)

Re: Colonizing Mexes with java wrapper

Posted: 24 May 2014, 12:22
by meriton
Do know about callback.getMap().isPossibleToBuildAt(def, location, facing)? That works fine for me, and is probably more efficient than checking all units (where you would need to handle the size of the unit, as well).

Also, initializing with the first spot is incorrect (in case that is occupied and closest).

A better way is with a single loop:

Code: Select all

    float bestDistance = Float.MAX_VALUE;
    AIFloat3 bestSpot = null;
    for (AIFloat3 spot : spots) {
        if (map.isPossibleToBuildAt(mex, spot, 0) {
            float dist = distance(unitPos, spot);
            if (dist < bestDistance) {
                bestDistance = dist;
                bestSpot = spot;
            }
        }
    }

Re: Colonizing Mexes with java wrapper

Posted: 25 May 2014, 20:59
by Bla
Hm... I changed my design and got it to work... I had to rewrite the class that this was in, as it was very sloppy, and probably I had errors in it elsewhere that caused the method to not work, but isPossibleToBuildAt(def, location, facing) looks cool...

Re: [solved]Colonizing Mexes with java wrapper

Posted: 26 May 2014, 09:25
by hoijui
in some games (eg. BA), on most maps, it is possible to build like.. at least 4 lvl 1 mexes on a single mex spot, that cover the whole of the metal of that spot. thus, isPossibleToBuildAt is not a viable solution. it only would be viable if you play with only AIs, and all of them use the same algorithm to calculate mex spots.

Re: [solved]Colonizing Mexes with java wrapper

Posted: 27 May 2014, 01:08
by Petah
Here is some code to check if a metal spot is already captured:

https://github.com/Petah/BAI/blob/maste ... va#L49-L72