Author Topic: Outpost 2 Coding 201: Week 0  (Read 8787 times)

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3238
Outpost 2 Coding 201: Week 0
« on: December 18, 2010, 01:37:36 AM »
So you may remember that awhile I said I'd write a follow-up to my original coding tutorial.  You may also recall that the format of this tutorial would be a bit different: instead of just posting walls of text and practice ideas for you, I thought it would be better for people to work together on a complicated project.

So who would be interested in making an AI for OP2?

Now I won't lie to you: this isn't going to be easy.  We'll need to learn a lot of new tricks, and put a lot of time and effort into this.  There are going to be a lot of roadblocks in trying to get OP2 to do what we want it to do, and there will be times when we may not be able to achieve the goals we want.

Now, just in case you think I'm being over-dramatic about this, let me post a list of things the AI will need to do:

-Population Management
-Morale Management
-Food Management
-Power Management
-Building and Workforce Management (idling structures as needed, assigning scientists, etc.)
-Mine and Smelter Construction
-Ore Hauling
-Terrain Analysis
-Pathfinding
-Micromanagement
-Offense and Defense
-React to Opponents
-Base Layout and Construction
-Research
-And most importantly: Play by the same rules as a human

And that's just off the top of my head.  Anyways, I'm not trying to scare you off.  Well, maybe a little...  The point I'm trying to make is, this is going to be a huge investment for all of us, and I'm not going to make it if people aren't serious about it.  Don't just say "oh cool, an AI!" and offer to help but then never do anything.  Sign up only if you really are interested in this learning-by-doing approach and will be involved with the project regularly.

I'll give people until the end of the month.  If we don't have enough people by then, it's not happening.

Final note: You really should at least take a look at my Coding 101 tutorials if you haven't already.  But this is open to anyone.  So if BB/Hooman/etc. want to be involved in a big community project, your help would definitely be appreciated.
"As usual, colonist opinion is split between those who think the plague is a good idea, and those who are dying from it." - Outpost Evening Star

Outpost 2 Coding 101 Tutorials

Offline Hidiot

  • Hero Member
  • *****
  • Posts: 1018
Outpost 2 Coding 201: Week 0
« Reply #1 on: December 18, 2010, 03:16:07 AM »
I already have a working system that forces the AI to take researches from a fed list and research them. Might need some more work on how it assigns the scientists, but at least it's code that Hooman mostly wrote, so it should help.

I would continue work on my attempt to replace RecordBuilding, designing it in a way that ques buildings in the SF bay, just as a human would. Its design was also supposed to allow some ease of modification.
But I need to figure out how to get a compiler working on my laptop first...
"Nothing from nowhere, I'm no one at all"

Offline TH300

  • Hero Member
  • *****
  • Posts: 1404
    • http://op3game.net
Outpost 2 Coding 201: Week 0
« Reply #2 on: December 18, 2010, 04:30:47 AM »
I have thought a lot about writing a general ai for op2, in the past. Although great amounts of work would have to be done, it seems feasible to me. The most critical part is a clear concept, a structure that makes the ai flexible while keeping it modular (this will be crucial if several people are going to work on it). I'm not going to contribute to a "if-a-happens-then-do-b ai" and neither to a "just-do-what-the-buildorder-says ai".

The approach that I have in mind is totally priority based, i.e. tasks are assigned (variable) priorities and whatever has the highest priority is done first. There could be priority lists for several building types, e.g. one for structure factories, one for laboratories, one for convecs, one for assigning colonists to structures etc.
The ai would be given just one or very few major objectives, and it would know several tasks that should be done in order to achieve these major objectives. It would try to accomplish these tasks by priority, starting with the one that is most quickly done and/or brings the greatest return. Of course tasks can consist of several sub-tasks, etc.

If this project gets somewhere, I'll write more.
« Last Edit: December 18, 2010, 04:31:10 AM by TH300 »

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3238
Outpost 2 Coding 201: Week 0
« Reply #3 on: December 18, 2010, 11:29:54 AM »
Yeah, that was the idea, TH300.  Rather than write a detailed list of instructions, we just "teach" it how to play and see how it does on its own.
"As usual, colonist opinion is split between those who think the plague is a good idea, and those who are dying from it." - Outpost Evening Star

Outpost 2 Coding 101 Tutorials

Offline CK9

  • Administrator
  • Hero Member
  • *****
  • Posts: 6226
    • http://www.outpost2.net/~ck9
Outpost 2 Coding 201: Week 0
« Reply #4 on: December 18, 2010, 12:34:16 PM »
I'd love to contribute, but I'm afraid the way I think might hinder the project.  I look for the most efficient solution I can think of, which often turns out to be a switch or an if...then...  
CK9 in outpost
Iamck in runescape (yes, I still play...sometimes...)
srentiln in minecraft (I like legos, and I like computer games...it was only a matter of time...) and youtube...
xdarkinsidex on deviantart

yup, I have too many screen names

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3238
Outpost 2 Coding 201: Week 0
« Reply #5 on: December 18, 2010, 01:20:05 PM »
You'll be plenty of help.  I don't want anybody thinking "I'd love to help but I wouldn't be useful".  We can find something for you to do.
"As usual, colonist opinion is split between those who think the plague is a good idea, and those who are dying from it." - Outpost Evening Star

Outpost 2 Coding 101 Tutorials

Offline TH300

  • Hero Member
  • *****
  • Posts: 1404
    • http://op3game.net
Outpost 2 Coding 201: Week 0
« Reply #6 on: December 18, 2010, 02:21:11 PM »
Quote
I'd love to contribute, but I'm afraid the way I think might hinder the project.  I look for the most efficient solution I can think of, which often turns out to be a switch or an if...then...
I think, there will be a point in development, when its just about implementing predefined functions and filling tables - nothing that requires the right idea. Hence, you can probably help us, CK.

Offline Arklon

  • Administrator
  • Hero Member
  • *****
  • Posts: 1269
Outpost 2 Coding 201: Week 0
« Reply #7 on: December 19, 2010, 07:28:57 PM »
Quote
I already have a working system that forces the AI to take researches from a fed list and research them. Might need some more work on how it assigns the scientists, but at least it's code that Hooman mostly wrote, so it should help.

I would continue work on my attempt to replace RecordBuilding, designing it in a way that ques buildings in the SF bay, just as a human would. Its design was also supposed to allow some ease of modification.
But I need to figure out how to get a compiler working on my laptop first...
That could be improved upon by utilizing some hax so that you don't need to manually define all prerequisite techs in the list of techs.

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4955
Outpost 2 Coding 201: Week 0
« Reply #8 on: December 20, 2010, 03:14:58 AM »
Quote
That could be improved upon by utilizing some hax so that you don't need to manually define all prerequisite techs in the list of techs.

Agreed. You could scan the tech tables using a tree traversal algorithm and automatically build a list of prerequisites for any given tech. This had occured to me years ago, but I don't think I ever got around to actually writing any code. I believe I was also trying to generalize the idea so it could handle building and vehicle prerequisites as well. I probably should have just stuck with the tech tree though, since it actually ends up forming..., well, maybe not a tree, but a forest at least. There are no cyclic dependencies with the techs, as there are between vehicles and buildings. (Convec builds VF, which builds Convecs).

Mind you... it's more like there are no cycles in a properly formed tech tree. I suppose you could actually specify one with cycles, and the game would probably load it. That would likely break code that didn't detect cycles and it would get caught in infinite loops. Probably not worth worrying about though. At least not for just the tech tree. However, that would be something needed for a more general system that needed to deal with the dependecies between buildings and vehicles.
 

Offline TH300

  • Hero Member
  • *****
  • Posts: 1404
    • http://op3game.net
Outpost 2 Coding 201: Week 0
« Reply #9 on: December 20, 2010, 03:50:24 AM »
Cycle detection is not black magic. We could do that quite easily.

Nevertheless remains the question whether we want to keep it general or not. Keeping it general would mean to load not only the tech tree dynamically, but also the sheets (beacon yields, unit props, morale stuff, etc.). This could be done and I believe that the hardest part would be to load all that stuff - integrating variable numbers into existing formulas will be simple -. But I'd rather first write the specific thing and then make it more general. All that we'd have to do is design it in such a way that generalization will be possible in the future.

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4955
Outpost 2 Coding 201: Week 0
« Reply #10 on: December 21, 2010, 12:36:15 AM »
Cycle detecting isn't so much hard as just annoying. Particularly when you have to work with data structures that are already fixed, and in a memory model that only really allows for static allocation if you want it to work with the saved game infrastructure. The real question though is what do you do when you detect a cycle? A cycle might be a real impossibility, in which case abondon the goal, and perhaps persue a backup goal, if any. It might be a temporary impossibility, and conditions will change that make it possible, such as through level triggers, or waiting enough time for collect some resource (ore, wreckage). Unless you're clear about what prerequisites are being checked, it's not obvious what constitutes a cycle, and whether or not it's a temporary or complete impossibility.


Also keep in mind how complex the rules of Outpost 2 can be. You'll probably want to deal with just a subset of possible cases to keep things simple.

Consider this: To build a structure, you need a convec loaded with a kit at the build site. This requires a convec with the kit that can path find to that proper location. To get a convec loaded with a kit you need an empty convec docked at an operational structure factory containing the kit so it can be loaded. This might require an empty convec that can path find to the desired structure factory. To make the structure factory operational you need enough power, workers, and a CC connection. To build the kit you need an empty cargo bay and enough ore, and the required tech to build the kit. If you don't have the right tech, then it needs to be researched. You may need to research multiple techs to get the desired tech. You might also need to build an appropriate lab to research the tech. You might need to train workers into scientists to have someone available to do the research. If there is no CC connection you might need to build a tube. If there is no deployed CC (land rush), you might need to deploy a CC stored in a convec. If there are no workers, you might need to idle another structure, or wait until you have enough workers. If you can't idle structures and need to wait for more population, then for your population to grow you need a nursery and university operational, and your morale needs to be half decent (or have lots of med centers). If you have no empty convec then you either need to empty one (build it's kit, unload it's kit, discard it's kit), or build a new convec. Now, to build anything you need ore. This requires having either an active storage facility containing ore, or a non-idled (but possibly disabled) smelter storing the ore. If insufficient ore is stored, then you might need to unload smelted metal in cargo trucks (land rush), or you need an active smelter, and cargo trucks containing ore. For this you probably need an active mine to fill trucks with ore (unless they are pre-filled, perhaps for a land rush using ore instead of metal). This might require building cargo trucks or robo miners at a vehicle factory (which requires ore). It might require simply deploying a pre-existing robo miner. You have a beacon surveyed, right? Or perhaps you need a robo surveyor to be built or to survery a beacon. This requires reachable beacons on the map. They exist, right? Ok, which one is the best to go for? Maybe there were cargo trucks occupied some other way. Perhaps their cargo can be dumped (rare, or rubble) and they can be repurposed. If the smelter isn't active then you may need people, or power, or a CC connection. Do you have enough tokamaks, or other power sources?

Now... suppose that original structure you wanted to deploy was the smelter. Or maybe it was the structure factory, or the vechicle factory, or the command center, or a tokamak. That makes things a little more cyclic. Do you have enough resources to complete the task in any given case above? Are you sure?

Did you notice some of the above required choices? If no empty convec is available, do you wait for one to become available, or do you build a new one?

Ok, now what order should you complete the tasks in? The wrong order can cause delays and might appear obviously dumb to a human opponent.

What algorithm do you use to place needed structures? Does it account for explosive buildings like advanced labs and spaceports? What about EMP from tokamaks. Does it place a command center or structure factory in a good defensive position? Does any of that really matter? I suppose it might be more fun if the computer built an advanced lab right next to a CC or SF.

Ok, what about other goals, such as launching a starship. You have 200 colonists right? What are the preconditions to getting 200 colonists? How does time factor into this, and what needs to be built? Is this goal set explicitly by the level designer, or is the AI more generic and figured this out using the victory conditions for the level?


At any rate, you're probably realizing by now why I never got anything done. :P
I've got quite a few written notes that suggest analysis paralysis may have been a factor.  :blush:
 

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3238
Outpost 2 Coding 201: Week 0
« Reply #11 on: December 21, 2010, 01:49:37 AM »
Not our problem.  Stop using a crappy techtree.

Seriously, if the techtree goes A -> B -> C -> A then it's a poorly made techtree.  Whoever made it needs to fix the bugs in it first.
 
"As usual, colonist opinion is split between those who think the plague is a good idea, and those who are dying from it." - Outpost Evening Star

Outpost 2 Coding 101 Tutorials

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4955
Outpost 2 Coding 201: Week 0
« Reply #12 on: December 21, 2010, 02:56:49 AM »
Yes, it can be ignored for tech trees (that are well formed), but the cycle problem still exists in general if you're looking at dependencies between say convecs and vehicle factories. You can't build one without the other. Then there are structure factories that are self dependent.
 

Offline TH300

  • Hero Member
  • *****
  • Posts: 1404
    • http://op3game.net
Outpost 2 Coding 201: Week 0
« Reply #13 on: December 21, 2010, 05:47:16 AM »
Thanks, Hooman, for the summary. Trust me, I had most of that in mind already and I'm sure, that all problems can be solved. (although not easily)

1. the cycle that you described could and should be broken anywhere, where the required resources are available. But maybe its a better idea to look at the cycle in the opposite direction. Lets assume, the ai wants to build a vehicle factory (when it has only a sf, a cc, a smelter, a robo miner, some trucks, a convec, no metals stored).
- The ai should build a mine at a good place in any case (as a human player would). Yes, in some weird situations this may not be appropriate, but these can be handled.
- The ai should put the trucks on an ore route in any case (as a human player would)
- As soon as enough metals are available the ai should start building the vf kit at the sf
- The ai should drive the empty convec to the sf bay
- when the kit is finish, it should be loaded into the convec and then built.
Thats it. I don't claim that this is general. But it demonstrates the idea: Not everything has to be based on direct dependencies. Instead of having the build-a-vf-code look for an empty convec, the empty convec should look for a structure kit somewhere and build that.

Sorry, I was a bit imprecise in my first post in this thread. I had clearly not that in mind what Hooman described. That would lead to chaos as soon as several objectives needed the same convec at the same time (or in any similar case).

In a concept document that I just dug out, I described my approach as "resource based", i.e. there are several resources (convecs, cargo trucks, miners, metals, structure factories, vehicle factories, laboratories, workers, scientists, etc.) that can be assigned tasks. In the optimal case all these resources are always occupied. And that is best achieved if they don't just sit there and wait until they're needed, but instead look for something to do. The metals could, for example, look for the building/vehicle that is most needed and let it be produced. Colonists could be assigned to those structures which are most needed. Thats what I meant with "having several priority lists" in my first post. And there would of course still be interaction between those priority lists.

Now, there will of course be some complicated parts like a function which looks for a place to build at or a function which looks for a place to deploy a base at (in LR games) or even functions that do the micromanagement of combat vecs in combats. But I have some basic ideas for those already, which I will post after refining them.

And of course tasks need to be done in the right order in order to prevent delays. Lots of fine tuning will be needed here. But it is possible.
« Last Edit: December 21, 2010, 05:56:54 AM by TH300 »

Offline jcj94

  • Sr. Member
  • ****
  • Posts: 407
    • http://techfusion-279.com
Outpost 2 Coding 201: Week 0
« Reply #14 on: March 16, 2011, 07:12:16 PM »
Quote

Agreed. You could scan the tech tables using a tree traversal algorithm and automatically build a list of prerequisites for any given tech.
This sounds similar to what Civilization 3 does,

It tells its AI to get to THIS tech, and has some form of making understand that they each have their own prerequisite to move one.  It would then research based on time, Higher time= higher priory..

Do you think we could have an AI that would research Adv. Lab techs as it researches standard lab techs?

Offline TH300

  • Hero Member
  • *****
  • Posts: 1404
    • http://op3game.net
Outpost 2 Coding 201: Week 0
« Reply #15 on: March 17, 2011, 01:40:47 AM »
Quote
Do you think we could have an AI that would research Adv. Lab techs as it researches standard lab techs?
That would be of minor difficulty. If we could make an ai, we could easily make it research adv. Lab techs if available.

But if you had paid attention, you'd know that we are not making an ai.