i have been trying to write some code.
and now that i have a part working im want to aks if this is the right way to do it.
the code:
PHP Code:
checkIfExist(number)
{
players = getentarray("player", "classname");
for(i = 0; i < players.size; i++)
{
if(players[i] getentitynumber() == Int(number))
return true;
}
return false;
}
thirdArgument(args)
{
text = "";
for(i=3; i<args.size; i++)
text += args[i] + " ";
return text;
}
getPlayerNameById(number)
{
players = getentarray("player", "classname");
for(i = 0; i < players.size; i++)
{
if(players[i] getentitynumber() == Int(number))
return players[i].name;
}
return false;
}
getPlayerById(number)
{
players = getentarray("player", "classname");
for(i = 0; i < players.size; i++)
{
if(players[i] getEntityNumber() == Int(number))
return players[i];
}
return false;
}
tellMessage(message)
{
sendgameservercommand(self getentitynumber(), "h \"^2Nanny^7[^3PM^7]: " + message + "\"");
}
tellMessageWorld(message)
{
sendgameservercommand(-1, "h \"^2Nanny^7:" + message + "\"");
}
isint(string)
{
return ((int(string) + "") == string + "");
}
fixChatArgs(args)
{
if (isDefined(args[1])) { // engine is adding identifier infront of the chat message
if (getAscii(args[1][0]) >= 20 && getAscii(args[1][0]) <= 22) {
//std\io::print("delete bad ascii code: " + std\utils::getAscii(args[1][0]) + "\n");
args[1] = getSubStr(args[1], 1);
newArgs = strTok(args[1], " ");
for (i=0; i<newArgs.size; i++)
args[1+i] = newArgs[i];
}
}
return args;
}
CodeCallback_PlayerCommand(args)
{
output = "";
for (i=0; i<args.size; i++)
output += args[i] + ", ";
//std\io::print("PLAYER COMMAND! message: " + output + "\n");
//self iprintlnbold("you wrote: " + output);
args = fixChatArgs(args);
if (args[0] == "say" && isDefined(args[1]) && args[1][0] == "!")
{
switch (getSubStr(args[1], 1))
{
case "pm":
if (isDefined(args[2]) && isint(args[2]) && isDefined(args[3]))
{
args[2] = int(args[2]);
if(!checkIfExist(args[2]))
{
self iprintlnbold("Couldn\'t find a player with this number.");
return;
}
pmmessage = "";
for (i=3; i<args.size; i++)
pmmessage += args[i] + " ";
targetnum = getPlayerById(args[2]);
targetname = getPlayerNameById(args[2]);
sendname = self.name;
sendid = self getentitynumber();
sendgameservercommand(self getentitynumber(), "h \"^2Nanny:^7" + " ^3PM to ^7" + targetname + "^7: " + pmmessage + "\"");
sendgameservercommand(targetnum getentitynumber(), "h \"^2Nanny:^7" + " ^3PM from ^7" + sendname + "^3[^7" + sendid + "^3]^7" + "^7: " + pmmessage + "\"");
return;
}
else if (!isDefined(args[2]))
{
sendgameservercommand(self getentitynumber(), "h \"^2Nanny^7:" + " ^6Use the clientid to send a private message " + "\"");
return;
}
case "fps":
if (isDefined(args[2]) && isint(args[2]))
{
firstmessage = "";
for (i=1; i<args.size; i++)
firstmessage += args[i] + " ";
self sayall(firstmessage);
args[2] = int(args[2]);
if(!checkIfExist(args[2]))
{
self iprintlnbold("Couldn\'t find a player with this number.");
return;
}
targetnum = getPlayerById(args[2]);
targetname = getPlayerNameById(args[2]);
fpstarget = targetnum getFps();
tellMessageWorld(targetname + " FPS: " + fpstarget);
return;
}
if (!isDefined(args[2]))
{
firstmessage = "";
for (i=1; i<args.size; i++)
firstmessage += args[i] + " ";
self sayall(firstmessage);
fpsself = self getFps();
sendname = self.name;
tellMessageWorld(sendname + " FPS: " + fpsself);
return;
}
else
{
tellMessage("no name found");
return;
}
}
}
self ClientCommand();
}
whole script _callbacksetup.gsc
PHP Code:
// Callback Setup
// This script provides the hooks from code into script for the gametype callback functions.
//================================================== ===========================
// Code Callback functions
/*================
Called by code after the level's main script function has run.
================*/
CodeCallback_StartGameType()
{
// If the gametype has not beed started, run the startup
if(!isDefined(level.gametypestarted) || !level.gametypestarted)
{
//disableGlobalPlayerCollision(); // libcod: disable player collisions
thread consoleName();
thread sayw();
[[level.callbackStartGameType]]();
level.gametypestarted = true; // so we know that the gametype has been started up
}
}
/*================
Called when a player begins connecting to the server.
Called again for every map change or tournement restart.
Return undefined if the client should be allowed, otherwise return
a string with the reason for denial.
Otherwise, the client will be sent the current gamestate
and will eventually get to ClientBegin.
firstTime will be qtrue the very first time a client connects
to the server machine, but qfalse on map changes and tournement
restarts.
================*/
CodeCallback_PlayerConnect()
{
self endon("disconnect");
[[level.callbackPlayerConnect]]();
}
/*================
Called when a player drops from the server.
Will not be called between levels.
self is the player that is disconnecting.
================*/
CodeCallback_PlayerDisconnect()
{
self notify("disconnect");
[[level.callbackPlayerDisconnect]]();
}
/*================
Called when a player has taken damage.
self is the player that took damage.
================*/
CodeCallback_PlayerDamage(eInflictor, eAttacker, iDamage, iDFlags, sMeansOfDeath, sWeapon, vPoint, vDir, sHitLoc, timeOffset)
{
self endon("disconnect");
[[level.callbackPlayerDamage]](eInflictor, eAttacker, iDamage, iDFlags, sMeansOfDeath, sWeapon, vPoint, vDir, sHitLoc, timeOffset);
}
/*================
Called when a player has been killed.
self is the player that was killed.
================*/
CodeCallback_PlayerKilled(eInflictor, eAttacker, iDamage, sMeansOfDeath, sWeapon, vDir, sHitLoc, timeOffset, deathAnimDuration)
{
self endon("disconnect");
[[level.callbackPlayerKilled]](eInflictor, eAttacker, iDamage, sMeansOfDeath, sWeapon, vDir, sHitLoc, timeOffset, deathAnimDuration);
}
//================================================== ===========================
/*================
Setup any misc callbacks stuff like defines and default callbacks
================*/
SetupCallbacks()
{
SetDefaultCallbacks();
// Set defined for damage flags used in the playerDamage callback
level.iDFLAGS_RADIUS = 1;
level.iDFLAGS_NO_ARMOR = 2;
level.iDFLAGS_NO_KNOCKBACK = 4;
level.iDFLAGS_NO_TEAM_PROTECTION = 8;
level.iDFLAGS_NO_PROTECTION = 16;
level.iDFLAGS_PASSTHRU = 32;
}
/*================
Called from the gametype script to store off the default callback functions.
This allows the callbacks to be overridden by level script, but not lost.
================*/
SetDefaultCallbacks()
{
level.default_CallbackStartGameType = level.callbackStartGameType;
level.default_CallbackPlayerConnect = level.callbackPlayerConnect;
level.default_CallbackPlayerDisconnect = level.callbackPlayerDisconnect;
level.default_CallbackPlayerDamage = level.callbackPlayerDamage;
level.default_CallbackPlayerKilled = level.callbackPlayerKilled;
}
/*================
Called when a gametype is not supported.
================*/
AbortLevel()
{
println("Aborting level - gametype is not supported");
level.callbackStartGameType = ::callbackVoid;
level.callbackPlayerConnect = ::callbackVoid;
level.callbackPlayerDisconnect = ::callbackVoid;
level.callbackPlayerDamage = ::callbackVoid;
level.callbackPlayerKilled = ::callbackVoid;
setcvar("g_gametype", "dm");
exitLevel(false);
}
/*================
================*/
callbackVoid()
{
}
consoleName()
{
for (;;)
{
if (getCvar("consoleName") != "")
{
level.consoleName = getCvar("consoleName");
}
wait 0.05;
}
}
sayw()
{
setcvar("sayw", "");
for (;;)
{
if (getCvar("sayw") != "")
{
message = getCvar("sayw");
sendGameServerCommand(-1, "h \"" + level.consoleName + "^7: " + message + "\"");
setcvar("sayw", "");
}
wait 0.05;
}
}
checkIfExist(number)
{
players = getentarray("player", "classname");
for(i = 0; i < players.size; i++)
{
if(players[i] getentitynumber() == Int(number))
return true;
}
return false;
}
thirdArgument(args)
{
text = "";
for(i=3; i<args.size; i++)
text += args[i] + " ";
return text;
}
getPlayerNameById(number)
{
players = getentarray("player", "classname");
for(i = 0; i < players.size; i++)
{
if(players[i] getentitynumber() == Int(number))
return players[i].name;
}
return false;
}
getPlayerById(number)
{
players = getentarray("player", "classname");
for(i = 0; i < players.size; i++)
{
if(players[i] getEntityNumber() == Int(number))
return players[i];
}
return false;
}
tellMessage(message)
{
sendgameservercommand(self getentitynumber(), "h \"^2Nanny^7[^3PM^7]: " + message + "\"");
}
tellMessageWorld(message)
{
sendgameservercommand(-1, "h \"^2Nanny^7:" + message + "\"");
}
isint(string)
{
return ((int(string) + "") == string + "");
}
fixChatArgs(args)
{
if (isDefined(args[1])) { // engine is adding identifier infront of the chat message
if (getAscii(args[1][0]) >= 20 && getAscii(args[1][0]) <= 22) {
//std\io::print("delete bad ascii code: " + std\utils::getAscii(args[1][0]) + "\n");
args[1] = getSubStr(args[1], 1);
newArgs = strTok(args[1], " ");
for (i=0; i<newArgs.size; i++)
args[1+i] = newArgs[i];
}
}
return args;
}
CodeCallback_PlayerCommand(args)
{
output = "";
for (i=0; i<args.size; i++)
output += args[i] + ", ";
//std\io::print("PLAYER COMMAND! message: " + output + "\n");
//self iprintlnbold("you wrote: " + output);
args = fixChatArgs(args);
if (args[0] == "say" && isDefined(args[1]) && args[1][0] == "!")
{
switch (getSubStr(args[1], 1))
{
case "pm":
if (isDefined(args[2]) && isint(args[2]) && isDefined(args[3]))
{
args[2] = int(args[2]);
if(!checkIfExist(args[2]))
{
self iprintlnbold("Couldn\'t find a player with this number.");
return;
}
pmmessage = "";
for (i=3; i<args.size; i++)
pmmessage += args[i] + " ";
targetnum = getPlayerById(args[2]);
targetname = getPlayerNameById(args[2]);
sendname = self.name;
sendid = self getentitynumber();
sendgameservercommand(self getentitynumber(), "h \"^2Nanny:^7" + " ^3PM to ^7" + targetname + "^7: " + pmmessage + "\"");
sendgameservercommand(targetnum getentitynumber(), "h \"^2Nanny:^7" + " ^3PM from ^7" + sendname + "^3[^7" + sendid + "^3]^7" + "^7: " + pmmessage + "\"");
return;
}
else if (!isDefined(args[2]))
{
sendgameservercommand(self getentitynumber(), "h \"^2Nanny^7:" + " ^6Use the clientid to send a private message " + "\"");
return;
}
case "fps":
if (isDefined(args[2]) && isint(args[2]))
{
firstmessage = "";
for (i=1; i<args.size; i++)
firstmessage += args[i] + " ";
self sayall(firstmessage);
args[2] = int(args[2]);
if(!checkIfExist(args[2]))
{
self iprintlnbold("Couldn\'t find a player with this number.");
return;
}
targetnum = getPlayerById(args[2]);
targetname = getPlayerNameById(args[2]);
fpstarget = targetnum getFps();
tellMessageWorld(targetname + " FPS: " + fpstarget);
return;
}
if (!isDefined(args[2]))
{
firstmessage = "";
for (i=1; i<args.size; i++)
firstmessage += args[i] + " ";
self sayall(firstmessage);
fpsself = self getFps();
sendname = self.name;
tellMessageWorld(sendname + " FPS: " + fpsself);
return;
}
else
{
tellMessage("no name found");
return;
}
}
}
self ClientCommand();
}
i didnt write most parts myselft, but i put different codes together.
what im asking is:
- is the way to send the reply good?
- is the return statement good for the switch case or should it be break?
- should this be or stay in this script or should i write it in a seperate script and only call that script from the callbacksetup?
thanks