Useful Snippets

Quote:
Originally Posted by ******
Посмотреть сообщение
What are you on about? Saying something is the best you've ever seen is a complement, not an insult!
I love you. LOL
Reply

Quote:
Originally Posted by stormchaser206
Посмотреть сообщение
Who you talking to?


If your talking to me: This is a topic where you post snippets, not insult people.
Brainstorming.
Reply

How to get MaxVehicles on Sever

This snipped add function "GetMaxVehicles()", very easy to use.
pawn Код:
new
    Veiculos = -1
;

stock sDestroyVehicle(vehicleid)
{
    if(GetVehicleModel(vehicleid)) return false;
    Veiculos--;
    return CallRemoteFunction("DestroyVehicle", "i", vehicleid);
}

stock sAddStaticVehicle(modelid, Float:spawn_x, Float:spawn_y, Float:spawn_z, Float:angle, color1, color2)
{
    Veiculos++;
    return CallRemoteFunction("AddStaticVehicle", "ifffffii", modelid, Float:spawn_x, Float:spawn_y, Float:spawn_z, Float:angle, color1, color2);
}

stock sAddStaticVehicleEx(modelid, Float:spawn_x, Float:spawn_y, Float:spawn_z, Float:angle, color1, color2, respawn_delay)
{
    Veiculos++;
    return CallRemoteFunction("AddStaticVehicleEx", "ifffffiii", modelid, Float:spawn_x, Float:spawn_y, Float:spawn_z, Float:angle, color1, color2, respawn_delay);
}

stock sCreateVehicle(modelid, Float:x, Float:y, Float:z, Float:angle, color1, color2, respawn_delay)
{
    Veiculos++;
    return CallRemoteFunction("CreateVehicle", "iffffiii", modelid, Float:x, Float:y, Float:z, Float:angle, color1, color2, respawn_delay);
}


#define DestroyVehicle      sDestroyVehicle
#define AddStaticVehicle    sAddStaticVehicle
#define AddStaticVehicleEx  sAddStaticVehicleEx
#define CreateVehicle       sCreateVehicle
#define GetMaxVehicles()    Veiculos



You can make test..
pawn Код:
public OnFilterScriptInit()
{
    AddStaticVehicle(411, 0.0,0.0,0.0, 0.0,  0,0);
    AddStaticVehicle(411, 0.0,0.0,0.0, 0.0,  0,0);
    AddStaticVehicle(411, 0.0,0.0,0.0, 0.0,  0,0);
    AddStaticVehicle(411, 0.0,0.0,0.0, 0.0,  0,0);
    AddStaticVehicle(411, 0.0,0.0,0.0, 0.0,  0,0);
    AddStaticVehicle(411, 0.0,0.0,0.0, 0.0,  0,0);
    AddStaticVehicle(411, 0.0,0.0,0.0, 0.0,  0,0);
    AddStaticVehicle(411, 0.0,0.0,0.0, 0.0,  0,0);
    AddStaticVehicle(411, 0.0,0.0,0.0, 0.0,  0,0);
    AddStaticVehicle(411, 0.0,0.0,0.0, 0.0,  0,0);
   
    printf("Have: %d vehicles", GetMaxVehicles());
    DestroyVehicle(0);
    printf("Now, Have: %d Veнvehicles", GetMaxVehicles());
    return 1;
}
Reply

Why so.. so.. different? Its very easy without configuring script that much:
pawn Код:
stock GetTotalVehicles()
{
    new VehicleCount = 0;
    for(new vehicle = 1; vehicle < MAX_VEHICLES; vehicle++)
    {
          new Float:Pos[3];
          GetVehiclePos(vehicle, Pos[0], Pos[1], Pos[2]);
          if(Pos[0] != 0.0 && Pos[1] != 0.0) VehicleCount++
    }
    return VehicleCount;
}
Reply

"Max" in GetMaxPlayers means maximum, just FYI. So here's a correct version of your function:
pawn Код:
stock GetMaxVehicles() {
    return MAX_VEHICLES;
}
Reply

Even easier:
pawn Код:
stock GetVehiclesCount() {
    new
        iCount
    ;
    for(new i = 1; i < MAX_VEHICLES; ++i) {
        if(GetVehicleModel(i)) {
            iCount++;
        }
    }
    return iCount;
}
Reply

pawn Код:
stock GetVehiclesCount() {
new car = CreateVehicle(522,0,0,0,0,0,0,-1);
DestroyVehicle(car);
car = car-1;
return car;
}
Reply

Quote:
Originally Posted by Patchwerk
View Post
pawn Code:
stock GetVehiclesCount() {
new car = CreateVehicle(522,0,0,0,0,0,0,-1);
DestroyVehicle(car);
car = car-1;
return car;
}
Create 8 cars then delete the third, the one created in this function would take the place of the third, thus returning 2.
Reply

Quote:
Originally Posted by Patchwerk
Посмотреть сообщение
pawn Код:
stock GetVehiclesCount() {
new car = CreateVehicle(522,0,0,0,0,0,0,-1);
DestroyVehicle(car);
car = car-1;
return car;
}
pawn Код:
#include <a_samp>

stock GetTotalVehicles()
{  
    new vehc = 0;
    for(new x; x != MAX_VEHICLES; ++x) {
        if(GetVehicleModel(x)) vehc++;
    }
    return vehc;
}

main () {
   
   
    CreateVehicle(520, 2109.1763, 1503.0453, 32.2887, 82.2873, 0, 1, 60); // 1
    CreateVehicle(520, 2109.1763, 1503.0453, 32.2887, 82.2873, 0, 1, 60); // 2
    CreateVehicle(520, 2109.1763, 1503.0453, 32.2887, 82.2873, 0, 1, 60); // 3   
    CreateVehicle(520, 2109.1763, 1503.0453, 32.2887, 82.2873, 0, 1, 60); // 4
    CreateVehicle(520, 2109.1763, 1503.0453, 32.2887, 82.2873, 0, 1, 60); // 5 
    CreateVehicle(520, 2109.1763, 1503.0453, 32.2887, 82.2873, 0, 1, 60); // 6
    CreateVehicle(520, 2109.1763, 1503.0453, 32.2887, 82.2873, 0, 1, 60); // 7
   
    DestroyVehicle(4); // 6
    DestroyVehicle(6); // 5
    DestroyVehicle(2); // 4
   
    printf("have %d vehicles on server", GetTotalVehicles()); // = 4 vehicles
   
    DestroyVehicle(1); //
   
    printf("have %d vehicles on server", GetTotalVehicles()); // = 3 vehicles
   
    quit(); // http://slice-vps.nl/ppg/ :p
}
or (fasthest)

pawn Код:
#include <a_samp>

new totalVeh  = 0;

#define GetTotalVehicles() totalVeh

forward InternalCVeh(modelid, Float:x, Float:y, Float:z, Float:angle, color1, color2, respawn_delay);
public InternalCVeh(modelid, Float:x, Float:y, Float:z, Float:angle, color1, color2, respawn_delay) {
    return CreateVehicle(modelid, x, y, z, angle, color1, color2, respawn_delay);
}

forward InternalDVeh(id);
public  InternalDVeh(id) {
    return DestroyVehicle(id);
}

#define CreateVehicle   totalVeh ++, InternalCVeh
#define DestroyVehicle(%0)  totalVeh  = GetVehicleModel(%0) ? totalVeh  - 1 :totalVeh ,  InternalDVeh(%0)

main () {
   
   
    CreateVehicle(520, 2109.1763, 1503.0453, 32.2887, 82.2873, 0, 1, 60); // 1
    CreateVehicle(520, 2109.1763, 1503.0453, 32.2887, 82.2873, 0, 1, 60); // 2
    CreateVehicle(520, 2109.1763, 1503.0453, 32.2887, 82.2873, 0, 1, 60); // 3   
    CreateVehicle(520, 2109.1763, 1503.0453, 32.2887, 82.2873, 0, 1, 60); // 4
    CreateVehicle(520, 2109.1763, 1503.0453, 32.2887, 82.2873, 0, 1, 60); // 5 
    CreateVehicle(520, 2109.1763, 1503.0453, 32.2887, 82.2873, 0, 1, 60); // 6
    CreateVehicle(520, 2109.1763, 1503.0453, 32.2887, 82.2873, 0, 1, 60); // 7
   
    DestroyVehicle(4); // 6
    DestroyVehicle(6); // 5
    DestroyVehicle(2); // 4
   
    printf("have %d vehicles on server", GetTotalVehicles()); // = 4 vehicles
   
    DestroyVehicle(1); //
   
    printf("have %d vehicles on server", GetTotalVehicles()); // = 3 vehicles
   
    quit(); // http://slice-vps.nl/ppg/ :p
}
Reply

Why put the count code in the defines instead of the functions? And what about AddStaticVehicle? And filterscripts? RyDeR`'s code is the most robust yet.

Also, since when is that a good global variable name?
Reply

There is a problem in the code? Here it worked perfectly. Only do now to AddStaticVehicle and others. You know it.

_

-> https://sampwiki.blast.hk/wiki/GetVehicleModel
Quote:

The vehicle model ID, or 0 if the vehicle doesn't exist.

But you need to create loops in all vehicles ids (slower). It is better to use redefine functions
Reply

For DM servers, a way to prevents "g abuse" without loops finding a driver.

Put into OnPlayerEnterVehicle or OnPlayerStateChange:
pawn Код:
new pVehicle = GetPlayerVehicleID(playerid), engine, invalidparam;
GetVehicleParamsEx(pVehicle, engine, invalidparam, invalidparam, invalidparam, invalidparam, invalidparam, invalidparam);
if(engine == -1) ClearAnimations(playerid);//Or RemovePlayerFromVehicle(OnPlayerStateChange&passenger newstate)
Reply

Quote:
Originally Posted by [FeK]DraKiNs
View Post
There is a problem in the code? Here it worked perfectly. Only do now to AddStaticVehicle and others. You know it.

_

-> https://sampwiki.blast.hk/wiki/GetVehicleModel

But you need to create loops in all vehicles ids (slower). It is better to use redefine functions
The main problem with your code is that it doesn't work if you add vehicles inside gamemodes and filterscripts, they will be counted separately. Next to that the issue of where to put the addition, and the fact that you don't use ALS are minor.
Reply

Quote:
Originally Posted by ******
Посмотреть сообщение
The main problem with your code is that it doesn't work if you add vehicles inside gamemodes and filterscripts, they will be counted separately. Next to that the issue of where to put the addition, and the fact that you don't use ALS are minor.
If you want to adapt it to your gamemode, make to AddStaticVehicle, AddStaticVehicleEx, etc.. If possible use global server variables (GVars !?) and includes with code. I'm SURE it is functional
Reply

OK, run these two modes together and look at the output:

pawn Code:
#include <a_samp>
new totalVeh  = 0;

#define GetTotalVehicles1() totalVeh

stock GetTotalVehicles2()
{  
    new vehc = 0;
    for(new x; x != MAX_VEHICLES; ++x) {
        if(GetVehicleModel(x)) vehc++;
    }
    return vehc;
}

forward InternalCVeh(modelid, Float:x, Float:y, Float:z, Float:angle, color1, color2, respawn_delay);
public InternalCVeh(modelid, Float:x, Float:y, Float:z, Float:angle, color1, color2, respawn_delay) {
    return CreateVehicle(modelid, x, y, z, angle, color1, color2, respawn_delay);
}

forward InternalDVeh(id);
public  InternalDVeh(id) {
    return DestroyVehicle(id);
}

#define CreateVehicle   totalVeh ++, InternalCVeh
#define DestroyVehicle(%0)  totalVeh  = GetVehicleModel(%0) ? totalVeh  - 1 :totalVeh ,  InternalDVeh(%0)

main()
{
}

public OnGameModeInit()
{
    CreateVehicle(520, 2109.1763, 1503.0453, 32.2887, 82.2873, 0, 1, 60);
    printf("You have %d or %d vehicles on server", GetTotalVehicles1(), GetTotalVehicles2());
}

pawn Code:
#define FILTERSCRIPT

#include <a_samp>
new totalVeh  = 0;

#define GetTotalVehicles1() totalVeh

stock GetTotalVehicles2()
{  
    new vehc = 0;
    for(new x; x != MAX_VEHICLES; ++x) {
        if(GetVehicleModel(x)) vehc++;
    }
    return vehc;
}

forward InternalCVeh(modelid, Float:x, Float:y, Float:z, Float:angle, color1, color2, respawn_delay);
public InternalCVeh(modelid, Float:x, Float:y, Float:z, Float:angle, color1, color2, respawn_delay) {
    return CreateVehicle(modelid, x, y, z, angle, color1, color2, respawn_delay);
}

forward InternalDVeh(id);
public  InternalDVeh(id) {
    return DestroyVehicle(id);
}

#define CreateVehicle   totalVeh ++, InternalCVeh
#define DestroyVehicle(%0)  totalVeh  = GetVehicleModel(%0) ? totalVeh  - 1 :totalVeh ,  InternalDVeh(%0)

public OnFilterScriptInit()
{
    CreateVehicle(520, 2109.1763, 1503.0453, 32.2887, 82.2873, 0, 1, 60);
    printf("You have %d or %d vehicles on server", GetTotalVehicles1(), GetTotalVehicles2());
}
I'm not even bothered about AddStaticVehicle/Ex at this point.
Reply

Well, you cannot just get last ID of car.

What if Vehicle ID 100, 124, 156,175,201 is destroyed? Then it will replace id 100, and return 99.
Reply

Quote:
Originally Posted by aco_SRBIJA
View Post
Well, you cannot just get last ID of car.

What if Vehicle ID 100, 124, 156,175,201 is destroyed? Then it will replace id 100, and return 99.
Exactly my point on the other page.
The only good way to do it by hooking CreateVehicle is by having a global counter variable that is incremented in Create* and decremented on Destroy*.

But then you obviously have the problem of multiple different scripts having individual vehicle counts, therefore the only good way is looping through them all and checking if the model is valid.


I had another idea but I think it's too much work for the results really:
- Create a variable that holds the highest id.
- Hook CreateV. function so when a vehicle is created it checks if the ID is higher than the last marked ID from CreateV.
- If it is assign the new highest ID to the variable.
- Loop up to that ID on the count function, this way the loop doesn't to unneeded work if you have a server with only 200 vehicles on (MAX_VEHICLES is 2000 right?)

I guess that would work across multiple scripts right?
Reply

Cant it be improved if we leave strcat out and pass the address directly to strfind ?
Reply

Quote:
Originally Posted by Nero_3D
View Post
Cant it be improved if we leave strcat out and pass the address directly to strfind ?
Indeed, there's no need for strcat.

Btw, using pointers.inc, this function could be as simple as:
pawn Code:
stock strdel_words(string[], ...) {
    new pos = 0, len;
   
    for (new i = numargs() - 1; i > 0; i--) {
        len = strlen(@A[i]);
       
        while (-1 != (pos = strfind(string, @A[i], true)))
            strdel(string, pos, pos + len);
    }
}
Reply

Quote:
Originally Posted by Slice
View Post
Indeed, there's no need for strcat.
[/pawn]
Could you make a strreplace that works in OnPlayerText properly? even RyDeR`'s has a bug in OnPlayerText ;'\

EDIT: LOL I just realized you made one.. gonna test thanks

----

Here's a function to find a free slot in a unique column

Code:
SELECT MIN(Table.Uniqueid + 1) AS freeID\
	FROM Table\
	LEFT JOIN Table tf1\
	ON Table.Uniqueid + 1 = tf1.Uniqueid\
	WHERE tf1.Uniqueid IS NULL
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)