[Include] y_races - Finally ported, More races, Less memory.
#1

y_races
Introduction

The races system in YSI is one of the very first bits of code I wrote for it, back before it was even called YSI. The main "OnPlayerEnterCheckpoint" code remained almost identical to its original version from "San Andreas:Underground" 7 years ago until today when I made some updates (but the main structure is STILL the same).

This library fairly obviously provides functions for defining and running races. You can have (by default) up to 16 races running in parallel, with 1024 checkpoints between them. The old system was set to 256 checkpoints per race - this new version doesn't restrict the number per-race but globally instead, so you can have one with 1 checkpoint (a drag-race perhaps) and another with 1023 (some sort of ridiculous Le-Mans length tour).

Use

Using the basic system is very simple, but as with all of YSI there are many options that can be changed by advanced users:

pawn Code:
#include <YSI\y_races>

new
    gRace;

public OnScriptInit()
{
    // Declare a new race.
    gRace = Race_Create();
    // Set it to have 0 laps.
    Race_SetLaps(gRace, 0);
    // Set the price to join the race to $200.
    // Note that if you want, you can set this to 0 and create your own payment system.
    Race_SetEntry(gRace, 200);
}
That code creates and initialises a race. The function "Race_Create" also has several optional parameters so you can write the code above as:

pawn Code:
public OnScriptInit()
{
    gRace = Race_Create(.laps = 0, .entry = 200);
}
Now this race is very boring - it doesn't have any starting points or any checkpoints, so let's add some:

pawn Code:
// Add some points to the starting grid.
// This is a drag race between two cars along the LV airstrip.
// X, Y, Z, Angle.
Race_AddStart(gRace, 425.0, 2488.0, 16.2, 90.0);
Race_AddStart(gRace, 425.0, 2512.0, 16.2, 90.0);
// Add two checkpoints - its a drag race there and back!
// No angle this time.  Arrows and chequred flags are done automatically.
Race_AddCheckpoint(gRace, -78.5, 2500.0, 16.1);
Race_AddCheckpoint(gRace, 434.0, 2500.0, 16.1);
Finally, we need some players:

pawn Code:
YCMD:join(playerid, params[], help)
{
    #pragma unused params
    if (help)
    {
        SendClientMessage(playerid, 0xFF0000AA, "Adds you to the race.");
    }
    else
    {
        Race_PlayerJoin(playerid, gRace);
    }
    return 1;
}
And the very last thing, once everyone has been added to the race. Start it:

pawn Code:
YCMD:start(playerid, params[], help)
{
    #pragma unused params
    if (help)
    {
        SendClientMessage(playerid, 0xFF0000AA, "Begin the race.");
    }
    else
    {
        Race_Start(gRace);
    }
    return 1;
}
Callbacks

This include adds several callbacks that can be used:

pawn Code:
// Called when a player finishes the race.
forward OnPlayerFinishRace(playerid, race, position, prize, time);

// Called when a player drops out of the race.
forward OnPlayerExitRace(playerid, race);

// Called when the race is over.
forward OnRaceEnd(race);
Note that currently none of these are forwarded, nor are they handled by y_hooks.

Race_Create

As mentioned above, this function has a number of optional parameters. They are:
  • laps = 0 - The number of laps to run the race for. If this value is "0" then the race is a straight shot to some point. If the value is anything other than "0" then the start checkpoint is also the end checkpoint.

  • entry = 0 - How much a race costs to enter. Set to 0 for a custom entry/prize system. The prize money is relative to the entry cost and the number of people who entered - the first three positions win by default.

  • countdown = 3 - When the race starts players are frozen in place and a timer counting down is shown. By default this goes from 3.

  • arial = false - Is this race for planes (i.e. should it use the donut checkpoints instead of the regular vehicle ones)?

  • fixedPrize = true - Use the default inbuilt prize money system.

  • exitTime = 0 - How many seconds can a player be out of their vehicle during a race before they are disqualified? 0 is infinite.

  • interior = 0 - Is the race in an interior? And if so, which one?

  • world = 0 - The virtual world to host the race in.

  • restart = false - By default, once a race is completed it will be deleted from the system. Set this parameter to "true" to enable the race to be reused.
Note that all of these options also have equivalent "Race_SetXXX" functions that can be called instead.

Position

This include also has experimental code to get a player's position in the race. Simply call:

pawn Code:
Race_GetPlayerPosition(playerid);
As often as you want to update any indication of their position. Note that the information is only updated once every 500 ms (half a second - load balanced).

Download

This is part of YSI:

https://sampforum.blast.hk/showthread.php?tid=194480
Reply
#2

Why there are no checkpoints appear? And race didn't start :/

pawn Code:
//top
new race1;
//gminit
    race1 = Race_Create(.countdown=5);
    Race_AddStart(race1, 425.0, 2488.0, 16.2, 90.0);
    Race_AddStart(race1, 425.0, 2512.0, 16.2, 90.0);
    Race_AddCheckpoint(race1, -78.5, 2500.0, 16.1);
    Race_AddCheckpoint(race1, 434.0, 2500.0, 16.1);

CMD:join(playerid)
{
    Race_PlayerJoin(race1, playerid);
    return 1;
}

CMD:start(playerid)
{
    Race_Start(race1);
    return 1;
}

//PS: I use your script ;d
Reply
#3

pawn Code:
Race_PlayerJoin(playerid, race1);
I've fixed this in the first post too - thanks. I need to add tags to "Race:" variables so the compiler gets things like this. This is confusing as it is backwards to almost every other function in YSI!
Reply
#4

So.. I need to re-download it?
Reply
#5

No, just swap the parameter order.
Reply
#6

Quote:
Originally Posted by SA-MP Wiki
Race_PlayerJoin
This function add a player to the race.
This should be used before Race_Start or you're going to have a very boring race with no-one taking part.
https://sampwiki.blast.hk/wiki/YSI:Races

First Race_PlayerJoin, and then Race_Start, no?
But I have bugs, when race has been finish, I can't start the same race again.
Reply
#7

Yes - I've explained THAT in the first post too! I don't write these things for the fun of it...

Anyway, I've added in master system and groups support now.
Reply
#8

Ok i downloaded the YSI package again and now i am getting this error on compilation.
Code:
C:\Users\Bhupesh-PC\Desktop\pawno 3x\pawno\include\YSI\y_iterate.inc(203) : fatal error 111: user error: "Old foreach.inc files are no longer compatible with YSI."
Reply
#9

Delete foreach.inc or upgrade it.
Reply
#10

It's possible to create air races ?
Reply
#11

Yes, as documented in the first post...

Edit: OK, there was a typo - I wrote "places" instead of "planes", so I'll assume you did read the post and this was just my bad!
Reply
#12

I defined, that the race could be started again.

But if it starts a second time the Checkpoint won't be showed. Why?
Reply
#13

What is the difference between Race_SetPlayer(race, playerid, true) and Race_PlayerJoin ?
Reply
#14

Nothing. "Race_PlayerJoin" is the old version, "Race_SetPlayer" is the new version required by the "y_groups" API, but either can be used in user code.
Reply
#15

Ah ok and is there any way to get the players vehicle id?
Reply
#16

GetPlayerVehicleID?
Reply
#17

Gosh sorry i was confused.
Reply
#18

just havin a problem of ... i started race #1 .... when i start race #2... both race gets destroyed... any idea?
Reply
#19

Just amazing! Nice one Y_Less
Reply
#20

Excuse to me the idea of re-living through the topic.

The include is late very much in to compile, is very slow...
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)