Saturday, April 23, 2011

SAMP Scripting Tutorial: Slap N' Slay

Here is the code

#include <a_samp>

#define RED 0xD20000FF
#define WHITE 0xFFFFFFFF

public OnFilterScriptInit()
{
 print("\n--------------------------------------");
 print(" Slap N' Slay by TheDarkJoker94");
 print("--------------------------------------\n");
 return 1;
}

// The strtok function
strtok(const string[], &index)
{
 new length = strlen(string);
 while ((index < length) && (string[index] <= ' '))
 {
  index++;
 }

 new offset = index;
 new result[20];
 while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
 {
  result[index - offset] = string[index];
  index++;
 }
 result[index - offset] = EOS;
 return result;
}

// The Slap function
Slap(victimid, victimhp, adminid)
{
 new Float: health;
 GetPlayerHealth(victimid, health); // Get victim's health
 health = health - victimhp; // Calculate new health (victimhp is the ammount of HP you want to take from the victim)
 SetPlayerHealth(victimid, health);
 new name[64], output[128];
 GetPlayerName(adminid, name, 64); // Get slapper's name
 format(output, 128, "You have been slapped by %s! [-%d]", name, victimhp);
 SendClientMessage(victimid, RED, output); // Send the message to the victim
 new Float:x,Float:y,Float:z; 
 GetPlayerPos(victimid, x,y,z); // Get victim's position
 PlayerPlaySound(victimid, 1130, x, y, z); // Play the sound at that position
 SetPlayerVelocity(victimid, 0.0, 0.0, 0.2); // A 0.2 velocity in the Z direction will make the player to jump
}

public OnPlayerCommandText(playerid, cmdtext[])
{
 new cmd[128], tmp[28], vicid, vichp, idx;
 cmd = strtok(cmdtext, idx);
 if (strcmp("/slap", cmd, true) == 0)
 {
  tmp = strtok(cmdtext, idx);
  if(strlen(tmp) == 0) return SendClientMessage(playerid, WHITE, "USAGE: /slap [VictimID] [AmmountHP]");
  vicid = strval(tmp);
        tmp = strtok(cmdtext, idx);
        if(strlen(tmp) == 0) return SendClientMessage(playerid, WHITE, "USAGE: /slap [VictimID] [AmmountHP]");
        vichp = strval(tmp);
        Slap(vicid, vichp, playerid);
  return 1;
 }
 else if (strcmp("/slay", cmd, true) == 0)
 {
     tmp = strtok(cmdtext, idx);
  if(strlen(tmp) == 0) return SendClientMessage(playerid, WHITE, "USAGE: /slay [VictimID]");
  vicid = strval(tmp);
  SetPlayerHealth(vicid, 0.0);
  new name[64], output[128];
  GetPlayerName(playerid, name, 64);
  format(output, 128, "You have been slayed by %s!", name);
  SendClientMessage(vicid, RED, output);
  return 1;
 }
 return 0;
}

No comments:

Post a Comment