29.10.2010, 13:04
(
Последний раз редактировалось Cameltoe; 20.04.2012 в 10:37.
)
- Vehicle Ownership
As there's a lot of people struggling with Mysql, i decided to write an tutorial for you guys.I assume you guys know how to use mysql, and does have some pawn knowledge.
In this tutorial i'll be using Strickens plugin.
First some define's:
pawn Код:
#define SCRIPT_CARS 100 // You could also #undef MAX_VEHICLES and define it once again.. that's really up to you.
#define DEFAULT_VEHICLE_OWNER "Dealership"
#define DEFAULT_SELL_PRICE 50000
As this is an tutorial about Vehicle Ownership and basic mysql im not showing how to import the db structure.
Here's the db structure anyway:
Код:
CREATE TABLE IF NOT EXISTS `vehicles` ( `id` int(5) NOT NULL AUTO_INCREMENT, `owner` varchar(25) NOT NULL, `model` int(5) NOT NULL, `price` int(11) NOT NULL, `x` float NOT NULL, `y` float NOT NULL, `z` float NOT NULL, `a` float NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
To make this really easy i'm using SScanf.
These two enums keeps track of the loaded positions and how many vehicles that is currently loaded.
pawn Код:
enum loadedInfo
{
Vehicles = 0,
}
new LoadedInfo[loadedInfo];
enum posInfo
{
Float: X,
Float: Y,
Float: Z,
Float: R,
}
pawn Код:
enum vInfo // You could really name this anything.. Foo, pop, vehicle, vehicles whatever you like.
{
Id, // This is needed as SScanf loads the car variables through this Enum, i'll get back to that later.
Owner[25],
Model,
Price,
Float:Pos[posInfo], // Arrays ftw.
}
new VehicleInfo[SCRIPT_CARS][vInfo]; // Remember this ? the define we just made.. Great ! you'r actually reading.
pawn Код:
stock LoadVehicles() // You might want to make this an public so you could call it on an timer.
{
new Query[255], id;
format(Query, sizeof(Query), "SELECT * FROM vehicles"); // Formats the query for "mysql_query"
mysql_query(Query); // Querys the "Query" Variable.
mysql_store_result(); // Stores the result from Query
while(mysql_fetch_row(Query,"|")) // Splits the row
{
id = LoadedInfo[Vehicles];
sscanf(Query, "p<|>e<is[25]iiffff>", VehicleInfo[id]); // Pretty neat ehh? [ ID, OWNER, MODEL, PRICE, POS X, POS Y, POS Z, POS A ]
new Color1 = random(126); new Color2 = random(126);
CreateVehicle(VehicleInfo[id][Model],VehicleInfo[id][Pos][X],VehicleInfo[id][Pos][Y],VehicleInfo[id][Pos][Z],VehicleInfo[id][Pos][R],Color1,Color2, 60*10000);
LoadedInfo[Vehicles] = LoadedInfo[Vehicles] + 1;
}
}
SScanf Loads the db vars into our enum in one simple line.
"p<|>" = Delimiter. / Split's the string.
"e<>" = Enum. / Uses the enum we made.
"i" = Int. / Loads int's. Model, Price.
"f" = Float. / Loads Float's. Position.
"s[25]" = String. / Loads String's. Owner.
Command's:
pawn Код:
command(createveh, playerid, params[]) // Create vehicle command, not Tested.
{
new vID, Query[200];
if(sscanf(params, "i", vID)) return SendClientMessage(playerid, COLOR_ADMIN, "Usage: /createveh [ ModelID ]");
new Float:pPOS[4];
GetPlayerPos(playerid, pPOS[0], pPOS[1], pPOS[2]);
GetPlayerFacingAngle(playerid , pPOS[3]);
format(Query, sizeof(Query), "INSERT INTO vehicles (id, owner, model, price, x, y, z, a) VALUES (NULL, '%s', %d, %d, %f, %f, %f, %f);",DEFAULT_VEHICLE_OWNER,vID, DEFAULT_VEHICLE_PRICE, pPOS[0], pPOS[1], pPOS[2], pPOS[3]);
mysql_query(Query);
format(Query, sizeof(Query), "Created an %d at x: %f y: %f z: %f a: %f);",vID,pPOS[0], pPOS[1], pPOS[2], pPOS[3]);
SendClientMessage(playerid, COLOR_ADMIN, Query);
return 1;
}
command(reloadvehicles, playerid, params[]) // Reload's Vehicles
{
for(new i; i < SCRIPT_CARS; i++)
{
if(IsVehicleConnected(i)) DestroyVehicle(i);
}
LoadVehicles();
SendClientMessage(playerid, COLOR_ADMIN, "Vehicle's reloaded");
}
pawn Код:
public OnPlayerEnterVehicle(playerid, vehicleid)
{
if(strmatch(VehicleInfo[vehicleid][Owner], GetName(playerid)))
{
return 1;
}
else
{
// clear animations, cant be bothered searching it up x))
}
return 1;
}
stock strmatch(const String1[], const String2[])
{
if ((strcmp(String1, String2, true, strlen(String2)) == 0) && (strlen(String2) == strlen(String1)))
{
return true;
}
else
{
return false;
}
}
Questions etc will be answered as far as my knowledge reaches.
I need to to credit a couple of people:
SScanf - ******.
Mysql - icognito.
Making everything possible - the sa-mp team.
Writing this shit - Me.
As you can see this tutorial does not include how to make the commands / saving function needed in an vehicle ownership. if many people request i will do it. But for now, I'll leave it as it stands.
Here's some tutorials to fully "complete" this tutorial:
https://sampforum.blast.hk/showthread.php?tid=91130 - By GTA_Rules ( Restrict cars to one name )
https://sampforum.blast.hk/showthread.php?tid=118379 - By [HiC]TheKiller ( Buyable houses )
https://sampforum.blast.hk/showthread.php?tid=179206 - By Kwarde ( house system (with vehicles) )