Raised This Month: $ Target: $400
 0% 

Solved [TF2] Convert Source Python plugins into a Sourcemod plugin


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Lazyneer
Junior Member
Join Date: Sep 2016
Old 09-30-2016 , 12:11   [TF2] Convert Source Python plugins into a Sourcemod plugin
Reply With Quote #1

A friend of mine made 2 Source Python plugins which are critical for the custom gamemode I'm hosting on my server. But because Source Python is annoying to install, I can't make more servers with those 2 plugins. So I would like a Sourcemod plugin of them.

The first plugin switches players on team RED to BLU when they die after the server fired the command "switch_death_on". When the server fires the command "switch_death_off", it stops switching players to team BLU. I would like to keep the commands.
Spoiler

The second plugin ends the round when there are no more players in team RED.
Spoiler

Last edited by Lazyneer; 09-30-2016 at 15:08.
Lazyneer is offline
nosoop
Veteran Member
Join Date: Aug 2014
Old 09-30-2016 , 12:40   Re: [TF2] Convert Source Python plugins into a Sourcemod plugin
Reply With Quote #2

Here's more-or-less a direct port from what I could see. Hopefully it works.

Code:
#include <sourcemod>
#include <tf2_stocks>

#pragma semicolon 1
#pragma newdecls required

public Plugin myinfo = {
    name        = "Omniwolf's Teamswitch Plugin",
    author      = "Omniwolf",
    description = "Switches RED Survivors to BLU",
    version     = "1.0",
    url         = ""
};

int g_bHooked;

public void OnPluginStart() {
    RegServerCmd("switch_death_on", OnSwitchEnable);
    RegServerCmd("switch_death_off", OnSwitchDisable);
}

public Action OnSwitchEnable(int argc) {
    if (!g_bHooked) {
        HookEvent("player_death", OnPlayerDeath, EventHookMode_Post);
        g_bHooked = true;
    } else {
        PrintToServer("Already on.");
    }
}

public Action OnSwitchDisable(int argc) {
    if (g_bHooked) {
        UnhookEvent("player_death", OnPlayerDeath, EventHookMode_Post);
        g_bHooked = false;
    } else {
        PrintToServer("Already off.");
    }
}

public void OnPlayerDeath(Event event, const char[] name, bool dontBroadcast) {
    int client = GetClientOfUserId(event.GetInt("userid"));
    if (client && TF2_GetClientTeam(client) == TFTeam_Red) {
        TF2_ChangeClientTeam(client, TFTeam_Blue);
    }
}
Code:
#include <sourcemod>
#include <tf2_stocks>

#pragma semicolon 1
#pragma newdecls required

public Plugin myinfo = {
    name        = "Omniwolf's Auto Restart Zombies",
    author      = "Omniwolf",
    description = "Auto Restarts when RED is empty",
    version     = "1.0",
    url         = ""
};

public void OnPluginStart() {
    HookEvent("player_death", OnPlayerDeath, EventHookMode_PostNoCopy);
    HookEvent("player_team", OnPlayerTeam, EventHookMode_PostNoCopy);
}

public void OnPlayerDeath(Event event, const char[] name, bool dontBroadcast) {
    EmptyRed();
}

public void OnPlayerTeam(Event event, const char[] name, bool dontBroadcast) {
    EmptyRed();
}

void EmptyRed() {
    if (GetTeamClientCount(view_as<int>(TFTeam_Red)) == 0) {
        CreateTimer(1.0, OnRedEmpty, _, TIMER_FLAG_NO_MAPCHANGE);
    }
}

public Action OnRedEmpty(Handle timer, any data) {
    ServerCommand("sm_settime 1; sm_hsay Round Over");
    return Plugin_Handled;
}
__________________
I do TF2, TF2 servers, and TF2 plugins.
I don't do DMs over Discord -- PM me on the forums regarding inquiries.
AlliedModders Releases / Github / TF2 Server / Donate (BTC / BCH / coffee)

Last edited by nosoop; 09-30-2016 at 12:47. Reason: made EmptyRed use GetTeamClientCount, fixed event hook calls
nosoop is offline
Lazyneer
Junior Member
Join Date: Sep 2016
Old 09-30-2016 , 13:08   Re: [TF2] Convert Source Python plugins into a Sourcemod plugin
Reply With Quote #3

The team switcher plugin works fine, but the auto restart doesnt work right. It ends the round as soon someone dies, connects or disconnects, whether team RED is empty or not.
Lazyneer is offline
nosoop
Veteran Member
Join Date: Aug 2014
Old 09-30-2016 , 14:34   Re: [TF2] Convert Source Python plugins into a Sourcemod plugin
Reply With Quote #4

Quote:
Originally Posted by Lazyneer View Post
[...] the auto restart doesnt work right. It ends the round as soon someone dies, connects or disconnects, whether team RED is empty or not.
Copied the code after the edits? It earlier had an incorrect test that checks that the team size was greater than zero instead of equal to it, which would explain why the round ends prematurely. Oops.

If that's not it, it sounds like some weird SourcePython-specific quirk (I know some Python, but I have no idea how SourcePython handles things), or maybe the events aren't being handled in the correct order. The hooked player_team event in the Python code suggests the round would end if any player joins any non-RED team including spectate.

Could check if the round is running (see EmptyRed function below); no idea if that'd help any though.

Code:
#include <sourcemod>
#include <tf2_stocks>

#pragma semicolon 1
#pragma newdecls required

public Plugin myinfo = {
    name        = "Omniwolf's Auto Restart Zombies",
    author      = "Omniwolf",
    description = "Auto Restarts when RED is empty",
    version     = "1.0",
    url         = ""
};

public void OnPluginStart() {
    HookEvent("player_death", OnPlayerDeath, EventHookMode_PostNoCopy);
    HookEvent("player_team", OnPlayerTeam, EventHookMode_PostNoCopy);
}

public void OnPlayerDeath(Event event, const char[] name, bool dontBroadcast) {
    EmptyRed();
}

public void OnPlayerTeam(Event event, const char[] name, bool dontBroadcast) {
    EmptyRed();
}

void EmptyRed() {
    if (GameRules_GetRoundState() == RoundState_RoundRunning && GetTeamClientCount(view_as<int>(TFTeam_Red)) == 0) {
        CreateTimer(1.0, OnRedEmpty, _, TIMER_FLAG_NO_MAPCHANGE);
    }
}

public Action OnRedEmpty(Handle timer, any data) {
    ServerCommand("sm_settime 1; sm_hsay Round Over");
    return Plugin_Handled;
}
I'll revisit this a bit later in the day if it remains unfulfilled.
__________________
I do TF2, TF2 servers, and TF2 plugins.
I don't do DMs over Discord -- PM me on the forums regarding inquiries.
AlliedModders Releases / Github / TF2 Server / Donate (BTC / BCH / coffee)
nosoop is offline
Lazyneer
Junior Member
Join Date: Sep 2016
Old 09-30-2016 , 15:08   Re: [TF2] Convert Source Python plugins into a Sourcemod plugin
Reply With Quote #5

The edited version works like I wanted. Thank you.
Lazyneer is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 13:31.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode