[solved]Colonizing Mexes with java wrapper

[solved]Colonizing Mexes with java wrapper

Here is where ideas can be collected for the skirmish AI in development

Moderators: hoijui, Moderators

Post Reply
Bla
Posts: 79
Joined: 25 Feb 2013, 14:44

[solved]Colonizing Mexes with java wrapper

Post 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;
        }
Last edited by Bla on 25 May 2014, 20:59, edited 1 time in total.
User avatar
Petah
Posts: 426
Joined: 13 Jan 2008, 19:40

Re: Colonizing Mexes with java wrapper

Post 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
abma
Spring Developer
Posts: 3798
Joined: 01 Jun 2009, 00:08

Re: Colonizing Mexes with java wrapper

Post 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)
meriton
Posts: 18
Joined: 11 Jul 2009, 14:03

Re: Colonizing Mexes with java wrapper

Post 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;
            }
        }
    }
Bla
Posts: 79
Joined: 25 Feb 2013, 14:44

Re: Colonizing Mexes with java wrapper

Post 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...
User avatar
hoijui
Former Engine Dev
Posts: 4344
Joined: 22 Sep 2007, 09:51

Re: [solved]Colonizing Mexes with java wrapper

Post 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.
User avatar
Petah
Posts: 426
Joined: 13 Jan 2008, 19:40

Re: [solved]Colonizing Mexes with java wrapper

Post 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
Post Reply

Return to “AI”