Useful Functions

Shouldn't maxLen = sizeof (strSrc)? Because you don't want to copy -4 bytes by default.^^
Reply

It's indeed not in correct order as far as I can see. You just have to reverse it for correct output. Also better use a static variable in this case.
Reply

No, it's correct. Have tested it now.

---

And here is the renamed function and it's opposite to reverse it.

pawn Код:
stock WordToBytes (word, &byte0, &byte1, &byte2, &byte3, mode = 0)
{
    switch (mode)
    {
        case 0:
        {
            new bytes [4 char]; // 4 char = 1
            bytes [0] = word;
            byte3 = bytes {0};
            byte2 = bytes {1};
            byte1 = bytes {2};
            byte0 = bytes {3};
        }

        default:
        {
            byte0 = word & 0xFF;
            byte1 = (word & 0xFF00) >>> 8;
            byte2 = (word & 0xFF0000) >>> 16;
            byte3 = (word & 0xFF000000) >>> 24;
        }
    }
}

stock BytesToWord (byte0, byte1, byte2, byte3, &word, mode = 0)
{
    switch (mode)
    {
        case 0:
        {
            new bytes [4 char]; // 4 char = 1
            bytes {0} = byte3;
            bytes {1} = byte2;
            bytes {2} = byte1;
            bytes {3} = byte0;
            word = bytes [0];
        }

        default:
        {
            word = byte0; // & 0xFF;
            word |= (byte1 << 8); // & 0xFF00;
            word |= (byte2 << 16); // & 0xFF0000;
            word |= (byte3 << 24); // & 0xFF000000;
        }
    }
}
Reply

The order of both modes are the same, agreed, but what I actually meant is the order of the main order of the bytes itself:
pawn Код:
new
    byte[4]
;
GetBytes(0x1A2B3C4D, byte[0], byte[1], byte[2], byte[3]);
printf("%02x %02x %02x %02x", byte[0], byte[1], byte[2], byte[3]); // prints "4D 3C 2B 1A"
Which should be "1A 2B 3C 4D" as order; that's what I meant with reverse. But that's not very important I think.
Reply

Oh, thanks, I actually prefer getting it in "correct" order. So I will revert it.
Reply

Hey,

I was doing some research on Game development. and I found a few things that could be usefull for samp.

Here's a quick one, its the dot product.
that means, it checks if a player is in someone else's field of view.
The default FoV(Field of View) is around 140, but you can change it while using it

Код:
stock InFieldOfView(playerid, from, const radius = 140, const range = -1)
{
	new result;
	new Float:x1, Float:y1, Float:z1, Float:a;
    new Float:x2, Float:y2, Float:z2;
    new Float:D1, Float:D2, Float:V1, Float:V2;
    
	GetPlayerPos(from, x1, y1, z1);
	GetPlayerPos(playerid, x2, y2, z2);
	GetPlayerFacingAngle(from, a);

	D1 = x1/sqrt(x1^2+y1^2);
	D2 = y1/sqrt(x1^2+y1^2);
	
    V1 = x2/sqrt(x2^2+y2^2);
	V2 = y2/sqrt(x2^2+y2^2);

	if(sqrt((x2-x1)^2+(y2-y1)^2) <= range && range != -1)
	{
		result = acos(D1*V1 + D2*V2);
		if(result <= radius) return 0;
	}
	return 1;
}
Its untested, but i think my math is fairly good :P

Usage:
Код:
if(InFieldOfView(playerid, npc))
{
//ATTACK NPC!! ATTACK!!
}
Good luck!
Reply

pawn Код:
stock
    GetBytes(value, bytes[1]) bytes[0] = value;
pawn Код:
new
    bytes[4 char]; // bytes[1];
GetBytes(0x1A2B3C4D, bytes);
printf("%x %x %x %x", bytes{0}, bytes{1}, bytes{2}, bytes{3}); // {}
Reply

Example Usage:
pawn Код:
printf( " %i ", isValidModelID( 1655 ) ); // This would return 1 because objectid 1655 is a ramp. If the parameter was 5, it would return 0 because 5 is an invalid object modelID

pawn Код:
stock isValidModelID( modelID ) // Built by Cyanide
{
    if( modelID >= 321 && modelID <= 397 || modelID >= 700 && modelID <= 725 || modelID >= 1000 && modelID <= 1020 || modelID >= 1210 && modelID <= 1325 || modelID >= 1420 && modelID <= 1620 || modelID >= 1971 && modelID <= 4522 || modelID >= 2589 && modelID <= 3399 )
        return true;

    new
        objectVariation[ ] =
    {
        2918, //Mine
        1433, //Table
        1484, //Beer Bottle
        1486, //Other Beer Bottle
        1518, //Television
        1655, //Ramp
        1765, //Chair
        1766, //Couch
        1211, //Fire Hydrant
        1209, //Vending Machine
        1227, //Save Disk
        1222, //Barrel (Non Explosive)
        1225, //Barrel (Shoot it to make it explode)
        1340, //Chili Dogs Stand
        2222, //Donuts
        2985, //Mounted Machine Gun
        2964, //Pool Table
        3267, //SAM missile launcher (Area 69 / SF aircraft carrier)
        3279, //Area 51 lookout Tower
        3761, //Shelfs
        4563, //Sky Scraper
        8493, //Ship in Las Venturas
        9900, //Star tower
        13592,//Loop the loop
        13594,//Ring
        5482, //Roads - Start
        5483,
        5484,
        5485,
        5486,
        5487,
        5488,
        5489,
        5490,
        5491,
        5492,
        5493,
        5494,
        5495,
        5496,
        5497,
        5498,
        5499,
        5500,
        5501,
        5502,
        5503,
        5504,
        5505,
        5506,
        5507,
        5508,
        5509,
        5510,
        5511,
        5512, //Roads - End
        969,
        981,
        1206, //vending machine <--- Use This = Crash
        1210, //briefcase
        1216, //phone booth
        1219, //palette
        1223, //lamppost
        1341, //ice cream cart
        1342, //noodle stand
        1378, //huge crane
        1384, //top part of construction crane
        1383, //crane stand
        1543, //bottle of beer
        1562, //white jet seat
        1572, //airport trolley
        1608, //shark
        1609, //turtle
        1612, //submarine (from SF port, huge)
        1622, //security camera
        1684, //construction container
        3092, //Tied up dead cop
        2803, //bag of meat
        2589, //carcass on a hook
        2590, //hook
        2804, //Meat
        2805, //Meat
        2806, //Meat
        16410, //grave stones
        12961, //grave stones 2 (open grave)
        12986, //well
        2896, //coffin
        2905, //leg
        2906, //arm
        2907, //torso
        2908, //head
        2976, //green gloop (2976 and 3082 are great for a Area 69 Capture the capsule mode or something)
        3082, //ammo capsule
        3007, //chop cop torso
        3008, //chop cop right arm
        3009, //chop cop left arm
        3010, //chop cop right leg
        3011, //chop cop left leg
        3012, //chop cop head
        3028, //sword
        3070, //goggles
        3071, //dumb bell right
        3072, //dumb bell left
        2627, //treadmill
        2628, //bench 2
        2629, //bench 1
        2630, //bike
        3074, //painting
        3077, //blackboard(3077,3086,3085,3078,and 3088 would be cool for a script based on the movie "60 seconds")
        3086, //cross off
        3085, //60 seconds list 1
        3078, //60 seconds list 2
        3088, //60 seconds list 3
        3108, //base jump target
        3113, //carrier door
        3114, //carrier lift 2
        3115, //carrier lift 1
        2114, //basketball
        1318, //Arrow
        1317, //cylinder
        1316, //hoop
        1559, //diamond checkpoint thing
        384-393, //Clothes based Objects <---Crashes game
        394-397, //Something to do with hands
        301-319, //Cut scene Objects (Like CJ's Suitcase, ETC...) <---?Freeze Game? SAMP-Server crashes?
        5269, //wall of boxes
        17019, //lots of containers (big object)
        1257, //bus stop
        1686, //Fuel distributor
        16776, //Peckers Feed And Seed Chicken
        3345, //Caravan by Abandoned Airport
        923, //Packing_carates2
        933, //CJ_CABLEROLL
        944, //Packing_carates04
        1338, //BinNt08_LA
        1372, //CJ_Dump2_LOW
        1415, //DYN_DUMPSTER
        4516, //roadblock
        4524, //big roadblock
        4527, //small roadblock
        1459, //breakable small roadblock, //police swat thing
        1422, //unbreakable small roadblock
        13667, //monkey man!
        13666, //loop big
        13629, //TV screen
        1593, //stinger
        8493,
        6976,
        1608,
        13592,
        9900, //big skyscraper SF
        1600-1609, //fish
        1598, //ball
        9455, //big land mass
        11490, //torenos ranch
        3556, //small blue house
        3557, //small purple house
        3590, //small house
        3608, //mansion
        3609, //big house
        3608, //big house
        3619, //villa
        3607, //villa
        1431, //5 boxes
        1432, //round table with 3 red chairs
        1433, //bar table
        1427, //road blocker
        1378, //big yellow crane
        1384, //top part of yellow crane
        1383, //crane stand
        3872, //floodlight beams
        1655, //jump
        14866, //bed
        3461, //torch (with flames)
        3243, //teepee
        3279 , //area51 tower
        1596, //satellite (small)
        1278, //stadium light
        1271, //crate
        2780 //smoke machine
    };

    for( new i = 0; i != sizeof objectVariation; ++ i )
    {
        if( modelID == objectVariation[ i ] )
            return true;
    }
    return false;
}
Reply

Quote:
Originally Posted by ******
Посмотреть сообщение
There are much faster ways of doing that (such as the existing InValidModel function) which don't have huge loops, making run time O(1) instead of O(N).
Ah. Since you have mentioned that, I have searched up the mentioning function and and found Slice's Hold Studio's IsValidHoldModel, which seems more faster. Thanks.
Reply

EDIT: Sorry, didn't see there was a page after that post.
Reply

pawn Код:
stock IsVehicleInCheckpoint(vehicleid, checkpointid)
{
    if(!IsValidDynamicCP(checkpointid)) return 0;
   
    new Float:x, Float:y, Float:z, Float:cpx, Float:cpy, Float:cpz,Float:range;
    if(!GetVehiclePos(vehicleid, x, y, z)) return 0;
   
    Streamer_GetFloatData(STREAMER_TYPE_CP, checkpointid, E_STREAMER_X, cpx);
    Streamer_GetFloatData(STREAMER_TYPE_CP, checkpointid, E_STREAMER_Y, cpy);
    Streamer_GetFloatData(STREAMER_TYPE_CP, checkpointid, E_STREAMER_Z, cpz);
    Streamer_GetFloatData(STREAMER_TYPE_CP, checkpointid, E_STREAMER_DISTANCE, range);
    if(PointInRangeOfPoint(x, y, z, cpx, cpy, cpz, range)) return checkpointid;
    return 0;
}
stock PointInRangeOfPoint(Float:x, Float:y, Float:z, Float:X, Float:Y, Float:Z, Float:range)
{
    X -= x; Y -= y; Z -= z;
    return ((X * X) + (Y * Y) + (Z * Z)) < (range * range);
}
Requires Incognito's Streamer. or your own streamer.
Reply

Quote:
Originally Posted by Donya
Посмотреть сообщение
pawn Код:
stock IsVehicleInCheckpoint(vehicleid, checkpointid)
{
    if(!IsValidDynamicCP(checkpointid)) return 0;
   
    new Float:x, Float:y, Float:z, Float:cpx, Float:cpy, Float:cpz,Float:range;
    if(!GetVehiclePos(vehicleid, x, y, z)) return 0;
   
    Streamer_GetFloatData(STREAMER_TYPE_OBJECT, checkpointid, E_STREAMER_X, cpx);
    Streamer_GetFloatData(STREAMER_TYPE_OBJECT, checkpointid, E_STREAMER_Y, cpy);
    Streamer_GetFloatData(STREAMER_TYPE_OBJECT, checkpointid, E_STREAMER_Z, cpz);
    Streamer_GetFloatData(STREAMER_TYPE_OBJECT, checkpointid, E_STREAMER_DISTANCE, range);
    if(PointInRangeOfPoint(x, y, z, cpx, cpy, cpz, range)) return checkpointid;
    return 0;
}
stock PointInRangeOfPoint(Float:x, Float:y, Float:z, Float:X, Float:Y, Float:Z, Float:range)
{
    X -= x; Y -= y; Z -= z;
    return ((X * X) + (Y * Y) + (Z * Z)) < (range * range);
}
Requires Incognito's Streamer. or your own streamer.
You are using STREAMER_TYPE_OBJECT instead of STREAMER_TYPE_CP
Reply

damn, sorry lol.

was testing something else

also(this might not be perfect with heavy(big) vehicles)
Reply

Also, shouldn't it be
pawn Код:
return PointInRangeOfPoint(x, y, z, cpx, cpy, cpz, range);
// instead of
if(PointInRangeOfPoint(x, y, z, cpx, cpy, cpz, range)) return checkpointid;
    return 0;
So it will return 1 if the vehicle is in the CP and otherwise return 0.
Reply

well i put it kinda like that because i wanted it to return the checkpoint id.

so heres the better version for you guys

pawn Код:
stock IsVehicleInCheckpoint(vehicleid, checkpointid)
{
    if(!IsValidDynamicCP(checkpointid)) return 0;
   
    new Float:x, Float:y, Float:z, Float:cpx, Float:cpy, Float:cpz,Float:range;
    if(!GetVehiclePos(vehicleid, x, y, z)) return 0;
   
    Streamer_GetFloatData(STREAMER_TYPE_CP, checkpointid, E_STREAMER_X, cpx);
    Streamer_GetFloatData(STREAMER_TYPE_CP, checkpointid, E_STREAMER_Y, cpy);
    Streamer_GetFloatData(STREAMER_TYPE_CP, checkpointid, E_STREAMER_Z, cpz);
    Streamer_GetFloatData(STREAMER_TYPE_CP, checkpointid, E_STREAMER_DISTANCE, range);
    return PointInRangeOfPoint(x, y, z, cpx, cpy, cpz, range);
}
stock PointInRangeOfPoint(Float:x, Float:y, Float:z, Float:X, Float:Y, Float:Z, Float:range)
{
    X -= x; Y -= y; Z -= z;
    return ((X * X) + (Y * Y) + (Z * Z)) < (range * range);
}
yep to garsino's post below
Reply

Quote:
Originally Posted by Donya
Посмотреть сообщение
well i put it kinda like that because i wanted it to return the checkpoint id.

so heres the better version for you guys
Yeah, but checkpoint ID would always be the one you specified anyway
Reply

That will only work for strings and one variable, if I'm not mistaken. You're better off using the definition ****** created to do the same thing. Except his supports as many variables you want, and of any type.
pawn Код:
new FALSE = false;
#define SendFormattedMessage(%0,%1,%2,%3) do{new string[128];format(string,sizeof(string),(%2),%3);SendClientMessage((%0),(%1),string);}while(FALSE)
//Edited - Thanks ******. (Original credits to you anyway, lol)
I hope that's the correct code. I didn't feel like searching through his thousands of posts, and I'm not on a computer to test.
Reply

Here another quick function because I got bored a little.

Код:
stock choose({int,_}:...)
{
	new numbers = numargs();
	new numb = random(numbers*100); // I added '100' to make it be a bit more random
	return getarg(numb/100);
}
The function is a little bit like 'Random()', but my version only choses from given numbers.
this could be usefull for random skins.

Example:
Код:
SetPlayerSkin(playerid, choose(165, 166, 280, 281, 282, 283, 284, 288)
in this case your skin whil be a 'Law Enforcement' skin.
Reply

Well, rapgangsta made same function some weeks ago

stock randoMix(...) return getarg(random(numargs()));
Reply

Sorry:
pawn Код:
stock RandomNumber(...)
    return getarg(random(numarg()));
EDIT: BaubaS beat me.
Reply


Forum Jump:


Users browsing this thread: 8 Guest(s)