[Tutorial] Vehicle System (Dini)
#1

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!

First we want to include Dini and zcmd(Easier command scripting include). Put this ontop of your script!
PHP код:
#include <zcmd>
#include <dini> 
Well done! Thats one step through! But we are still not their lets keep on going!


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 :) 
Another step done! Lets keep on going!

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.519.2819268.0262,1,1,0,"GSLS - 0"}, // This is ID 0 and is never used but keep it defined!
    
{540,-2947.5,-2927.519.2819268.0262,1,1,0,"GSLS - 1"}, // ID 1
    
{540,-2957.5,-2927.519.2819268.0262,1,1,0,"GSLS - 2"}, // ID 2
    
{540,-2967.5,-2927.519.2819268.0262,1,1,0,"GSLS - 3"}, // ID 3
    
{540,-2977.5,-2927.519.2819268.0262,1,1,0,"GSLS - 4"// ID 4
}; 
Few! Thats done! Let's get to where the magic really happens!

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;

Alright! Lets get to the loading!

OK, Let's make a stock LoadVehicles in this case
PHP код:
stock LoadVehicles()
{
    for(new 
1sizeof(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_ExistsCarFile(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(iStrsizeof(iStr), "GSLS - %d"i); // We will not format the plate and set it later on
            
SetVehicleNumberPlate(iiStr); // 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! 
Wow! Can't believe you actually read through everything Still... No stopping! Lets keep on going! GOGOGO

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(iStrsizeof(iStr), "GSLS - %d"i);
    
SetVehicleNumberPlate(iiStr);
    
SetVehicleToRespawn(i); // Respawn's the vehicle itself

Last but not least we will add the CarFile stock!
PHP код:
stock CarFile(vID)
{
    new 
fn[20];
    
format(fnsizeof(fn), "Cars/Car%d.txt"vID);
    return 
fn;

NOTE: You have to make a folder called "Cars" inside the folder "Scriptfiles" for the server to run and load!

Now let's add this stock at the end of script!
PHP код:
stock myStrcpy(dest[], src[])
{
    new 
0;
    while ((
dest[i] = src[i])) i++;

DONE! We have finally finished this system! You can add new things to the enum anytime. For example vLocked,
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!
Reply


Messages In This Thread
Vehicle System (Dini) - by yoran765 - 26.10.2013, 10:47
Respuesta: Vehicle System (Dini) - by DanDRT - 26.10.2013, 12:01
Re: Vehicle System (Dini) - by PT - 26.10.2013, 13:40
Re: Vehicle System (Dini) - by yoran765 - 26.10.2013, 14:08
Respuesta: Re: Vehicle System (Dini) - by DanDRT - 26.10.2013, 14:13
Re: Vehicle System (Dini) - by ***Niko*** - 26.10.2013, 16:51
Re: Vehicle System (Dini) - by Vince - 26.10.2013, 18:08
Re: Vehicle System (Dini) - by yoran765 - 26.10.2013, 18:12
Re: Vehicle System (Dini) - by BielCOP - 26.10.2013, 19:49
Re: Vehicle System (Dini) - by Pottus - 26.10.2013, 19:58

Forum Jump:


Users browsing this thread: 1 Guest(s)