Advertisement

Suggestions on AI to code

Started by August 16, 2010 09:42 PM
3 comments, last by Narf the Mouse 14 years, 3 months ago
The game is a space conquest 4X. It's in a very basic state right now, but as you can build fleets and use them to colonize planets, I've decided to take the first step and add a basic AI which will colonize planets. There's no population yet, so the colonizer AI need mostly be a framework.

Fleets, rather than individual ships, are the focus of the game. Fleet speed is relative to the square root of fleet size. There's no real physics in the newtonian sense; fleets move at their speed. The game is pausable real-time; there's an automatic interrupt whenever something reports it has no orders; the player can also pause at any point. This is in an attempt to meld real-time and turn-based dynamics. The first version (call it 0.1) will be one player vs one AI.

My AI skill is Beginner; I'm looking for good concepts, techniques and coding styles for AI for my level and this game.

Edit: An AI to build fleets will also be required, but is not strictly necessary. At the moment, being able to turn on the Colonizer AI and have it direct fleets appropriately is the goal.

The most likely future composition of fleets will be into Scout, Frigate, Cruiser, Destroyer, Battleship, Freighter and Colony ship counts.
Now seeking advice on the colonizer AI I've written. It's very simple; it makes a list of fleets which don't have orders and stars which are either unowned or don't have a fleet headed to colonize them. That's the planning stage. When the update is run, it runs through the unordered fleets and orders each fleet to colonize the first open star it finds.

I'm guessing the implementation can be done better.
        private Player iAmPlayer;        private GameData gameData;        private List<Fleet> unorderedFleets;        private List<Star> stars;        private Dictionary<Star, bool> starsOpenForColonization;        public bool Plan(double dt)        {            // This doesn't seem to have a use yet.            if (stars == null)            {                stars = new List<Star>();                Star obj;                foreach (SpaceObject spaceObject in gameData.SpaceObjects)                {                    obj = spaceObject as Star;                    if (obj != null)                    {                        stars.Add(obj);                        starsOpenForColonization.Add(obj, true);                    }                }            }            // At the beginning, assume all stars are open.            for (int t = 0; t < starsOpenForColonization.Count; ++t)            {                starsOpenForColonization[starsOpenForColonization.Keys.ElementAt(t)] = true;            }            {                // Assume no available fleets.                unorderedFleets.Clear();                Fleet obj;                Star star;                foreach (SpaceObject spaceObject in gameData.SpaceObjects)                {                    obj = spaceObject as Fleet;                    if (obj != null)                    {                        // No waypoint, no orders, we can use the fleet.                        if (obj.Waypoint == null)                            unorderedFleets.Add(obj);                        // Waypoint is colonize, then there's a star that we don't need to send anything to.                        else if (obj.Waypoint.Order == OrderType.Colonize)                        {                            star = obj.Waypoint.SpaceObject as Star;                            if (star != null)                            {                                starsOpenForColonization[star] = false;                            }                        }                    }                    else                    {                        // If we're looking at a star and its not owned, then that's a star that needs colonization.                        star = spaceObject as Star;                        if (star != null)                        {                            if (star.Player != null)                            {                                starsOpenForColonization[star] = false;                            }                        }                    }                }            }            return true;        }        public bool Update(double dt)        {            // Loop through the fleets without orders, order then to colonize the first open star.            int t2 = 0;            for (int t = 0; t < unorderedFleets.Count; ++t)            {                while (t2 < starsOpenForColonization.Count &&                        !starsOpenForColonization.Values.ElementAt(t2))                    ++t2;                if (t2 < starsOpenForColonization.Count)                {                    unorderedFleets[t].Waypoint = (Waypoint)starsOpenForColonization.Keys.ElementAt(t2);                    unorderedFleets[t].Waypoint.Order = OrderType.Colonize;                    ++t2;                }                else                    break;            }            return true;        }


...Maybe I should stick this in For Beginners?
Advertisement
Regarding no replies: I think people can't say whether it's good or not. Whether it provides an fun challenge for the human(s) in your game is all that matters. This kind of AI is going to be very specific to the game, so it's kind of hard applying any general insight.

My biggest problem when writing an AI for a similar game (though TBS) was:
1) making sure the algorithms that handled a large amount of data were not too slow (often this conincided with keeping them linear in run time to input size). 2) making the AI see ahead and keeping focused (there is an issue calculating decisions for the state at one instant, when the effects are delayed until later).
You are eventually going to want to consider things such as the relative value of star systems. Is there a reason that one may be more valuable than another? Also, what is the distance from your home base. These two items sets up and equation where you are considering the value and the distance at the same time. Obviously I can't show you here because it would be parameterized with design-time coefficients as to what is more important (value/distance).

What falls out of this formula however, is a numerical score rating which star systems to explore/colonize first. That way, you aren't selecting a cheap-ass planet on the other side of the galaxy when there is a good one right next door. The fine-tuning sorts out where that cross-over is. e.g. Do I go 10 units for a good planet or 7 units for a not-quite-as-good planet?

There are plenty of factors that you can eventually include in that "suitability" formula. Things like nearest friendly neighbors. After all, we all know that remote outposts get whacked by the bad guys. It might be an incredible mining planet, but it isn't worth colonizing if you can't protect it.

And it goes on from there...

Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit
Author of the book, Behavioral Mathematics for Game AI
Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play

"Reducing the world to mathematical equations!"

Thanks, you've both given me things to think about.

This topic is closed to new replies.

Advertisement