Author Topic: OP2 Scenario Project for C#  (Read 28257 times)

Offline TechCor

  • Jr. Member
  • **
  • Posts: 75
Re: OP2 Scenario Project for C#
« Reply #100 on: August 22, 2019, 10:23:19 PM »
For those just arriving, there is an AI demo video.


Yes, the debug points are from the local player's combat manager. The targets are the "Threat Zones" including defensive points. The DNA markers are the combat units' pathfinder destinations.

Project is using .NET Standard 2.0 built with VS 2017.

Here are the files required to play:

https://github.com/TechCor8/OP2DotNetMissionSDK/tree/master/Outpost2

cTest.dll - The native C++ plugin that contains the ExportLevelDetails.
cTest.opm - The JSON file with startup settings. You can configure player/unit settings and which players use the AI. No compile required. Reading of this file can be disabled in C#.
DotNetInterop.dll - The interface between the native plugin and C#, and C# and the Outpost 2 API.
DotNetMissionSDK.dll - The C# mission DLL. This is the generic version for use with JSON/editor missions. For building a custom C# mission, see the GitHub ReadMe.


You can easily integrate the AI into your existing C++ missions. Just add DotNetInterop as a reference and call the required functions:

https://github.com/TechCor8/OP2DotNetMissionSDK/blob/master/NativeMissionSDK/NativePlugin/LevelMain.cpp

That will hook C# into the C++ mission. From there, you can manually enable the bot in C# CustomLogic.cs if using a custom C# DLL, or use the JSON file to manage player setup (which will be "your_dll_name.opm").
« Last Edit: August 22, 2019, 10:29:32 PM by TechCor »

Offline Vagabond

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 914
Re: OP2 Scenario Project for C#
« Reply #101 on: August 23, 2019, 04:30:20 PM »
Any idea how this would work on Linux using Wine? A decent subset of Outpost 2 players use Wine.

I was going to ask if you could statically compile against .net2.0. However since C# compiles into IL and not native, I suppose the real question is whether Wine would support running a compiled C# application?

Targeting .net 2.0 probably would improve compatibility, being around for as long as it has.

-Brett

Offline TechCor

  • Jr. Member
  • **
  • Posts: 75
Re: OP2 Scenario Project for C#
« Reply #102 on: August 23, 2019, 06:08:06 PM »
I don't use Linux, so I don't know.

Hooman said he was able to get the C# project to successfully compile on Linux. However, the interop could be a problem point.

Someone would need to try it out and report back what any issues are.

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4824
Re: OP2 Scenario Project for C#
« Reply #103 on: August 25, 2019, 12:04:30 AM »
Hey, very cool. That was quite an impressive AI demo video. I assume it automatically adapts to the level? We've never had a general purpose AI before.

Would be really cool to deploy an AI for a level with just 1 line of setup code, or even as an in game option for multiplayer maps.

Offline TechCor

  • Jr. Member
  • **
  • Posts: 75
Re: OP2 Scenario Project for C#
« Reply #104 on: August 26, 2019, 01:11:17 AM »
Yes, it will work with any starting base or even no base at all (if you want to do combat only).


I've now added the weighted goal system. The AI previously had a hardcoded task order.

Each top-level task/goal calculates a 0-1 importance. These goals are then sorted with the most important goals executed first. Static weights are used to control how important a goal is overall. For example, maybe you don't want the AI to ever expand mining operations, so you set the weight to 0.

The goal system dramatically increases the bot's ability to adapt to the situation. Each goal can have its importance calculated from a variety of factors. For example, expanding mining is more important if reserves are running low. Same with food supply. Building combat units more important if the enemy has combat units.

Already, I am seeing a substantial change in AI behavior.

Instead of building agridomes until a surplus is reached, it instead decides to build a vehicle factory because it has enough metal in reserve. Perfecting morale is no longer mandatory, instead opting to build a handful to boost it a little. Combat units start appearing within a couple minutes, and the first player's CC went down in 5 minutes. I think I'll upload a new video of this to show the improvement.

A lot of additional goals will be added to tailor the AI's personalities. Convec counts, scouts, light towers, evac transports will all be weighted goals. Tweaking the values can give straight aggressive AIs, or dawdling ones that build all the optional stuff.

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4824
Re: OP2 Scenario Project for C#
« Reply #105 on: August 26, 2019, 08:49:11 AM »
Hmm, would be very neat if the AI could be dropped into multiplayer missions for the extra player slots. Might even be possible to make them playable single player against AI opponents.

Offline TechCor

  • Jr. Member
  • **
  • Posts: 75
Re: OP2 Scenario Project for C#
« Reply #106 on: August 27, 2019, 03:16:47 PM »
That would be cool, but outside my knowledge island.


AI Changes
The top-level "Fix Disconnected Structures" has been removed. That task used to connect the closest structures to an earthworker until all structures were connected. Instead there is now a ConnectStructureTask that is a (blocking) sibling to BuildStructureTask under the new MaintainStructureTask. MaintainStructureTask now controls the number of structures to maintain of a particular type. MaintainStructureTask also has a RepairStructureTask under it for making sure that structures are actually operable and not critically damaged.

The result of this is that structures will be connected and repaired based on importance, and some structures may even be abandoned if they are not important enough. Full repairs are still a top-level goal, but it will have lower importance compared to other goals.


Labor Management
The AI base management is adapting pretty well. Unfortunately, the labor manager isn't cooperating. The base manager builds a second smelter when metal runs low, but the labor manager decides to put workers towards morale instead.

I've been thinking of creating a labor task tree that would set the activation priority order that the labor manager uses. The issue that I haven't worked out is how to prevent labor from being improperly reassigned when importance drops. The smelters shouldn't shut down just because we have enough metal and need more combat units! (Or should they?)

I suppose I need to think about what an optimal labor strategy looks like.

EDIT:
It just occurred to me that, since the goals are not tied directly to the number of active structures, an ActivateStructure task just might work. The bot would start performing all sorts of micro. I can see it now! Morale goes to 90, residences shut down, factories turn on and start building. Morale drops to 70, factories turn off, residences turn back on. Ha!


Morale and Food
Also, I've realized that food production may impact morale. I've never thought about this before since I personally maintain a surplus. However, the bot has "figured out" that it can kill all opponents before needing a second agridome with the current starting food.

Where does food rank in morale importance? Is it better to build an agridome or a residence when it comes to morale?
« Last Edit: August 27, 2019, 03:43:23 PM by TechCor »

Offline Crow!

  • Jr. Member
  • **
  • Posts: 72
Re: OP2 Scenario Project for C#
« Reply #107 on: August 27, 2019, 04:44:53 PM »
You can find morale.txt on this forum, and it shows the relative effect on morale of various things.

I'm not sure what constitutes "high" versus "med" levels of crowdedness, but food storage being in deficit appears to have the same effect as residences being highly crowded.

As far as I know, building excess residences than required to get below 100% does nothing for morale other than give you a backup should one get disabled for some reason.

I'm also a little unsure if everything actually works the way morale.txt would suggest; med centers, for example, seem to indicate their crowdedness doesn't affect morale, but it definitely does.
« Last Edit: August 27, 2019, 04:49:30 PM by Crow! »
Speedruns, my FFIV game randomizer, and more can be found at my twitch page:
https://twitch.tv/iicrowii

Offline TechCor

  • Jr. Member
  • **
  • Posts: 75
Re: OP2 Scenario Project for C#
« Reply #108 on: September 08, 2019, 03:36:00 AM »
Thanks, Crow! I added agridomes to the AI morale task.

Labor Manager now takes base manager's goal priorities into account. I've also made a number of fixes and tweaks to the AI to improve base management.

- AI will build structure factories at each command center (importance represented as a factory/cc ratio).
- AI will build kits at the closest factory to the deploy site.
- AI will enable/disable command centers depending on whether there are connected buildings that want to be enabled.


Some problems I've run into:

- Scorpions don't attack. They don't appear to have a weapon either (It is set to map_id.None).
- Can't get the structure factory kit being built. I did a memory dump and could not identify it. This would be nice for producing parallel structure kits without making the same type on accident.

I'm going to start working on research prioritization, so tech level will be set to 1 for testing. Eventually, I will get to much needed combat work.

Last video of 6p tech level 13:

Offline leeor_net

  • Administrator
  • Hero Member
  • *****
  • Posts: 2210
    • LairWorks Entertainment
Re: OP2 Scenario Project for C#
« Reply #109 on: September 08, 2019, 02:28:21 PM »
This is very exciting! It's good to see that this is still being developed. Could make for some very interesting missions and make computer players in multiplayer a thing. :D

Offline lordpalandus

  • Hero Member
  • *****
  • Posts: 782
Re: OP2 Scenario Project for C#
« Reply #110 on: September 08, 2019, 11:23:29 PM »
Looks pretty awesome. Great job TechCor!
Currently working on Cataclysm of Chaos, Remade.
Link to OPU page = http://forum.outpost2.net/index.php/topic,6073.0.html

Offline TechCor

  • Jr. Member
  • **
  • Posts: 75
Re: OP2 Scenario Project for C#
« Reply #111 on: September 17, 2019, 05:52:52 AM »
Thanks for the comments, guys!

Research is coming along. Almost time for a new video.

Current Changes:
  • I ditched ResearchManager and created a new ResearchTask that gets added as a prerequisite to other tasks in the task tree.
  • PerformTask() now returns detailed info instead of a bool. Instead of canceling lower priority tasks, they now check the reason the high priority task failed. If a higher task failed due to common metal, the lower task can't use common metal, etc. This allows more tasks to run in parallel without causing permanent delays on higher priority issues.
  • Because ResearchTask is now in the task tree, labs are sorted by correct priority for Labor Manager. Labor Manager will move scientists around based on where they are needed. Morale more important? Fewer researchers. Research insanely important? Idle morale structures and go full blast! (Not even kidding. I witnessed this and thought it was awesome! Will need to tweak priorities though...)

I was originally thinking of randomly picking upgrades to research (Automated Diagnostics, etc), but I've come up with a good priority-based scheme. The goals that handle things like "MaintainArmy" and "MaintainMorale" will have a topic list. If the goal is high enough priority, that research takes precedence. It already does this for structure/vehicle required topics, so might as well keep it going!

I've noticed a bug where GetEdenCost() returns 0 for the Observatory's research topic. I have no idea why this happens, but it prevents Eden from doing required research. The AI is now building Meteor Defenses without an Observatory due to this.

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4824
Re: OP2 Scenario Project for C#
« Reply #112 on: September 18, 2019, 03:46:53 AM »
This has really taken shape quickly. I'm quite impressed. That AI actually looks quite good, and sounds like it's quite versatile.

The AI really seems to have a strange fascination with patrolling starflares though. Don't know if I'd want starflares patrolling my base. :P

Offline lordpalandus

  • Hero Member
  • *****
  • Posts: 782
Re: OP2 Scenario Project for C#
« Reply #113 on: September 19, 2019, 12:56:31 AM »
... Do starflares explode violently if shot at by the enemy? I can't remember if they do or not.
Currently working on Cataclysm of Chaos, Remade.
Link to OPU page = http://forum.outpost2.net/index.php/topic,6073.0.html

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4824
Re: OP2 Scenario Project for C#
« Reply #114 on: September 19, 2019, 05:15:21 PM »
I don't remember either. But assuming the enemy floods your base with an army, what are you going to do? Attack them?  ???

Offline leeor_net

  • Administrator
  • Hero Member
  • *****
  • Posts: 2210
    • LairWorks Entertainment
Re: OP2 Scenario Project for C#
« Reply #115 on: September 19, 2019, 05:16:10 PM »
No, they can be neutralized before they detonate. Kind of a balancing trade-off me thinks. Gives the defending player an opportunity to minimize damage.

As for the flood, the strategy was EMP's and hoping to hell you got them all before they could move again.

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4824
Re: OP2 Scenario Project for C#
« Reply #116 on: September 19, 2019, 05:19:36 PM »
No no, we're talking about defending with starflares, not attacking with them. ;)

Offline Vagabond

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 914
Re: OP2 Scenario Project for C#
« Reply #117 on: September 19, 2019, 09:37:44 PM »
Well look at the bright side, it isn't defending bases with supernova tanks...

I'd really like to see this applied to multiplayer missions somehow so people could play multiplayer missions locally. Maybe it would make a nice teaming effort someday in the future to make a reality.

-Brett

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4824
Re: OP2 Scenario Project for C#
« Reply #118 on: September 20, 2019, 06:58:03 AM »
Collateral damage? You'd have to be very careful with those supernovas. Or just pretend like you're Arnold Schwarzenegger, then all explosions are cool. :P

I agree about applying this to multiplayer missions. We'll have to hurry up with the op2ext updates and write a patch for it. I would love to have an AI module. :D

Offline leeor_net

  • Administrator
  • Hero Member
  • *****
  • Posts: 2210
    • LairWorks Entertainment
Re: OP2 Scenario Project for C#
« Reply #119 on: September 20, 2019, 10:40:10 AM »
No no, we're talking about defending with starflares, not attacking with them. ;)

Yeesh, leave it to me to misunderstand.  :-[

But yeah, in that case, that would be somewhat dangerous.  :o

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4824
Re: OP2 Scenario Project for C#
« Reply #120 on: September 21, 2019, 02:37:46 AM »
A bit off topic here, but someone should totally make a map based on that idea. Disable all weapons except for starflares, so the player is forced to defend with them. Then an AI can send wave after wave of their own tanks at the player's base.  ;)

Speaking of AI, could the AI code here handle attack waves coming in from off the side of the map?  :)

Offline TechCor

  • Jr. Member
  • **
  • Posts: 75
Re: OP2 Scenario Project for C#
« Reply #121 on: September 21, 2019, 04:42:35 AM »
Wow, lots of casual talk in this thread! Here I thought the community was dead. :D

---
Figured out why Observatories weren't getting researched properly. Turns out Cost 0 means "Free" and -1 means "Can't Research". Probably a source of a lot of research problems I didn't notice.

Added in all the extra research options. The only thing that won't get researched is Consumerism because I don't have a task for the Consumer Factory yet. The majority of the optional topics are in MaintainPopulationGoal and MaintainArmyGoal, with a handful in ExpandRareMiningGoal, and the rest that didn't have a good place were put into MaintainResearchGoal. Topics required for units/structures, are already handled on an "as needed" basis.

There is a bug where goal priorities rapidly switch due to labor moving around. Going to make a video this weekend anyway, as I won't have time to work on this again until the holidays.


A bit off topic here, but someone should totally make a map based on that idea. Disable all weapons except for starflares, so the player is forced to defend with them. Then an AI can send wave after wave of their own tanks at the player's base.  ;)

Speaking of AI, could the AI code here handle attack waves coming in from off the side of the map?  :)
The AI will take control of anything it is given. Combat Management is separate from Base Management, so you don't need any structures for it to work. However, if they happen to have a base they might prioritize defending (which could be a good thing!).

Offline TechCor

  • Jr. Member
  • **
  • Posts: 75
Re: OP2 Scenario Project for C#
« Reply #122 on: September 21, 2019, 09:48:31 AM »
Here's the research video; No more free tech.



If you are wondering why the AI is so slow to get weapons, it is because the MaintainArmy goal is so far down the list. This is because the AI is reactive not proactive. MaintainArmy's importance is based on enemy strength. If no one else builds weapons, it won't either, until there is nothing else to do. This will be changed at some point to increase importance based on perceived threat. For example, if the enemy has a vehicle factory, an advanced lab, or worse, BOTH, importance would increase.

Offline leeor_net

  • Administrator
  • Hero Member
  • *****
  • Posts: 2210
    • LairWorks Entertainment
Re: OP2 Scenario Project for C#
« Reply #123 on: September 21, 2019, 09:52:21 AM »
Sounds like a fairly passive AI. Are there plans on having different 'personalities'? Age of Empires had several AI personalities that would range from extremely passive to extremely aggressive, it made for more interesting single-player and even multi-player campaigns.

Offline TechCor

  • Jr. Member
  • **
  • Posts: 75
Re: OP2 Scenario Project for C#
« Reply #124 on: September 21, 2019, 10:55:58 AM »
Sounds like a fairly passive AI. Are there plans on having different 'personalities'? Age of Empires had several AI personalities that would range from extremely passive to extremely aggressive, it made for more interesting single-player and even multi-player campaigns.
Please understand this is a lot of work and not even close to done.

There are three major elements to the AI.

The first is the task tree that lets the AI do everything possible in the game. This is simply stuff like "I want a laser lynx, but first I need X tech and a vehicle factory, but before that I need Y tech and a convec" etc. This is about 90% complete. I need to add solar sats, consumer factories, EMP missiles, and some useless stuff like light towers. Then a lot of time will be spent fixing all the little issues that crop up during the execution of these tasks.

The second is the goal system, which processes the priorities for the task tree. "This is the most important thing to take care of right now, I should make a laser lynx to solve that problem...[enter task tree]". These goals are sorted by the 0.0 to 1.0 importance value, which is combined with a weight passed at startup. The goals include all sorts of things, but there are a few critical ones that are typically high in the priority list (ExpandCommon/Rare, MaintainPop, MaintainArmy). I still need to add a useless goal set for scouts, evac transports, and dozers, and then tweak the importance formulas.

Finally, the combat system is independently ran. This system assesses enemy state and creates zones that act as a combat plan. These are sorted based on importance, and groups are populated in this sorted order. Any zone groups that don't have units assigned are put into a list for Base Management to grab and process in MaintainArmy. While the core infrastructure is there, it has not been revised since the first draft, at all. I'd say it's probably 20% complete, if that. Still need to stop ESGs from shooting buildings, Scorpions from not firing, and generally fix all sorts of poor decisions it is currently making.

This is a pretty good summary of how the AI works and where I'm at with it. The key thing to point out here is how I'm working on it. Notice I start at the bottom (task tree) and work up to self-actualization (Destroy thy enemies).

So what does this have to do with your question?

Well, the goal system weights mentioned above affect goal priorities. For example, you can increase the MaintainArmy weight and decrease other weights to increase the focus on building troops. Or you could decrease ExpandCommon/Rare if the AI shouldn't build more bases/mines/smelters. You can also decrease MaintainPop and let the people be miserable to death, or decrease MaintainFood to allow them to starve. Combat goals will also have a similar thing, but it is barely on the radar at the moment. At some point there will be preset weights to choose from so that mission developers don't need to fiddle with the weights themselves, BUT the AI is not ready for that. EVERYTHING else must be complete first. Making presets now would be a waste of time since they will most certainly break as the core is developed further.

Customization is the very, very last thing to do. With all systems complete, it's all about doing things like disabling morale structures and useless units, setting weights, and some optional features (I want to add a base preplanner).


tl;dr, I haven't gotten to it yet.