Fandom

Surfpup's tConfig Mod Wiki

Making a custom invasion

124pages on
this wiki
Add New Page
Comments5 Share

Setting up the invasionEdit

In Yourmodnamefolder\Global\World.cs, you have to put a static boolean: let's name it customInvasionActive.

But if you also want a counter for how many NPCs you killed for that invasion and the warnings, so let's add some static ints, customInvasionCount and customInvasionWarn, and a boolean to say it needs to give a waring: needsToWarn.

Updating those numbers needs to configurated in UpdateWorld.

You also want to add some warining/start/stop text, so let's add that, too, via methods.

Let's take a look at our code:

public static bool customInvasionActive = false; // le invasion boolean
public static bool needsToWarn = false; // le warning boolean
public static int customInvasionCount = 0; // le counter
public static int customInvasionWarn = 1800; // le warning message timer
public static int invasionDir = 1; // it is coming from the west or from the east?
// ----------------------------------------------------------------------------
public static void Initialize() { // reset everything
    customInvasionActive = false;
    needsToWarn = false;
    customInvasionCount = 0;
    customInvasionWarn = 1800; // 1800 frames is 1/2 minute.
    invasionDir = 1;
}
// ----------------------------------------------------------------------------
public static void StartCustomInvasion() {
    customInvasionActive = true; // let te invasion be active (logic)
    customInvasionCount = 0; // reset it to avoid cheating
    customInvasionWarn = 1800;
    needsToWarn = false;
    if (Main.netMode == 0) Main.NewText("The custom invasion has arrived!);
    else NetMessage.SendData(25, -1, -1, "The custom invasion has arrived!", 255, 255f, 255f, 255f, 0);
}
// ----------------------------------------------------------------------------
public static void EndCustomInvasion() {
    customInvasionActive = false; // let te invasion be inactive (logic)
    customInvasionCount = 0; // reset it to avoid cheating
    customInvasionWarn = 1800;
    needsToWarn = false;
   if (Main.netMode == 0) Main.NewText("The custom invasion has been defeated!");
    else NetMessage.SendData(25, -1, -1, "The custom invasion has been defeated!", 255, 255f, 255f, 255f, 0);
}
// ----------------------------------------------------------------------------
public static void SendWarning() {
    if (invasionDir == 1) {
        if (Main.netMode == 0) Main.NewText("The custom invasion is coming from the west!");
    else NetMessage.SendData(25, -1, -1, "The custom invasion is coming from the west!", 255, 255f, 255f, 255f, 0);
    }
    else if (invasionDir == -1) {
    if (Main.netMode == 0) Main.NewText("The custom invasion is coming from the west!");
    else NetMessage.SendData(25, -1, -1, "The custom invasion is coming from the west!", 255, 255f, 255f, 255f, 0);
    }
}

Updating the invasionEdit

Now we need to let it be updated: when to start/stop/send messages.

That's where UpdateWorld comes in.

public static void UpdateWorld() {
    if (needsToWarn) { // if it's warning time
        customInvasionWarn--; // lower it by 1 per frame
    }
    if (customInvasionWarn == 1799) { // 1 less then max
        // set direction
        if (Main.rand.Next(2) == 0) invasionDir = -1;
        else invasionDir = 1;
        SendWarning(); // and send the message
    }
    if (customInvasionWarn <= 0) { // if the warning counter is less or equal to 0
        needsToWarn = false; // not warning time anymore
    }
    if (customInvasionCount >= 150) { // if over or equal to 150 invasion npcs killed
        EndCustomInvasion(); // end the custom invasion
    }

    if (Main.rand.Next(100000)) { // optional: let the invasion be started at random
        needsToWarn = true;
    }
}

Now we have to say when an invasion npc is killed. set this in the cs file of that npc:

public void NPCLoot() { // code when an npc dies
    if (ModWorld.customInvasionActive) // when the invasion is active
        ModWorld.customInvasionCount++; // let the counter go up by 1
}

Save/Load the invasion stateEdit

What if I exit the world and rejoin?

you might ask...

That's why the Save/Load methods exist! These methods are called AFTER Initialize, so don't worry.

public void Save(BinaryWriter W) {
    // write everything
    W.Write(customInvasionActive);
    W.Write(customInvasionCount);
    W.Write(customInvasionWarn);
    W.Write(invasionDir);
    W.Write(needsToWarn);
}
public void Load(BinaryReader R, int V) {
    // now load everything from the save data
    customInvasionActive = R.ReadBoolean();
    customInvasionCount = R.ReadInt32();
    customInvasionWarn = R.ReadInt32();
    invasionDir = R.ReadInt32();
    needsToWarn = R.ReadBoolean();
}

That's why the Save/Load methods exist! These methods are called AFTER Initialize, so don't worry.

Letting the invasion start/stopEdit

You can start the invasion how you want to, like with a summoning item.

public void UseItem(Player P, int PID) {
    ModWorld.needsToWarn = true; // set it to true. invasion will start.
    Main.PlaySound(15, -1, -1, 0); // play the summoning sound (optional)
}

Or you can do it when an npc dies (void NPCLoot()), spawns(bool SpawnNPC(int, int, int), when a player dies(void PostKill(Player, double, int, bool, string), or....

Now stopping:

already included: in World.cs/UpdateWorld() from part 2: Updating the invasion.

How to let  the invasion's npc spawnEdit

It's very easy. You have to put this in the NPC's cs file:

public bool SpawnNPC(int x, int y, int PID) {
    if (ModWorld.customInvasionActive && Main.rand.Next(3) == 0) { // when the invasion is active, and a 1 in 3 chance
         return true; // let the npc be spawned
    }
    return false; // else: nope.avi
}

Making the npc's maxSpawns during the invasion ultra highEdit

This is why ModGeneric.UpdateSpawn() is epic.

(this is in the file Yourmodsname\Global\Generic.cs)

public void UpdateSpawn() {
    if (ModWorld.customInvasionActive)
        NPC.maxSpawns = 30; // allows a total of 30 npc being active

    // optional: let the invasion's npcs be the only one who spawn
    for (int n = 0; n < Main.npc.Length; n++) {
        NPC N = Main.npc[n];
        if (N.townNPC) continue; // ignore town npcs
        if (N.type == Config.npcDefs.byName["thenpcsname"].type || N.type == Config.npcDefs.byName["theothernpcsname"].type) continue // ignor the npc's of the invasion
        Main.npc[n].active = false; // all other npcs: be unactive (lets them disappear)
    }
}

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.