PPC_Housing.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // This function returns the first free house-slot for the given player
  2. Player_GetFreeHouseSlot(playerid)
  3. {
  4. // Check if the player has room for another house (he hasn't bought the maximum amount of houses per player yet)
  5. // and get the slot-id
  6. for (new HouseIndex; HouseIndex < MAX_HOUSESPERPLAYER; HouseIndex++) // Loop through all house-slots of the player
  7. if (APlayerData[playerid][Houses][HouseIndex] == 0) // Check if this house slot is free
  8. return HouseIndex; // Return the free HouseIndex for this player
  9. // If there were no free house-slots, return "-1"
  10. return -1;
  11. }
  12. // This function returns the maximum number of car-slots, based on the house-level
  13. House_GetMaxCarSlots(HouseID)
  14. {
  15. // Return the maximum number of carslots, based on the house-level (every level has one carslot, so return the houselevel)
  16. return AHouseData[HouseID][HouseLevel];
  17. }
  18. // This function returns the first free carslot in the given house (or -1 if no free slot is found)
  19. House_GetFreeCarSlot(HouseID)
  20. {
  21. // Get the maximum number of carslots for this house (based on the house-level)
  22. new MaxCarSlots = House_GetMaxCarSlots(HouseID);
  23. // Get the maximum number of carslots for this house and make a loop through all carslots for this house
  24. for (new CarSlot; CarSlot < MaxCarSlots; CarSlot++)
  25. {
  26. // Check if the carslot is empty
  27. if (AHouseData[HouseID][VehicleIDs][CarSlot] == 0)
  28. return CarSlot; // Return the carslot-id
  29. }
  30. // If no carslots are free, return -1
  31. return -1;
  32. }
  33. // This function sets ownership to the given player
  34. House_SetOwner(playerid, HouseID)
  35. {
  36. // Setup local variables
  37. new HouseSlotFree, Name[24], Msg[128];
  38. // Get the first free house-slot from this player
  39. HouseSlotFree = Player_GetFreeHouseSlot(playerid);
  40. // Check if the player has a free house-slot
  41. if (HouseSlotFree != -1)
  42. {
  43. // Get the player's name
  44. GetPlayerName(playerid, Name, sizeof(Name));
  45. // Store the house-id for the player
  46. APlayerData[playerid][Houses][HouseSlotFree] = HouseID;
  47. // Let the player pay for the house
  48. RewardPlayer(playerid, -AHouseData[HouseID][HousePrice], 0);
  49. // Set the house as owned
  50. AHouseData[HouseID][Owned] = true;
  51. // Store the owner-name for the house
  52. format(AHouseData[HouseID][Owner], 24, Name);
  53. // Set the level to 1
  54. AHouseData[HouseID][HouseLevel] = 1;
  55. // Set the default house-name ("<playername>'s house")
  56. format(AHouseData[HouseID][HouseName], 100, TXT_DefaultHouseName, Name);
  57. // Also, update the pickup and map-icon for this house
  58. House_UpdateEntrance(HouseID);
  59. // Save the player-file (and also his houses/businesses)
  60. PlayerFile_Save(playerid);
  61. // Let the player know he bought the house
  62. format(Msg, 128, TXT_PlayerBoughtHouse, AHouseData[HouseID][HousePrice]);
  63. SendClientMessage(playerid, 0xFFFFFFFF, Msg);
  64. }
  65. else
  66. SendClientMessage(playerid, 0xFFFFFFFF, TXT_PlayerOwnsMaxHouses);
  67. return 1;
  68. }
  69. // This function is used to spawn back at the entrance of your house
  70. House_Exit(playerid, HouseID)
  71. {
  72. // Set the player in the normal world again
  73. SetPlayerVirtualWorld(playerid, 0);
  74. SetPlayerInterior(playerid, 0);
  75. // Set the position of the player at the entrance of his house
  76. SetPlayerPos(playerid, AHouseData[HouseID][HouseX], AHouseData[HouseID][HouseY], AHouseData[HouseID][HouseZ]);
  77. // Also clear the tracking-variable to track in which house the player is
  78. APlayerData[playerid][CurrentHouse] = 0;
  79. // Check if there is a timer-value set for exiting the house (this timer freezes the player while the environment is being loaded)
  80. if (ExitHouseTimer > 0)
  81. {
  82. // Don't allow the player to fall
  83. TogglePlayerControllable(playerid, 0);
  84. // Let the player know he's frozen for 5 seconds
  85. GameTextForPlayer(playerid, TXT_ExitHouseReloadEnv, ExitHouseTimer, 4);
  86. // Start a timer that will allow the player to fall again when the environment has loaded
  87. SetTimerEx("House_ExitTimer", ExitHouseTimer, false, "ii", playerid, HouseID);
  88. }
  89. return 1;
  90. }
  91. forward House_ExitTimer(playerid, HouseID);
  92. public House_ExitTimer(playerid, HouseID)
  93. {
  94. // Allow the player to move again (environment should have been loaded now)
  95. TogglePlayerControllable(playerid, 1);
  96. // Respawn the player's vehicles near the house (only the vehicles that belong to this house)
  97. for (new CarSlot; CarSlot < 10; CarSlot++)
  98. if (AHouseData[HouseID][VehicleIDs][CarSlot] != 0)
  99. SetVehicleToRespawn(AHouseData[HouseID][VehicleIDs][CarSlot]);
  100. return 1;
  101. }
  102. // This function adds a pickup for the given house
  103. House_CreateEntrance(HouseID)
  104. {
  105. // Setup local variables
  106. new Msg[128], Float:x, Float:y, Float:z;
  107. // Get the coordinates of the house's pickup (usually near the door)
  108. x = AHouseData[HouseID][HouseX];
  109. y = AHouseData[HouseID][HouseY];
  110. z = AHouseData[HouseID][HouseZ];
  111. // Add a new pickup at the house's location (usually near the door), green = free, blue = owned
  112. if (AHouseData[HouseID][Owned] == true)
  113. {
  114. // Create a blue house-pickup (house is owned)
  115. AHouseData[HouseID][PickupID] = CreateDynamicPickup(1272, 1, x, y, z, 0);
  116. // Create the 3DText that appears above the house-pickup (displays the housename and the name of the owner)
  117. format(Msg, 128, TXT_PickupHouseOwned, AHouseData[HouseID][HouseName], AHouseData[HouseID][Owner], AHouseData[HouseID][HouseLevel]);
  118. AHouseData[HouseID][DoorText] = CreateDynamic3DTextLabel(Msg, 0x008080FF, x, y, z + 1.0, 50.0);
  119. // Add a streamed icon to the map (red house), type = 32, color = 0, world = 0, interior = 0, playerid = -1, drawdist = 150.0
  120. if (ShowBoughtHouses == true)
  121. AHouseData[HouseID][MapIconID] = CreateDynamicMapIcon(x, y, z, 32, 0, 0, 0, -1, 150.0);
  122. }
  123. else
  124. {
  125. // Create a green house-pickup (house is free)
  126. AHouseData[HouseID][PickupID] = CreateDynamicPickup(1273, 1, x, y, z, 0);
  127. // Create the 3DText that appears above the house-pickup (displays the price of the house)
  128. format(Msg, 128, TXT_PickupHouseForSale, AHouseData[HouseID][HousePrice], AHouseData[HouseID][HouseMaxLevel]);
  129. AHouseData[HouseID][DoorText] = CreateDynamic3DTextLabel(Msg, 0x008080FF, x, y, z + 1.0, 50.0);
  130. // Add a streamed icon to the map (green house), type = 31, color = 0, world = 0, interior = 0, playerid = -1, drawdist = 150.0
  131. AHouseData[HouseID][MapIconID] = CreateDynamicMapIcon(x, y, z, 31, 0, 0, 0, -1, 150.0);
  132. }
  133. }
  134. // This function changes the pickup (and map-icon) for the given house (used when buying or selling a house)
  135. House_UpdateEntrance(HouseID)
  136. {
  137. // Setup local variables
  138. new Msg[128], Float:x, Float:y, Float:z;
  139. // Get the coordinates of the house's pickup (usually near the door)
  140. x = AHouseData[HouseID][HouseX];
  141. y = AHouseData[HouseID][HouseY];
  142. z = AHouseData[HouseID][HouseZ];
  143. // Destroy the pickup and map-icon near the house's entrance
  144. DestroyDynamicPickup(AHouseData[HouseID][PickupID]);
  145. DestroyDynamicMapIcon(AHouseData[HouseID][MapIconID]);
  146. // Add a new pickup at the house's location (usually near the door), green = free, blue = owned
  147. if (AHouseData[HouseID][Owned] == true)
  148. {
  149. // Create a blue house-pickup (house is owned)
  150. AHouseData[HouseID][PickupID] = CreateDynamicPickup(1272, 1, x, y, z, 0);
  151. // Update the 3DText that appears above the house-pickup (displays the housename and the name of the owner)
  152. format(Msg, 128, TXT_PickupHouseOwned, AHouseData[HouseID][HouseName], AHouseData[HouseID][Owner], AHouseData[HouseID][HouseLevel]);
  153. UpdateDynamic3DTextLabelText(AHouseData[HouseID][DoorText], 0x008080FF, Msg);
  154. // Add a streamed icon to the map (red house), type = 32, color = 0, world = 0, interior = 0, playerid = -1, drawdist = 150.0
  155. if (ShowBoughtHouses == true)
  156. AHouseData[HouseID][MapIconID] = CreateDynamicMapIcon(x, y, z, 32, 0, 0, 0, -1, 150.0);
  157. }
  158. else
  159. {
  160. // Create a green house-pickup (house is free)
  161. AHouseData[HouseID][PickupID] = CreateDynamicPickup(1273, 1, x, y, z, 0);
  162. // Update the 3DText that appears above the house-pickup (displays the price of the house)
  163. format(Msg, 128, TXT_PickupHouseForSale, AHouseData[HouseID][HousePrice], AHouseData[HouseID][HouseMaxLevel]);
  164. UpdateDynamic3DTextLabelText(AHouseData[HouseID][DoorText], 0x008080FF, Msg);
  165. // Add a streamed icon to the map (green house), type = 31, color = 0, world = 0, interior = 0, playerid = -1, drawdist = 150.0
  166. AHouseData[HouseID][MapIconID] = CreateDynamicMapIcon(x, y, z, 31, 0, 0, 0, -1, 150.0);
  167. }
  168. }
  169. // This function adds a vehicle to the house (if possible)
  170. House_AddVehicle(HouseID, cModel, cPaint, cComponents[], Float:cx, Float:cy, Float:cz, Float:crot, Col1, Col2)
  171. {
  172. // Setup local variables
  173. new vid, CarSlot;
  174. // Get a free carslot from the house
  175. CarSlot = House_GetFreeCarSlot(HouseID);
  176. // Check if there is a free carslot
  177. if (CarSlot != -1)
  178. {
  179. // Create a new vehicle and get the vehicle-id
  180. vid = CreateVehicle(cModel, cx, cy, cz, crot, Col1, Col2, 600);
  181. // Store the vehicle-id in the house's free carslot
  182. AHouseData[HouseID][VehicleIDs][CarSlot] = vid;
  183. // Save the model of the vehicle
  184. AVehicleData[vid][Model] = cModel;
  185. // Save the paintjob of the vehicle and apply it
  186. AVehicleData[vid][PaintJob] = cPaint;
  187. if (cPaint != 0)
  188. ChangeVehiclePaintjob(vid, cPaint - 1);
  189. // Also update the car-color
  190. ChangeVehicleColor(vid, Col1, Col2);
  191. // Save the colors of the vehicle
  192. AVehicleData[vid][Color1] = Col1;
  193. AVehicleData[vid][Color2] = Col2;
  194. // Save the components of the vehicle and apply them
  195. for (new i; i < 14; i++)
  196. {
  197. AVehicleData[vid][Components][i] = cComponents[i];
  198. // Check if the componentslot has a valid component-id
  199. if (AVehicleData[vid][Components][i] != 0)
  200. AddVehicleComponent(vid, AVehicleData[vid][Components][i]); // Add the component to the vehicle
  201. }
  202. // Save the spawn-data of the vehicle
  203. AVehicleData[vid][SpawnX] = cx;
  204. AVehicleData[vid][SpawnY] = cy;
  205. AVehicleData[vid][SpawnZ] = cz;
  206. AVehicleData[vid][SpawnRot] = crot;
  207. // Also set the fuel to maximum
  208. AVehicleData[vid][Fuel] = MaxFuel;
  209. // Also set the owner
  210. AVehicleData[vid][Owned] = true;
  211. format(AVehicleData[vid][Owner], 24, AHouseData[HouseID][Owner]);
  212. // Save the HouseID for the vehicle
  213. AVehicleData[vid][BelongsToHouse] = HouseID;
  214. }
  215. else // No free carslot was found, return 0
  216. return 0;
  217. // Exit the function and return the vehicle-id
  218. return vid;
  219. }
  220. // This function is used only when you respawn your vehicles by exiting your house
  221. House_ReplaceVehicle(HouseID, CarSlot)
  222. {
  223. // Setup local variables
  224. new vid, cModel, cPaint, cComponents[14], Float:cx, Float:cy, Float:cz, Float:crot, Col1, Col2, Float:Health, cFuel;
  225. new panels, doors, lights, tires;
  226. // Get the data from the already existing vehicle that was parked before
  227. vid = AHouseData[HouseID][VehicleIDs][CarSlot];
  228. cModel = AVehicleData[vid][Model];
  229. cPaint = AVehicleData[vid][PaintJob];
  230. cFuel = AVehicleData[vid][Fuel];
  231. for (new i; i < 14; i++)
  232. cComponents[i] = AVehicleData[vid][Components][i];
  233. Col1 = AVehicleData[vid][Color1];
  234. Col2 = AVehicleData[vid][Color2];
  235. cx = AVehicleData[vid][SpawnX];
  236. cy = AVehicleData[vid][SpawnY];
  237. cz = AVehicleData[vid][SpawnZ];
  238. crot = AVehicleData[vid][SpawnRot];
  239. GetVehicleHealth(vid, Health);
  240. GetVehicleDamageStatus(vid, panels, doors, lights, tires);
  241. // Delete the vehicle and clear the data
  242. Vehicle_Delete(vid);
  243. // Create a new vehicle in the same carslot
  244. vid = House_AddVehicle(HouseID, cModel, cPaint, cComponents, Float:cx, Float:cy, Float:cz, Float:crot, Col1, Col2);
  245. // Update the fuel of the vehicle to the previous setting
  246. AVehicleData[vid][Fuel] = cFuel;
  247. // Update the health to what it was before and update the bodywork
  248. SetVehicleHealth(vid, Health);
  249. UpdateVehicleDamageStatus(vid, panels, doors, lights, tires);
  250. return vid;
  251. }
  252. // This function is used only when a player logs out (the vehicles are unloaded)
  253. House_RemoveVehicles(HouseID)
  254. {
  255. // Setup local variables
  256. new vid;
  257. // Loop through all carslots of this house
  258. for (new CarSlot; CarSlot < 10; CarSlot++)
  259. {
  260. // Get the vehicle-id
  261. vid = AHouseData[HouseID][VehicleIDs][CarSlot];
  262. // Check if there was a vehicle in this carslot
  263. if (vid != 0)
  264. {
  265. // Delete the vehicle and clear the data
  266. DestroyVehicle(vid);
  267. AHouseData[HouseID][VehicleIDs][CarSlot] = 0;
  268. AVehicleData[vid][Owned] = false;
  269. AVehicleData[vid][Owner] = 0;
  270. AVehicleData[vid][Model] = 0;
  271. AVehicleData[vid][PaintJob] = 0;
  272. for (new i; i < 14; i++)
  273. AVehicleData[vid][Components][i] = 0;
  274. AVehicleData[vid][SpawnX] = 0.0;
  275. AVehicleData[vid][SpawnY] = 0.0;
  276. AVehicleData[vid][SpawnZ] = 0.0;
  277. AVehicleData[vid][SpawnRot] = 0.0;
  278. AVehicleData[vid][BelongsToHouse] = 0;
  279. }
  280. }
  281. }
  282. // This function calculates the sell-price for the given house
  283. House_CalcSellPrice(HouseID)
  284. {
  285. // Setup local variables
  286. new SellPrice, NumUpgrades, UpgradePrice;
  287. // Calculate 50% of the original buying price (base-price for selling)
  288. SellPrice = AHouseData[HouseID][HousePrice] / 2;
  289. // Calculate the number of upgrades applied to the house
  290. NumUpgrades = AHouseData[HouseID][HouseLevel] - 1;
  291. // Also calculate 50% for each upgrade, based on the percentage for upgrading the house
  292. UpgradePrice = ((AHouseData[HouseID][HousePrice] / 100) * HouseUpgradePercent) * NumUpgrades;
  293. // Add 50% of the upgrade-price to the sell-price
  294. SellPrice = SellPrice + UpgradePrice;
  295. // Return the total sell-price to the calling function
  296. return SellPrice;
  297. }
  298. // his function returns "1" if the given player is the owner of the given house
  299. House_PlayerIsOwner(playerid, HouseID)
  300. {
  301. // Loop through all houses owner by this player
  302. for (new i; i < MAX_HOUSESPERPLAYER; i++)
  303. {
  304. // Check if the player owns the house in any of his house-slots
  305. if (APlayerData[playerid][Houses][i] == HouseID)
  306. return 1;
  307. }
  308. // If the player doesn't own the house, return 0
  309. return 0;
  310. }