AI War 2:Adding Factions

From Arcen Wiki
Jump to navigation Jump to search

This is going to a quick and dirty way of setting up a basic faction, as well as providing a heavily commented example faction that does work, but isn't balanced or otherwise useful for play. Overall, this should provide a good framework to start with that is easy for you to read and understand. You can also take additional code from other factions as necessary if you aren't sure what can or can't be added.

This is going to be building an ExampleFaction provided by CRZgatecrusher on the Forums

Getting Started

To start, go ahead and download the basic necessary components if you haven't already. This tutorial will be using Visual Studio 2017 (https://visualstudio.microsoft.com/downloads/), the release build of AI War 2 version 0.873, the AIWar2ExternalCode Project that is shipped with the game, and NotePad++ (https://notepad-plus-plus.org/download/v7.7.1.html). Note that you do not have to use the two tools listed if you don't want to download them, but they can be useful, at least for following along.

Get all of those set up and updated and all that fun stuff. Then, open your Project in Visual Studio. Go ahead and navigate to the Special Factions folder, right click on it, go to Add, then "New Item..."; you will then select Code File as the option you want to use. Naming it something that corresponds to the faction name is recommended, but not required. I named mine ExampleFaction.cs

INSERT IMAGE HERE

Starting the Code

Let's start by telling the code what namespaces we are using.

using Arcen.AIW2.Core;
using System;
using System.Collections.Generic;
using System.Text;
using Arcen.Universal;

We want to call Arcen.AIW2.Core and Arcen.Universal because they are baked into the game and contain additional things we can use and call. The system ones are just good defaults to have, and nothing else is needed.

Now go ahead and set up your namespace that we will be working in. For anything you do in External code, you will always want to use namespace Arcen.AIW2.External. Now, to define the class. Go ahead and use start with public class SpecialFaction_ExampleFaction : BaseSpecialFaction. This says to make a new public class named SpecialFaction_ExampleFaction based off of the the BaseSpecialFaction. In other words, if it appears in the BaseSpecialFaction file, you can pull it over here and modify it easily.

At this point, we can start with the protected override values. There are only going to be three of them for this example, and they will be

protected override string TracingName => "ExampleFaction"; 
protected override bool EverNeedsToRunLongRangePlanning => true;
protected override FInt MinimumSecondsBetweenLongRangePlannings => FInt.FromParts( 10, 000 );

While there are plenty more you can, for this basic example faction, those are the only three that are required. At this point your code should look like this, but minus the comments:

INSERT IMAGE NUMBER 2 HERE

Let's go ahead and start with making this faction friendly to the AI and its special forces, but hostile to everyone else. For simplicity, that will include factions that are allied with the AI, since this faction spawns Hunter/Killers, and they're mean. Think wasps, except worse. Anyways, to set it up, use public override void SetStartingFactionRelationships( Faction ExampleFaction ). In that, go ahead and put base.SetStartingFactionRelationships( ExampleFaction ); just so it runs its normal stuff.

Here is going to be the most complicated part of the code. We're going to want to start a for loop that iterates through every faction so we can set their relationships up. for ( int i = 0; i < World_AIW2.Instance.Factions.Count; i++ ) will start that loop for us. In that loop, set up a new Faction variable that just pulls whatever faction we're on at this time so we can reference that later on a bit easier. Faction otherFaction = World_AIW2.Instance.Factions[i]; will do that for you and name the variable otherFaction. Now, check if otherFaction and ExampleFaction are the same thing. If so, just continue.

Now we want to start a switch loop. For our purposes, we're going to use it to switch between whether it is a Human faction, AI faction, or some other SpecialFaction. To start that, just use switch ( otherFaction.Type ), then

case FactionType.Player: 
    faction.MakeHostileTo( otherFaction ); 
    otherFaction.MakeHostileTo( Examplefaction );
    break;

What that does is if the FactionType we are looking at is a Player, it will make ExampleFaction hostile to it, and vice versa. You do need to make both hostile to each other, otherwise one faction will just stare at the other and ask "Why are you attacking me bro?" Go ahead and add similar code for FactionType.AI and FactionType.SpecialFaction. For the AI, we want it to be friendly, so use MakeFriendlyTo(), instead of MakeHostileTo() like the Player code did.

For the SpecialFactions, we want to add some special lines in there to deal with the AI special forces, since we want them to be friendly to the H/K's as well. For this, we can do this:

case FactionType.SpecialFaction: 
    if ( otherFaction.Implementation is SpecialFaction_HunterFleet || otherFaction.Implementation is SpecialFaction_AISpecialForces )
    {
        faction.MakeFriendlyTo( otherFaction ); 
        otherFaction.MakeFriendlyTo( ExampleFaction );
    }
    else
    {
        faction.MakeHostileTo( otherFaction );
        otherFaction.MakeHostileTo( ExampleFaction );
    }
    break;

The If statement there will check if the SpecialFaction is the HunterFleet or AI Special Forces, and if so, make them friendly. The else handles anything that isn't one of those two, and sets them hostile to the H/K's.

TO BE CONTINUED