Skip to content

Instantly share code, notes, and snippets.

@kaZax
Last active June 12, 2022 04:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaZax/7069003 to your computer and use it in GitHub Desktop.
Save kaZax/7069003 to your computer and use it in GitHub Desktop.
House System for SA-MP server. Author: kaZax
#include <a_samp>
#include <a_mysql>
#include <streamer>
// House System by kaZax
// 03.09.2013
// SAMP-RU.ORG
#define MAX_HOUSES 100
#define DIALOG_BUYHOUSE 600
#define DIALOG_HOUSEINFO 601
#define DIALOG_HOUSEMENU 602
#define DIALOG_INTERIORS 603
#define DIALOG_COSTINFO 604
#define DIALOG_WRITENAME 605
#define white 0xFFFFFFFF
#define green 0x00FF00FF
#define red 0xFF0000FF
#define SQL_HOST "188.120.244.51"
#define SQL_DB "housesys"
#define SQL_USER "samp"
#define SQL_PASS "baiko"
#define SQL_DEBUG
enum HOUSEDATA
{
owner[24],
cost,
interior,
Float:hx,
Float:hy,
Float:hz,
pickup,
Text3D:txt,
name[24],
lock
};
enum PLAYERDATA
{
HouseID,
CurrentID,
bool:HaveHouse
};
new pstruct[MAX_PLAYERS][PLAYERDATA];
new struct[MAX_HOUSES][HOUSEDATA];
new houses;
public OnFilterScriptInit()
{
mysql_connect(SQL_HOST, SQL_USER, SQL_DB, SQL_PASS);
if(mysql_ping()) print("MySQL соединение установлено!");
mysql_query("SET NAMES 'cp1251'");
mysql_query("SET CHARACTER SET 'cp1251'");
#if defined SQL_DEBUG
mysql_debug(1);
#endif
LoadAllHousesFromDB();
DisableInteriorEnterExits();
return 1;
}
public OnFilterScriptExit()
{
SaveAllHousesToDB();
mysql_close();
return 1;
}
public OnPlayerConnect(playerid)
{
FindPlayerHouse(playerid);
return 1;
}
public OnPlayerCommandText(playerid, cmdtext[])
{
new p[2][128], cmd[128], idx;
cmd = strtok(cmdtext, idx);
if(!strcmp(cmd,"/housemenu",true))
{
ShowPlayerHouseMenu(playerid);
return 1;
}
if(!strcmp(cmd,"/exit",true))
{
if(pstruct[playerid][CurrentID] == INVALID_PLAYER_ID)return SendClientMessage(playerid,red,"Вы не входили в дом!");
SetPlayerPosIntVW(playerid,struct[pstruct[playerid][CurrentID]][hx],struct[pstruct[playerid][CurrentID]][hy],struct[pstruct[playerid][CurrentID]][hz],0,0);
pstruct[playerid][CurrentID] = INVALID_PLAYER_ID;
SendClientMessage(playerid,white,"Вы вышли из дома");
return 1;
}
if(!strcmp(cmd, "/savebase", true))
{
if(!IsPlayerAdmin(playerid))return SendClientMessage(playerid,white,"Вы не Rcon администратор!");
SaveAllHousesToDB();
SendClientMessage(playerid,white,"База данных успешно сохранена.");
return 1;
}
if(!strcmp(cmd, "/deletehouse", true))
{
if(!IsPlayerAdmin(playerid))return SendClientMessage(playerid,white,"Вы не Rcon администратор!");
p[0] = strtok(cmdtext,idx);
if(!strlen(p[0]))return SendClientMessage(playerid,white,"CMD: /deletehouse [id]");
if(strval(p[0]) < 0)return SendClientMessage(playerid,white,"ID не меньше 0!");
DestroyDynamic3DTextLabel(struct[strval(p[0])][txt]);
DestroyDynamicPickup(struct[strval(p[0])][pickup]);
struct[strval(p[0])][cost] = 0;
SaveAllHousesToDB();
return 1;
}
if(!strcmp(cmd, "/ch", true))
{
if(!IsPlayerAdmin(playerid))return SendClientMessage(playerid,white,"Вы не Rcon администратор!");
if(houses == MAX_HOUSES)return SendClientMessage(playerid,white,"Лимит домов !");
p[0] = strtok(cmdtext, idx);
if(!strlen(p[0]))return SendClientMessage(playerid,white,"CMD: /ch [цена] [интерьер]");
if(strval(p[0]) < 1)return SendClientMessage(playerid,white,"Цена не ниже {00ff00}$1");
struct[houses][cost] = strval(p[0]);
p[1] = strtok(cmdtext,idx);
if(!strlen(p[1]))return SendClientMessage(playerid,white,"CMD: /ch [цена] [интерьер]");
if(strval(p[1]) > 6 || strval(p[1]) < 1)return SendClientMessage(playerid,white,"Интерьер от 1 до 6");
struct[houses][interior] = strval(p[1]);
new Float:pp[3], str[128];
GetPlayerPos(playerid,pp[0],pp[1],pp[2]);
struct[houses][hx] = pp[0];
struct[houses][hy] = pp[1];
struct[houses][hz] = pp[2];
format(struct[houses][name],24,"HouseName");
format(struct[houses][owner],24,"None");
format(str,sizeof(str),"ID: %d\nДом на продаже!\nЦена: $%d",houses,struct[houses][cost]);
struct[houses][txt] = CreateDynamic3DTextLabel(str, white, pp[0],pp[1],pp[2],20.0,INVALID_PLAYER_ID,INVALID_VEHICLE_ID,0,-1,-1,-1,20.0);
struct[houses][pickup] = CreateDynamicPickup(1273, 23, pp[0],pp[1],pp[2],-1,-1,-1,20.0);
format(str,sizeof(str),"House # {ffffff}%d{00ff00} created",houses);
houses++;
SendClientMessage(playerid,green,str);
SaveAllHousesToDB();
return 1;
}
return 0;
}
public OnPlayerPickUpDynamicPickup(playerid, pickupid)
{
for(new id;id<MAX_HOUSES;id++)
{
if(pickupid == struct[id][pickup])
{
pstruct[playerid][CurrentID] = id;
new str[128];
if(!strcmp(struct[id][owner], "None", true))
{
format(str,sizeof(str),"{ffffff}HOUSE ID: {00ff00}%d\n{ffffff}Цена: {00ff00}$%d\n{ffffff}Желаете приобрести этот дом?",id,struct[id][cost]);
ShowPlayerDialog(playerid,DIALOG_BUYHOUSE,DIALOG_STYLE_MSGBOX,"Покупка дома",str,"Да","Нет");
break;
}
else
{
format(str,sizeof(str),"{ffffff}HOUSE ID: {00ff00}%d\n{ffffff}Владелец: {00ff00}%s\n{ffffff}Название: {00ff00}%s",id,struct[id][owner],struct[id][name]);
ShowPlayerDialog(playerid,DIALOG_HOUSEINFO,DIALOG_STYLE_MSGBOX,"Информация дома",str,"Войти","Отмена");
break;
}
}
}
return 1;
}
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
if(dialogid == DIALOG_HOUSEMENU)
{
if(!response)return 0;
if(listitem == 0)
{
if(struct[pstruct[playerid][HouseID]][lock])struct[pstruct[playerid][HouseID]][lock] = 0;
else struct[pstruct[playerid][HouseID]][lock] = 1;
ShowPlayerHouseMenu(playerid);
}
if(listitem == 1)ShowPlayerDialog(playerid, DIALOG_INTERIORS, DIALOG_STYLE_LIST, "Интерьеры для дома", "Unused safe house\t{00FF00}$5000\n\
Golden Bed Motel\t{00FF00}$8000\n\
Hashbury House\t{00FF00}$11000\n\
Johnsons House\t{00ff00}$14000\n\
Madd Doggs Mansion\t{00ff00}$17000\n\
Red Bed Motel Room\t{00ff00}$20000", "Купить", "Отмена");
if(listitem == 2)
{
new str[128];
format(str,sizeof(str),"{ffffff}Стоймость дома: {00ff00}$%d\n{ffffff}Продать можно за: {00ff00}$%d",struct[pstruct[playerid][HouseID]][cost],struct[pstruct[playerid][HouseID]][cost]*80/100);
ShowPlayerDialog(playerid, DIALOG_COSTINFO, DIALOG_STYLE_MSGBOX, "Информация о цене", str, "Okay", "");
}
if(listitem == 3)ShowPlayerDialog(playerid,DIALOG_WRITENAME, DIALOG_STYLE_INPUT, "Название дома", "Введите новое название для дома\nНе больше 24 символов:", "Ввести", "Отмена");
if(listitem == 4)
{
format(struct[pstruct[playerid][HouseID]][owner],24,"None");
UpdateHouse(pstruct[playerid][HouseID]);
GivePlayerMoney(playerid,struct[pstruct[playerid][HouseID]][cost]*80/100);
SetPlayerPosIntVW(playerid,struct[pstruct[playerid][HouseID]][hx],struct[pstruct[playerid][HouseID]][hy],struct[pstruct[playerid][HouseID]][hz],0,0);
pstruct[playerid][HouseID] = INVALID_PLAYER_ID;
pstruct[playerid][CurrentID] = INVALID_PLAYER_ID;
pstruct[playerid][HaveHouse] = false;
SaveAllHousesToDB();
SendClientMessage(playerid,white,"Дом успешно продан!");
}
return 1;
}
if(dialogid == DIALOG_WRITENAME)
{
if(!response)return 0;
if(strlen(inputtext) > 25)return ShowPlayerDialog(playerid,DIALOG_WRITENAME, DIALOG_STYLE_INPUT, "Название дома", "Введите новое название для дома\nНе больше 24 символов:", "Ввести", "Отмена");
format(struct[pstruct[playerid][HouseID]][name],24,"%s",inputtext);
UpdateHouse(pstruct[playerid][HouseID]);
SaveAllHousesToDB();
ShowPlayerHouseMenu(playerid);
return 1;
}
if(dialogid == DIALOG_INTERIORS)
{
if(!response)return 0;
if(listitem == 0)
{
if(GetPlayerMoney(playerid) < 5000)return SendClientMessage(playerid,red,"Недостаточно денег для покупки!");
GivePlayerMoney(playerid,-5000);
struct[pstruct[playerid][HouseID]][interior] = 1;
}
if(listitem == 1)
{
if(GetPlayerMoney(playerid) < 8000)return SendClientMessage(playerid,red,"Недостаточно денег для покупки!");
GivePlayerMoney(playerid,-8000);
struct[pstruct[playerid][HouseID]][interior] = 2;
}
if(listitem == 2)
{
if(GetPlayerMoney(playerid) < 11000)return SendClientMessage(playerid,red,"Недостаточно денег для покупки!");
GivePlayerMoney(playerid,-11000);
struct[pstruct[playerid][HouseID]][interior] = 3;
}
if(listitem == 3)
{
if(GetPlayerMoney(playerid) < 14000)return SendClientMessage(playerid,red,"Недостаточно денег для покупки!");
GivePlayerMoney(playerid,-14000);
struct[pstruct[playerid][HouseID]][interior] = 4;
}
if(listitem == 4)
{
if(GetPlayerMoney(playerid) < 17000)return SendClientMessage(playerid,red,"Недостаточно денег для покупки!");
GivePlayerMoney(playerid,-17000);
struct[pstruct[playerid][HouseID]][interior] = 5;
}
if(listitem == 5)
{
if(GetPlayerMoney(playerid) < 20000)return SendClientMessage(playerid,red,"Недостаточно денег для покупки!");
GivePlayerMoney(playerid,-20000);
struct[pstruct[playerid][HouseID]][interior] = 6;
}
Enter(playerid);
SaveAllHousesToDB();
return 1;
}
if(dialogid == DIALOG_BUYHOUSE)
{
if(!response)
{
pstruct[playerid][CurrentID] = INVALID_PLAYER_ID;
return 0;
}
if(pstruct[playerid][HaveHouse])
{
SendClientMessage(playerid,red,"У вас уже есть дом!");
pstruct[playerid][CurrentID] = INVALID_PLAYER_ID;
return 0;
}
if(GetPlayerMoney(playerid) < struct[pstruct[playerid][CurrentID]][cost])return SendClientMessage(playerid,red,"У вас не хватает денег!");
GivePlayerMoney(playerid,-struct[pstruct[playerid][CurrentID]][cost]);
format(struct[pstruct[playerid][CurrentID]][owner],24,"%s",gn(playerid));
UpdateHouse(pstruct[playerid][CurrentID]);
pstruct[playerid][HouseID] = pstruct[playerid][CurrentID];
pstruct[playerid][HaveHouse] = true;
SaveAllHousesToDB();
return 1;
}
if(dialogid == DIALOG_HOUSEINFO)
{
if(!response)
{
pstruct[playerid][CurrentID] = INVALID_PLAYER_ID;
return 0;
}
if(struct[pstruct[playerid][CurrentID]][lock])
{
pstruct[playerid][CurrentID] = INVALID_PLAYER_ID;
SendClientMessage(playerid,red,"Дом закрыт!");
return 0;
}
Enter(playerid);
GameTextForPlayer(playerid,"/exit",800,1);
return 1;
}
return 0;
}
stock Enter(playerid)
{
switch(struct[pstruct[playerid][CurrentID]][interior])
{
case 1:SetPlayerPosIntVW(playerid,2324.419921,-1145.568359,1050.710083,12,pstruct[playerid][CurrentID]+1);//Unused safe house
case 2:SetPlayerPosIntVW(playerid,2251.85,-1138.16,1050.63,9,pstruct[playerid][CurrentID]+1);//Golden Bed Motel Room
case 3:SetPlayerPosIntVW(playerid,2260.76,-1210.45,1049.02,10,pstruct[playerid][CurrentID]+1);//Hashbury House
case 4:SetPlayerPosIntVW(playerid,2496.65,-1696.55,1014.74,3,pstruct[playerid][CurrentID]+1);//Johnsons House, The
case 5:SetPlayerPosIntVW(playerid,1299.14,-794.77,1084.00,5,pstruct[playerid][CurrentID]+1);//Madd Doggs Mansion
case 6:SetPlayerPosIntVW(playerid,2262.83,-1137.71,1050.63,10,pstruct[playerid][CurrentID]+1);//Red Bed Motel Room
}
}
stock SetPlayerPosIntVW(playerid,Float:x,Float:y,Float:z,int,vw)
{
SetPlayerPos(playerid,x,y,z);
SetPlayerInterior(playerid,int);
SetPlayerVirtualWorld(playerid,vw);
return 1;
}
stock UpdateHouse(id)
{
new str[128];
DestroyDynamicPickup(struct[id][pickup]);
if(!strcmp(struct[id][owner], "None", true))
{
format(str,sizeof(str),"ID: %d\nДом на продаже!\nЦена: $%d",id,struct[id][cost]);
UpdateDynamic3DTextLabelText(struct[id][txt], white, str);
struct[id][pickup] = CreateDynamicPickup(1273, 23, struct[id][hx],struct[id][hy],struct[id][hz],-1,-1,-1,20.0);
}
else
{
format(str,sizeof(str),"ID: %d\nВладелец: %s\nНазвание: %s",id,struct[id][owner],struct[id][name]);
UpdateDynamic3DTextLabelText(struct[id][txt], white, str);
struct[id][pickup] = CreateDynamicPickup(1272, 23, struct[id][hx],struct[id][hy],struct[id][hz],-1,-1,-1,20.0);
}
}
stock FindPlayerHouse(playerid)
{
for(new id;id<MAX_HOUSES;id++)
{
if(struct[id][cost])
{
if(!strcmp(struct[id][owner],gn(playerid)))
{
pstruct[playerid][HaveHouse] = true;
pstruct[playerid][HouseID] = id;
break;
}
}
}
}
stock SaveAllHousesToDB()
{
new data[256];
mysql_query("DELETE FROM `house`");
for(new id;id<MAX_HOUSES;id++)
{
if(!struct[id][cost])continue;
format(data,sizeof(data),"INSERT INTO `house`(`owner`, `cost`, `interior`, `hx`, `hy`, `hz`, `name`, `lock`) VALUES ('%s','%d','%d','%f','%f','%f','%s','%d')",struct[id][owner],struct[id][cost],struct[id][interior],struct[id][hx],struct[id][hy],struct[id][hz],struct[id][name],struct[id][lock]);
mysql_query(data);
}
}
stock LoadAllHousesFromDB()
{
new data[256],str[128];
mysql_query("SELECT * FROM `house`");
mysql_store_result();
while(mysql_fetch_row_format(data, " "))
{
new i;
strval(strtok(data, i));
format(struct[houses][owner],24,"%s",strtok(data, i));
struct[houses][cost] = strval(strtok(data, i));
struct[houses][interior] = strval(strtok(data, i));
struct[houses][hx] = floatstr(strtok(data, i));
struct[houses][hy] = floatstr(strtok(data, i));
struct[houses][hz] = floatstr(strtok(data, i));
format(struct[houses][name],24,"%s",strtok(data, i));
struct[houses][lock] = strval(strtok(data, i));
if(!strcmp(struct[houses][owner], "None", true))
{
format(str,sizeof(str),"ID: %d\nДом на продаже!\nЦена: $%d",houses,struct[houses][cost]);
struct[houses][txt] = CreateDynamic3DTextLabel(str, white, struct[houses][hx],struct[houses][hy],struct[houses][hz],20.0,INVALID_PLAYER_ID,INVALID_VEHICLE_ID,0,-1,-1,-1,20.0);
struct[houses][pickup] = CreateDynamicPickup(1273, 23, struct[houses][hx],struct[houses][hy],struct[houses][hz],-1,-1,-1,20.0);
}
else
{
format(str,sizeof(str),"ID: %d\nВладелец: %s\nНазвание: %s",houses,struct[houses][owner],struct[houses][name]);
struct[houses][txt] = CreateDynamic3DTextLabel(str, white, struct[houses][hx],struct[houses][hy],struct[houses][hz],20.0,INVALID_PLAYER_ID,INVALID_VEHICLE_ID,0,-1,-1,-1,20.0);
struct[houses][pickup] = CreateDynamicPickup(1272, 23, struct[houses][hx],struct[houses][hy],struct[houses][hz],-1,-1,-1,20.0);
}
houses++;
}
mysql_free_result();
printf("Loaded %d houses.",houses);
}
strtok(const string[], &index)
{
new length = strlen(string);
while ((index < length) && (string[index] <= ' '))
{
index++;
}
new offset = index;
new result[20];
while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
{
result[index - offset] = string[index];
index++;
}
result[index - offset] = EOS;
return result;
}
stock gn(playerid)
{
new names[MAX_PLAYER_NAME+1];
GetPlayerName(playerid, names, sizeof(names));
return names;
}
stock ShowPlayerHouseMenu(playerid)
{
if(pstruct[playerid][HaveHouse] == false)return SendClientMessage(playerid,white,"У вас нету дома!");
if(pstruct[playerid][CurrentID] != pstruct[playerid][HouseID])return SendClientMessage(playerid,white,"Сначала войдите в свой дом!");
new locked[16],listitem[256];
if(struct[pstruct[playerid][HouseID]][lock])locked = "Закрыт";
else locked = "Открыт";
format(listitem,sizeof(listitem),"Открыть/Закрыть\t{00FF00}%s\n\
Интерьер\t\t{00FF00}%d\n\
Стоимость\t\t{00FF00}$%d\n\
Название\t\t{00FF00}%s\n\
Продать дом",locked,struct[pstruct[playerid][HouseID]][interior],struct[pstruct[playerid][HouseID]][cost],struct[pstruct[playerid][HouseID]][name]);
ShowPlayerDialog(playerid, DIALOG_HOUSEMENU, DIALOG_STYLE_LIST, "Управление домом", listitem, "Выбрать", "Отмена");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment