Advertisement
Guest User

Bebro dinaminė namų sistema

a guest
Aug 11th, 2013
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 10.08 KB | None | 0 0
  1.  
  2. /*
  3.             Dinaminė namų sistema.
  4.             Autorius "Bebras"
  5.             Turėk pagarbos autoriui ir jo darbui - netrink jo vardo.
  6. */
  7.  
  8. /*
  9.     native LoadHouses(Connection_Handler);
  10.     native AddHouse(Connection_Handler,HousePrice,InteriorID,Float:EntranceX,Float:EntranceY,Float:EntranceZ);
  11.     native GetFreeHouseArraySlot();
  12.     native ReloadHouses(Connection_Handler);
  13.     native Houses_OnPlayerKeyStateChange(playerid,newkeys,oldkeys,key,Float:distance=5.0);
  14.     native IsPlayerInRangeOfHouseEntrance(playerid,Float:distance=5.0);
  15.     native IsPlayerInRangeOfHouseExit(playerid,Float:distance=5.0);
  16.     native LockHouse(playerid,houseArrayindex=-1);
  17.     native UnlockHouse(playerid,houseArrayindex=-1);
  18.     native IsHouseLocked(houseArrayIndex);
  19.     native ChangeHouseInterior(Connection_Handler,houseArrayIndex,newInteriorID);
  20.     native DeleteHouse(Connection_Handler,houseArrayIndex);
  21.     native ChangeHouseOwner(Connection_Handler,houseArrayIndex,newowner[]);
  22.     native ChangeHousePrice(Connection_Handler,houseArrayIndex,newprice);
  23. */
  24.  
  25. #if !defined MAX_HOUSES
  26.     #define MAX_HOUSES 100
  27. #endif
  28. #if !defined LABEL_COLOR
  29.     #define LABEL_COLOR     0xFFFF00FF
  30. #endif
  31. #if !defined isnull
  32.     #define isnull(%1) ((!(%1[0])) || (((%1[0]) == '\1') && (!(%1[1]))))
  33. #endif
  34.  
  35. enum hint {
  36.     Float:XX,
  37.     Float:YY,
  38.     Float:ZZ,
  39.     Inte
  40. };
  41. new const HouseInteriors[][hint] = {
  42. {-750.80,491.00,1371.70,1},
  43. {2251.85,-1138.16,1050.63,9}
  44. };
  45. enum h_info {
  46.     HouseId,
  47.     Owner[MAX_PLAYER_NAME],
  48.     Float:EnX,
  49.     Float:EnY,
  50.     Float:EnZ,
  51.     Inter,
  52.     Float:ExX,
  53.     Float:ExY,
  54.     Float:ExZ,
  55.     Price,
  56.     Locked,
  57.     Text3D:Label
  58. };
  59. new Houses[MAX_HOUSES][h_info];
  60.  
  61.  
  62.  
  63. stock LoadHouses(Connection_Handler)
  64. {
  65.     mysql_function_query(Connection_Handler,"SELECT * FROM houses",true,"OnHouseLoad","i",Connection_Handler);
  66.     return 1;
  67. }
  68.  
  69. forward OnHouseLoad(Connection_Handler);
  70. public OnHouseLoad(Connection_Handler)
  71. {
  72.     new rows,fields,tmp[32],ticks = GetTickCount(),string[62];
  73.     cache_get_data(rows,fields);
  74.     for(new i; i<rows; i++)
  75.     {
  76.         cache_get_row(i,0,tmp,Connection_Handler);  Houses[i][HouseId]  = strval(tmp);
  77.         cache_get_row(i,1,tmp,Connection_Handler)format(Houses[i][Owner],sizeof(tmp),tmp);
  78.         cache_get_row(i,2,tmp,Connection_Handler);  Houses[i][EnX]      = floatstr(tmp);
  79.         cache_get_row(i,3,tmp,Connection_Handler);  Houses[i][EnY]      = floatstr(tmp);
  80.         cache_get_row(i,4,tmp,Connection_Handler);  Houses[i][EnZ]      = floatstr(tmp);
  81.         cache_get_row(i,5,tmp,Connection_Handler);  Houses[i][Inter]    = strval(tmp);
  82.         cache_get_row(i,6,tmp,Connection_Handler);  Houses[i][ExX]      = floatstr(tmp);
  83.         cache_get_row(i,7,tmp,Connection_Handler);  Houses[i][ExY]      = floatstr(tmp);
  84.         cache_get_row(i,8,tmp,Connection_Handler);  Houses[i][ExZ]      = floatstr(tmp);
  85.         cache_get_row(i,9,tmp,Connection_Handler);  Houses[i][Price]    = strval(tmp);
  86.         cache_get_row(i,10,tmp,Connection_Handler); Houses[i][Locked]   = strval(tmp);
  87.        
  88.         printf("OIwner:%s",Houses[i][Owner]);
  89.         if(isnull(Houses[i][Owner])) format(string,sizeof(string),"Namas parduodamas\nKaina:%d",Houses[i][Price]);
  90.         else format(string,sizeof(string),"Savininkas: %s",Houses[i][Owner]);
  91.         Houses[i][Label] = Create3DTextLabel(string,LABEL_COLOR,Houses[i][EnX],Houses[i][EnY],Houses[i][EnZ],25.0,0,1);
  92.     }
  93.     printf("Uzkrauti %d namai. Krovimo trukme: %dMS",rows,GetTickCount() - ticks);
  94.     return 1;
  95. }
  96.  
  97. stock AddHouse(Connection_Handler,HousePrice,InteriorID,Float:EntranceX,Float:EntranceY,Float:EntranceZ)
  98. {
  99.     new slot = GetFreeHouseArraySlot(),query[150];
  100.     if(slot == -1)
  101.     {
  102.         print("[ERROR]Pasiektas namu limitas. Istrinkite senus arba padidinkite \"MAX_HOUSES\".");
  103.         return 0;
  104.     }
  105.     Houses[slot][Price] = HousePrice;
  106.     Houses[slot][ExX] = HouseInteriors[InteriorID][XX];
  107.     Houses[slot][ExY] = HouseInteriors[InteriorID][YY];
  108.     Houses[slot][ExZ] = HouseInteriors[InteriorID][YY];
  109.     Houses[slot][Inter] = HouseInteriors[InteriorID][Inte];
  110.     Houses[slot][EnX] = EntranceX;
  111.     Houses[slot][EnY] = EntranceY;
  112.     Houses[slot][EnZ] = EntranceZ;
  113.     format(query,sizeof(query),"Namas parduodamas\nKaina:%d",Houses[slot][Price]);
  114.     Houses[slot][Label] = Create3DTextLabel(query,LABEL_COLOR,Houses[slot][EnX],Houses[slot][EnY],Houses[slot][EnZ],15.0,0,1);
  115.     format(query,sizeof(query),"INSERT INTO houses (EnX,EnY,EnZ,Inter,ExX,ExY,ExZ,Price)VALUES(%f,%f,%f,%d,%f,%f,%f,%d)",
  116.     Houses[slot][EnX],Houses[slot][EnY],Houses[slot][EnZ],Houses[slot][Inter],Houses[slot][ExX],
  117.     Houses[slot][ExY],Houses[slot][ExZ],Houses[slot][Price]);
  118.     mysql_function_query(Connection_Handler,query,true,"OnHouseAdd","i",slot);
  119.     return 1;
  120. }
  121. forward OnHouseAdd(slot);
  122. public OnHouseAdd(slot)
  123. {
  124.     Houses[slot][HouseId] = mysql_insert_id();
  125.     return 1;
  126. }
  127. stock ReloadHouses(Connection_Handler)
  128. {
  129.     for(new i; i<MAX_HOUSES; i++)
  130.     {  
  131.         Delete3DTextLabel(Houses[i][Label]);
  132.         LoadHouses(Connection_Handler);
  133.     }
  134.     return 1;
  135. }
  136. stock Houses_OnPlayerKeyStateChange(playerid,newkeys,oldkeys,key,Float:distance=5.0)
  137. {
  138.     if((newkeys & key) && !(oldkeys & key))
  139.     {
  140.         if(IsHouseLocked(GetPlayerHouseId(playerid,distance))) return 0;
  141.         if(IsPlayerInRangeOfHouseEntrance(playerid,distance))
  142.         {
  143.             SetPlayerInterior(playerid,Houses[GetPlayerHouseId(playerid,distance)][Inter]);
  144.             SetPlayerPos(playerid,Houses[GetPlayerHouseId(playerid,distance)][ExX],Houses[GetPlayerHouseId(playerid,distance)][ExY],Houses[GetPlayerHouseId(playerid,distance)][ExZ]);
  145.             SetPlayerVirtualWorld(playerid,Houses[GetPlayerHouseId(playerid,distance)][HouseId]);
  146.         }
  147.         else if(IsPlayerInRangeOfHouseExit(playerid,distance))
  148.         {
  149.             SetPlayerInterior(playerid,0);
  150.             SetPlayerPos(playerid,Houses[GetPlayerHouseId(playerid,distance)][EnX],Houses[GetPlayerHouseId(playerid,distance)][EnY],Houses[GetPlayerHouseId(playerid,distance)][EnZ]);
  151.             SetPlayerVirtualWorld(playerid,0);
  152.         }
  153.     }
  154.     return 1;
  155. }
  156. stock LockHouse(playerid,houseArrayindex=-1)
  157. {
  158.     new name[MAX_PLAYER_NAME];
  159.     GetPlayerName(playerid,name,sizeof(name));
  160.     if(houseArrayindex == -1) houseArrayindex = GetPlayerHouseId(playerid);
  161.     if(isnull(Houses[houseArrayindex][Owner])) return 0;
  162.     else if(strcmp(name,Houses[houseArrayindex][Owner])) return 0;
  163.     if(Houses[houseArrayindex][Locked] == 0)
  164.     {
  165.         Houses[houseArrayindex][Locked] = 1;
  166.         return 1;
  167.     }
  168.     return 0;
  169. }
  170. stock UnlockHouse(playerid,houseArrayindex=-1)
  171. {
  172.     new name[MAX_PLAYER_NAME];
  173.     GetPlayerName(playerid,name,sizeof(name));
  174.     if(houseArrayindex == -1) houseArrayindex = GetPlayerHouseId(playerid);
  175.     if(isnull(Houses[houseArrayindex][Owner])) return 0;
  176.     else if(strcmp(name,Houses[houseArrayindex][Owner])) return 0;
  177.     if(Houses[houseArrayindex][Locked] == 1)
  178.     {
  179.         Houses[houseArrayindex][Locked] = 0;
  180.         return 1;
  181.     }
  182.     return 0;
  183. }
  184. stock IsHouseOwned(houseArrayIndex)
  185. {
  186.     if(isnull(Houses[houseArrayIndex][Owner])) return true;
  187.     return false;
  188. }
  189. stock IsHouseLocked(houseArrayIndex)
  190. {
  191.     if(Houses[houseArrayIndex][Locked] ==1 ) return true;
  192.     else return false;
  193. }
  194. stock IsPlayerInRangeOfHouseEntrance(playerid,Float:distance=5.0)
  195. {
  196.     for(new i; i<MAX_HOUSES; i++)
  197.     {
  198.         if(IsPlayerInRangeOfPoint(playerid,distance,Houses[i][EnX],Houses[i][EnY],Houses[i][EnZ])) return true;
  199.     }
  200.     return false;
  201. }
  202. stock IsPlayerInRangeOfHouseExit(playerid,Float:distance=5.0)
  203. {
  204.     for(new i; i<MAX_HOUSES; i++)
  205.     {
  206.         if(IsPlayerInRangeOfPoint(playerid,distance,Houses[i][ExX],Houses[i][ExY],Houses[i][ExZ])) return true;
  207.     }
  208.     return false;
  209. }
  210. stock ChangeHousePrice(Connection_Handler,houseArrayIndex,newprice)
  211. {
  212.     new query[60];
  213.     Houses[houseArrayIndex][Price] = newprice;
  214.     format(query,sizeof(query),"UPDATE houses SET Price = %d WHERE HouseID=%d",Houses[houseArrayIndex][Price],Houses[houseArrayIndex][HouseId]);
  215.     mysql_function_query(Connection_Handler,query,false,"House_SendQuery","");
  216.     if(!isHouseOwned(houseArrayIndex)) format(query,sizeof(query),"Namas parduodamas\nKaina: %d",Houses[houseArrayIndex][Price]);
  217.     Update3DTextLabelText(Houses[houseArrayIndex][Label],LABEL_COLOR,query);
  218.     return 1;
  219. }
  220. stock ChangeHouseOwner(Connection_Handler,houseArrayIndex,newowner[])
  221. {
  222.     new query[80];
  223.     strins(Houses[houseArrayIndex][Owner],newowner,0,24);
  224.     format(query,sizeof(query),"Savininkas: %s", Houses[houseArrayIndex][Owner]);
  225.     Update3DTextLabelText(Houses[houseArrayIndex][Label],LABEL_COLOR,query);
  226.     format(query,sizeof(query),"UPDATE houses SET Owner ='%s' WHERE HouseID=%d",Houses[houseArrayIndex][Owner],Houses[houseArrayIndex][HouseId]);
  227.     mysql_function_query(Connection_Handler,query,false,"House_SendQuery","");
  228.     return 1;
  229. }
  230. stock DeleteHouse(Connection_Handler,houseArrayIndex)
  231. {
  232.     new query[40];
  233.     Delete3DTextLabel(Houses[houseArrayIndex][Label]);
  234.     format(query,sizeof(query),"DELETE FROM houses WHERE HouseID=%d",Houses[houseArrayIndex][HouseId]);
  235.     mysql_function_query(Connection_Handler,query,false,"House_SendQuery","");
  236.     Houses[houseArrayIndex][HouseId] = 0;
  237.     return 1;
  238. }
  239. stock ChangeHouseInterior(Connection_Handler,houseArrayIndex,newInteriorID)
  240. {
  241.     new query[160];
  242.     Houses[houseArrayIndex][ExX] = HouseInteriors[newInteriorID][XX];
  243.     Houses[houseArrayIndex][ExY] = HouseInteriors[newInteriorID][YY];
  244.     Houses[houseArrayIndex][ExZ] = HouseInteriors[newInteriorID][ZZ];
  245.     Houses[houseArrayIndex][Inter]= HouseInteriors[newInteriorID][Inte];
  246.     format(query,sizeof(query),"UPDATE houses SET ExX=%f,ExY=%f,ExZ=%f,Inter=%d WHERE HouseID=%d",
  247.         HouseInteriors[newInteriorID][XX],HouseInteriors[newInteriorID][YY],
  248.         HouseInteriors[newInteriorID][ZZ],HouseInteriors[newInteriorID][Inte],
  249.         Houses[houseArrayIndex][HouseId]);
  250.     mysql_function_query(Connection_Handler,query,false,"House_SendQuery","");
  251.     return 1;
  252. }
  253. stock GetFreeHouseArraySlot()
  254. {
  255.     for(new i; i<MAX_HOUSES; i++)
  256.     {
  257.         if(Houses[i][HouseId] == 0) return i;
  258.     }
  259.     return -1;
  260. }
  261. stock GetPlayerHouseId(playerid,Float:dis=5.0)
  262. {
  263.     for(new i; i<MAX_HOUSES; i++)
  264.     {
  265.         if(GetPlayerVirtualWorld(playerid) == 0) if(IsPlayerInRangeOfPoint(playerid,dis,Houses[i][EnX],Houses[i][EnY],Houses[i][EnZ])) return i;
  266.         else if(GetPlayerVirtualWorld(playerid) == Houses[i][HouseId]) return i;
  267.     }
  268.     return -1;
  269. }
  270. forward House_SendQuery();
  271. public House_SendQuery()
  272. {
  273.     return 1;
  274. }
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283. public OnQueryError( errorid, error[], callback[], query[], connectionHandle )
  284. {
  285.     printf("EID: %d | Error: %s | Query: %s", errorid, error, query);
  286.     return 1;
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement