AI War 2:Adding Factions

From Arcen Wiki
Revision as of 07:03, 13 October 2019 by CRZgatecrusher (talk | contribs)
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 (, the release build of AI War 2 version 0.873, the AIWar2ExternalCode Project that is shipped with the game, and NotePad++ ( 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


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:


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 );

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 );
        faction.MakeHostileTo( otherFaction );
        otherFaction.MakeHostileTo( ExampleFaction );

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.

After that the H/K's will have to be seeded (spawned) into the map. we have to override a void (start a new condition for the code to run and the type of code it is. should have told you that earlier. conditions can only run specific type of code in it. but the code for editing voids tends to be

public override void SeedStartingEntities_LaterEverythingElse( Faction faction, Galaxy galaxy, ArcenSimContext Context, MapTypeData mapType);
Code here

This peace of code determines what and how entities are seeded into the AIW2 map:

Mapgen_Base.Mapgen_SeedSpecialEntities(Context, galaxy, faction, SpecialEntityType.None, "HunterKiller" ,
SeedingType.HardcodedCount, (Intensity*2), MapGenCountPerPlanet.One, MapGenSeedStyle.BigBad, 2, 0, PlanetSeedingZone.MostAnywhere);

The first 3 variables for this command can be left the same all the time. The next variable is the SpecialEntityType. This variable is not in the scope of this wiki page but becasue the H/Ks are not a special type this is set to none. the tag in this case is "HunterKiller" which is the appropriate Tag for this unit. A tag is a reference to an Entity/Entities. An entity can have multiple tags.

SeedingType determines some of the variables further on. for this wiki page it is a HardcodedCount. the next variable is determing how many H/Ks should be seeded on the map and the one after that determines how may H/Ks can spawn on an individual planet. The next variable is related to how this unit seeds in relation to other seeded units (edit if I'm wrong).

the next two number determine where the H/Ks spawn in relation to the Human and AI homeworld/s respectively. PlanetSeedingZone determines where on the planet the H/K spawns. i.e. on the edge or nearer to the center or both. It's been set to Most Anywhere becasue they will move anyways.

The Variable Intensity is created from the code

int Intensity = faction.Ex_MinorFactionCommon_GetPrimitives().Intensity;

and some XML code is required for this but I would say (READ THE COMMENTED XML FOR IT <add XML to the end>). Put the code above the code above like this:

ArcenSimContext Context, MapTypeData mapType);
int Intensity = faction.Ex_MinorFactionCommon_GetPrimitives().Intensity;
Mapgen_Base.Mapgen_SeedSpecialEntities(Context, galaxy, faction, SpecialEntityType.None, "HunterKiller" ,
SeedingType.HardcodedCount, (Intensity*2), MapGenCountPerPlanet.One, MapGenSeedStyle.BigBad, 2, 0, PlanetSeedingZone.MostAnywhere);

Great... now that the H/Ks will spawn in what will they do? well currently they'll do nothing but that will change...