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

Offline TechCor

  • Jr. Member
  • **
  • Posts: 61
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: 897
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: 61
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: 4790
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: 61
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: 4790
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: 61
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: 70
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: 61
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: 2201
    • 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: 767
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: 61
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: 4790
Re: OP2 Scenario Project for C#
« Reply #112 on: Today at 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