PPC_MissionsTrucking.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. // Forward the function to Load or Unload goods during missions (used by a timer)
  2. forward Trucker_LoadUnload(playerid);
  3. // This function is called when a truckdriver enters a checkpoint
  4. Trucker_OnPlayerEnterCheckpoint(playerid)
  5. {
  6. // Check if the player is inside his vehicle while entering a checkpoint
  7. if (GetPlayerVehicleID(playerid) == APlayerData[playerid][VehicleID])
  8. {
  9. // Also check if the player still has his trailer attached
  10. if (APlayerData[playerid][TrailerID] == GetVehicleTrailer(GetPlayerVehicleID(playerid)))
  11. {
  12. // Check the jobstep
  13. switch (APlayerData[playerid][JobStep])
  14. {
  15. // JobStep is 1 (truckdriver is loading his goods at the checkpoint)
  16. case 1: GameTextForPlayer(playerid, TXT_TruckerLoadingGoods, 5000, 4);
  17. // JobStep is 2 (truckdriver is unloading his goods at the checkpoint) or 3 (unloading for convoys)
  18. case 2, 3: GameTextForPlayer(playerid, TXT_TruckerUnloadingGoods, 5000, 4);
  19. }
  20. // Disable the player's actions (he cannot move anymore)
  21. TogglePlayerControllable(playerid, 0);
  22. // Start a timer (Public function "LoadUnload(playerid)" gets called when the timer runs out)
  23. APlayerData[playerid][LoadingTimer] = SetTimerEx("Trucker_LoadUnload", 5000, false, "d" , playerid);
  24. }
  25. else
  26. SendClientMessage(playerid, 0xFFFFFFFF, TXT_NeedTrailerToProceed);
  27. }
  28. else
  29. SendClientMessage(playerid, 0xFFFFFFFF, TXT_NeedVehicleToProceed);
  30. return 1;
  31. }
  32. // After a truckdriver entered a checkpoint, a timer is created. This function is called when the timer runs out
  33. public Trucker_LoadUnload(playerid)
  34. {
  35. // Check if the player is inside a convoy
  36. if (APlayerData[playerid][InConvoy] == true)
  37. {
  38. // If the player just loaded his goods at the loading-point
  39. if (APlayerData[playerid][JobStep] == 1)
  40. {
  41. APlayerData[playerid][JobStep] = 2; // Set the next step of the convoy-job (wait until all members have loaded their cargo)
  42. TextDrawSetString(APlayerData[playerid][MissionText], TXT_WaitingMembersToLoadCargo);
  43. }
  44. // If the player just delivered his goods at the unloading-point
  45. if (APlayerData[playerid][JobStep] == 3)
  46. {
  47. APlayerData[playerid][JobStep] = 4; // Set the next step of the convoy-job (wait until all members have unloaded their cargo)
  48. TextDrawSetString(APlayerData[playerid][MissionText], TXT_WaitingMembersToUnLoadCargo);
  49. }
  50. DisablePlayerCheckpoint(playerid); // Delete the loading/unloading-checkpoint
  51. TogglePlayerControllable(playerid, 1); // Enable the player again (he can move again)
  52. return 1; // Don't allow the rest of the function to be executed
  53. }
  54. // If the player isn't inside a convoy, this part is executed
  55. // Check the JobStep
  56. switch (APlayerData[playerid][JobStep])
  57. {
  58. case 1: // Player must load his goods
  59. {
  60. // Setup local variables
  61. new StartLoc[50], EndLoc[50], Load[50], RouteText[255], Float:x, Float:y, Float:z, UnloadMsg[100];
  62. // Set JobStep to 2 (unloading goods)
  63. APlayerData[playerid][JobStep] = 2;
  64. // Delete the loading-checkpoint
  65. DisablePlayerCheckpoint(playerid);
  66. // Get the startlocation, endlocation and the load texts
  67. format(StartLoc, 50, ALocations[APlayerData[playerid][JobLoc1]][LocationName]);
  68. format(EndLoc, 50, ALocations[APlayerData[playerid][JobLoc2]][LocationName]);
  69. format(Load, 50, ALoads[APlayerData[playerid][LoadID]][LoadName]);
  70. // Randomly set the load as overloaded (15% chance the load is overloaded)
  71. Trucker_SetRandomOverloaded(playerid);
  72. // Pre-format the missiontext (there may be some parts appended when overloaded/mafiaload
  73. format(RouteText, 255, TXT_HaulingCargoFromToDeliver, Load, StartLoc, EndLoc);
  74. // Check if the player is overloaded
  75. if (APlayerData[playerid][Overloaded] == true)
  76. {
  77. // Add "(OL)" to the missiontext to let the player know he's been overloaded
  78. format(RouteText, 255, "%s%s", RouteText, " ~r~(OL)~w~");
  79. // Send a message to the player to let him know he's been overloaded
  80. SendClientMessage(playerid, 0xFFFFFFFF, TXT_TruckerOverloaded);
  81. }
  82. // Check if the player is carrying a mafia-load
  83. if (ALoads[APlayerData[playerid][LoadID]][Mafia] == true)
  84. {
  85. // Add "(ML)" to the missiontext to let the player know his load is wanted by the mafia
  86. format(RouteText, 255, "%s%s", RouteText, " ~r~(ML)~w~");
  87. // If the player is carrying a mafia-load, inform him about it
  88. GameTextForPlayer(playerid, TXT_TruckerMafiaInterested, 5000, 4);
  89. // Also set the data for the player to indicate he's carrying a mafiaload
  90. APlayerData[playerid][MafiaLoad] = true;
  91. // Also set the player's trailer ID (or the vehicle itself) as Mafia-load in the array "AVehicleMafiaLoad"
  92. if (APlayerData[playerid][TrailerID] == 0)
  93. AVehicleData[APlayerData[playerid][VehicleID]][MafiaLoad] = true; // The player has no trailer, so set his main vehicle as wanted by the mafia
  94. else
  95. AVehicleData[APlayerData[playerid][TrailerID]][MafiaLoad] = true; // The player has a trailer, so set his trailer as wanted by the mafia
  96. }
  97. // Set the TextDraw so the player can see it
  98. TextDrawSetString(APlayerData[playerid][MissionText], RouteText);
  99. // Grab the x, y, z positions for the second location (to unload the goods)
  100. x = ALocations[APlayerData[playerid][JobLoc2]][LocX];
  101. y = ALocations[APlayerData[playerid][JobLoc2]][LocY];
  102. z = ALocations[APlayerData[playerid][JobLoc2]][LocZ];
  103. // Create a checkpoint where the player should unload the goods
  104. SetPlayerCheckpoint(playerid, x, y, z, 7);
  105. // Inform the player that he must unload his goods
  106. format(UnloadMsg, 100, TXT_DeliverCargoTo, Load, EndLoc);
  107. SendClientMessage(playerid, 0xFFFFFFFF, UnloadMsg);
  108. }
  109. case 2: // Player is delivering his goods
  110. {
  111. // Setup local variables
  112. new StartLoc[50], EndLoc[50], Load[50], Msg1[128], Msg2[128], Name[24], BonusMsg[128];
  113. // Get the player name
  114. GetPlayerName(playerid, Name, sizeof(Name));
  115. // Get the startlocation, endlocation and the load texts
  116. format(StartLoc, 50, ALocations[APlayerData[playerid][JobLoc1]][LocationName]);
  117. format(EndLoc, 50, ALocations[APlayerData[playerid][JobLoc2]][LocationName]);
  118. format(Load, 50, ALoads[APlayerData[playerid][LoadID]][LoadName]);
  119. // Construct the message sent to all players that this player completed a trucking mission
  120. format(Msg1, 128, TXT_PlayerCompletedTruckJob, Name, Load);
  121. format(Msg2, 128, TXT_PlayerCompletedTruckJobInfo, StartLoc, EndLoc);
  122. SendClientMessageToAll(0xFFFFFFFF, Msg1);
  123. SendClientMessageToAll(0xFFFFFFFF, Msg2);
  124. // Setup local variables
  125. new Float:x1, Float:y1, Float:x2, Float:y2, Float:Distance, Message[128], Payment, Bonus;
  126. // Grab the x, y, z positions for the first location (to load the goods)
  127. x1 = ALocations[APlayerData[playerid][JobLoc1]][LocX];
  128. y1 = ALocations[APlayerData[playerid][JobLoc1]][LocY];
  129. // Grab the x, y, z positions for the second location (to unload the goods)
  130. x2 = ALocations[APlayerData[playerid][JobLoc2]][LocX];
  131. y2 = ALocations[APlayerData[playerid][JobLoc2]][LocY];
  132. // Calculate the distance between both points
  133. Distance = floatsqroot(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)));
  134. // Calculate the payment for the player
  135. Payment = floatround((Distance * ALoads[APlayerData[playerid][LoadID]][PayPerUnit]), floatround_floor);
  136. // Check if the player has done the bonus mission
  137. if (RandomBonusMission[MissionFinished] == false)
  138. {
  139. // Check all paramters (load, startlocation and end-location)
  140. if (RandomBonusMission[RandomLoad] == APlayerData[playerid][LoadID])
  141. if (RandomBonusMission[RandomStartLoc] == APlayerData[playerid][JobLoc1])
  142. if (RandomBonusMission[RandomEndLoc] == APlayerData[playerid][JobLoc2])
  143. {
  144. Payment = Payment * 2; // Double the payment is the player was the first to do the bonus mission
  145. RandomBonusMission[MissionFinished] = true; // Only one player can do the bonus mission, a new one is chosen next
  146. format(BonusMsg, 128, "{00BBFF}Player {FFBB00}%s{00BBFF} has finished the bonus mission", Name);
  147. SendClientMessageToAll(0xFFFFFFFF, BonusMsg);
  148. }
  149. }
  150. // Pay the player based on the distance between the loading-point and unloading-point
  151. RewardPlayer(playerid, Payment, 0);
  152. // Send a message to let the player know he finished his mission and got paid
  153. format(Message, 128, TXT_RewardJob, Payment);
  154. SendClientMessage(playerid, 0xFFFFFFFF, Message);
  155. // Add 25% bonus if the player has been overloaded
  156. if (APlayerData[playerid][Overloaded] == true)
  157. {
  158. // Calculate the bonus
  159. Bonus = (Payment * 25) / 100;
  160. // Pay the bonus to the player
  161. RewardPlayer(playerid, Bonus, 0);
  162. // Send a message to let the player know he was overloaded and got paid
  163. format(Message, 128, TXT_TruckerBonusOverloaded, Bonus);
  164. SendClientMessage(playerid, 0xFFFFFFFF, Message);
  165. }
  166. // Add 50% bonus if the player has delivered a mafia load (mafia couldn't steal his load)
  167. if (APlayerData[playerid][MafiaLoad] == true)
  168. {
  169. // Calculate the bonus
  170. Bonus = (Payment * 50) / 100;
  171. // Pay the bonus to the player
  172. RewardPlayer(playerid, Bonus, 0);
  173. // Send a message to let the player know he was overloaded and got paid
  174. format(Message, 128, TXT_TruckerBonusMafiaLoad, Bonus);
  175. SendClientMessage(playerid, 0xFFFFFFFF, Message);
  176. }
  177. // Add 10% bonus if the player has delivered the load with his own truck
  178. if (AVehicleData[APlayerData[playerid][VehicleID]][Owned] == true)
  179. {
  180. // Calculate the bonus
  181. Bonus = (Payment * 10) / 100;
  182. // Pay the bonus to the player
  183. RewardPlayer(playerid, Bonus, 0);
  184. // Send a message to let the player know he was overloaded and got paid
  185. format(Message, 128, TXT_TruckerBonusOwnVehicle, Bonus);
  186. SendClientMessage(playerid, 0xFFFFFFFF, Message);
  187. }
  188. // Also add score-points to the score of the player based on the distance between the loading and unloading points
  189. if (Distance > 3000.0)
  190. RewardPlayer(playerid, 0, 2); // Distance is larger than 3000 units, so add 2 points
  191. else
  192. RewardPlayer(playerid, 0, 1); // Distance is less than 3000 units, so add 1 point
  193. // Increase the stats for completing a trucking job
  194. APlayerData[playerid][StatsTruckerJobs]++;
  195. // Also save the data (in case the server crashes, progress would be lost)
  196. PlayerFile_Save(playerid);
  197. // End the current trucker job (clear mission-data)
  198. Trucker_EndJob(playerid);
  199. }
  200. }
  201. // Enable the player again (he can move again)
  202. TogglePlayerControllable(playerid, 1);
  203. return 1;
  204. }
  205. // This function randomly determines if the load is overloaded and adds 2 to the playerwanted-level
  206. Trucker_SetRandomOverloaded(playerid)
  207. {
  208. // Setup local variables
  209. new Name[24], Msg[128];
  210. // Check the vehicle model that the player is driving
  211. switch (GetVehicleModel(GetPlayerVehicleID(playerid)))
  212. {
  213. case VehicleCementTruck: return 0; // A cementtruck cannot be overloaded
  214. case VehicleLineRunner, VehicleTanker, VehicleRoadTrain: // When driving a LineRunner, Tanker or RoadTrain
  215. {
  216. // A Fluids-trailer cannot be overloaded
  217. if (GetVehicleModel(GetVehicleTrailer(GetPlayerVehicleID(playerid))) == VehicleTrailerFluids) return 0;
  218. }
  219. }
  220. // The player wasn't driving one of the above vehicle models, so this one can be overloaded
  221. // There is a 15% chance that your load will be overloaded
  222. if (random(100) <= 15)
  223. {
  224. // Set overloaded for this player to True
  225. APlayerData[playerid][Overloaded] = true;
  226. // Add 2 to the player's wanted level
  227. SetPlayerWantedLevel(playerid, GetPlayerWantedLevel(playerid) + 2);
  228. // Inform the police this trucker is overloaded
  229. GetPlayerName(playerid, Name, sizeof(Name));
  230. format(Msg, 128, "{00FF00}Trucker {FFFF00}%s{00FF00} is overloaded, pursue and fine him", Name);
  231. Police_SendMessage(Msg);
  232. }
  233. return 1;
  234. }
  235. // This function is called when a truckdriver wants to start a job by entering "/work" and has no truckers license
  236. Trucker_StartRandomJob(playerid)
  237. {
  238. // Check if a job could be set correctly (player must be driving a valid trucking vehicle)
  239. if (Trucker_SetRandomJob(playerid) != 0)
  240. {
  241. // Setup local variables
  242. new StartLoc[50], EndLoc[50], Load[50], RouteText[255], Float:x, Float:y, Float:z, LoadMsg[128];
  243. // Job has started
  244. APlayerData[playerid][JobStarted] = true;
  245. // Set jobstep to 1 (going to load the goods)
  246. APlayerData[playerid][JobStep] = 1;
  247. // Get the startlocation, endlocation and the load texts
  248. format(StartLoc, 50, ALocations[APlayerData[playerid][JobLoc1]][LocationName]);
  249. format(EndLoc, 50, ALocations[APlayerData[playerid][JobLoc2]][LocationName]);
  250. format(Load, 50, ALoads[APlayerData[playerid][LoadID]][LoadName]);
  251. // Combine it all into a string for the TextDraw (the player can see this all the time) to describe the mission
  252. format(RouteText, 255, TXT_HaulingCargoFromToPickup, Load, StartLoc, EndLoc);
  253. // Set the TextDraw so the player can see it
  254. TextDrawSetString(APlayerData[playerid][MissionText], RouteText);
  255. // Grab the x, y, z positions for the first location
  256. x = ALocations[APlayerData[playerid][JobLoc1]][LocX];
  257. y = ALocations[APlayerData[playerid][JobLoc1]][LocY];
  258. z = ALocations[APlayerData[playerid][JobLoc1]][LocZ];
  259. // Create a checkpoint where the player should load the goods
  260. SetPlayerCheckpoint(playerid, x, y, z, 7);
  261. // Set the job-fail-time for the global vehicle-timer
  262. APlayerData[playerid][VehicleTimerTime] = Job_TimeToFailMission;
  263. // Inform the player that he must load his goods
  264. format(LoadMsg, 128, TXT_PickupCargoAt, Load, StartLoc);
  265. SendClientMessage(playerid, 0xFFFFFFFF, LoadMsg);
  266. }
  267. return 1;
  268. }
  269. // This function sets a random job based on the player's vehicle and returns 1 if a job has been set
  270. // The function returns 0 if a job couldn't be set (if the player is driving an invalid vehicle to start trucking-jobs)
  271. Trucker_SetRandomJob(playerid)
  272. {
  273. // If the player is the driver of the vehicle (GetPlayerVehicleSeat returns -1 if the player is not in a vehicle)
  274. if (GetPlayerVehicleSeat(playerid) == 0)
  275. {
  276. // Check the vehicle-model of the player to decide which job the player can get
  277. switch (GetVehicleModel(GetPlayerVehicleID(playerid)))
  278. {
  279. case VehicleFlatbed, VehicleDFT30: // Select a random job from the routes that don't require a trailer and store the data for the player (Flatbed or DFT-30)
  280. return Trucker_SetRandomJobData(playerid, PCV_TruckerNoTrailer);
  281. case VehicleCementTruck: // Select a random job from the routes for cementtrucks and store the data for the player
  282. return Trucker_SetRandomJobData(playerid, PCV_TruckerCementTruck);
  283. case VehicleLineRunner, VehicleTanker, VehicleRoadTrain: // If the player's vehicle is a "LineRunner", "Tanker" or "RoadTrain"
  284. {
  285. // Select a job based on the trailer model of the player
  286. switch (GetVehicleModel(GetVehicleTrailer(GetPlayerVehicleID(playerid))))
  287. {
  288. case VehicleTrailerCargo, VehicleTrailerCargo2: // Select a random job from the routes that require a cargo-trailer and store the data for the player
  289. return Trucker_SetRandomJobData(playerid, PCV_TruckerCargoTrailer);
  290. case VehicleTrailerOre: // Select a random job from the routes that require a ore-trailer and store the data for the player
  291. return Trucker_SetRandomJobData(playerid, PCV_TruckerOreTrailer);
  292. case VehicleTrailerFluids: // Select a random job from the routes that require a fluids-trailer and store the data for the player
  293. return Trucker_SetRandomJobData(playerid, PCV_TruckerFluidsTrailer);
  294. }
  295. }
  296. }
  297. }
  298. // If no job could be set correctly, return 0
  299. return 0;
  300. }
  301. // This function chooses a random product for the trucker with a given vehicle-type and also the start-location and end-location
  302. Trucker_SetRandomJobData(playerid, PCV_Needed)
  303. {
  304. // Get a random Load from the loads that are defined for truckers with the given vehicle-type
  305. APlayerData[playerid][LoadID] = Product_GetRandom(PCV_Needed);
  306. // Get a random start-location and end-location for this load
  307. APlayerData[playerid][JobLoc1] = Product_GetRandomStartLoc(APlayerData[playerid][LoadID]);
  308. APlayerData[playerid][JobLoc2] = Product_GetRandomEndLoc(APlayerData[playerid][LoadID]);
  309. // Store the vehicleID (required to be able to check if the player left his vehicle)
  310. APlayerData[playerid][VehicleID] = GetPlayerVehicleID(playerid);
  311. // Store the trailerID (required to be able to check if the player lost his trailer)
  312. APlayerData[playerid][TrailerID] = GetVehicleTrailer(GetPlayerVehicleID(playerid));
  313. // Return 1 to indicate that a job has been set correctly
  314. return 1;
  315. }
  316. // This function is used to cleanup the current job
  317. Trucker_EndJob(playerid)
  318. {
  319. if (APlayerData[playerid][JobStarted] == true)
  320. {
  321. // Clear the Mafia-wanted status of the vehicle (or trailer) in the array "AVehicleData"
  322. if (APlayerData[playerid][TrailerID] == 0)
  323. AVehicleData[APlayerData[playerid][VehicleID]][MafiaLoad] = false; // The player has no trailer, so clear his main vehicle as wanted by the mafia
  324. else
  325. AVehicleData[APlayerData[playerid][TrailerID]][MafiaLoad] = false; // The player has a trailer, so clear his trailer as wanted by the mafia
  326. // Clear all data about the job from the player, so he can start a new one
  327. APlayerData[playerid][JobStarted] = false;
  328. APlayerData[playerid][JobStep] = 0;
  329. APlayerData[playerid][JobID] = 0;
  330. APlayerData[playerid][VehicleTimerTime] = 0;
  331. APlayerData[playerid][VehicleID] = 0;
  332. APlayerData[playerid][TrailerID] = 0;
  333. APlayerData[playerid][LoadID] = 0;
  334. APlayerData[playerid][JobLoc1] = 0;
  335. APlayerData[playerid][JobLoc2] = 0;
  336. APlayerData[playerid][MafiaLoad] = false;
  337. // Delete the checkpoint
  338. DisablePlayerCheckpoint(playerid);
  339. // Reset the missiontext
  340. TextDrawSetString(APlayerData[playerid][MissionText], Trucker_NoJobText);
  341. // Kill the LoadingTimer
  342. KillTimer(APlayerData[playerid][LoadingTimer]);
  343. // Check if the player has been overloaded
  344. if (APlayerData[playerid][Overloaded] == true)
  345. {
  346. // Clear the overloaded status of the player
  347. APlayerData[playerid][Overloaded] = false;
  348. // Check if the player has a wanted level of 2 or higher
  349. if (GetPlayerWantedLevel(playerid) >= 2)
  350. SetPlayerWantedLevel(playerid, GetPlayerWantedLevel(playerid) - 2); // Reduce the wanted level by 2
  351. else
  352. SetPlayerWantedLevel(playerid, 0); // If the player has a wanted level of less than 2, reset the wanted level to 0
  353. }
  354. }
  355. return 1;
  356. }