Hi there.
Im trying to create a plugin which makes Knife Only Duels at the start of every map.
Winners of the duels become Terrorists - Losers become CTs. (hiders (Ts) and seekers (CTs) in HnS)
If there are even number of players, lets say 6 - it will create 3 duels.
If there are odd number of players, lets say 7, it will still create 3 duels but 1 guy at random will be assigned as CT (loser)
The "Knife only duels round" will start after 15 seconds of warmup has been played (so all players can connect if the map has changed)
After duels and when the actual game starts - it will remove the knifes from the Terrorists team but will not remove the knifes from the CTs.
Since Iam not a good coder, I tried using ChatGPT and here is where we ended up which is not compileable:
Code:
#include <sourcemod>
#include <sdktools>
#include <cstrike>
public void OnPluginStart()
{
RegConsoleCmd("sm_kniferound", Command_KnifeRound);
RegGameEvent("round_start", Event_RoundStart);
}
public Action Command_KnifeRound(int client, const char[] args)
{
// Check if the game has not yet started
if (gamestatus != GAME_COMMENCING) {
PrintToChat(client, "Error: Cannot start knife round while game is in progress.");
return Plugin_Handled;
}
// Get the number of players
int num_players = GetClientCount();
// Check if there are at least 2 players
if (num_players < 2) {
PrintToChat(client, "Error: Cannot start knife round with less than 2 players.");
return Plugin_Handled;
}
// Determine the number of duels to create
int num_duels = num_players / 2;
// If the number of players is odd, remove one player at random to be a CT loser
if (num_players % 2 != 0) {
int loser = RandomInt(1, num_players);
SetEntProp(loser, Prop_Send, "m_iTeamNum", 3); // Set loser to CT team
num_duels = (num_players - 1) / 2;
}
// Start the warmup period
ServerCommand("mp_warmup_start");
// Schedule the knife round to start after 15 seconds
CreateTimer(15.0, KnifeRound_TimerCallback);
// Return Plugin_Handled to indicate success
return Plugin_Handled;
}
public Action KnifeRound_TimerCallback(Handle timer)
{
// Get the number of players
int num_players = GetClientCount();
// Check if there are at least 2 players
if (num_players < 2) {
PrintToChatAll("Error: Cannot start knife round with less than 2 players.");
return Plugin_Handled;
}
// Determine the number of duels to create
int num_duels = num_players / 2;
// If the number of players is odd, remove one player at random to be a CT loser
if (num_players % 2 != 0) {
int loser = RandomInt(1, num_players);
SetEntProp(loser, Prop_Send, "m_iTeamNum", 3); // Set loser to CT team
num_duels = (num_players - 1) / 2;
}
// Shuffle the player order
int player_order[num_players];
for (int i = 0; i < num_players; i++) {
player_order[i] = i + 1;
}
ShuffleArray(player_order, num_players);
// Start each duel
for (int i = 0; i < num_duels; i++) {
int player1 = player_order[i * 2];
int player2 = player_order[i * 2 + 1];
PrintToChatAll("Duel: %s vs %s", GetClientName(player1), GetClientName(player2));
SetEntProp(player1, Prop_Send, "m_iTeamNum", 2); // Set player1 to T team
SetEntProp(player2, Prop_Send, "m_iTeamNum", 2); // Set player2 to T team
}
// Schedule the end of the knife round
CreateTimer(10.0, Timer_KnifeRoundEnd);
return Plugin_Handled;
}
public void Event_RoundStart(Handle event, const char[] name, bool dontBroadcast)
{
if (strcmp(name, "round_start") == 0)
{
int playerCount = GetClientCount();
if (playerCount >= 2)
{
Command_KnifeRound(0, 0);
}
}
}
public void Timer_KnifeRoundEnd(Handle timer)
{
int players[MaxClients+1];
int playerNum = 0;
for (int i = 1; i <= MaxClients; i++)
{
if (!IsClientInGame(i))
continue;
players[playerNum] = i;
playerNum++;
}
int numDuels = playerNum / 2;
int currentDuel = 0;
while (currentDuel < numDuels)
{
int player1 = players[currentDuel*2];
int player2 = players[currentDuel*2+1];
SetClientTeam(player1, Client_Team_Terrorists);
SetClientTeam(player2, Client_Team_Terrorists);
currentDuel++;
}
if (playerNum % 2 == 1)
{
int randomPlayer = players[RandomInt(0, playerNum-1)];
SetClientTeam(randomPlayer, Client_Team_CounterTerrorists);
}
}
I would also be happy with a team knife fight instead of duels.
I would really appreciate some help.
Thank you!