Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #if defined _zRace
- #endinput
- #endif
- #define _zRace
- #if !defined mysql_connect
- #error "Es necesario para que zRace funcione que sea agregado el include MySQL"
- #endif
- #define MAX_RACES 20
- #define MAX_STEPS 50
- #define MESSAGE_PRINTABLE (true)
- #define MESSAGE_NO_PRINTABLE (false)
- #define INVALID_RACE_ID (-1)
- enum RacePlayerEN{
- pCount,
- pRace,
- };
- enum RaceEnum{
- rID,
- rName,
- rExist,
- rStart,
- Float:rPos[4],
- rVW,
- rINT
- };
- enum ARRAY_TRACK_RACEs{
- rID,
- rExist,
- rOwner,
- Float:rPos[4]
- };
- new ARRAY_TRACK_RACE[MAX_RACES][MAX_STEPS][ARRAY_TRACK_RACEs],
- n@dRace[MAX_RACES][RaceEnum],
- DataRace[MAX_PLAYERS][RacePlayerEN],
- gRaceConections;
- #define RaceExist(%1) (0 <= (%1) < MAX_RACES && _Valid@Race((%1)))
- #define rDATA_SV "localhost"
- #define rDATA_USER "root"
- #define rDATA_SQL "x367"
- #define rDATA_PASS "holamundo12345"
- stock Float:cache_get_field_floatr(row, const field_name[])
- {
- new
- str[16];
- cache_get_field_content(row, field_name, str, gRaceConections);
- return floatstr(str);
- }
- stock sql@ConnectRace(){
- gRaceConections = mysql_connect(rDATA_SV,
- rDATA_USER,
- rDATA_SQL,
- rDATA_PASS);
- }
- cache_get_field_intr(row, const field_name[]){
- new
- str[12];
- cache_get_field_content(row, field_name, str, gRaceConections);
- return strval(str);
- }
- public OnFilterScriptInit()
- {
- c@_InitRace();
- #if defined RX_OnFilterScriptInit
- return RX_OnFilterScriptInit();
- #else
- return 1;
- #endif
- }
- public OnGameModeInit()
- {
- c@_InitRace();
- #if defined RX_OnGameModeInit
- return RX_OnGameModeInit();
- #else
- return 1;
- #endif
- }
- public OnPlayerEnterRaceCheckpoint(playerid)
- {
- c@EnterPlayerCP(playerid);
- #if defined RX_OnPlayerEnterRaceCheckpoint
- return RX_OnPlayerEnterRaceCheckpoint(playerid);
- #else
- return 1;
- #endif
- }
- stock GetTrackRestants(playerid, raceid)
- {
- new count;
- if(RaceExist(raceid))
- {
- for(new i = GetPlayerStep(playerid); i != MAX_STEPS; i ++)
- {
- if(TrackExist(raceid, i) && ARRAY_TRACK_RACE[raceid][i][rOwner] == n@dRace[raceid][rID])
- {
- count ++;
- }
- }
- }
- return (count-1);
- }
- stock GetNextFinish(playerid){
- new raceid = GetPlayerRace(playerid);
- if(!RaceExist(raceid))
- return 0;
- new
- count,
- total = GetPlayerStep(playerid);
- for(new i; i != MAX_STEPS; i ++)
- {
- if(ARRAY_TRACK_RACE[raceid][i][rExist] && ARRAY_TRACK_RACE[raceid][i][rOwner] == n@dRace[raceid][rID])
- {
- if(count > total)
- {
- return i;
- }
- count ++;
- }
- }
- return 0;
- }
- stock c@EnterPlayerCP(playerid)
- {
- new
- raceid = GetPlayerRace(playerid),
- step = GetPlayerStep(playerid),
- left = GetTrackRestants(playerid, raceid),
- next = GetNextFinish(playerid);
- if(left < 1)
- {
- CallLocalFunction("OnPlayerFinishRace", "d", playerid);
- DataRace[playerid][pCount] = 0;
- }
- else
- {
- DataRace[playerid][pCount]++;
- PlayerPlaySound(playerid, 1057, 0.0, 0.0, 0.0);
- CallLocalFunction("OnPlayerTrackRace", "dddd", playerid, raceid, left, left);
- SetRaceCPEx(playerid, raceid, ((step > 0) ? (step+1) : (step)), next);
- }
- }
- forward OnPlayerFinishRace(playerid, raceid);
- forward OnPlayerTrackRace(playerid, raceid, steps, left);
- #if defined _ALS_OnPlayerEnterRaceCP
- #undef OnPlayerEnterRaceCheckpoint
- #else
- #define _ALS_OnPlayerEnterRaceCP
- #endif
- #define OnPlayerEnterRaceCheckpoint RX_OnPlayerEnterRaceCheckpoint
- #if defined RX_OnPlayerEnterRaceCheckpoint
- forward RX_OnPlayerEnterRaceCheckpoint(playerid);
- #endif
- #if defined _ALS_OnFilterScriptInit
- #undef OnFilterScriptInit
- #else
- #define _ALS_OnFilterScriptInit
- #endif
- #if defined _ALS_OnGameModeInit
- #undef OnGameModeInit
- #else
- #define _ALS_OnGameModeInit
- #endif
- #define OnFilterScriptInit RX_OnFilterScriptInit
- #define OnGameModeInit RX_OnGameModeInit
- #if defined RX_OnFilterScriptInit
- forward RX_OnFilterScriptInit();
- #endif
- #if defined RX_OnGameModeInit
- forward RX_OnGameModeInit();
- #endif
- stock static c@_InitRace()
- {
- sql@ConnectRace();
- if(mysql_errno(gRaceConections) != 0)
- return print("DATABASE 'RACES' no conected");
- mysql_function_query(gRaceConections, "SELECT * FROM `races`", true, "sql@LoadRace_", "");
- return 1;
- }
- forward sql@LoadRace_(); public sql@LoadRace_()
- {
- if(mysql_errno(gRaceConections) != 0)
- return 0;
- new rows, fields;
- cache_get_data(rows, fields, gRaceConections);
- for (new i = 0; i < rows; i ++) if (i < MAX_RACES)
- {
- n@dRace[i][rExist] = true;
- n@dRace[i][rID] = cache_get_field_intr(i, "id");
- n@dRace[i][rINT] = cache_get_field_intr(i, "int");
- n@dRace[i][rVW] = cache_get_field_intr(i, "vw");
- n@dRace[i][rPos][0] = cache_get_field_floatr(i, "pos0");
- n@dRace[i][rPos][1] = cache_get_field_floatr(i, "pos1");
- n@dRace[i][rPos][2] = cache_get_field_floatr(i, "pos2");
- n@dRace[i][rPos][3] = cache_get_field_floatr(i, "pos3");
- cache_get_field_content(i, "name", n@dRace[i][rName], gRaceConections);
- }
- new
- str[64];
- for (new i = 0; i < MAX_RACES; i ++) if (n@dRace[i][rExist])
- {
- format(str, sizeof(str), "SELECT * FROM `tracks` WHERE `raceowner` = '%d'", n@dRace[i][rID]);
- mysql_function_query(gRaceConections, str, true, "sql@LoadTracks", "d", i);
- }
- return 1;
- }
- forward sql@LoadTracks(track);
- public sql@LoadTracks(track)
- {
- new
- rows,
- fields;
- cache_get_data(rows, fields, gRaceConections);
- for (new i = 0; i != rows; i ++)
- {
- ARRAY_TRACK_RACE[track][i][rExist] = true;
- ARRAY_TRACK_RACE[track][i][rID] = cache_get_field_intr(i, "ID");
- ARRAY_TRACK_RACE[track][i][rOwner] = cache_get_field_intr(i, "raceowner");
- ARRAY_TRACK_RACE[track][i][rPos][0] = cache_get_field_floatr(i, "pos0");
- ARRAY_TRACK_RACE[track][i][rPos][1] = cache_get_field_floatr(i, "pos1");
- ARRAY_TRACK_RACE[track][i][rPos][2] = cache_get_field_floatr(i, "pos2");
- printf("i: %d, Owner %d (rID: %d)", i , ARRAY_TRACK_RACE[track][i][rOwner], ARRAY_TRACK_RACE[track][i][rID]);
- }
- return 1;
- }
- stock TrackCountOfRace(raceid)
- {
- new
- c,
- i;
- while(i < MAX_STEPS)
- {
- printf("i: %d, Owner %d, IDRace %d", i, ARRAY_TRACK_RACE[raceid][i][rOwner], n@dRace[raceid][rID]);
- if(TrackExist(raceid, i) && ARRAY_TRACK_RACE[raceid][i][rOwner] == n@dRace[raceid][rID])
- {
- printf("TrackCountOfRace: %d", i);
- c ++;
- }
- i ++;
- }
- return (c);
- }
- stock StartRace(playerid, raceid)
- {
- if(RaceExist(raceid)){
- DataRace[playerid][pRace] = raceid;
- DataRace[playerid][pCount] = 0;
- new step = GetPlayerStep(playerid),
- next = step+1;
- SetRaceCPEx(playerid, raceid, step, next);
- }
- return 1;
- }
- stock GetTrackPos(raceid, trackid, &Float:X, &Float:Y, &Float:Z)
- {
- if((RaceExist(raceid) || TrackExist(raceid, trackid)))
- {
- X = ARRAY_TRACK_RACE[raceid][trackid][rPos][0];
- Y = ARRAY_TRACK_RACE[raceid][trackid][rPos][1];
- Z = ARRAY_TRACK_RACE[raceid][trackid][rPos][2];
- return 1;
- }
- return 0;
- }
- stock GetPlayerStep(playerid)
- {
- if(IsPlayerConnected(playerid))
- {
- if(RaceExist(GetPlayerRace(playerid)))
- {
- return (DataRace[playerid][pCount]);
- }
- }
- return 0;
- }
- stock GetPlayerRace(playerid)
- {
- if(IsPlayerConnected(playerid))
- {
- if(RaceExist(DataRace[playerid][pRace]))
- {
- return (DataRace[playerid][pRace]);
- }
- }
- return 0;
- }
- stock SetRaceCPEx(playerid, raceid, step, next)
- {
- if(IsPlayerConnected(playerid))
- {
- if(RaceExist(DataRace[playerid][pRace]))
- {
- SetPlayerRaceCheckpoint(playerid,0, ARRAY_TRACK_RACE[raceid][step][rPos][0], ARRAY_TRACK_RACE[raceid][step][rPos][1], ARRAY_TRACK_RACE[raceid][step][rPos][2],
- ARRAY_TRACK_RACE[raceid][next][rPos][0], ARRAY_TRACK_RACE[raceid][next][rPos][1], ARRAY_TRACK_RACE[raceid][next][rPos][2], 5.0);
- }
- }
- return 1;
- }
- stock CreateTrack(raceid, Float:sX, Float:sY, Float:sZ)
- {
- if(RaceExist(raceid))
- {
- new query[128];
- for(new i; i != MAX_STEPS; i++) if(!ARRAY_TRACK_RACE[raceid][i][rExist])
- {
- ARRAY_TRACK_RACE[raceid][i][rExist] = true;
- ARRAY_TRACK_RACE[raceid][i][rOwner] = n@dRace[raceid][rID];
- format(query, sizeof(query), "INSERT INTO `tracks` (`raceowner`) VALUES('%d')", raceid);
- mysql_tquery(gRaceConections, query, "SQLCreatetRack", "iifff", raceid, i, sX, sY, sZ);
- return i;
- }
- }
- return -1;
- }
- forward SQLCreatetRack(raceid, id, Float:x, Float:y, Float:z); public SQLCreatetRack(raceid, id, Float:x, Float:y, Float:z)
- {
- if(TrackExist(raceid, id))
- {
- ARRAY_TRACK_RACE[raceid][id][rID] = cache_insert_id(gRaceConections);
- SetTrackPos(raceid, id, x, y, z);
- return 1;
- }
- return 0;
- }
- stock StrAvailableRaces()
- {
- new
- stri[34], c;
- for(new i; i != MAX_RACES; i ++) if(RaceExist(i))
- {
- format(stri, sizeof(stri), "%s%d, ", stri, i);
- c++;
- }
- if(c < 1){
- format(stri, sizeof(stri), "Nothing ..");
- }
- return stri;
- }
- stock SetRacePos(raceid, Float:X, Float:Y, Float:Z)
- {
- if(RaceExist(raceid))
- {
- n@dRace[raceid][rPos][0] = X;
- n@dRace[raceid][rPos][1] = Y;
- n@dRace[raceid][rPos][2] = Z;
- return 1;
- }
- return 0;
- }
- stock GetRacePos(raceid, &Float:X, &Float:Y, &Float:Z)
- {
- if(RaceExist(raceid)){
- X = n@dRace[raceid][rPos][0];
- Y = n@dRace[raceid][rPos][1];
- Z = n@dRace[raceid][rPos][2];
- return 1;
- }
- return 0;
- }
- stock GetRaceFacingAngle(raceid, &Float:R)
- {
- if(RaceExist(raceid)){
- R = n@dRace[raceid][rPos][3];
- return 1;
- }
- return 0;
- }
- stock SetRaceFacingAngle(raceid, Float:R)
- {
- if(RaceExist(raceid)){
- n@dRace[raceid][rPos][3] = R;
- return 1;
- }
- return 0;
- }
- stock GetRaceVirtualWorld(raceid)
- {
- if(RaceExist(raceid)){
- return (n@dRace[raceid][rVW]);
- }
- return 0;
- }
- stock GetRaceInterior(raceid)
- {
- if(RaceExist(raceid)){
- return (n@dRace[raceid][rINT]);
- }
- return 0;
- }
- stock SetRaceVirtualWorld(raceid, newvw)
- {
- if(RaceExist(raceid)){
- n@dRace[raceid][rVW] = newvw;
- }
- return 0;
- }
- stock SetRaceInterior(raceid, newint){
- if(RaceExist(raceid)){
- n@dRace[raceid][rINT] = newint;
- }
- return 0;
- }
- stock SetRaceName(raceid, name[]){
- if(RaceExist(raceid)){
- format(n@dRace[raceid][rName], 24, "%s", name);
- }
- return 0;
- }
- stock CreateRace(Float:sX, Float:sY, Float:sZ, interior, virtualworld, name[] = "Untitled")
- {
- new
- query[128];
- for(new i; i != MAX_RACES; i++) if(!n@dRace[i][rExist])
- {
- n@dRace[i][rExist] = true;
- SetRaceName(i, name);
- format(query, sizeof(query), "INSERT INTO `races` (`name`) VALUES('%s')", SQL_ReturnEscapedR(n@dRace[i][rName]));
- mysql_tquery(gRaceConections, query, "SqlCreateRC", "ifffii", i, sX, sY, sZ, interior, virtualworld);
- return i;
- }
- return -1;
- }
- forward SqlCreateRC(id, Float:sX, Float:sY, Float:sZ, interior, virtualworld); public SqlCreateRC(id, Float:sX, Float:sY, Float:sZ, interior, virtualworld)
- {
- if(RaceExist(id)){
- n@dRace[id][rID] = cache_insert_id(gRaceConections);
- n@dRace[id][rPos][0] = sX;
- n@dRace[id][rPos][1] = sY;
- n@dRace[id][rPos][2] = sZ;
- n@dRace[id][rINT] = interior;
- n@dRace[id][rVW] = virtualworld;
- UpdateRaceDatabaseAll(id);
- return 1;
- }
- return 0;
- }
- stock UpdateRaceDatabaseAll(raceid){
- if(!n@dRace[raceid][rExist])
- return 0;
- static
- Float:pPos[4],
- str[64];
- GetRacePos(raceid, pPos[0], pPos[1], pPos[2]);
- GetRaceFacingAngle(raceid, pPos[3]);
- for(new i = 0; i != 4; i ++){
- format(str, sizeof(str), "pos%d", i);
- UpdateRowRaceFloat(raceid, str, pPos[i]);
- }
- UpdateRowRaceInt(raceid, "vw", GetRaceVirtualWorld(raceid));
- UpdateRowRaceInt(raceid, "int", GetRaceInterior(raceid));
- return 1;
- }
- stock UpdateRowRaceStr(id, rUpdate[], variable[])
- {
- if(RaceExist(id)){
- new query[200];
- format(query, sizeof(query), "UPDATE `races` SET `%s` = '%s' WHERE `id` = '%d'",
- rUpdate,
- variable,
- n@dRace[id][rID]);
- mysql_tquery(gRaceConections, query, "", "");
- return 1;
- }
- return 0;
- }
- stock SetTrackPos(raceid, trackid, Float:tX, Float:tY, Float:tZ){
- if(!ARRAY_TRACK_RACE[raceid][trackid][rExist])
- return 0;
- ARRAY_TRACK_RACE[raceid][trackid][rPos][0] = tX;
- ARRAY_TRACK_RACE[raceid][trackid][rPos][1] = tY;
- ARRAY_TRACK_RACE[raceid][trackid][rPos][2] = tZ;
- return 1;
- }
- stock cSQL@track(id@race, id@track){
- if(RaceExist(raceid)){
- new query[128];
- format(query, sizeof(query), "UPDATE `tracks` SET `pos0` = '%.4f', `pos1` = '%.4f', `pos2` = '%.4f' WHERE `ID` = '%d'",
- ARRAY_TRACK_RACE[raceid][trackid][rPos][0],
- ARRAY_TRACK_RACE[raceid][trackid][rPos][1],
- ARRAY_TRACK_RACE[raceid][trackid][rPos][2],
- ARRAY_TRACK_RACE[raceid][trackid][rID]);
- mysql_tquery(gRaceConections, query, "", "");
- return 1;
- }
- return 0;
- }
- stock TrackExist(r_@i, r_@t){
- return ((0 <= (r_@i) < MAX_STEPS && (ARRAY_TRACK_RACE[r_@i][r_@t][rExist])));
- }
- stock _Valid@Race(r_@i){
- return (n@dRace[r_@i][rExist]);
- }
- stock UpdateRowRaceFloat(id, rUpdate[], Float:variable)
- {
- if(RaceExist(id)){
- new query[200];
- format(query, sizeof(query), "UPDATE `races` SET `%s` = '%f' WHERE `id` = '%d'",
- rUpdate,
- variable,
- n@dRace[id][rID]);
- mysql_tquery(gRaceConections, query, "", "");
- return 1;
- }
- return 0;
- }
- stock UpdateRowRaceInt(id, rUpdate[], variable)
- {
- if(RaceExist(id)){
- new query[200];
- format(query, sizeof(query), "UPDATE `races` SET `%s` = '%d' WHERE `id` = '%d'",
- rUpdate,
- variable,
- n@dRace[id][rID]);
- mysql_tquery(gRaceConections, query, "", "");
- }
- return 1;
- }
- stock SQL_ReturnEscapedR(string[])
- {
- new
- entry[256];
- mysql_real_escape_string(string, entry, gRaceConections);
- return entry;
- }
- stock GetParticipants(raceid = 0){
- if(RaceExist(id))
- {
- new c_@,
- i;
- while(i < GetMaxPlayers())
- {
- if(IsPlayerConnected(playerid) && Participante[i] == raceid){
- c_@ ++;
- }
- i ++;
- }
- return (c_@);
- }
- return 0;
- }
- stock GetActiveRaces()
- {
- new
- _@c,
- i;
- while(i < (MAX_RACES))
- {
- if(RaceExist(i)){
- _@c ++;
- }
- i++;
- }
- return (_@c);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement