Race System
#1

I'm trying to create a race system base on rRace code.
Well actually it is like a edited one but half of it is scripted by me.

Now let's get to the point, I've problem loading the race checkpoints.
Actually the races are automatically loaded (All of them). There is a starting point.
Player can /joinrace if he is in starting point, After /joinrace there will be a countdown (different to that rRace).
30 to 1. If the countdown reach to 5, the race is locked and no one can join anymore. Now the problem is
after the countdown is finished, The race checkpoint is always set to the coordinates 0.0, 0.0, 0.0 (Blueberry Acres)
I don't understand why this keeps happening. Here's the code

pawn Код:
public OnGameModeInit()
{
    for(new x=0; x < MAX_RACES; x++)
    {
        if(fexist(RacePath(x)))
        {
            format(RaceInfo[x][RaceName], 128, "%s", dini_Get(RacePath(x), "RaceName"));
            RaceInfo[x][StartPoint][0] = dini_Float(RacePath(x), "StartX");
            RaceInfo[x][StartPoint][1] = dini_Float(RacePath(x), "StartY");
            RaceInfo[x][StartPoint][2] = dini_Float(RacePath(x), "StartZ");
            RaceInfo[x][StartPSize] = dini_Float(RacePath(x), "StartCPSize");
            RaceInfo[x][TotalCP] = dini_Int(RacePath(x), "TotalCP");
            new rstring[128+30];
            format(rstring, sizeof rstring, "RaceName: %s\nTotal Race Checkpoints: %i", RaceInfo[x][RaceName], RaceInfo[x][TotalCP]);
            RaceInfo[x][Label] = CreateDynamic3DTextLabel(rstring, -1, RaceInfo[x][StartPoint][0], RaceInfo[x][StartPoint][1], RaceInfo[x][StartPoint][2], DDISTANCE,  INVALID_PLAYER_ID, INVALID_VEHICLE_ID, 0, -1, -1, -1, SDISTANCE);
            RaceInfo[x][StartCP] = CreateDynamicRaceCP(2, RaceInfo[x][StartPoint][0], RaceInfo[x][StartPoint][1], RaceInfo[x][StartPoint][2], 0.0, 0.0, 0.0, RaceInfo[x][StartPSize], -1, -1, -1, SDISTANCE);
            TotalRaces++;
        }
        if(fexist(RacePath2(x)))
        {
            new string[128];
            for(new c=0; c < RaceInfo[x][TotalCP]; c++)
            {
                format(string, sizeof string, "CP_%d_PosX", c); CPCoords[c][0] = dini_Float(RacePath2(x), string);
                format(string, sizeof string, "CP_%d_PosY", c); CPCoords[c][1] = dini_Float(RacePath2(x), string);
                format(string, sizeof string, "CP_%d_PosZ", c); CPCoords[c][0] = dini_Float(RacePath2(x), string);
            }
        }
    }
    printf("Total Races: %i", TotalRaces);
    return 1;
}

public OnGameModeExit()
{
    TotalRaces = 0;
    for(new x=0; x < MAX_RACES; x++)
    {
        if(fexist(RacePath(x)))
        {
            DestroyDynamic3DTextLabel(RaceInfo[x][Label]);
            DestroyDynamicRaceCP(RaceInfo[x][StartCP]);
            JoinCount[x] = 0;
            KillTimer(CountTimer[x]);
            CountD[x] = 30;
        }
    }
    return 1;
}

public OnPlayerEnterRaceCheckpoint(playerid)
{
    if(CProgess[playerid] == RaceInfo[PlayerRace[playerid]][TotalCP])
    {
        new Prize[2];
        switch(Position[PlayerRace[playerid]])
        {
            case 1: Prize[0] = (random(random(5000)) + 10000), Prize[1] = 10;
            case 2: Prize[0] = (random(random(4500)) + 9000), Prize[1] = 9;
            case 3: Prize[0] = (random(random(4000)) + 8000), Prize[1] = 8;
            case 4: Prize[0] = (random(random(3500)) + 7000), Prize[1] = 7;
            case 5: Prize[0] = (random(random(3000)) + 6000), Prize[1] = 6;
            case 6: Prize[0] = (random(random(2500)) + 5000), Prize[1] = 5;
            case 7: Prize[0] = (random(random(2000)) + 4000), Prize[1] = 4;
            case 8: Prize[0] = (random(random(1500)) + 3000), Prize[1] = 3;
            case 9: Prize[0] = (random(random(1000)) + 2000), Prize[1] = 2;
            default: Prize[0] = random(random(1000)), Prize[1] = 1;
        }
        GivePlayerMoney(playerid, Prize[0]);
        SetPlayerScore(playerid, GetPlayerScore(playerid) + Prize[1]);
        PlayerRace[playerid] = 0;
    }
    else
    {
        CProgess[playerid]++;
        SetCP(playerid, CProgess[playerid], CProgess[playerid]+1, RaceInfo[PlayerRace[playerid]][TotalCP]);
        PlayerPlaySound(playerid, 1137, 0.0, 0.0, 0.0);
    }
    return 1;
}

public OnPlayerEnterDynamicRaceCP(playerid, checkpointid)
{
    new string[128];
    for(new i=0; i < MAX_RACES; i++)
    {
        if(checkpointid == RaceInfo[i][StartCP])
        {
            if(RaceInfo[i][Start] == 0)
            {
                IsInRace[playerid] = i;
                format(string, sizeof string, "~w~RaceName: %s~n~Total Race CPs: %i~n~To join this race type /joinrace!", RaceInfo[i][RaceName], RaceInfo[i][TotalCP]);
                GameTextForPlayer(playerid, string, 2500, 3);
            }
            else
            {
                SendClientMessage(playerid, COLOR_RED, "Sorry, This race has been already started, Please wait till this race ended!");
            }
        }
    }
    return 1;
}
public OnPlayerLeaveDynamicRaceCP(playerid, checkpointid)
{
    for(new i=0; i < MAX_RACES; i++)
    {
        if(checkpointid == RaceInfo[i][StartCP])
        {
            if(IsInRace[playerid] == i)
            {
                IsInRace[playerid] = 0;
            }
        }
    }
    return 1;
}
stock Float:GetDistanceToRace(playerid, i)
{
    new Float:x1,
        Float:y1,
        Float:z1,
        Float:x2,
        Float:y2,
        Float:z2
    ;
    GetPlayerPos(playerid, x1, y1, z1);
    x2 = RaceInfo[i][StartPoint][0];
    y2 = RaceInfo[i][StartPoint][1];
    z2 = RaceInfo[i][StartPoint][2];
    return floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));
}
CMD:joinrace(playerid, params[])
{
    new race = GetClosetRace(playerid);
    if(race == 0) return SendClientMessage(playerid, COLOR_RED, "ERROR: You aren't near in any race starting points!");
    if(RaceInfo[IsInRace[playerid]][Start] == 1) return SendClientMessage(playerid, COLOR_RED, "ERROR: This race is already running, Please wait till this race is ended!");
    if(Joined[playerid] == true) return SendClientMessage(playerid, COLOR_RED, "ERROR: You already joined a race!");
    if(!IsPlayerInAnyVehicle(playerid)) return SendClientMessage(playerid, COLOR_RED, "ERROR: You must be inside of any vehicle to join the race!");
    Joined[playerid] = true;
    if(JoinCount[IsInRace[playerid]] == 0)
    {
        CountD[PlayerRace[playerid]] = 30;
        CountTimer[PlayerRace[playerid]] = SetTimerEx("RaceCount", 1000, true, "dd", playerid, PlayerRace[playerid]);
    }
    JoinCount[IsInRace[playerid]]++;
    PlayerRace[playerid] = IsInRace[playerid];
    IsInRace[playerid] = 0;
    GameTextForPlayer(playerid, "~w~Please wait till the race starts~n~The race is still waiting for other racers!", 3500, 3);
    TogglePlayerControllable(playerid, 0);
    SetCameraBehindPlayer(playerid);
    return 1;
}

forward RaceCount(playerid, raceid);
public RaceCount(playerid, raceid)
{
    new string[90];
    format(string, sizeof string, "~w~Race will start in %i", CountD[raceid]);
    GameTextForPlayer(playerid, string, 1000, 3);
    new lstring[128];
    switch(CountD[raceid])
    {
        case 30: format(lstring, sizeof lstring, "RACE: The race %s(ID:%i) will start in 30 seconds, Go to the start checkpoint and /joinrace if you want to join!", RaceInfo[raceid][RaceName], raceid), SendClientMessageToAll(COLOR_YELLOW, lstring);
        case 20: format(lstring, sizeof lstring, "RACE: The race %s(ID:%i) will start in 20 seconds, Go to the start checkpoint and /joinrace if you want to join!", RaceInfo[raceid][RaceName], raceid), SendClientMessageToAll(COLOR_YELLOW, lstring);
        case 10: format(lstring, sizeof lstring, "RACE: The race %s(ID:%i) will start in 10 seconds, Go to the start checkpoint and /joinrace if you want to join!", RaceInfo[raceid][RaceName], raceid), SendClientMessageToAll(COLOR_YELLOW, lstring);
        case 5: format(lstring, sizeof lstring, "RACE: You can't join the race %s(ID:%d) anymore, It will start now!", RaceInfo[raceid][RaceName], raceid), RaceInfo[raceid][Start] = 1, SendClientMessageToAll(COLOR_YELLOW, lstring);
    }
    if(CountD[raceid] >= 1)
    {
        CountD[raceid]--;
    }
    else
    {
        DestroyDynamicRaceCP(RaceInfo[PlayerRace[playerid]][StartCP]);
        DisablePlayerRaceCheckpoint(playerid);
        SetCP(playerid, CProgess[playerid], CProgess[playerid]+1, RaceInfo[PlayerRace[playerid]][TotalCP]);
        TogglePlayerControllable(playerid, 1);
        GameTextForPlayer(playerid, "~w~Go! ~g~Go! ~y~Go!", 2000, 3);
        CountD[raceid] = 30;
        KillTimer(CountTimer[raceid]);
    }
    return 1;
}
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    new
        string[130],
        Float:vPos[3]
    ;
    if(BuilderMod[playerid] == true)
    {
        if(newkeys & KEY_FIRE)
        {
            if(BuildCP > MAX_CP_RACE) return SendClientMessage(playerid, -1, "ERROR: You reached the maximum amount of checkpoints!");
            if(!IsPlayerInAnyVehicle(playerid)) return SendClientMessage(playerid, -1, "ERROR: You need to be in a vehicle");
            GetVehiclePos(GetPlayerVehicleID(playerid), vPos[0], vPos[1], vPos[2]);
            format(string, sizeof(string), "CP_%d_PosX", BuildCP), dini_FloatSet(RacePath2(PRaceID[playerid]), string, vPos[0]);
            format(string, sizeof(string), "CP_%d_PosY", BuildCP), dini_FloatSet(RacePath2(PRaceID[playerid]), string, vPos[1]);
            format(string, sizeof(string), "CP_%d_PosZ", BuildCP), dini_FloatSet(RacePath2(PRaceID[playerid]), string, vPos[2]);
            format(string, sizeof(string), "RACE: Checkpoint '%d' has been setted!", BuildCP+1);
            SendClientMessage(playerid, -1, string);
            BuildCP++;
        }
        if(newkeys & KEY_SECONDARY_ATTACK)
        {
            for(new x=0; x < MAX_RACES; x++)
            {
                if(fexist(RacePath(x)))
                {
                    DestroyDynamic3DTextLabel(RaceInfo[x][Label]);
                    DestroyDynamicRaceCP(RaceInfo[x][StartCP]);
                    format(RaceInfo[x][RaceName], 128, "%s", dini_Get(RacePath(x), "RaceName"));
                    RaceInfo[x][StartPoint][0] = dini_Float(RacePath(x), "StartX");
                    RaceInfo[x][StartPoint][1] = dini_Float(RacePath(x), "StartY");
                    RaceInfo[x][StartPoint][2] = dini_Float(RacePath(x), "StartZ");
                    RaceInfo[x][StartPSize] = dini_Float(RacePath(x), "StartCPSize");
                    RaceInfo[x][TotalCP] = dini_Int(RacePath(x), "TotalCP");
                    new rstring[128+30];
                    format(rstring, sizeof rstring, "RaceName: %s\nTotal Race Checkpoints: %i", RaceInfo[x][RaceName], RaceInfo[x][TotalCP]);
                    RaceInfo[x][Label] = CreateDynamic3DTextLabel(rstring, -1, RaceInfo[x][StartPoint][0], RaceInfo[x][StartPoint][1], RaceInfo[x][StartPoint][2], DDISTANCE,  INVALID_PLAYER_ID, INVALID_VEHICLE_ID, 0, -1, -1, -1, SDISTANCE);
                    RaceInfo[x][StartCP] = CreateDynamicRaceCP(0, RaceInfo[x][StartPoint][0], RaceInfo[x][StartPoint][1], RaceInfo[x][StartPoint][2], CPCoords[x][0], CPCoords[x][1], CPCoords[x][2], RaceInfo[x][StartPSize], -1, -1, -1, SDISTANCE);
                }
                if(fexist(RacePath2(x)))
                {
                    new sstring[128];
                    for(new c=1; c < RaceInfo[x][TotalCP]; c++)
                    {
                        format(sstring, sizeof sstring, "CP_%d_PosX", c); CPCoords[c][0] = dini_Float(RacePath2(x), sstring);
                        format(sstring, sizeof sstring, "CP_%d_PosY", c); CPCoords[c][1] = dini_Float(RacePath2(x), sstring);
                        format(sstring, sizeof sstring, "CP_%d_PosZ", c); CPCoords[c][0] = dini_Float(RacePath2(x), sstring);
                    }
                }
            }
            RaceInfo[PRaceID[playerid]][TotalCP] = BuildCP;
            dini_IntSet(RacePath(PRaceID[playerid]), "TotalCP", RaceInfo[PRaceID[playerid]][TotalCP]);
            PRaceID[playerid] = 0;
            BuilderMod[playerid] = false;
            SendClientMessage(playerid, COLOR_RED, "RACE: Creating racing is finished!");
        }
    }
    return 1;
}

CMD:buildrace(playerid, params[])
{
    new raceid,
        Float:startcpsize,
        Float:StartCPpos[3],
        RName[128]
    ;
    GetPlayerPos(playerid, StartCPpos[0], StartCPpos[1], StartCPpos[2]);
    if(BuilderMod[playerid] == true) return SendClientMessage(playerid, COLOR_RED, "ERROR: You're already in Race Builder Mode!");
    if(sscanf(params, "iF(9.0)s[128]", raceid, startcpsize, RName)) return SendClientMessage(playerid, COLOR_RED, "USAGE: /buildrace [RaceID] [StartCPSize] [RaceName]");
    if(raceid == 0) return SendClientMessage(playerid, COLOR_RED, "ERROR: RaceID 0 can't be taken, That id is buggy!");
    if(fexist(RacePath(raceid))) return SendClientMessage(playerid, COLOR_RED, "ERROR: This RaceID is already taken!");
    if(fexist(RacePath2(raceid))) return SendClientMessage(playerid, COLOR_RED, "ERROR: This RaceID is already taken!");
    BuilderMod[playerid] = true;
    format(RaceInfo[raceid][RaceName], 128, "%s", RName);
    dini_Create(RacePath(raceid));
    dini_Create(RacePath2(raceid));
    dini_Set(RacePath(raceid), "RaceName", RName);
    dini_FloatSet(RacePath(raceid), "StartX", StartCPpos[0]); RaceInfo[raceid][StartPoint][0] = StartCPpos[0];
    dini_FloatSet(RacePath(raceid), "StartY", StartCPpos[1]); RaceInfo[raceid][StartPoint][1] = StartCPpos[1];
    dini_FloatSet(RacePath(raceid), "StartZ", StartCPpos[2]); RaceInfo[raceid][StartPoint][2] = StartCPpos[2];
    dini_FloatSet(RacePath(raceid), "StartCPSize", RaceInfo[raceid][StartPSize] = startcpsize);
    SendClientMessage(playerid, COLOR_YELLOW, "RACE: You've created the Race Information!");
    SendClientMessage(playerid, COLOR_YELLOW, "RACE: To insert the race checkpoints, Go to the race cp positions and press LMB!");
    SendClientMessage(playerid, COLOR_YELLOW, "RACE: If you already done creating race checkpoints, press ENTER!");
    SendClientMessage(playerid, -1, "RACE: Remember, The maximum race checkpoints for each races is "#MAX_CP_RACE"");
    PRaceID[playerid] = raceid;
    return 1;
}

stock GetClosetRace(playerid)
{
    new Float:Distance;
    for(new i; i<MAX_RACES; i++)
    {
        Distance = GetDistanceToRace(playerid, i);
        if(Distance < 2.0)
        {
            return i;
        }
    }
    return 0;
}
stock RacePath(id)
{
    new file[128];
    format(file, sizeof file, R_PATH, id);
    return file;
}
stock RacePath2(id)
{
    new file[128];
    format(file, sizeof file, R_PATH2, id);
    return file;
}
stock SetCP(playerid, PrevCP, NextCP, MaxCP) //Credits to Ryder's Race script
{
    if(NextCP == MaxCP) SetPlayerRaceCheckpoint(playerid, 1, CPCoords[PrevCP][0], CPCoords[PrevCP][1], CPCoords[PrevCP][2], CPCoords[NextCP][0], CPCoords[NextCP][1], CPCoords[NextCP][2], CP_SIZE);
    else SetPlayerRaceCheckpoint(playerid, 0, CPCoords[PrevCP][0], CPCoords[PrevCP][1], CPCoords[PrevCP][2], CPCoords[NextCP][0], CPCoords[NextCP][1], CPCoords[NextCP][2], CP_SIZE);
    return 1;
}
RacePath - (All races information such as RaceName etc...)
RacePath2 - (All race checkpoints stored here)

1.ini (RaceName: Idiot)

Код:
RaceName=Idiot
StartX=181.412811
StartY=-72.240425
StartZ=0.979114
StartCPSize=9.000000
TotalCP=6
CP1.ini

Код:
CP_0_PosX=200.179260
CP_0_PosY=-72.320594
CP_0_PosZ=0.994084
CP_1_PosX=228.868057
CP_1_PosY=-72.079696
CP_1_PosZ=0.999921
CP_2_PosX=256.294525
CP_2_PosY=-72.274017
CP_2_PosZ=1.000924
CP_3_PosX=284.760589
CP_3_PosY=-72.383628
CP_3_PosZ=1.005748
CP_4_PosX=328.504180
CP_4_PosY=-72.554306
CP_4_PosZ=0.995686
CP_5_PosX=330.335601
CP_5_PosY=-72.561553
CP_5_PosZ=0.995421
Reply
#2

pawn Код:
for(new c=0; c < RaceInfo[x][TotalCP]; c++)
            {
                format(string, sizeof string, "CP_%d_PosX", c); CPCoords[c][0] = dini_Float(RacePath2(x), string);
                format(string, sizeof string, "CP_%d_PosY", c); CPCoords[c][1] = dini_Float(RacePath2(x), string);
                format(string, sizeof string, "CP_%d_PosZ", c); CPCoords[c][0] = dini_Float(RacePath2(x), string);
            }
I saw potential mistakes here.
CPCoords[c][0]=...
CPCoords[c][1]=...
CPCoords[c][0]=... <- [2]?
Also, in my opinion CPCoords[c][0] should look like this CPCoords[x][c][0]. Reason why I think it should be like that is: for each race there are different checpoint's coordinates. So their variable have to be different, they store different values.

I don't think this makes the bug you are saying, but still mistakes.
Reply
#3

Thanks for making out the point let me try it.
I will edit this post regarding to the topic in the moment!
Reply
#4

Put CProgess[playerid] = 0; when player joins race.
Reply
#5

Now another new problem all works fine

Except, If i go to the next race checkpoint it somehow trolls me and disappears.
I've to go in Starting Point again to see that next race checkpoint and disappears doing this step will make you crazy.

How to fix it?

pawn Код:
public OnPlayerEnterDynamicRaceCP(playerid, checkpointid)
{
    for(new i=0; i<MAX_RACES; i++)
    {
        if(checkpointid == StartCP[i])
        {
            CPProgess[playerid] = 0;
            RaceID[playerid] = i;
            SetCP(playerid, RaceID[playerid], CPProgess[playerid], CPProgess[playerid]+1, TotalCP[RaceID[playerid]]);
        }
    }
    return 1;
}

stock SetCP(playerid, id, PrevCP, NextCP, MaxCP)
{
    if(NextCP == MaxCP) SetPlayerRaceCheckpoint(playerid, 1, CPCoords[id][PrevCP][0], CPCoords[id][PrevCP][1], CPCoords[id][PrevCP][2], CPCoords[id][NextCP][0], CPCoords[id][NextCP][1], CPCoords[id][NextCP][2], CP_SIZE);
    else SetPlayerRaceCheckpoint(playerid, 0, CPCoords[id][PrevCP][0], CPCoords[id][PrevCP][1], CPCoords[id][PrevCP][2], CPCoords[id][NextCP][0], CPCoords[id][NextCP][1], CPCoords[id][NextCP][2], CP_SIZE);
    printf("RaceID: %i", id);
}
stock LoadRaceCP(loop, id)
{
    new string[90];
    format(string, sizeof string, "CP_%i_X", loop);
    CPCoords[id][loop][0] = dini_Float(RaceCPath(id), string);
    format(string, sizeof string, "CP_%i_Y", loop);
    CPCoords[id][loop][1] = dini_Float(RaceCPath(id), string);
    format(string, sizeof string, "CP_%i_Z", loop);
    CPCoords[id][loop][2] = dini_Float(RaceCPath(id), string);
}
stock LoadRace(id, Float:nextx, Float:nexty, Float:nextz)
{
    new string[128];
    format(RaceInfo[id][RaceName], 128, "%s", dini_Get(RaceIPath(id), "RaceName"));
    RaceInfo[id][SPo][0] = dini_Float(RaceIPath(id), "StartPointX");
    RaceInfo[id][SPo][1] = dini_Float(RaceIPath(id), "StartPointY");
    RaceInfo[id][SPo][2] = dini_Float(RaceIPath(id), "StartPointZ");
    RaceInfo[id][SPS] = dini_Float(RaceIPath(id), "StartCPSize");
    format(string, sizeof string, "RaceName: %s\nRace ID: %i\nTotal Race CP: %i", RaceInfo[id][RaceName], id, TotalCP[id]);
    StartLabel[id] = CreateDynamic3DTextLabel(string, -1, RaceInfo[id][SPo][0], RaceInfo[id][SPo][1], RaceInfo[id][SPo][2], 50.0, INVALID_PLAYER_ID, INVALID_VEHICLE_ID, 0, 0, -1, -1, 40.0);
    StartCP[id] = CreateDynamicRaceCP(0, RaceInfo[id][SPo][0], RaceInfo[id][SPo][1], RaceInfo[id][SPo][2], nextx, nexty, nextz, RaceInfo[id][SPS], 0, -1, -1, 40.0);
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)