Trying to create a vehicle system
#1

Hi guys, how are you?
I'm trying to create a vehicle system for my RP Gamemode.
I've made something like that, but I'm not sure if that's how you make a vehicle system.

OnGameModeInit:
pawn Код:
for (new i = 0; i < MAX_VEHICLES; i++) {
        if (GetVehicleModel(i)) {
            format(szQuery, sizeof(szQuery), "SELECT `vid` FROM `vehicles` WHERE `vid` = '%d'", i);
            mysql_function_query(dbHandle, szQuery, true, "CheckExistVehicle", "d", i);
        }
    }
CheckExistVehicle:
pawn Код:
public CheckExistVehicle(vehicleid) {
    new rows, fields;
    cache_get_data(rows, fields);
    if (rows) {
        format(szQuery, sizeof(szQuery), "SELECT `model`,`posX`,`posY`,`posZ`,`angle`,`color1`,`color2`,`fuel`,`rental`,`owned`,`owner`,`factionowned`,`factionid` FROM `vehicles` WHERE `vid` = '%d'", vehicleid);
        mysql_function_query(dbHandle, szQuery, true, "GetVehicleInfo", "d", vehicleid);
    }
    else {
        vInfo[vehicleid][vModel] = GetVehicleModel(vehicleid);
        GetVehiclePos(vehicleid, vInfo[vehicleid][vPos][0], vInfo[vehicleid][vPos][1], vInfo[vehicleid][vPos][2]);
        GetVehicleZAngle(vehicleid, vInfo[vehicleid][vAngle]);
        if (GetVehicleModel(vehicleid) == 596 || GetVehicleModel(vehicleid) == 427) { // Police Car (LSPD) | FBI Truck
            vInfo[vehicleid][vColor1] = 0;
            vInfo[vehicleid][vColor2] = 1;
        }
        else if (GetVehicleModel(vehicleid) == 490) { // FBI Rancher
            vInfo[vehicleid][vColor1] = 0;
            vInfo[vehicleid][vColor2] = 0;
        }
        else if (GetVehicleModel(vehicleid) == 420 || GetVehicleModel(vehicleid) == 438) { // Taxies
            vInfo[vehicleid][vColor1] = 6;
            vInfo[vehicleid][vColor2] = 6;
        }
        else {
            GetVehicleColor(vehicleid, vInfo[vehicleid][vColor1], vInfo[vehicleid][vColor2]);
        }
        vInfo[vehicleid][vEngine] = false;
        vInfo[vehicleid][vLocked] = true;
        vInfo[vehicleid][vFuel] = 100;
        vInfo[vehicleid][vRental] = 0;
        vInfo[vehicleid][vRented] = false;
        format(vInfo[vehicleid][vRentOwner], 24, "%s", "None");
        vInfo[vehicleid][vOwned] = 0;
        format(vInfo[vehicleid][vOwner], 24, "%s", "None");
        vInfo[vehicleid][vFactionOwned] = 0;
        vInfo[vehicleid][vFactionID] = -1;
       
        format(szQuery, sizeof(szQuery), "INSERT INTO `vehicles` VALUES(NULL, '%d', '%d', '%f', '%f', '%f', '%f', '%d', '%d', '%d', '%d', '%d', '%s', '%d', '%d')", vehicleid, vInfo[vehicleid][vModel], vInfo[vehicleid][vPos][0], vInfo[vehicleid][vPos][1], vInfo[vehicleid][vPos][2], vInfo[vehicleid][vAngle], vInfo[vehicleid][vColor1], vInfo[vehicleid][vColor2], vInfo[vehicleid][vFuel], vInfo[vehicleid][vRental], vInfo[vehicleid][vOwned], vInfo[vehicleid][vOwner], 0, -1);
        mysql_function_query(dbHandle, szQuery, false, "", "");
       
        ChangeVehicleColor(vehicleid, vInfo[vehicleid][vColor1], vInfo[vehicleid][vColor2]);
    }
    return 1;
}
GetVehicleInfo:
pawn Код:
public GetVehicleInfo(vehicleid) {
    new rows, fields, temp[30];
    cache_get_data(rows, fields);
   
    cache_get_field_content(0, "model", temp);      vInfo[vehicleid][vModel] = strval(temp);
    cache_get_field_content(0, "posX", temp);       vInfo[vehicleid][vPos][0] = floatstr(temp);
    cache_get_field_content(0, "posY", temp);       vInfo[vehicleid][vPos][1] = floatstr(temp);
    cache_get_field_content(0, "posZ", temp);       vInfo[vehicleid][vPos][2] = floatstr(temp);
    cache_get_field_content(0, "angle", temp);      vInfo[vehicleid][vAngle]  = floatstr(temp);
    cache_get_field_content(0, "color1", temp);     vInfo[vehicleid][vColor1] = strval(temp);
    cache_get_field_content(0, "color2", temp);     vInfo[vehicleid][vColor2] = strval(temp);
    cache_get_field_content(0, "fuel", temp);       vInfo[vehicleid][vFuel] = strval(temp);
    cache_get_field_content(0, "rental", temp);     vInfo[vehicleid][vRental] = strval(temp);
    cache_get_field_content(0, "owned", temp);      vInfo[vehicleid][vOwned] = strval(temp);
    cache_get_field_content(0, "owner",             vInfo[vehicleid][vOwner]);
       
    ChangeVehicleColor(vehicleid, vInfo[vehicleid][vColor1], vInfo[vehicleid][vColor2]);
    return 1;
}
Is it how it should be done? Are there any improvements I can make?
Thanks
Reply
#2

pawn Код:
for (new i = 0; i < MAX_VEHICLES; i++) {
    if (GetVehicleModel(i)) {
        format(szQuery, sizeof(szQuery), "SELECT `vid` FROM `vehicles` WHERE `vid` = '%d'", i);
        mysql_function_query(dbHandle, szQuery, true, "CheckExistVehicle", "d", i);
    }
}
This might be too heavy for your database to handle (remember that database query is the most time-and-resource-eating process). It'd be better if you loaded all data from database, and then worked on cached data
Reply
#3

Quote:
Originally Posted by Misiur
Посмотреть сообщение
pawn Код:
for (new i = 0; i < MAX_VEHICLES; i++) {
    if (GetVehicleModel(i)) {
        format(szQuery, sizeof(szQuery), "SELECT `vid` FROM `vehicles` WHERE `vid` = '%d'", i);
        mysql_function_query(dbHandle, szQuery, true, "CheckExistVehicle", "d", i);
    }
}
This might be too heavy for your database to handle (remember that database query is the most time-and-resource-eating process). It'd be better if you loaded all data from database, and then worked on cached data
Yes, it might be.
How can I load them all together and not one-by-one?
Reply
#4

You might want to tweak it further, but it should work nicely.

pawn Код:
for (new i = 0; i < MAX_VEHICLES; i++) {
    if (GetVehicleModel(i)) {
        format(szQuery, sizeof(szQuery), "SELECT `vid` FROM `vehicles` WHERE `vid` = '%d'", i);
        mysql_function_query(dbHandle, szQuery, true, "CheckExistVehicle", "d", i);
    }
}

//Changes to
mysql_function_query(dbHandle, "SELECT * FROM `vehicles`", true, "CheckExistVehicle", "");

public CheckExistVehicle() {
    new rows, fields, used_ids[MAX_VEHICLES], temp[30];
    cache_get_data(rows, fields);
    if (rows) {
        new vehicleid;
        for(new i = 0; i < rows; i++) {
            cache_get_field_content(i, "vid", temp);        vehicleid = strval(temp);
            if (GetVehicleModel(vehicleid)) {
                cache_get_field_content(i, "model", temp);      vInfo[vehicleid][vModel] = strval(temp);
                cache_get_field_content(i, "posX", temp);       vInfo[vehicleid][vPos][0] = floatstr(temp);
                cache_get_field_content(i, "posY", temp);       vInfo[vehicleid][vPos][1] = floatstr(temp);
                cache_get_field_content(i, "posZ", temp);       vInfo[vehicleid][vPos][2] = floatstr(temp);
                cache_get_field_content(i, "angle", temp);      vInfo[vehicleid][vAngle]  = floatstr(temp);
                cache_get_field_content(i, "color1", temp);     vInfo[vehicleid][vColor1] = strval(temp);
                cache_get_field_content(i, "color2", temp);     vInfo[vehicleid][vColor2] = strval(temp);
                cache_get_field_content(i, "fuel", temp);       vInfo[vehicleid][vFuel] = strval(temp);
                cache_get_field_content(i, "rental", temp);     vInfo[vehicleid][vRental] = strval(temp);
                cache_get_field_content(i, "owned", temp);      vInfo[vehicleid][vOwned] = strval(temp);
                cache_get_field_content(i, "owner",             vInfo[vehicleid][vOwner]);
               
                ChangeVehicleColor(vehicleid, vInfo[vehicleid][vColor1], vInfo[vehicleid][vColor2]);
               
                used_ids[vehicleid] = 1;
            }
        }
    }
    //After we're done with all the records, we process non-existent in database cars
    new inserts[1024];
    for(new vehicleid = 0; vehicleid < MAX_VEHICLES; vehicleid++) {
        if(used_ids[vehicleid] || !GetVehicleModel(vehicleid)) continue; //Skip this iteration
        vInfo[vehicleid][vModel] = GetVehicleModel(vehicleid);
        GetVehiclePos(vehicleid, vInfo[vehicleid][vPos][0], vInfo[vehicleid][vPos][1], vInfo[vehicleid][vPos][2]);
        GetVehicleZAngle(vehicleid, vInfo[vehicleid][vAngle]);
        new c1 = -1, c2 = -1;
        switch(GetVehicleModel(vehicleid)) {
            case 596, 427: {
                c1 = 0;
                c2 = 1;
            }
            case 490: {
                c1 = 0;
                c2 = 0;
            }
            case 420, 438: {
                c1 = 6;
                c2 = 6;
            }
            default: {
                GetVehicleColor(vehicleid, c1, c2);
            }
            vInfo[vehicleid][vColor1] = c1;
            vInfo[vehicleid][vColor2] = c2;*/
        }
        vInfo[vehicleid][vEngine] = false;
        vInfo[vehicleid][vLocked] = true;
        vInfo[vehicleid][vFuel] = 100;
        vInfo[vehicleid][vRental] = 0;
        vInfo[vehicleid][vRented] = false;
        vInfo[vehicleid][vRentOwner] = "None";
        vInfo[vehicleid][vOwned] = 0;
        vInfo[vehicleid][vOwner] = "None";
        vInfo[vehicleid][vFactionOwned] = 0;
        vInfo[vehicleid][vFactionID] = -1;        
       
        ChangeVehicleColor(vehicleid, vInfo[vehicleid][vColor1], vInfo[vehicleid][vColor2]);
        new tmp[128];
        format(tmp, sizeof(tmp), "(NULL, '%d', '%d', '%f', '%f', '%f', '%f', '%d', '%d', '%d', '%d', '%d', '%s', '%d', '%d')", vehicleid, vInfo[vehicleid][vModel], vInfo[vehicleid][vPos][0], vInfo[vehicleid][vPos][1], vInfo[vehicleid][vPos][2], vInfo[vehicleid][vAngle], vInfo[vehicleid][vColor1], vInfo[vehicleid][vColor2], vInfo[vehicleid][vFuel], vInfo[vehicleid][vRental], vInfo[vehicleid][vOwned], vInfo[vehicleid][vOwner], 0, -1);
        if(MAX_VEHICLES != (vehicleid + 1)) strcat(tmp, ", ");
        strcat(inserts, tmp);
    }
    new szQuery[1024];
    format(szQuery, sizeof(szQuery), "INSERT INTO `vehicles` VALUES %s", inserts);
    mysql_function_query(dbHandle, szQuery, false, "", "");
    return 1;
}
You might have troubles with string length 1023+1 chars limit, because if the vehicles limit is 2000, your insert string can be way longer than 1k characters. I don't know how to fix it yet in pawn :c
Reply
#5

Quote:
Originally Posted by Misiur
Посмотреть сообщение
You might want to tweak it further, but it should work nicely.

pawn Код:
for (new i = 0; i < MAX_VEHICLES; i++) {
    if (GetVehicleModel(i)) {
        format(szQuery, sizeof(szQuery), "SELECT `vid` FROM `vehicles` WHERE `vid` = '%d'", i);
        mysql_function_query(dbHandle, szQuery, true, "CheckExistVehicle", "d", i);
    }
}

//Changes to
mysql_function_query(dbHandle, "SELECT * FROM `vehicles`", true, "CheckExistVehicle", "");

public CheckExistVehicle() {
    new rows, fields, used_ids[MAX_VEHICLES], temp[30];
    cache_get_data(rows, fields);
    if (rows) {
        new vehicleid;
        for(new i = 0; i < rows; i++) {
            cache_get_field_content(i, "vid", temp);        vehicleid = strval(temp);
            if (GetVehicleModel(vehicleid)) {
                cache_get_field_content(i, "model", temp);      vInfo[vehicleid][vModel] = strval(temp);
                cache_get_field_content(i, "posX", temp);       vInfo[vehicleid][vPos][0] = floatstr(temp);
                cache_get_field_content(i, "posY", temp);       vInfo[vehicleid][vPos][1] = floatstr(temp);
                cache_get_field_content(i, "posZ", temp);       vInfo[vehicleid][vPos][2] = floatstr(temp);
                cache_get_field_content(i, "angle", temp);      vInfo[vehicleid][vAngle]  = floatstr(temp);
                cache_get_field_content(i, "color1", temp);     vInfo[vehicleid][vColor1] = strval(temp);
                cache_get_field_content(i, "color2", temp);     vInfo[vehicleid][vColor2] = strval(temp);
                cache_get_field_content(i, "fuel", temp);       vInfo[vehicleid][vFuel] = strval(temp);
                cache_get_field_content(i, "rental", temp);     vInfo[vehicleid][vRental] = strval(temp);
                cache_get_field_content(i, "owned", temp);      vInfo[vehicleid][vOwned] = strval(temp);
                cache_get_field_content(i, "owner",             vInfo[vehicleid][vOwner]);
               
                ChangeVehicleColor(vehicleid, vInfo[vehicleid][vColor1], vInfo[vehicleid][vColor2]);
               
                used_ids[vehicleid] = 1;
            }
        }
    }
    //After we're done with all the records, we process non-existent in database cars
    new inserts[1024];
    for(new vehicleid = 0; vehicleid < MAX_VEHICLES; vehicleid++) {
        if(used_ids[vehicleid] || !GetVehicleModel(vehicleid)) continue; //Skip this iteration
        vInfo[vehicleid][vModel] = GetVehicleModel(vehicleid);
        GetVehiclePos(vehicleid, vInfo[vehicleid][vPos][0], vInfo[vehicleid][vPos][1], vInfo[vehicleid][vPos][2]);
        GetVehicleZAngle(vehicleid, vInfo[vehicleid][vAngle]);
        new c1 = -1, c2 = -1;
        switch(GetVehicleModel(vehicleid)) {
            case 596, 427: {
                c1 = 0;
                c2 = 1;
            }
            case 490: {
                c1 = 0;
                c2 = 0;
            }
            case 420, 438: {
                c1 = 6;
                c2 = 6;
            }
            default: {
                GetVehicleColor(vehicleid, c1, c2);
            }
            vInfo[vehicleid][vColor1] = c1;
            vInfo[vehicleid][vColor2] = c2;*/
        }
        vInfo[vehicleid][vEngine] = false;
        vInfo[vehicleid][vLocked] = true;
        vInfo[vehicleid][vFuel] = 100;
        vInfo[vehicleid][vRental] = 0;
        vInfo[vehicleid][vRented] = false;
        vInfo[vehicleid][vRentOwner] = "None";
        vInfo[vehicleid][vOwned] = 0;
        vInfo[vehicleid][vOwner] = "None";
        vInfo[vehicleid][vFactionOwned] = 0;
        vInfo[vehicleid][vFactionID] = -1;        
       
        ChangeVehicleColor(vehicleid, vInfo[vehicleid][vColor1], vInfo[vehicleid][vColor2]);
        new tmp[128];
        format(tmp, sizeof(tmp), "(NULL, '%d', '%d', '%f', '%f', '%f', '%f', '%d', '%d', '%d', '%d', '%d', '%s', '%d', '%d')", vehicleid, vInfo[vehicleid][vModel], vInfo[vehicleid][vPos][0], vInfo[vehicleid][vPos][1], vInfo[vehicleid][vPos][2], vInfo[vehicleid][vAngle], vInfo[vehicleid][vColor1], vInfo[vehicleid][vColor2], vInfo[vehicleid][vFuel], vInfo[vehicleid][vRental], vInfo[vehicleid][vOwned], vInfo[vehicleid][vOwner], 0, -1);
        if(MAX_VEHICLES != (vehicleid + 1)) strcat(tmp, ", ");
        strcat(inserts, tmp);
    }
    new szQuery[1024];
    format(szQuery, sizeof(szQuery), "INSERT INTO `vehicles` VALUES %s", inserts);
    mysql_function_query(dbHandle, szQuery, false, "", "");
    return 1;
}
You might have troubles with string length 1023+1 chars limit, because if the vehicles limit is 2000, your insert string can be way longer than 1k characters. I don't know how to fix it yet in pawn :c
Wow! Thank you so much man! Wow!
I'll try it now and update if it works
Rep+!
UPDATE: It works and much faster! Thank you very much bro!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)