Useful Snippets

Word Scrambler

The code is a bit messy and needs to be tweaked. I hope someone else can improve it.

https://github.com/AliLogic/word-scr.../scrambler.pwn
Reply

Quote:
Originally Posted by Logic_
Посмотреть сообщение
You can alternatively use if (new i = strlen(text); text[i] != EOS; i--) for the loop. Your code is better than Lokii's.
That's wrong strlen gives the EOS index,loop wont even start iterating.
Reply

Quote:
Originally Posted by Logic_
Посмотреть сообщение
Word Scrambler

The code is a bit messy and needs to be tweaked. I hope someone else can improve it.

https://github.com/AliLogic/word-scr.../scrambler.pwn
Two things:
You don't need that much code to scramble a word.Take a look at shuffle array function written by SyS somewhere in
the forum.
Don't make a loop's iteration relay on a random variable's value.
Reply

Quote:
Originally Posted by GhostHacker9
Посмотреть сообщение
Two things:
You don't need that much code to scramble a word.Take a look at shuffle array function written by SyS somewhere in
the forum.
Don't make a loop's iteration relay on a random variable's value.
Point one, I'll take a look on it.
Point two, yeah, I was thinking of using an array keeping track of the letters that are not used yet. Or maybe something else?

Thanks for your suggestion.
Reply

A function that determines the distance between two geographic points based on haversine formula

Code:
PHP код:
stock Float:GeoDistance(Float:lat1, Float:lon1, Float:lat2, Float:lon2)
{
    const 
Float:R = 6371.0087714; // Earth radius
    
new Float:sin1 = floatsin((lat1 - lat2) /2 );
    new 
Float:sin2 = floatsin((lon1 - lon2) /2 );
    return 
2 * R * asin(floatsqroot(sin1 * sin1 + sin2 * sin2 * floatcos(lat1) * floatcos(lat2))) * (3.14 / 180);
} 
  • lat1 - latitude of the first point (in radians)
  • lon1 - longitude of the first point (in radians)
  • lat2 - latitude of the second point (in radians)
  • lon2 - longitude of the second point (in radians)
Example of use:
PHP код:
new Float:lat1 = 52.5243700 * (3.14 / 180);
new 
Float:lon1 = 13.4105300 * (3.14 / 180);
new 
Float:lat2 = 50.4546600 * (3.14 / 180);
new 
Float:lon2 = 30.5238000 * (3.14 / 180);
printf("Latitude 1: %f | Longtitude 1: %f\n\
        Latitude 2: %f | Longtitude 2: %f\n\
        Distance: %f"
, lat1, lon1, lat2, lon2, GeoDistance(lat1, lon1, lat2, lon2)); 
Reply

Realistic Car Crash damage

if you drive at high speed the player will get damage!

PHP код:
#include <a_samp>
Float:GetSpeed(playerid)
{
    new 
Float:x, Float:y, Float:z;
    
GetVehicleVelocity(GetPlayerVehicleID(playerid), x, y, z);
    return 
floatabs(floatsqroot(floatpower(x, 2) + floatpower(y, 2)))*180.5;
}
public 
OnVehicleDamageStatusUpdate(vehicleid, playerid)
{
    new 
Float:hp, Float:dmg;
    
dmg = GetSpeed(playerid);
    for(new 
i = 0, j = GetPlayerPoolSize(); i <= j; i++) 
    {
        if(
IsPlayerNPC(i) || GetPlayerVehicleID(i) != vehicleid) continue;
        
GetPlayerHealth(i, hp);
        
SetPlayerHealth(i, hp-(dmg/3));
    }
    return 
1;
} 
Reply

serverHang Hangs/pauses your server for what ever reason you would want it to.
param 1 = seconds
param 2 = bool


How it can be used:
Код:
If a server would like to release at a specific time, then they can convert seconds 
to mins by adding *3600 at the end their number (multiply by an hour). This will be good
if a server is having a opening beta release and they want their server to open/start at that
exact time, will the server is already running but not fully loaded. Place the function on
public OnGamemodeInit()

For example:

public OnGameModeInit()
{
    printf("Starting hang...");
    hang(1*3600, false); // hang the server for 3600 seconds = 1 hour
}

Output:

log.txt

Starting hang...

/* 3600 seconds later */

----------------------------------
  Bare Script

----------------------------------

Number of vehicle models: 0
Code:
Код:
hang(count, bool:hangDebug)
{
    new hangTick[2];
    hangTick[0] = GetTickCount();

    new hangTime = 0;

    for(;;)
    {
        new hangStamp = tickcount();
        while(tickcount() - hangStamp < 1000) {}
        if(hangTime > count) break;

        hangTick[1] = GetTickCount();

        hangTime++;

        if(hangDebug) printf("hanged for %ims", hangTick[1]-hangTick[0]); 
    }
    
    if(hangDebug) printf("hang stopped at %i seconds", hangTime);
}
Usage:
Код:
hang(4, false);
Output:
Код:
hanged for 998ms
hanged for 1999ms
hanged for 2999ms
hanged for 3999ms
hang stopped at 4 seconds
Reply

Quote:
Originally Posted by brauf
Посмотреть сообщение
...
It will freeze the server and prevent other code running. That's a veryyyyyyy bad idea.
Reply

Quote:
Originally Posted by Dayrion
Посмотреть сообщение
It will freeze the server and prevent other code running. That's a veryyyyyyy bad idea.
That's why I said it's useful if there's a new server and they want to launch on the exact time (e.g 5:00PM), automatically. They run that script, then after it stops "hanging" the server will fully load, allowing players to login.
Reply

Make NPC respond to you
Код:
public OnPlayerText(playerid, text[])
{
    if (strfind(text, "I need a taxi") != -1)
    {
        new string[80], name[MAX_PLAYER_NAME];
        GetPlayerName(playerid, name, sizeof(name));
 new Float:X, Float:Y, Float:Z, Float:Angle;

        
        format(string, sizeof(string), "Hey %s taxi is in your way!", name);
        SendChat(string);
GetPlayerPos(playerid, X, Y, Z);
GetPlayerFacingAngle(playerid, Angle);
Createvehicle// here 
    }
    return 1;
}
Make NPC write a command

Код:
 public OnPlayerText(playerid, text[])
{
    if (strfind(text, "fuck you") != -1)
    {
        new string[80], name[MAX_PLAYER_NAME];
        GetPlayerName(playerid, name, sizeof(name));
 
        SendCommand("/kick %i", playerid);
        format(string, sizeof(string), "Hey %s Fuck off!", name); // you can set a timer so the player can see the message
        SendChat(string);
    }
    return 1;
}
Not sure if the last snippet will work since I wrote it off phone but neverthless it’s funny how you can experiment with NPCs.
Also this is just an entertaining snippet.
Reply

Quote:
Originally Posted by brauf
Посмотреть сообщение
serverHang Hangs/pauses your server for what ever reason you would want it to.
param 1 = seconds
param 2 = bool


How it can be used:
Код:
If a server would like to release at a specific time, then they can convert seconds 
to mins by adding *3600 at the end their number (multiply by an hour). This will be good
if a server is having a opening beta release and they want their server to open/start at that
exact time, will the server is already running but not fully loaded. Place the function on
public OnGamemodeInit()
...
This is a terrible script. Some hosts may even disable your VPS if you're running at max CPU usage for a long time.
Reply

Halloween accessories

First release on Github
pawn Код:
#include <a_samp>
#include <sscanf2>
#include <zcmd>

CMD:halloween(playerid, params[])
{
    new option;
    if(sscanf(params, "d", option))
        {
            SendClientMessage(playerid, 0x8CAA63FF, "SYNTAX: {FFFFFF}/halloween 1 : Wizard Hat - 2 : Devil Mask");
            return SendClientMessage(playerid, 0x8CAA63FF, "DESCRIPTION: {FFFFFF}Used to attach either a hat or a mask on your head");
        }
   
    switch(option)
    {
        case 1:
        {
            if(IsPlayerAttachedObjectSlotUsed(playerid, 8))
                {
                    RemovePlayerAttachedObject(playerid, 8);
                    return SendClientMessage(playerid, 0x33CCFFFF, "INFO: {FFFFFF}You've just removed your Halloween hat successfully.");
                }
                 
            SetPlayerAttachedObject(playerid, 8, 19528, 2, 0.125, -0.015, 0.01, 0.0, 0.0, -32.5, 0.9, 0.84, 1.01, 0, 0);
            EditAttachedObject(playerid, 8);
            SendClientMessage(playerid, 0x33CCFFFF, "INFO: {FFFFFF}You've just put on your Halloween hat! You can remove it by retyping the command.");
        }
        case 2:
        {
            if(IsPlayerAttachedObjectSlotUsed(playerid, 9))
                {
                    RemovePlayerAttachedObject(playerid, 9);
                    return SendClientMessage(playerid, 0x33CCFFFF, "INFO: {FFFFFF}You've just removed your Halloween mask successfully.");
                }

            SetPlayerAttachedObject(playerid, 9, 11704, 2, 0.05, 0.1, 0.0, -5.099, 85.6, -179.4, 0.396, 0.739, 0.422, 0, 0);
            // Resized the original object which was too big by default
            EditAttachedObject(playerid, 9);  
            SendClientMessage(playerid, 0x33CCFFFF, "INFO: {FFFFFF}You've just put on your Halloween mask! You can remove it by retyping the command.");
        }
    }
    return 1;
}
Reply

deleted
Reply

Animations data: LIBRARY | NAME | INDEX

Data
https://pastebin.com/raw/eqrJX5KD

I wanted to put that up on wiki page, but apparently people are unable to login to wiki.

----
I was looking for that, but couldn't find it at all (even at not SA-MP's related pages).
I find that really useful to determine if player uses specific animation (better than using string comparision for animation names).

Additionally useful for FCNPC_SetAnimation function from FCNPC (which requires animation index).

edit://
Now on WIKI:
https://sampwiki.blast.hk/wiki/Animations#WEAPONS
Reply

HitMarker:

PHP код:
#include <a_samp>
static Text:aim_hit_td;
static 
aim_show[MAX_PLAYERS] = {false,...};
static const 
nweps[] =
{
    
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
    
34, 35, 39, 41, 42, 43
};
public 
OnFilterScriptInit()
{
    
aim_hit_td = TextDrawCreate(336.400512, 176.088546, "x");
    
TextDrawLetterSize(aim_hit_td, 0.263998, 0.629333);
    
TextDrawTextSize(aim_hit_td, -8.000000, 0.000000);
    
TextDrawAlignment(aim_hit_td, 1);
    
TextDrawColor(aim_hit_td, -16776961);
    
TextDrawSetShadow(aim_hit_td, 0);
    
TextDrawBackgroundColor(aim_hit_td, 255);
    
TextDrawFont(aim_hit_td, 1);
    
TextDrawSetProportional(aim_hit_td, 1);
    return 
1;
}
public 
OnPlayerDisconnect(playerid, reason)
{
    
aim_show[playerid] = false;
    return 
1;
}
public 
OnFilterScriptExit()
{
    
TextDrawDestroy(aim_hit_td);
    return 
1;
}
public 
OnPlayerTakeDamage(playerid, issuerid, Float:amount, weaponid, bodypart)
{
    for(new 
i = 0; i < sizeof(nweps); i++)
    {
        if(
weaponid == nweps[i]) return 0;
    }
    if(
issuerid != 0xFFFF && !IsPlayerNPC(issuerid) && !aim_show[issuerid])
    {
        
TextDrawShowForPlayer(issuerid, aim_hit_td);
        
SetTimerEx("HideTD", 200, false, "i", issuerid);
        
aim_show[issuerid] = true;
    }
    return 
0;
}
forward HideTD(playerid);
public 
HideTD(playerid)
{
    
TextDrawHideForPlayer(playerid, aim_hit_td);
    
aim_show[playerid] = false;
    return 
1;
} 
Reply

Quote:
Originally Posted by Lokii
Посмотреть сообщение
HitMarker:
What does?
Reply

Quote:
Originally Posted by SymonClash
View Post
What does?
Show red X on aim when you shoot players
Reply


Time script



PHP Code:
#include <a_samp>
static timer;
static 
Text:time;
public 
OnFilterScriptInit()
{
    
time = TextDrawCreate(552.900207, 32.800006, "00:00");
    
TextDrawLetterSize(time, 0.410333, 2.321777);
    
TextDrawAlignment(time, 1);
    
TextDrawColor(time, 255);
    
TextDrawSetShadow(time, 0);
    
TextDrawSetOutline(time, 1);
    
TextDrawBackgroundColor(time, -2139062017);
    
TextDrawFont(time, 0);
    
TextDrawSetProportional(time, 1);
    
timer = SetTimer("Update_Time", 1000, true);
    return 
1;
}
public 
OnFilterScriptExit()
{
    
TextDrawDestroy(time);
    
KillTimer(timer);
    return 
1;
}
public 
OnPlayerConnect(playerid)
{
    
TextDrawShowForPlayer(playerid, time);
    return 
1;
}
forward Update_Time();
public 
Update_Time()
{
    new 
str[6], h, m;
    
gettime(h, m);
    
format(str, sizeof(str), "%02d:%02d", h, m);
    
TextDrawSetString(time, str);
    return 
1;
} 
Reply

Quote:
Originally Posted by Kasichok
View Post

Time script

[image]...[/image]

PHP Code:
#include <a_samp>
static timer;
static 
Text:time;
public 
OnFilterScriptInit()
{
    
time = TextDrawCreate(552.900207, 32.800006, "00:00");
    
TextDrawLetterSize(time, 0.410333, 2.321777);
    
TextDrawAlignment(time, 1);
    
TextDrawColor(time, 255);
    
TextDrawSetShadow(time, 0);
    
TextDrawSetOutline(time, 1);
    
TextDrawBackgroundColor(time, -2139062017);
    
TextDrawFont(time, 0);
    
TextDrawSetProportional(time, 1);
    
timer = SetTimer("Update_Time", 1000, true);
    return 
1;
}
public 
OnFilterScriptExit()
{
    
TextDrawDestroy(time);
    
KillTimer(timer);
    return 
1;
}
public 
OnPlayerConnect(playerid)
{
    
TextDrawShowForPlayer(playerid, time);
    return 
1;
}
forward Update_Time();
public 
Update_Time()
{
    new 
str[6], h, m;
    
gettime(h, m);
    
format(str, sizeof(str), "%02d:%02d", h, m);
    
TextDrawSetString(time, str);
    return 
1;
} 
https://en.wikipedia.org/wiki/Snippet_(programming)

Quote:
Originally Posted by Wikipedia
Snippet is a programming term for a small region of re-usable source code, machine code, or text. Ordinarily, these are formally defined operative units to incorporate into larger programming modules.

[...]
Explain me how's your a snippet?
Reply

Seems like one to me. How is it not?
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)