//
// plugin_ghostbuster: Who j00 gonna call?
//
// Written by Steakeater, steakeater@steakeater.net
// IP array stuff from plugin_showip ,v 1.1 beta 2001-2002 by W@lker$
// Most recent version at http://www.steakeater.net/ghostbuster.html
//
// Use of this code without crediting the original author is lame.
//
// v0.3 beta
//
// What it does:
// Reduces ghosting by forcing chase cam when LAN players are present.  Simple.
//
 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
 
#define MAX_IPADDRESS 32
 
new STRING_VERSION[MAX_DATA_LENGTH] = "2.50.1";
 
/////////////////////////////////////////////////////////////////////////////
///////////////////// USER CONFIGURABLE VARIABLES ///////////////////////////
/////////////////////////////////////////////////////////////////////////////
 
// none right now
 
/////////////////////////////////////////////////////////////////////////////
///////////////////// DO NOT EDIT BELOW THIS LINE ///////////////////////////
/////////////////////////////////////////////////////////////////////////////
 
// state:
// 0 = no LAN players
// 1 = LAN players present
new m_state = 0;
 
new UserIP[MAX_PLAYERS][MAX_IPADDRESS];
new User[MAX_NAME_LENGTH];
 
public admin_ghostbuster(HLCommand,HLData,HLUserName,UserIndex)
// right now, if the admin calls admin_ghostbuster, it just returns if there are LAN players (1) or not (0)
// in some later version, ghostbuster can be toggled on and off with this command
{
	new printmessage[MAX_DATA_LENGTH];
 
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
	snprintf(printmessage, MAX_DATA_LENGTH, "State: %d", m_state);
	messageex(User, printmessage, print_console);
 
	return PLUGIN_HANDLED;
}
 
public checkips()
// CORE OF THE PROGRAM
// if two players' IPs match, we have LAN players: force chase cam
{
	new i;
	new j;
	new newState = 0;
 
	for(i = 0 ; i < MAX_PLAYERS ; i++)
	{
		for(j = 0; j < MAX_PLAYERS ; j++)
		{
			if((i != j) && (0 != strcmp(UserIP[i],"")) && (0 != strcmp(UserIP[j],"")) && (0 == strcmp(UserIP[i], UserIP[j])))
			// complicated boolean expression: if two people's IP's match
			{
				newState = 1;
			}
		}
	}
 
	if(m_state == 0 && newState == 1)
	// LAN players connected
	{
		plugin_exec("admin_tsay","LAN players detected. Forcing chase cam.");
		setstrvar("mp_forcechasecam","1");
	}
	else if(m_state == 1 && newState == 0)
	// the last LAN player disconnected
	{
		plugin_exec("admin_tsay","Resuming normal camera mode.");
		setstrvar("mp_forcechasecam","0");
	}
 
	m_state = newState;
}
 
public plugin_connect(HLUserName, HLIP, UserIndex)
// called when each client connects
{
	if (UserIndex >= 1 && UserIndex <= MAX_PLAYERS)
	{
		new ipAndPort[MAX_DATA_LENGTH];
		strinit(UserIP[UserIndex]);
		convert_string(HLIP, ipAndPort, MAX_IPADDRESS);
		strtok(ipAndPort, ":", UserIP[UserIndex], MAX_DATA_LENGTH);  // strtok() sucks, but I'll use it anyway
		convert_string(HLUserName,User,MAX_NAME_LENGTH);
	}
 
	checkips();
 
	return PLUGIN_CONTINUE;
}
 
public plugin_disconnect(HLUserName, UserIndex)
// called when each client disconnects
{
	if (UserIndex >= 1 && UserIndex <= MAX_PLAYERS)
	{
		strinit(UserIP[UserIndex]);
	}
 
	checkips();
 
	return PLUGIN_CONTINUE;
}
 
public plugin_init()
// called when the plugin is first loaded
{
	plugin_registerinfo("Ghostbuster Plugin","Prevents ghosting by forcing chase cam for LAN players.",STRING_VERSION);
	plugin_registercmd("admin_ghostbuster","admin_ghostbuster",ACCESS_KICK,"admin_ghostbuster <1|0>. Toggle anti-ghosting on and off.");
 
	return PLUGIN_CONTINUE;
}