/* This plugin is for making different characters. 
 * Based on admin_llama by Jaguar, and an idea from M00 of MSU POS Server. */
 
/* $Id: plugin_char.sma,v 1.0 2001/11/04 yensid $ */
 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
 
#define ACCESS_CHAR 8192
 
#define FLAG_INVALID 0
#define FLAG_CHAR 4
#define FLAG_IGNORE_NAME 2
 
new UserFlags[MAX_PLAYERS] = {FLAG_INVALID,...};
new UserOldName[MAX_PLAYERS] = {"",...};
new UserChar[MAX_PLAYERS] = {"",...};
new UserSay1[MAX_PLAYERS] = {"",...};
new UserSay2[MAX_PLAYERS] = {"",...};
new UserSay3[MAX_PLAYERS] = {"",...};
 
new STRING_VERSION[MAX_DATA_LENGTH] = "2.50.0";
 
AddUserFlag(UserIndex, Flag) {
	UserFlags[UserIndex] = UserFlags[UserIndex] | Flag;
}
 
RemoveUserFlag(UserIndex, Flag) {
	UserFlags[UserIndex] = UserFlags[UserIndex] & ~Flag;
}
 
public admin_char(HLCommand,HLData,HLUserName,UserIndex) {
	new Command[MAX_COMMAND_LENGTH];
	new Data[MAX_DATA_LENGTH];
	new iIndex;
	new Target[MAX_NAME_LENGTH];
	new Text[MAX_TEXT_LENGTH];
	new User[MAX_NAME_LENGTH];
	new Char[MAX_NAME_LENGTH];
	new Say1[MAX_TEXT_LENGTH];
	new Say2[MAX_TEXT_LENGTH];
	new Say3[MAX_TEXT_LENGTH];
	new TargetName[MAX_NAME_LENGTH];
	new SessionID;
	new WONID;
	new Team;
	new Dead;
 
	convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
	strgsplit(Data, " ", "'", Target, MAX_NAME_LENGTH, Char, MAX_NAME_LENGTH, Say1, MAX_TEXT_LENGTH, Say2, MAX_TEXT_LENGTH, Say3, MAX_TEXT_LENGTH);
	if(check_user(Target)==0) {
		selfmessage( "Unrecognized player: ");
		selfmessage( Target);
		return PLUGIN_HANDLED;
	}
	get_userindex(Target, iIndex);
	playerinfo(iIndex,TargetName,MAX_NAME_LENGTH,SessionID,WONID,Team,Dead);
	if (strlen(Say1)==0) {
		snprintf(Text, MAX_TEXT_LENGTH, "You must specify at least one phrase for %s to say.", TargetName);
		selfmessage(Text);
		return PLUGIN_HANDLED;
	}
	if (Dead==1) {
		snprintf(Text, MAX_TEXT_LENGTH, "%s is currently dead.  Please try again when %s is alive.", TargetName);
		selfmessage(Text);
		return PLUGIN_HANDLED;
	}
	say_command(User,Command,TargetName);
	if(check_immunity(TargetName)!=0) {
		snprintf(Text, MAX_TEXT_LENGTH, "Laf. You can't transform %s, you silly bear.", TargetName);
		say(Text);
	} else {
		AddUserFlag(iIndex, FLAG_CHAR);
		strcpy(UserChar[iIndex],Char,MAX_NAME_LENGTH);
		strcpy(UserOldName[iIndex],TargetName,MAX_NAME_LENGTH);
		strstripquotes(Say1);
		strstripquotes(Say2);
		strstripquotes(Say3);
		strcpy(UserSay1[iIndex],Say1,MAX_TEXT_LENGTH);
		strcpy(UserSay2[iIndex],Say2,MAX_TEXT_LENGTH);
		strcpy(UserSay3[iIndex],Say3,MAX_TEXT_LENGTH);
		snprintf(Text, MAX_TEXT_LENGTH, "name %s", UserChar[iIndex]);
		execclient(TargetName, Text);
		snprintf(Text, MAX_TEXT_LENGTH, "Transforming %s.", TargetName);
		selfmessage(Text);
		snprintf(Text, MAX_TEXT_LENGTH, "%s has been transformed in to a %s.^nEveryone point and laugh!", TargetName, UserChar[iIndex]);
		centersay(Text,12,0,255,0);
		execclient(TargetName, "say I love yensid");
	}
	return PLUGIN_HANDLED;
}
 
public admin_unchar(HLCommand,HLData,HLUserName,UserIndex) {
	new Command[MAX_COMMAND_LENGTH];
	new Data[MAX_DATA_LENGTH];
	new iIndex;
	new TargetName[MAX_NAME_LENGTH];
	new Text[MAX_TEXT_LENGTH];
	new User[MAX_NAME_LENGTH];
	new SessionID;
	new WONID;
	new Team;
	new Dead;
 
	convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
	if(check_user(Data)==0) {
		selfmessage("Unrecognized player: ");
		selfmessage(Data);
		return PLUGIN_HANDLED;
	}
	get_userindex(Data, iIndex);
	playerinfo(iIndex,TargetName,MAX_NAME_LENGTH,SessionID,WONID,Team,Dead);
	if (Dead==1) {
		snprintf(Text, MAX_TEXT_LENGTH, "%s is currently dead.  Please try again when %s is alive.", TargetName);
		selfmessage(Text);
		return PLUGIN_HANDLED;
	}
	say_command(User,Command,TargetName);
	/* How can someone with immunity be a char anyway?
	Better safe than sorry, though. */
	if(check_immunity(TargetName)!=0) {
		snprintf(Text, MAX_TEXT_LENGTH, "Laf. You can't transform %s back from being a character, you silly bear.", TargetName);
		say(Text);
	} else {
		RemoveUserFlag(iIndex, FLAG_CHAR);
		strcpy(UserSay1[iIndex], "", MAX_TEXT_LENGTH);
		strcpy(UserSay2[iIndex], "", MAX_TEXT_LENGTH);
		strcpy(UserSay3[iIndex], "", MAX_TEXT_LENGTH);
		if (strlen(UserOldName[iIndex])!=0) {
			snprintf(Text, MAX_TEXT_LENGTH, "name %s", UserOldName[iIndex]);
			execclient(TargetName, Text);
		} else {
			execclient(TargetName, "name NotACharacter");
		}
		snprintf(Text, MAX_TEXT_LENGTH, "Transforming %s.", TargetName);
		selfmessage(Text);
		snprintf(Text, MAX_TEXT_LENGTH, "The gods are merciful, %s is no longer a %s.", UserOldName[iIndex], UserChar[iIndex]);
		centersay(Text,12,0,255,0);
	}
	return PLUGIN_HANDLED;
}
 
public HandleSay(HLCommand,HLData,HLUserName,UserIndex) {
	new Command[MAX_COMMAND_LENGTH];
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
	new Text[MAX_TEXT_LENGTH];
 
	/* Ignore messages that come from the console */
	if (UserIndex == 0) {
		return PLUGIN_CONTINUE;
	}
 
	/* Check to see if this is a say_team message, and whether or not
	we should be trapping those. If not, leave.*/
	convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
	if (getvar("admin_gag_sayteam")==0) {
		if (streq(Command,"say_team")==1) {
			return PLUGIN_CONTINUE;
		}
	}
 
	/* Check to see if this person is char-fied. */
	strstripquotes(Data);
	if ((UserFlags[UserIndex] & FLAG_CHAR) == FLAG_CHAR) {
		/* They're a character.  Check to see if they're saying something character-ish.
		If they're not, eat this message, make them. */
		/* I tried using this:
 
		if(streq(data,"Ooorgle!")==0 && streq(data,"Bleeeat!")==0 && streq(data,"Brawwrr!")==0) {
 
		but it never worked.  Don't know why.  But this does.  So I use it. */
		new Match = FALSE;						
		if(streq(Data,UserSay1[UserIndex])==1) {
			Match = TRUE;
		} else if (streq(Data,UserSay2[UserIndex])==1) {
			Match = TRUE;
		} else if (streq(Data,UserSay3[UserIndex])==1) {
			Match = TRUE;
		}
 
		if(Match==FALSE) {
			new CharMsg = random(3);
			if (strlen(UserSay3[UserIndex])==0) {
				if (strlen(UserSay2[UserIndex])==0) {
					CharMsg = 0;
				} else {
					CharMsg = random(2);
				}
			}
			if(CharMsg==0) {
				snprintf(Text, MAX_TEXT_LENGTH, "say ^"%s^"", UserSay1[UserIndex]);
				execclient(User, Text);
			} else if (CharMsg==1) {
				snprintf(Text, MAX_TEXT_LENGTH, "say ^"%s^"", UserSay2[UserIndex]);
				execclient(User, Text);
			} else {
				snprintf(Text, MAX_TEXT_LENGTH, "say ^"%s^"", UserSay3[UserIndex]);
				execclient(User, Text);
			}
			return PLUGIN_HANDLED;
		}		
	}
 
	/* Otherwise, let this pass. */
	return PLUGIN_CONTINUE;
}
 
public plugin_connect(HLUserName, HLIP, UserIndex) {
	if (UserIndex >= 1 && UserIndex <= MAX_PLAYERS) {
		UserFlags[UserIndex] = FLAG_INVALID;
		strinit(UserOldName[UserIndex]);
		strinit(UserChar[UserIndex]);
		strinit(UserSay1[UserIndex]);
		strinit(UserSay2[UserIndex]);
		strinit(UserSay3[UserIndex]);
	}
	return PLUGIN_CONTINUE;
}
 
public plugin_disconnect(HLUserName, UserIndex) {
	if (UserIndex >= 1 && UserIndex <= MAX_PLAYERS) {
		UserFlags[UserIndex] = FLAG_INVALID;
		strinit(UserOldName[UserIndex]);
		strinit(UserChar[UserIndex]);
		strinit(UserSay1[UserIndex]);
		strinit(UserSay2[UserIndex]);
		strinit(UserSay3[UserIndex]);
	}
	return PLUGIN_CONTINUE;
}
 
public plugin_info(HLOldName,HLNewName,UserIndex) {
	new Text[MAX_TEXT_LENGTH];
	new NewName[MAX_NAME_LENGTH];
	new OldName[MAX_NAME_LENGTH];
 
	convert_string(HLNewName, NewName, MAX_NAME_LENGTH);
	convert_string(HLOldName, OldName, MAX_NAME_LENGTH);
 
	/* Only bother if the name has changed. */
	if(streq(OldName,NewName)==0) {
		/* Check for char-isation */
		if((UserFlags[UserIndex] & FLAG_CHAR) == FLAG_CHAR) {
			snprintf(Text, MAX_TEXT_LENGTH, "name %s", UserChar[UserIndex]);
			execclient(OldName, Text);
			return PLUGIN_HANDLED;
		}
	}
	return PLUGIN_CONTINUE;
}
 
public plugin_init() {
	plugin_registerinfo("Charcter creator plugin","Turns a player in to a character you make up.",STRING_VERSION);
 
	plugin_registercmd("admin_char","admin_char",ACCESS_CHAR,"admin_char <target> <char> '<say1>' ['<say2>'] ['<say3>']: Character-fy target.  Specify up to three phrases.  Surround multi word phrases with '");
	plugin_registercmd("admin_unchar","admin_unchar",ACCESS_CHAR,"admin_unchar <target>: Uncharacter-fy target.");
	plugin_registercmd("say","HandleSay",ACCESS_ALL);
	plugin_registercmd("say_team","HandleSay",ACCESS_ALL);
 
	return PLUGIN_CONTINUE;	
}