26.10.2013, 10:47
(
Последний раз редактировалось yoran765; 26.10.2013 в 18:44.
)
Yorans Vehicle System Tutorial!
Hi,
I have decided to start and write a tutorial... Why? Because im pretty bored right now :P Ooh well let me explain what we are going to do!
We are going to create a simple but fast (Dini) vehicle system! This system loads the Vehicle's from a .ini file (Please explain this! I no understand! "I will later on in the tutorial!") Well what are we waiting for? Let's GoGoGo!
I have decided to start and write a tutorial... Why? Because im pretty bored right now :P Ooh well let me explain what we are going to do!
We are going to create a simple but fast (Dini) vehicle system! This system loads the Vehicle's from a .ini file (Please explain this! I no understand! "I will later on in the tutorial!") Well what are we waiting for? Let's GoGoGo!
First we want to include Dini and zcmd(Easier command scripting include). Put this ontop of your script!
PHP код:
#include <zcmd>
#include <dini>
Alright now we will define how many vehicles there are allowed to be on the server. (Don't forget to add this under the includes we just defined!)
PHP код:
#undef MAX_VEHICLES
#define MAX_VEHICLES 5 // 5 For now :)
Alright, now we will make a Enum for the saving and storing of the Vehicles!(Explained in the code itself)
PHP код:
enum vInfo
{
vModel, // Vehicle Model
Float:vPosx, // Vehicle X Position
Float:vPosy, // Vehicle Y Position
Float:vPosz, // Vehicle Z Position
Float:vPosa, // Vehicle A Position
vColor1, // Vehicle Color ID 1
vColor2, // Vehicle Color ID 2
vvw, // The virtual world of the vehicle
vPlate[10], // And last but not least the plate :) Not necessary but why not?
}
// Alright! Not falling asleep? GOOD!
new VehicleInfo[][vInfo] = // How it saves so 540 = vModel - -2937.5 = Float:vPosx - Ect, ect...
{
{540,-2937.5,-2927.5, 19.2819, 268.0262,1,1,0,"GSLS - 0"}, // This is ID 0 and is never used but keep it defined!
{540,-2947.5,-2927.5, 19.2819, 268.0262,1,1,0,"GSLS - 1"}, // ID 1
{540,-2957.5,-2927.5, 19.2819, 268.0262,1,1,0,"GSLS - 2"}, // ID 2
{540,-2967.5,-2927.5, 19.2819, 268.0262,1,1,0,"GSLS - 3"}, // ID 3
{540,-2977.5,-2927.5, 19.2819, 268.0262,1,1,0,"GSLS - 4"} // ID 4
};
Under OnGameModeInit we will place this code: (Explained in the script itself)
PHP код:
public OnGameModeInit()
{
LoadVehicles(); // When you start/open the server it will Load all the vehicles we've set! (ID 1-4)
return 1;
}
OK, Let's make a stock LoadVehicles in this case
PHP код:
stock LoadVehicles()
{
for(new i = 1; i < sizeof(VehicleInfo); i++) // This will loop through all the Vehicle's! Remember when we made the enum? Let me refresh your brain abit and reshow you the code we made : new VehicleInfo[][vInfo] = (This will get/load the vehicles)
{
if( !dini_Exists( CarFile(i) ) ) // If the file doesn't exist
{
dini_Create(CarFile(i)); // We will make the file (CarFile = We will script soon)
printf("%s created!", CarFile(i)); // This will basiclly print witch ID has been made inside the Server Console
new iStr[10]; // String for the plate itself
dini_IntSet(CarFile(i), "model", VehicleInfo[i][vModel]); // Here we set the Vehicle Model
dini_FloatSet(CarFile(i), "x", VehicleInfo[i][vPosx]); // Here we set the position X of the Vehicle
dini_FloatSet(CarFile(i), "y", VehicleInfo[i][vPosy]); // Here we set the position Y of the Vehicle
dini_FloatSet(CarFile(i), "z", VehicleInfo[i][vPosz]); // Here we set the position Z of the Vehicle
dini_FloatSet(CarFile(i), "a", VehicleInfo[i][vPosa]); // Here we set the position A of the Vehicle
dini_IntSet(CarFile(i), "color1", VehicleInfo[i][vColor1]); // Here we set the Color ID 1 of the vehicle
dini_IntSet(CarFile(i), "color2", VehicleInfo[i][vColor2]); // Here we set the Color ID 2 of the vehicle
dini_Set(CarFile(i), "plate", "GSLS-NONREG"); // And last but not least the plate :)
CreateVehicle(VehicleInfo[i][vModel],VehicleInfo[i][vPosx], VehicleInfo[i][vPosy],VehicleInfo[i][vPosz],VehicleInfo[i][vPosa], VehicleInfo[i][vColor1],VehicleInfo[i][vColor2], 500000); // Alright! This is the code to create the vehicle. Zelf explainitory really... But the "500000" is basiclly the respawn timer
format(iStr, sizeof(iStr), "GSLS - %d", i); // We will not format the plate and set it later on
SetVehicleNumberPlate(i, iStr); // Here we set the plate, Depending on the ID so if I was infront of CAR ID 2 it will show GSLS - 2.
}
else
{
LoadVehicle(i); // If the file does exist it will load all the info from the .ini car file. (We will create the stock for it now.)
}
}
}
// NOTE: Compiling now will give you alot of errors!
Creating LoadVehicle stock
PHP код:
stock LoadVehicle(i) // If the .ini car file does exist it will execute this stock.
{
// All these lines will load the .ini CarFile. I explained everything before so this doesn't need explaining I guess :) If you have any questions feel free the tell me via a Reply on this topic.
new iStr[10];
VehicleInfo[i][vModel] = dini_Int(CarFile(i), "model");
VehicleInfo[i][vPosx] = dini_Float(CarFile(i), "x");
VehicleInfo[i][vPosy] = dini_Float(CarFile(i), "y");
VehicleInfo[i][vPosz] = dini_Float(CarFile(i), "z");
VehicleInfo[i][vPosa] = dini_Float(CarFile(i), "a");
VehicleInfo[i][vColor1] = dini_Int(CarFile(i), "color1");
VehicleInfo[i][vColor2] = dini_Int(CarFile(i), "color2");
myStrcpy(VehicleInfo[i][vPlate],dini_Get(CarFile(i), "plate"));
CreateVehicle(VehicleInfo[i][vModel],VehicleInfo[i][vPosx], VehicleInfo[i][vPosy],VehicleInfo[i][vPosz],VehicleInfo[i][vPosa], VehicleInfo[i][vColor1],VehicleInfo[i][vColor2], 500000);
format(iStr, sizeof(iStr), "GSLS - %d", i);
SetVehicleNumberPlate(i, iStr);
SetVehicleToRespawn(i); // Respawn's the vehicle itself
}
PHP код:
stock CarFile(vID)
{
new fn[20];
format(fn, sizeof(fn), "Cars/Car%d.txt", vID);
return fn;
}
Now let's add this stock at the end of script!
PHP код:
stock myStrcpy(dest[], src[])
{
new i = 0;
while ((dest[i] = src[i])) i++;
}
If you have any questions feel free to PM me or make a Reply to this post.
Credits
Yoran - Tutorial
If this helped you feel free to +REP me! Thanks!