Skip to content

Instantly share code, notes, and snippets.

@webprogramozo
Forked from wrdg/init.c
Created May 7, 2024 22:12
Show Gist options
  • Save webprogramozo/3cade8cc4c668bd35d631e204cb2bd59 to your computer and use it in GitHub Desktop.
Save webprogramozo/3cade8cc4c668bd35d631e204cb2bd59 to your computer and use it in GitHub Desktop.
Customizable spawn loadouts for regular and donator players; changes/additions to init.c
//* Script: Customizable spawn loadouts for regular and donator players
//* Author: Wardog
//* Donate: https://wrdg.net/donate
//*
//* Notes: Creates a directory in the Server Profile folder called SpawnLoadout. You can change these files in
//* in real time, without restarting the server to take effect.
//*
//* ./SpawnLoadout/CommonItems.txt = items spawned between both donators and regulars
//* ./SpawnLoadout/Regular.txt = loadout for regular players
//* ./SpawnLoadout/Donators/STEAMIDHERE.txt = directory where steam id files go for donators; loadouts for donators
//*
//* Loadout files are autogenerated if they're missing from the server profile
//* Loadouts can be changed real time while the server is running, no need to restart the server to take effect
//!!! THESE ARE ADDITIONS TO THE INIT.C FILE, NOT TO COMPLETELY REPLACE THE INIT.C FILE
class CustomMission : MissionServer
{
private const static string m_SpawnLoadoutDirectory = "$profile:SpawnLoadout/"; // root directory for SpawnLoadout
private const static string m_DonatorDirectory = m_SpawnLoadoutDirectory + "Donators/"; // directory for donator loadout text files
private const static string m_RegularLoadout = m_SpawnLoadoutDirectory + "Regular.txt"; // file for regular loadout
private const static string m_CommonItems = m_SpawnLoadoutDirectory + "CommonItems.txt"; // file for in common items for both regular and donator
void CustomMission()
{
FileHandle templateFile;
if (!FileExist(m_SpawnLoadoutDirectory))
{
MakeDirectory(m_SpawnLoadoutDirectory)
// create default CommonItems.txt
templateFile = OpenFile(m_CommonItems, FileMode.WRITE);
FPrintln(templateFile, "Rag 4\nHuntingKnife\nMatchbox\nHatchet\nFlashlight\nBattery9V\nSodaCan_Cola\nBakedBeansCan");
CloseFile(templateFile);
// create default Regular.txt
templateFile = OpenFile(m_RegularLoadout, FileMode.WRITE);
FPrintln(templateFile, "BomberJacket_Grey\nJeans_Black\nTaloonBag_Blue\nAthleticShoes_Grey");
CloseFile(templateFile);
}
if (!FileExist(m_DonatorDirectory))
{
string template = GetDonatorFile("STEAMIDHERE");
MakeDirectory(m_DonatorDirectory);
// create template donator file
templateFile = OpenFile(template, FileMode.WRITE);
FPrintln(templateFile, "BomberJacket_Blue\nJeans_Grey\nTaloonBag_Orange\nAthleticShoes_Brown");
CloseFile(templateFile);
}
}
//!!! REPLACES EXISTING METHOD
override void StartingEquipSetup(PlayerBase player, bool clothesChosen)
{
player.RemoveAllItems(); // clear all default spawning items
FileHandle donatorFile;
string line;
TStringArray contents = new TStringArray();
string file = GetDonatorFile(player.GetIdentity().GetPlainId());
if (FileExist(file))
{
SpawnLoadout(player, ReadFileLines(file)); // spawn donator loadout
return;
}
SpawnLoadout(player, ReadFileLines(m_RegularLoadout)); // spawn regular player loadout
}
private void SpawnLoadout(PlayerBase player, ref TStringArray loadout)
{
FileHandle loadoutFile;
string line;
// creates clothes loadout
foreach (string clothes : loadout)
player.GetInventory().CreateInInventory(clothes);
// creates common items
TStringArray items = ReadFileLines(m_CommonItems);
foreach (string item : items)
{
if (item.Contains(" ")) // check for space, which signifies a quantity item
{
CreateQuantityItem(player, item);
continue;
}
player.GetInventory().CreateInInventory(item);
}
}
private void CreateQuantityItem(PlayerBase player, string item)
{
TStringArray quantity = new TStringArray();
item.Split(" ", quantity);
ItemBase quantityItem = player.GetInventory().CreateInInventory(quantity[0]);
quantityItem.SetQuantity(quantity[1].ToFloat());
}
private string GetDonatorFile(string id)
{
return string.Format("%1%2.txt", m_DonatorDirectory, id);
}
private TStringArray ReadFileLines(string path)
{
FileHandle file;
string line;
TStringArray contents = new TStringArray();
file = OpenFile(path, FileMode.READ);
while (FGets(file, line) > 0)
{
line.Trim();
if (line != string.Empty)
{
contents.Insert(line);
line = string.Empty;
}
}
CloseFile(file);
return contents;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment