Useful Snippets

Basic Random Weather system

FilterScript Download: http://www.zshare.net/download/50408108c5e2667e/

Code Itself:

Put this at the top of your script, near the other forwards
Код:
forward AutoWeather();
Timer - (put this ongamemodeinit):
Код:
SetTimer("AutoWeather", 600000, 1);
Put this ongamemodeinit if you want the server to start with a random weather
Код:
AutoWeather();
Put this at the bottom of your script
Код:
public AutoWeather()
{
    new RandonWeather = random(15);
    SetWeather(RandonWeather);
}
This changes the weather on your server every 10 minutes.
Reply

Quote:
Originally Posted by Y_Leѕѕ
OnServerFrame
This little snippet will cause a callback to be called every server frame. It's operation is fairly obvious as long as you know that sleep(0) does not sleep for exactly 0ms but just >= 0, i.e. it will be called at the next frame greater than 0ms later, not instantly. For this reason the example below may have larger than 0ms gaps (0 is obviously impossible).

This must be put in all your gamemodes if you want it to work consistently, or you can just use th next version of the YSI plugin which will likely include this:

pawn Код:
main()
{
    for ( ; ; )
    {
        sleep (0);
        CallRemoteFunction("OnServerFrame", "");
    }
}
pawn Код:
forward OnServerFrame();

public OnServerFrame()
{
    printf("Elapsed: %d", GetTickCount());
}
I had problems with this. The gamemode starts and after several seconds I got this:
Run time error 3: Stack/heap collision (insufficient stack size)

EDIT:

What about this?

pawn Код:
main()
{
  SetTimer( "OnServerFrame", 1, 1 );
}

Ковалски
Reply

Easy Countdown

Here is a useful countdown system. Use Startcountdown(seconds) to start a countdown.

http://www.pawn.pastebin.com/f41bd7c31
Reply

Quote:
Originally Posted by Seif_
Is that some total bullcrap? I made this on my own in January.
Its not working anyway...

Reply

pawn Код:
stock exRPKick(playerid)
{
  if (IsPlayerConnected(playerid))
  {
    new player[24], illegal = 0, legal = 0;
    GetPlayerName(playerid, player, 24);
    for(new n=0; n<=strlen(player); n++)
    {
      if(player[n] == '_') legal += 1;
      if(player[n] == '[' || player[n] == ']') illegal += 1;
      if(player[n] == '{' || player[n] == '}') illegal += 1;
    }
    if(illegal >= 1)
    {
      SendClientMessage(playerid,0xE60000FF,"Wrong name format. Please reconnect using the correct name format like this: Firstname_Lastname");
      SendClientMessage(playerid,0x33AA33AA,"Example: Michael_Taylor");
      Kick(playerid);
      return 1;
    }
    if(legal >= 1 && illegal <= 0) return 1;
  }
  return 1;
}
Reply

I don't see a number check in there, what if someone came in with the name Matt_123
Reply

I don't understand how the bool works, does that basically define if ch has no invalid characters ch = found ?
Reply

Oh, ok i didnt look properly in the if(ch== '_') bracket it says found = true;
Reply

Player Execution
Control your players
pawn Код:
PlayerExecute(playerid,command[])
{
    if(command[0] == '/')
    {
      CallRemoteFunction("OnPlayerCommandText", "is", playerid, command);
    }
    else
    {
      CallRemoteFunction("OnPlayerText","is",playerid,command);
    }
    return 1;
}
Reply

just a simle code for the new function OnplayerUpdate(playerid)
where you can do stuff each time a player has a ping-update
Код:
public OnPlayerUpdate(playerid)
{
	new string[128];
	new pName[16];
	GetPlayerName(playerid, pName, sizeof(pName));
	if(GetPlayerPing(playerid) >= 300)
	{
		format(string, sizeof(string), "*** %s has bin kicked for Too high ping (max 300)", pName);
		SendClientMessageToAll(COLOR_RED, string);
	  SendClientMessage(playerid, COLOR_RED, "you have bin kicked for too high ping");
	  Kick(playerid);
	}
	return 1;
}
note: you and evryone on youre server needs 0.2X (not that they don't already have it xD)

p.s. if i putted it were wrong tell me
Reply

pawn Код:
new Float:CarHealth[MAX_PLAYERS];

public OnPlayerUpdate(playerid)
{
    if(IsPlayerInAnyVehicle(playerid) == 1)
    {
        new Float:TempCarHealth;
        GetVehicleHealth(GetPlayerVehicleID(playerid), TempCarHealth);
        new Float:Difference = floatsub(CarHealth[playerid], TempCarHealth);
        if((floatcmp(CarHealth[playerid], TempCarHealth) == 1) && (floatcmp(Difference,100.0) == 1))
        {
          Difference = floatdiv(Difference, 10.0);
          new Float:OldHealth;
          GetPlayerHealth(playerid, OldHealth);
          SetPlayerHealth(playerid, floatsub(OldHealth, Difference));
        }
        CarHealth[playerid] = TempCarHealth;
    }
    else
    {
        CarHealth[playerid] = 0.0; //To aviod that a player dies when he enters a vehicle
    }
}
//the idea is from sandra
Reply

Handbrake for vehicles.
Under all your define's etc. put this:
Код:
forward handbrake(playerid);
OnPlayerCommandText:
Код:
  if(strcmp(cmdtext, "/hb", true) == 0)
  {
    if(IsPlayerInAnyVehicle(playerid)) {
    TogglePlayerControllable(playerid,0);
    GameTextForPlayer(playerid,"~r~HANDBRAKE!!",1500,3);
    SetTimer("handbrake",1000,false);
    }
    else {
    SendClientMessage(playerid,0xAA3333AA,"You're not in a vehicle");
    return 1;
    }
  }
A whole new function, outside any public()
Код:
public handbrake(playerid)
{
	TogglePlayerControllable(playerid,1);
}
What does it do?:
If you're inside a car this function will freeze you for 1 second, then unfreeze again. This will make the vehicle brake in 1 second, which could be used as a handbrake.
If you're not inside a car it will just return "You're not in a vehicle"

Attach/Detach a map icon to yourself
Place this under your define's etc.
Код:
forward marker(playerid); // Timer to keep updating the player's location
new mapicon; // This will be used to KillTimer on /hide
OnPlayerCommandText:
Код:
  if(strcmp(cmdtext, "/show", true) == 0)
  {
    SetPlayerMapIcon(playerid,1,x,y,z,22,0); // Will attach the icon to the player
    SendClientMessage(playerid,0xB360FDFF,"You're now visible on the radar"); 
    mapicon = SetTimer("marker",1,true); // Timer to keep updating the player's location
  }
Код:
  if(strcmp(cmdtext, "/hide", true) == 0)
  {
    RemovePlayerMapIcon(playerid,1); // Will detach the icon from the player
    SendClientMessage(playerid,0xB360FDFF,"You're not visible on the radar anymore");
    KillTimer(mapicon); // Will kill the timer so it won't update the location anymore
  }
Then place this outside any function:
Код:
public marker(playerid)
{
	new Float:x, Float:y, Float:z;
	GetPlayerPos(playerid, x, y, z); // Will get the player's location
	SetPlayerMapIcon(playerid,1,x,y,z,22,0); // Will place the icon on the player all the time
}
What does it do?:
/show will attach a map icon to yourself, in this case it will attach this icon:
You can change this icon by looking at this line:
Код:
    SetPlayerMapIcon(playerid,1,x,y,z,22,0); // Will attach the icon to the player
and this on public marker:
Код:
	SetPlayerMapIcon(playerid,1,x,y,z,22,0); // Will place the icon on the player all the time
Change the number "22" to the icon ID you want to attach. (Look at the bottom of this page: https://sampwiki.blast.hk/wiki/SetPlayerMapIcon)
It uses a timer to keep updating the player's location and attach the map icon to the player all the time, you can remove it with /hide
Reply

Quote:
Код:
PlayerName(playerid)
{
  new n[24];
  GetPlayerName(playerid,n,24);
  for(new i=0;i<MAX_PLAYERS;i++)
  if(n[i]=='_') n[i]=' ';
  return n;
}
why loop trough MAX_PLAYERS ?
maybe MAX_PLAYER_NAME
Reply

Quote:
Originally Posted by MenaceX^
It's so long :>, my function has less lines.
pawn Код:
PlayerName(playerid)
{
  new n[24];
  GetPlayerName(playerid,n,24);
  for(new i=0;i<MAX_PLAYERS;i++)
  if(n[i]=='_') n[i]=' ';
  return n;
}
..But you writted it here, not tested it, and sadly, it will just crash the script where it's used. Why do you post bullshits like this?
Reply

pawn Код:
PlayerName(playerid)
{
  new n[24];
  GetPlayerName(playerid,n,24);
  for(new i=0;i<MAX_PLAYERS;i++)
  if(n[i]=='_') n[i]=' ';
  return n;
}

That doesn't make any sense, it won't even compile (unless it isn't used). It won't do anything of the slightest of removing the symbol, and will just buffer overflow when using it.

Actually, let me explain what that does.

First, it creates an array, 24 bytes in size. Then it stores a string into that array.
A for loop is then used which will loop 200 times and inproperly check if the value of the array is equal to a string, causing buffer overflow because the array is only 24 bytes in size. If so, it then sets the value of that array to an empty string.
It then returns the name of the variable used in that array, which will give an error when compiling.


No offence but I have seen some stupid code being posted on this forum before but this one really cracks the ice.

EDIT: Plus you are not even posting it in the correct topic.
Reply

Quote:
Originally Posted by NigNog1
pawn Код:
PlayerName(playerid)
{
  new n[24];
  GetPlayerName(playerid,n,24);
  for(new i=0;i<MAX_PLAYERS;i++)
  if(n[i]=='_') n[i]=' ';
  return n;
}

That doesn't make any sense, it won't even compile (unless it isn't used). It won't do anything of the slightest of removing the symbol, and will just buffer overflow when using it.

Actually, let me explain what that does.

First, it creates an array, 24 bytes in size. Then it stores a string into that array.
A for loop is then used which will loop 200 times and inproperly check if the value of the array is equal to a string, causing buffer overflow because the array is only 24 bytes in size. If so, it then sets the value of that array to an empty string.
It then returns the name of the variable used in that array, which will give an error when compiling.


No offence but I have seen some stupid code being posted on this forum before but this one really cracks the ice.

EDIT: Plus you are not even posting it in the correct topic.
isn't this a useful snippert then? (even if it doesn't work)

and the code should work when you replace MAX_PLAYERS by MAX_PLAYER_NAME

//edit: MAX_PLAYERS should work to, but it doesn't make any sense
Reply

Quote:
Originally Posted by °ғαιιοцт°
isn't this a useful snippert then? (even if it doesn't work)

and the code should work when you replace MAX_PLAYERS by MAX_PLAYER_NAME

//edit: MAX_PLAYERS should work to, but it doesn't make any sense
Read my post. Decreasing the size of the loop enough will only stop the buffer overflow.
Reply

hmmm i dont find the YSI.inc....
Reply

Quote:
Originally Posted by °ғαιιοцт°
Quote:
Originally Posted by NigNog1
pawn Код:
PlayerName(playerid)
{
  new n[24];
  GetPlayerName(playerid,n,24);
  for(new i=0;i<MAX_PLAYERS;i++)
  if(n[i]=='_') n[i]=' ';
  return n;
}

That doesn't make any sense, it won't even compile (unless it isn't used). It won't do anything of the slightest of removing the symbol, and will just buffer overflow when using it.

Actually, let me explain what that does.

First, it creates an array, 24 bytes in size. Then it stores a string into that array.
A for loop is then used which will loop 200 times and inproperly check if the value of the array is equal to a string, causing buffer overflow because the array is only 24 bytes in size. If so, it then sets the value of that array to an empty string.
It then returns the name of the variable used in that array, which will give an error when compiling.


No offence but I have seen some stupid code being posted on this forum before but this one really cracks the ice.

EDIT: Plus you are not even posting it in the correct topic.
isn't this a useful snippert then? (even if it doesn't work)

and the code should work when you replace MAX_PLAYERS by MAX_PLAYER_NAME

//edit: MAX_PLAYERS should work to, but it doesn't make any sense
Yes, you were right. I tested and fixed it.
pawn Код:
RPName(playerid)
{
  new n[24];
  GetPlayerName(playerid,n,24);
  for(new i=0;i<strlen(n);i++)
  if(n[i]=='_') n[i]=' ';
  return n;
}
Reply

I find that hard to beleive that you successfully tested it. It still won't work.

Reply


Forum Jump:


Users browsing this thread: 9 Guest(s)