Trying to create a vehicle system
#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


Messages In This Thread
Trying to create a vehicle system - by oKzrh - 02.08.2012, 11:23
Re: Trying to create a vehicle system - by Misiur - 02.08.2012, 11:54
Re: Trying to create a vehicle system - by oKzrh - 02.08.2012, 11:59
Re: Trying to create a vehicle system - by Misiur - 02.08.2012, 13:12
Re: Trying to create a vehicle system - by oKzrh - 02.08.2012, 13:23

Forum Jump:


Users browsing this thread: 2 Guest(s)