SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Filterscripts

Reply
 
Thread Tools Display Modes
Old 23/06/2018, 06:45 PM   #1141
Logic_
Banned
 
Join Date: Jun 2015
Posts: 1,513
Reputation: 305
Default Re: Useful Snippets

Quote:
Originally Posted by Calisthenics View Post
You still call strlen many times and it does work, just tested it to confirm.
pawn Code:
main()
{
    OnPlayerText(0, "   it works.");
}

public OnPlayerText(playerid, text[])
{
    for (new i = 0, j = strlen(text); i < j; i++)
    {
        if ('a' <= text[i] <= 'z')
        {
            //text[i] = toupper(text[i]);
            text[i] -= 32;
            break;
        }
    }
    printf("OPT: \"%s\"", text);
    return 1;
}
You can alternatively use if (new i; text[i] != EOS; i++) for the loop. Your code is better than Lokii's.

Last edited by Logic_; 25/06/2018 at 04:01 AM.
Logic_ is offline   Reply With Quote
Old 24/06/2018, 10:36 PM   #1142
Logic_
Banned
 
Join Date: Jun 2015
Posts: 1,513
Reputation: 305
Default Re: 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
Logic_ is offline   Reply With Quote
Old 25/06/2018, 03:57 AM   #1143
SyS
High-roller
 
SyS's Avatar
 
Join Date: Oct 2015
Posts: 1,948
Reputation: 504
Default Re: Useful Snippets

Quote:
Originally Posted by Logic_ View Post
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.
SyS is offline   Reply With Quote
Old 26/06/2018, 03:38 AM   #1144
GhostHacker9
Big Clucker
 
GhostHacker9's Avatar
 
Join Date: Jan 2017
Location: New Zealand
Posts: 105
Reputation: 109
Default Re: Useful Snippets

Quote:
Originally Posted by Logic_ View Post
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.
GhostHacker9 is offline   Reply With Quote
Old 26/06/2018, 04:45 AM   #1145
Logic_
Banned
 
Join Date: Jun 2015
Posts: 1,513
Reputation: 305
Default Re: Useful Snippets

Quote:
Originally Posted by GhostHacker9 View Post
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.
Logic_ is offline   Reply With Quote
Old 13/07/2018, 10:58 PM   #1146
Marmeladova
Banned
 
Join Date: Dec 2016
Location: Somewhere
Posts: 11
Reputation: 0
Default Re: Useful Snippets

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

Code:
PHP Code:
stock Float:GeoDistance(Float:lat1Float:lon1Float:lat2Float:lon2)
{
    const 
Float:6371.0087714// Earth radius
    
new Float:sin1 floatsin((lat1 lat2) /);
    new 
Float:sin2 floatsin((lon1 lon2) /);
    return 
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 Code:
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"
lat1lon1lat2lon2GeoDistance(lat1lon1lat2lon2)); 
Marmeladova is offline   Reply With Quote
Old 30/08/2018, 06:16 PM   #1147
Lokii
Huge Clucker
 
Join Date: Sep 2017
Posts: 375
Reputation: 91
Default Re: Useful Snippets

Realistic Car Crash damage

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

PHP Code:
#include <a_samp>

Float:GetSpeed(playerid)
{
    new 
Float:xFloat:yFloat:z;
    
GetVehicleVelocity(GetPlayerVehicleID(playerid), xyz);
    return 
floatabs(floatsqroot(floatpower(x2) + floatpower(y2)))*180.5;
}

public 
OnVehicleDamageStatusUpdate(vehicleidplayerid)
{
    new 
Float:hpFloat:dmg;
    
dmg GetSpeed(playerid);
    for(new 
0GetPlayerPoolSize(); <= ji++) 
    {
        if(
IsPlayerNPC(i) || GetPlayerVehicleID(i) != vehicleid) continue;
        
GetPlayerHealth(ihp);
        
SetPlayerHealth(ihp-(dmg/3));
    }
    return 
1;

__________________
Lokii is offline   Reply With Quote
Old 01/09/2018, 08:21 PM   #1148
brauf
Little Clucker
 
brauf's Avatar
 
Join Date: Jul 2018
Location: i live in narnia city Savage: 100%
Posts: 28
Reputation: 17
Default Re: Useful Snippets

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:
Code:
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:
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:
Code:
hang(4, false);

Output:
Code:
hanged for 998ms
hanged for 1999ms
hanged for 2999ms
hanged for 3999ms
hang stopped at 4 seconds
__________________
[SNIPPET] serverHang

brauf is offline   Reply With Quote
Old 03/09/2018, 08:05 PM   #1149
Dayrion
High-roller
 
Dayrion's Avatar
 
Join Date: Jan 2016
Location: France
Posts: 1,918
Reputation: 258
Default Re: Useful Snippets

Quote:
Originally Posted by brauf View Post
...
It will freeze the server and prevent other code running. That's a veryyyyyyy bad idea.
__________________
actor_plus - v5.0.2
I'm a goner, somebody catch my breath,
I'm a goner, somebody catch my breath
Dayrion is offline   Reply With Quote
Old 03/09/2018, 09:31 PM   #1150
brauf
Little Clucker
 
brauf's Avatar
 
Join Date: Jul 2018
Location: i live in narnia city Savage: 100%
Posts: 28
Reputation: 17
Default Re: Useful Snippets

Quote:
Originally Posted by Dayrion View Post
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.
__________________
[SNIPPET] serverHang

brauf is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
READ Usefull Links to snippets and other -Sneaky- Help Archive 5 02/05/2009 12:22 PM


All times are GMT. The time now is 06:39 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.