CMD:createhouse(playerid,params[])
{
if( pinfo[playerid][ Admin] < 5) return 0;
ShowPlayerDialog(playerid,DIALOG_CHOUSE1,DIALOG_STYLE_MSGBOX, "House Creator","Welcome to House Creator. From here you can create the houses you want by following 3 steps!\n"COL_RED"1."COL_WHITE"Choose Enter X,Y,Z\n"COL_RED"2."COL_WHITE"Choose Exit X.Y.Z\n"COL_RED"3."COL_WHITE"Set Virtual World, Interior and Price!","Continue","Cancel");
return 1;
}
case DIALOG_CHOUSE1:
{
if(!response)
{
SendClientMessage(playerid, COLOR_RED,"SERVER: You Choose not to create a house!");
}
else
{
SendClientMessage(playerid, -1," "COL_GREEN"HOUSE CREATOR"COL_WHITE": Choose enter X,Y,Z coordinates and press "COL_YELLOW"N"COL_WHITE" when you decide!");
hcreating[playerid] = 1;
}
}
case DIALOG_CHOUSE2:
{
if(response)
{
new string[240];
new hid;
if(!IsNumeric(inputtext)) return ShowPlayerDialog(playerid,DIALOG_CHOUSE2,DIALOG_STYLE_INPUT,"House Creator","ERROR : You must type an ID(Number)!","Select","");
format(string,sizeof(string),"You have choose EnterX: %f, EnterY: %f, EnterZ: %f\nExitX: %f, ExitY: %f, ExitZ: %f\nInteriorID: %d Please now Set house Virtual World",HouseInfo[hid][XEnt],HouseInfo[hid][YEnt],HouseInfo[hid][ZEnt],HouseInfo[hid][XExit],HouseInfo[hid][YExit],HouseInfo[hid][ZExit],inputtext);
ShowPlayerDialog(playerid, DIALOG_CHOUSE3,DIALOG_STYLE_INPUT,"House Creator", string,"Select","");
HouseInfo[hid][HInterior] = strval(inputtext);
}
}
case DIALOG_CHOUSE3:
{
if(response)
{
new string[240];
new hid;
if(!IsNumeric(inputtext)) return ShowPlayerDialog(playerid,DIALOG_CHOUSE2,DIALOG_STYLE_INPUT,"House Creator","ERROR : You must type an ID(Number)!","Select","");
format(string,sizeof(string),"You have choose EnterX: %f, EnterY: %f, EnterZ: %f\n ExitX: %f, ExitY: %f, ExitZ: %f\nInteriorID: %d Virtual World: %d\n Please now set a price for the house!",HouseInfo[hid][XEnt],HouseInfo[hid][YEnt],HouseInfo[hid][ZEnt],HouseInfo[hid][XExit],HouseInfo[hid][YExit],HouseInfo[hid][ZExit],HouseInfo[hid][HInterior],inputtext);
HouseInfo[hid][HVirtualWorld] = strval(inputtext);
ShowPlayerDialog(playerid, DIALOG_CHOUSE4,DIALOG_STYLE_INPUT,"House Creator", string,"Select","");
}
}
case DIALOG_CHOUSE4:
{
if(response)
{
new string[240];
new hid;
if(!IsNumeric(inputtext)) return ShowPlayerDialog(playerid,DIALOG_CHOUSE2,DIALOG_STYLE_INPUT,"House Creator","ERROR : You must type an ID(Number)!","Select","");
format(string,sizeof(string),"You have choose EnterX: %f, EnterY: %f, EnterZ: %f, ExitX: %f, ExitY: %f, ExitZ: %f, InteriorID: %d Virtual World: %d\n Price: %d. You have created the house!",HouseInfo[hid][XEnt],HouseInfo[hid][YEnt],HouseInfo[hid][ZEnt],HouseInfo[hid][XExit],HouseInfo[hid][YExit],HouseInfo[hid][ZExit],HouseInfo[hid][HInterior],HouseInfo[hid][HPrice],inputtext);
HouseInfo[hid][HPrice] = strval(inputtext);
ShowPlayerDialog(playerid, DIALOG_CHOUSE5,DIALOG_STYLE_MSGBOX,"House Creator", string,"End","");
}
}
case DIALOG_CHOUSE5:
{
if(response)
{
new hid;
CreateHouse(HouseInfo[hid][XEnt],HouseInfo[hid][YEnt],HouseInfo[hid][ZEnt],HouseInfo[hid][XExit],HouseInfo[hid][YExit],HouseInfo[hid][ZExit],HouseInfo[hid][HVirtualWorld],HouseInfo[hid][HInterior],HouseInfo[hid][HPrice]);
printf("ENTER X: %f Y: %f Z: %f EXIT X: %f Y: %f Z: %f vw: %d INTID: %d PRICE : %d",HouseInfo[hid][XEnt],HouseInfo[hid][YEnt],HouseInfo[hid][ZEnt],HouseInfo[hid][XExit],HouseInfo[hid][YExit],HouseInfo[hid][ZExit],HouseInfo[hid][HVirtualWorld],HouseInfo[hid][HInterior],HouseInfo[hid][HPrice]);
}
}
function CreateHouse(Float:Xen,Float:Yen,Float:Zen,Float:Xex,Float:Yex,Float:Zex,virtualworld,interiorid,price)
{
static hCount;
new hid = hCount; hCount += 1;
new string[100];
new query[350];
hid += 1;
HouseInfo[hid][XEnt] = Xen;
HouseInfo[hid][YEnt] = Yen;
HouseInfo[hid][ZEnt] = Zen;
HouseInfo[hid][XExit] = Xex;
HouseInfo[hid][YExit] = Yex;
HouseInfo[hid][ZExit] = Zex;
HouseInfo[hid][HVirtualWorld] = virtualworld;
HouseInfo[hid][HInterior] = interiorid;
HouseInfo[hid][HPrice] = price;
HouseInfo[hid][HOwned] = 0;
HouseInfo[hid][HLocked] = 0;
HouseInfo[hid][HOwnerID] = -1;
HouseInfo[hid][HPickup] = CreateDynamicPickup(HUNOWNED_PICKUP, 1, Xen, Yen, Zen,0,0,-1, 30.0);
format(string,sizeof(string),""COL_GREEN"HouseName"COL_WHITE": No Owner\n"COL_GREEN"Price"COL_WHITE": %d",price);
HouseInfo[hid][H3D] = CreateDynamic3DTextLabel(string, COLOR_YELLOW, Xen, Yen, Zen, 40.0);
mysql_format(mysql,query,sizeof(query), "INSERT INTO `houses` (`Xent`, `Yent`, `Zent`, `XExit`, `YExit`, `ZExit`, `HVirtualWorld`, `HInterior`, `HPrice`,\
`HOwned`, `HLocked`) VALUES (%f, %f, %f, %f, %f, %f, %d, %d, %d, 0, 0)", Xen,Yen,Zen,Xex,Yex,Zex,virtualworld,interiorid,price);
mysql_tquery(mysql, query, "RegisterHouse","i",hid);
return 1;
}
CMD:buyhouse(playerid,params[])
{
new hid = GetHouseID(playerid);
new price = HouseInfo[hid][HPrice];
if(hid == INVALID_HOUSE_ID) return SendClientMessage(playerid, -1,""COL_RED"SERVER"COL_WHITE"Invalid House id!") ;
if(GetPlayerMoney(playerid) < price) return SendClientMessage(playerid, -1, ""COL_RED"SERVER"COL_WHITE": You don't have so much money to buy the house");
if(HouseInfo[hid][HOwned] == 1) return SendClientMessage(playerid,-1,""COL_RED"SERVER"COL_WHITE"You can't buy this house because it is already bought!");
if(HouseInfo[hid][HOwnerID] == pinfo[playerid][ID]) return SendClientMessage(playerid, -1, ""COL_RED"You can't buy your own house!");
//if(!IsPlayerNearHouse(playerid)) return SendClientMessage(playerid, -1, ""COL_RED"SERVER"COL_WHITE"You can't buy a house if you are not near it!");
ShowPlayerDialog(playerid,DIALOG_BUYHOUSE,DIALOG_STYLE_INPUT,"BUY A HOUSE",""COL_RED"You want to buy a house! Since you can afford it please insert HouseName you want!","OK","Cancel");
return 1;
}
case DIALOG_BUYHOUSE:
{
new hid = GetHouseID(playerid);
new query[200];
if(!response) return SendClientMessage(playerid, -1, ""COL_RED"SERVER"COL_WHITE": You decided not to buy a house. The house sale got cancelled!");
if(strlen(inputtext) > 30) return SendClientMessage(playerid, -1, ""COL_RED"SERVER"COL_WHITE"You must set a name less than 30 characters!");
mysql_format(mysql,query,sizeof(query),"UPDATE `houses` SET `HName` = '%e' WHERE `hID` = '%d'",inputtext,hid);
mysql_tquery(mysql,query,"i",hid);
format(HouseInfo[hid][HName],30,"%s",inputtext);
}
LoadHouses()
{
new Cache:result,rows;
new label[250];
result = mysql_query(mysql, "SELECT * FROM `houses`");
if((rows = cache_num_rows()))
{
for(new i=0; i < rows; i++)
{
HouseInfo[i][hID] = cache_get_field_content_int(i, "hID");
HouseInfo[i][XEnt] = cache_get_field_content_float(i, "XEnt"); //we're getting a field 4 from row 0. And since it's an integer, we use cache_get_row_int
HouseInfo[i][YEnt] = cache_get_field_content_float(i, "YEnt"); //Above
HouseInfo[i][ZEnt] = cache_get_field_content_float(i, "ZEnt");//Above
HouseInfo[i][XExit] = cache_get_field_content_float(i, "XExit");//Above. Since player's position is a float, we use cache_get_field_content_float
HouseInfo[i][YExit] = cache_get_field_content_float(i, "YExit");//Above
HouseInfo[i][ZExit] = cache_get_field_content_float(i, "ZExit");//Above
HouseInfo[i][HVirtualWorld] = cache_get_field_content_int(i, "HVirtualWorld");
HouseInfo[i][HInterior] = cache_get_field_content_int(i, "HInterior");
HouseInfo[i][HPrice] = cache_get_field_content_int(i, "HPrice");
HouseInfo[i][HOwnerID] = cache_get_field_content_int(i, "HOwnerID");
HouseInfo[i][HOwned] = cache_get_field_content_int(i, "HOwned");
HouseInfo[i][HLocked] = cache_get_field_content_int(i, "HLocked");
cache_get_field_content(i, "HName", HouseInfo[i][HName], 1, MAX_HOUSE_NAME);
if(HouseInfo[i][HOwned] == 1)
{
format(label,sizeof(label),""COL_GREEN"HouseName"COL_WHITE":%s\n"COL_GREEN"",HouseInfo[i][HName]);
HouseInfo[i][H3D] = CreateDynamic3DTextLabel(label, COLOR_YELLOW, HouseInfo[i][XEnt], HouseInfo[i][YEnt], HouseInfo[i][ZEnt], 40.0);
printf("HOUSE ID : %d Housename : %s FloatX:%f Float Y: %f FloatZ: %f",HouseInfo[i][hID],HouseInfo[i][HName],HouseInfo[i][XEnt], HouseInfo[i][YEnt], HouseInfo[i][ZEnt]);
CreateDynamicPickup(HOWNED_PICKUP, 1, HouseInfo[i][XEnt], HouseInfo[i][YEnt], HouseInfo[i][ZEnt]);
}
else if(HouseInfo[i][HOwned] == 0)
{
format(label,sizeof(label),""COL_GREEN"HouseName"COL_WHITE": No Owner\n"COL_GREEN"Price"COL_WHITE": %d",HouseInfo[i][HPrice]);
HouseInfo[i][H3D] = CreateDynamic3DTextLabel(label, COLOR_YELLOW, HouseInfo[i][XEnt], HouseInfo[i][YEnt], HouseInfo[i][ZEnt], 40.0);
CreateDynamicPickup(HUNOWNED_PICKUP, 1, HouseInfo[i][XEnt], HouseInfo[i][YEnt], HouseInfo[i][ZEnt]);
}
}
printf("Successfully Loaded All Houses! ( %d )",rows);
}
else
{
print("There are no houses to load!");
}
cache_delete(result);
}
function BuyHouse(playerid)
{
new houseid = GetHouseID(playerid); // houseid is a slot not real ID
if(houseid == INVALID_HOUSE_ID) print("ERROR: BuyHouse failed, trying to buy house ID 0!");
else{
new str[128];
HouseInfo[houseid][HOwned] = 1;
HouseInfo[houseid][HOwnerID] = pinfo[playerid][ID]; //when i call loadhouses function i have to use the same somehow so server will understand that ownerid = account id again got it?
GivePlayerMoney(GetPlayerMoney(playerid), -HouseInfo[houseid][HPrice]);
mysql_format(mysql,str,sizeof(str),"UPDATE `houses` SET `HOwnerID`= %d, `HOwned` = 1 WHERE `hID` = %d",pinfo[playerid][ID],HouseInfo[houseid][hID]); // HouseInfo[houseid][hID] its real houseid
mysql_query(mysql,str);
format(str,sizeof(str),""COL_GREEN"HouseName"COL_WHITE":%s\n"COL_GREEN"",HouseInfo[houseid][HName]);
UpdateDynamic3DTextLabelText(HouseInfo[houseid][H3D],COLOR_YELLOW,str);
UpdateHousePickup(playerid);
}
return 1;
}
function RegisterHouse(houseid)
{
HouseInfo[houseid][hID] = cache_insert_id();
printf("House Registered ID : %d",HouseInfo[houseid][hID]);
return 1;
}
mysql_format(mysql,query,sizeof(query), "INSERT INTO `houses` (`Xent`, `Yent`, `Zent`, `XExit`, `YExit`, `ZExit`, `HVirtualWorld`, `HInterior`, `HPrice`,\ `HOwned`, `HLocked`) VALUES (%f, %f, %f, %f, %f, %f, %d, %d, %d, 0, 0, 0, 0)", Xen,Yen,Zen,Xex,Yex,Zex,virtualworld,interiorid,price);
You should save 'HOwned' and 'HLocked' values too.
Код:
mysql_format(mysql,query,sizeof(query), "INSERT INTO `houses` (`Xent`, `Yent`, `Zent`, `XExit`, `YExit`, `ZExit`, `HVirtualWorld`, `HInterior`, `HPrice`,\ `HOwned`, `HLocked`) VALUES (%f, %f, %f, %f, %f, %f, %d, %d, %d, 0, 0, 0, 0)", Xen,Yen,Zen,Xex,Yex,Zex,virtualworld,interiorid,price); |
1) Are you trying to get the house IDs to start at 1 instead of 0?
2) I would recommend making your house count a global variable that is intialized in LoadHouses() because your houseids are likely to become unstable without it. |
1.Yes
2. Could you provide me with an example of how could this be implemented? |
for(new i = 0; i <= rows; i++)
new HouseCount = 0;
LoadHouses()
{
new Cache:result,rows;
new label[250];
result = mysql_query(mysql, "SELECT * FROM `houses`");
HouseCount = cache_num_rows();
for(new i=0; i <= HouseCount; i++)
{
HouseInfo[i+1][hID] = cache_get_field_content_int(i, "hID");
HouseInfo[i+1][XEnt] = cache_get_field_content_float(i, "XEnt"); //we're getting a field 4 from row 0. And since it's an integer, we use cache_get_row_int
HouseInfo[i+1][YEnt] = cache_get_field_content_float(i, "YEnt"); //Above
HouseInfo[i+1][ZEnt] = cache_get_field_content_float(i, "ZEnt");//Above
HouseInfo[i+1][XExit] = cache_get_field_content_float(i, "XExit");//Above. Since player's position is a float, we use cache_get_field_content_float
HouseInfo[i+1][YExit] = cache_get_field_content_float(i, "YExit");//Above
HouseInfo[i+1][ZExit] = cache_get_field_content_float(i, "ZExit");//Above
HouseInfo[i+1][HVirtualWorld] = cache_get_field_content_int(i, "HVirtualWorld");
HouseInfo[i+1][HInterior] = cache_get_field_content_int(i, "HInterior");
HouseInfo[i+1][HPrice] = cache_get_field_content_int(i, "HPrice");
HouseInfo[i+1][HOwnerID] = cache_get_field_content_int(i, "HOwnerID");
HouseInfo[i+1][HOwned] = cache_get_field_content_int(i, "HOwned");
HouseInfo[i+1][HLocked] = cache_get_field_content_int(i, "HLocked");
cache_get_field_content(i, "HName", HouseInfo[i+1][HName], 1, MAX_HOUSE_NAME);
if(HouseInfo[i+1][HOwned] == 1)
{
format(label,sizeof(label),""COL_GREEN"HouseName"COL_WHITE":%s\n"COL_GREEN"",HouseInfo[i+1][HName]);
HouseInfo[i+1][H3D] = CreateDynamic3DTextLabel(label, COLOR_YELLOW, HouseInfo[i+1][XEnt], HouseInfo[i][YEnt], HouseInfo[i][ZEnt], 40.0);
printf("HOUSE ID : %d Housename : %s FloatX:%f Float Y: %f FloatZ: %f",HouseInfo[i+1][hID],HouseInfo[i+1][HName],HouseInfo[i+1][XEnt], HouseInfo[i+1][YEnt], HouseInfo[i+1][ZEnt]);
CreateDynamicPickup(HOWNED_PICKUP, 1, HouseInfo[i+1][XEnt], HouseInfo[i+1][YEnt], HouseInfo[i+1][ZEnt]);
}
else if(HouseInfo[i+1][HOwned] == 0)
{
format(label,sizeof(label),""COL_GREEN"HouseName"COL_WHITE": No Owner\n"COL_GREEN"Price"COL_WHITE": %d",HouseInfo[i][HPrice]);
HouseInfo[i][H3D] = CreateDynamic3DTextLabel(label, COLOR_YELLOW, HouseInfo[i+1][XEnt], HouseInfo[i+1][YEnt], HouseInfo[i+1][ZEnt], 40.0);
CreateDynamicPickup(HUNOWNED_PICKUP, 1, HouseInfo[i+1][XEnt], HouseInfo[i+1][YEnt], HouseInfo[i+1][ZEnt]);
}
}
printf("Successfully Loaded All Houses! ( %d )",rows);
cache_delete(result);
}
That's the reason it's not working correctly. Your houses array begins at 0, but you're trying to start it at 1. What this means is that your loop in LoadHouses() needs to be
pawn Код:
As for using a global housecount variable, all you need to do is create a global variable with a name of your choice, and when you call LoadHouses(), it should set the count equal to the number of rows returned by the SQL query. pawn Код:
|