Skip to content

Simple KnifeFight

KissLick edited this page May 15, 2016 · 10 revisions

Say, we want to create a simple knife fight game (Red team vs. Blue team and Red team vs. Red team). First of all, you will need to know how to create a plugin for SourceMod (at least basic one).


Now we can move to creating the knife fight game. So let's start from scratch.

#include <sourcemod>

public Plugin:myinfo =
{
	// You should name your plugin "[TG] something", so they are grouped together in plugin list
	name = "[TG] SimpleKnifeFight",
	author = "Raska",
	description = "",
	version = "0.1",
	url = ""
}

Now you will want to add TeamGames include.

#include <sourcemod>
#include <teamgames>

public Plugin:myinfo =
{
	name = "[TG] SimpleKnifeFight",
	author = "Raska",
	description = "",
	version = "0.1",
	url = ""
}

At this point, you need to register your game into TeamGames.

#include <sourcemod>
#include <teamgames>

// Just for better work with game id ;-)
#define GAME_ID	"SimpleKnifeFight"

public Plugin:myinfo =
{
	name = "[TG] SimpleKnifeFight",
	author = "Raska",
	description = "",
	version = "0.1",
	url = ""
}

// We are able to register the game here
public OnLibraryAdded(const String:name[])
{
	if (StrEqual(name, "TeamGames")) {
		// Register your game (register it as both TeamGame and RedOnly game)
		TG_RegGame(GAME_ID, TG_TeamGame | TG_RedOnly);
	}
}

// Remove/UnRegister the game when this plugin unloads
public OnPluginEnd()
{
	TG_RemoveGame(GAME_ID);
}

To let TeamGames know your game's name, just use forward TG_AskModuleName. This forward is called everytime TeamGames needs to know the module (game or menu item) name (e.g. somebody opened TG menu). And of course, everything in TeamGames should be translatable, so use translations, it's really simple ;-)

#include <sourcemod>
#include <teamgames>

#define GAME_ID	"SimpleKnifeFight"

public Plugin:myinfo =
{
	name = "[TG] SimpleKnifeFight",
	author = "Raska",
	description = "",
	version = "0.1",
	url = ""
}

public OnPluginStart()
{
	// Everything in TeamGames should be in translation files
	LoadTranslations("TG.SimpleKnifeFight.phrases");
}

public OnLibraryAdded(const String:name[])
{
	if (StrEqual(name, "TeamGames")) {
		TG_RegGame(GAME_ID, TG_TeamGame | TG_RedOnly);
	}
}

public OnPluginEnd()
{
	TG_RemoveGame(GAME_ID);
}

// TeamGames core ask for game/menu item name (probably somebody opened TG menu)
public TG_AskModuleName(TG_ModuleType:type, const String:id[], client, String:name[], maxSize, &TG_MenuItemStatus:status)
{
	// Check if it's your game
	if (type == TG_Game && StrEqual(id, GAME_ID)) {
		Format(name, maxSize, "%T", "GameName", client);
	}
}

The next step is the use of forward TG_OnMenuSelected. Here we can "catch" when somebody chose our game in TG menu, so we can start the game or open new menu for game settings.

#include <sourcemod>
#include <teamgames>

#define GAME_ID	"SimpleKnifeFight"

public Plugin:myinfo =
{
	name = "[TG] SimpleKnifeFight",
	author = "Raska",
	description = "",
	version = "0.1",
	url = ""
}

public OnPluginStart()
{
	LoadTranslations("TG.SimpleKnifeFight.phrases");
}

public OnLibraryAdded(const String:name[])
{
	if (StrEqual(name, "TeamGames")) {
		TG_RegGame(GAME_ID, TG_TeamGame | TG_RedOnly);
	}
}

public OnPluginEnd()
{
	TG_RemoveGame(GAME_ID);
}

public TG_AskModuleName(TG_ModuleType:type, const String:id[], client, String:name[], maxSize, &TG_MenuItemStatus:status)
{
	if (type == TG_Game && StrEqual(id, GAME_ID)) {
		Format(name, maxSize, "%T", "GameName", client);
	}
}

// Somebody selected game/menu item in TG menu
public TG_OnMenuSelected(TG_ModuleType:type, const String:id[], TG_GameType:gameType, client)
{
	// Check if it's your game
	if (type == TG_Game && StrEqual(id, GAME_ID)) {

		// Start your game
			// This opens game start menu, so player can start or reject the game (don't do game preparation here).
			// Also the forward TG_OnGamePrepare is called when the start menu is confirmed
		TG_StartGame(client, GAME_ID, gameType);
	}
}

As the last step, to finish our game, we need to use forward TG_OnGamePrepare and prepare players for the game.

#include <sourcemod>
#include <teamgames>

#define GAME_ID	"SimpleKnifeFight"

public Plugin:myinfo =
{
	name = "[TG] SimpleKnifeFight",
	author = "Raska",
	description = "",
	version = "0.1",
	url = ""
}

public OnPluginStart()
{
	LoadTranslations("TG.SimpleKnifeFight.phrases");
}

public OnLibraryAdded(const String:name[])
{
	if (StrEqual(name, "TeamGames")) {
		TG_RegGame(GAME_ID, TG_TeamGame | TG_RedOnly);
	}
}

public OnPluginEnd()
{
	TG_RemoveGame(GAME_ID);
}

public TG_AskModuleName(TG_ModuleType:type, const String:id[], client, String:name[], maxSize, &TG_MenuItemStatus:status)
{
	if (type == TG_Game && StrEqual(id, GAME_ID)) {
		Format(name, maxSize, "%T", "GameName", client);
	}
}

public TG_OnMenuSelected(TG_ModuleType:type, const String:id[], TG_GameType:gameType, client)
{
	if (type == TG_Game && StrEqual(id, GAME_ID)) {

		TG_StartGame(client, GAME_ID, gameType);
	}
}

// Game preparation started
public TG_OnGamePrepare(const String:id[], TG_GameType:gameType, client, const String:gameSettings[], Handle:dataPack)
{
	// Check if it's your game
	if (!StrEqual(id, GAME_ID))
		return;

	// Prepare players for your game
	for (new i = 1; i <= MaxClients; i++)
	{
		// Skip all non-team players (CTs, non-team Ts and Spectator)
		if (!TG_IsPlayerRedOrBlue(i))
			continue;

		GivePlayerItem(i, "weapon_knife");
		SetEntityHealth(i, 35);
	}
}

Full code here.