Preventing stock weapons being given in IDKFA cheat?

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
User avatar
MartinHowe
Posts: 2041
Joined: Mon Aug 11, 2003 1:50 pm
Location: Waveney, United Kingdom

Preventing stock weapons being given in IDKFA cheat?

Post by MartinHowe »

Is there any way to prevent stock weapons being given in IDKFA cheat?

I have a mod that defines several weapons; some are new, some replace the stock weapons with functional equivalents based on a flexible base class (so they are not derived from the originals). However, IDKFA gives the stock weapons as well. I have found a way to prevent them being given at all or otherwise spawned:

Code: Select all

// --- DISABLE STOCK WEAPONS ---------------------------------------------------

gameinfo
{
    AddEventHandlers = "DisableStockWeapons"
}

// -----------------------------------------------------------------------------

// --- DISABLE STOCK WEAPONS ---------------------------------------------------

version "4.5"

// --- DEFAULT -----------------------------------------------------------------

Class DisableStockWeapons : EventHandler
{
    // =========================================================================

    override void WorldThingSpawned(WorldEvent e)
    {
        actor thing = e.Thing;
        name cn = thing.GetClassName();
        switch (cn)
        {
            case 'Chainsaw':
            case 'Pistol':
            case 'Shotgun':
            case 'SuperShotgun':
            case 'Chaingun':
            case 'RocketLauncher':
            case 'PlasmaRifle':
            case 'BFG9000':
                thing.Destroy();
                break;
        }
    }

    // =========================================================================
}

// -----------------------------------------------------------------------------
But this is a bit clumsy and it runs for every item spawned; it also would happen even if a stock weapon is explicitly spawned in the console or by a script. Is there a less hacky way to do it and limited to cheat giving?
User avatar
phantombeta
Posts: 2113
Joined: Thu May 02, 2013 1:27 am
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia with Vulkan support
Location: Brazil

Re: Preventing stock weapons being given in IDKFA cheat?

Post by phantombeta »

Does your mod define its own PlayerPawn? If so, you can override the give cheat in it.
User avatar
MartinHowe
Posts: 2041
Joined: Mon Aug 11, 2003 1:50 pm
Location: Waveney, United Kingdom

Re: Preventing stock weapons being given in IDKFA cheat?

Post by MartinHowe »

phantombeta wrote: Sat Jul 06, 2024 4:12 pm Does your mod define its own PlayerPawn? If so, you can override the give cheat in it.
Thanks, didn't know about that. Sadly this is a generic mod that wants to be as non-intrusive as possible and overriding player classes is about as intrusive as it gets as there's no way to know what player classes will be in use by users. This is the kind of thing that event handlers were made for but GZDoom does not have a 'reason code' in any of its event handlers AFAICS; that would make event handlers much more useful.
yum13241
Posts: 853
Joined: Mon May 10, 2021 8:08 pm
Preferred Pronouns: He/Him
Operating System Version (Optional): EndeavorOS (basically Arch)
Graphics Processor: Intel with Vulkan/Metal Support

Re: Preventing stock weapons being given in IDKFA cheat?

Post by yum13241 »

Try this:

Code: Select all

class ReplacementHandler : EventHandler
{
	//===========================================================================
	//
	// Handle swapping out vanilla weapons
	//
	//===========================================================================

	override void PlayerEntered(PlayerEvent e)
	{
		PlayerPawn pmo = players[e.PlayerNumber].mo;
		if (!pmo) return;
		SwapWeapons(pmo);
	}

	override void PlayerRespawned(PlayerEvent e)
	{
		PlayerPawn pmo = players[e.PlayerNumber].mo;
		if (!pmo) return;
		SwapWeapons(pmo);
	}
	
	override void WorldTick(void)
	{
		// I can't think of a better way LOL
		for (int i = 0; i < MAXPLAYERS; i++)
		{
			if (playeringame[i])
			{
				let pmo = players[i].mo;
				if (pmo)
				{
					SwapWeapons(pmo);
				}
			}
		}
	}

	//===========================================================================
	//
	// Give smooth weapons to player
	//
	//===========================================================================

	void SwapWeapons(PlayerPawn pmo)
	{
		if (pmo.FindInventory("Pistol") && pmo.player.ReadyWeapon is "Pistol")
		{
			pmo.TakeInventory("Pistol", 0x7FFFFFFF);
			pmo.GiveInventory("ADDYOURPISTOLHERE", 1);
			pmo.TakeInventory("Clip", 20);
		}

		if (pmo.FindInventory("Fist") && pmo.player.ReadyWeapon is "Fist")
		{
			pmo.TakeInventory("Fist", 0x7FFFFFFF);
			pmo.GiveInventory("ADDYOURFISTHERE", 1);
		} // just do this for every weapon lol, so when a stock weapon is cheated, it immediately gets taken.
	}

	//===========================================================================
	//
	// Replace spawned weapons in the world
	//
	//===========================================================================

	override void CheckReplacement(ReplaceEvent e)
	{
		if (e.Replacee == "Fist") e.Replacement = "ADDYOURFIST";
		else if (e.Replacee == "Chainsaw") e.Replacement = "ADDYOURCSAW"
		else if (e.Replacee == "Pistol") e.Replacement = "ADDYOURPISTOL";
		else if (e.Replacee == "Shotgun") e.Replacement = "ADDYOURSHOTGUN";
		
		else if (e.Replacee == "Chaingun") e.Replacement = "ADDYOURCGUN";
		else if (e.Replacee == "BFG9000") e.Replacement = "ADDYOURBFG";
		
		else if (e.Replacee == "RocketLauncher") e.Replacement = "ADDYOURRLAUNCHER";
		else if (e.Replacee == "PlasmaRifle") e.Replacement = "ADDYOURPGUN";
		else if (e.Replacee == "BFG9000") e.Replacement = "ADDYOURBFG";
	}
}
That's originally Nash's code, not mine. I used it in an unreleased project of mine and then re-generalized it for you. That last part handles spawns in world, which also means summon weapname, due to how GZDoom works, while the SwapWeapons() function handles giving through cheats or any instance where a stock weapon would enter the inventory.

Return to “Scripting”