Advertisement
Syky

Weather Bug

Jul 20th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. //////////////////////////////////////////////////////////////////////////////// CHANGE WEATHER FUNCTION (somewhere is LOCAL error after PLAYER spawn)
  2.  
  3. // First three will hold our time
  4. local globalhours = 12;
  5. local globalminutes = 0;
  6.  
  7. // This will hold a formated version of time
  8. // which will be sent to client with triggerClientEvent
  9. local globaltime = "12:00";
  10.  
  11. // Holds our current weather
  12. local serverWeather = "DT_RTRclear_day_noon";
  13.  
  14. // This holds the weather that the player has
  15. // If this does not match with serverWeather then we set the weather again.
  16. local playerWeather = array(MAX_PLAYERS, 0);
  17.  
  18. // In playerConnect, set the playerWeather variable
  19. function playerConnect(playerid) {
  20. // Just set it to an empty string
  21. playerWeather[playerid] <- "";
  22. }
  23. addEventHandler("onPlayerConnect", playerConnect);
  24.  
  25. /**
  26. * This array contains all the weathers and are ordered to fit the hour
  27. * Layout:
  28. * [HOUR_START, HOUR_END, ["LIST", "OF", "WEATHERS", ...] ]
  29. *
  30. * So if time is between HOUR_START and HOUR_END, then it will pick one of the
  31. * weathers that exist in the same slot.
  32. * You can recheck and reorder the weathers, if you have atleast one weather in
  33. * a slot then it should not break, since the code checks how many weathers the
  34. * slot contains.
  35. */
  36. local Weathers = [
  37. // This is between the hours of 00:00 and 02:00 (night)
  38. [0, 2, "DT_RTRclear_day_early_morn1", "DT_RTRfoggy_day_early_morn1", "DT_RTRrainy_day_early_morn", "DT_RTRclear_day_early_morn1"],
  39. // This is between the hours of 03:00 and 05:00 (night / early morning)
  40. [3, 5, "DT_RTRclear_day_early_morn2", "DT_RTRfoggy_day_morning", "DT_RTRrainy_day_morning", "DT_RTRclear_day_early_morn2"],
  41. // This is between the hours of 06:00 and 08:00 (morning) etc.
  42. [6, 8, "DT_RTRclear_day_morning", "DT_RTRfoggy_day_noon", "DT_RTRrainy_day_noon", "DT_RTRclear_day_morning"],
  43. [9, 11, "DT_RTRclear_day_noon", "DT_RTRfoggy_day_afternoon", "DT_RTRrainy_day_afternoon", "DT_RTRclear_day_noon"],
  44. [12, 14, "DT_RTRclear_day_afternoon", "DT_RTRfoggy_day_late_afternoon", "DT_RTRrainy_day_late_afternoon", "DT_RTRclear_day_afternoon"],
  45. [15, 17, "DT_RTRclear_day_late_afternoon", "DT_RTRfoggy_day_evening", "DT_RTRrainy_day_evening", "DT_RTRclear_day_late_afternoon"],
  46. [18, 20, "DT_RTRclear_day_evening", "DT_RTRclear_day_late_even", "DT_RTRfoggy_day_late_even", "DT_RTRrainy_day_late_even"],
  47. [21, 23, "DT_RTRclear_day_night", "DT_RTRfoggy_day_night", "DT_RTRrainy_day_night", "DT_RTRclear_day_night"]];
  48.  
  49. // With this we check when its time to change the weather,
  50. // pretty much random but at the same time fits the hour
  51. local triggerWeatherChange = 10;
  52.  
  53.  
  54.  
  55. // In scriptInit, create a timer to call Time function
  56. function scriptInit() {
  57.  
  58. // Create timer to call function Time with duration of 1 second indefinitely
  59. timer(Time, 2000, -1);
  60. }
  61. addEventHandler("onScriptInit", scriptInit);
  62.  
  63.  
  64. // Our timer function, called from scriptInit
  65. function Time() {
  66. // This increments slowly until we do a check
  67. // whether it's time to change weather or not
  68. triggerWeatherChange--;
  69. //log("Weather change in " + triggerWeatherChange);
  70.  
  71. // Increment minutes
  72. globalminutes++;
  73.  
  74. // When seconds go above 60,
  75. // increment minutes and reset seconds
  76.  
  77. // Same for minutes but increment hours
  78. // and reset minutes
  79. if (globalminutes >= 60) {
  80. globalhours++;
  81. globalminutes = 0;
  82. }
  83. // Almost the same for hours but
  84. // resets both hours and minutes
  85. if (globalhours >= 24) {
  86. globalhours = 0;
  87. globalminutes = 0;
  88. }
  89.  
  90.  
  91. /**
  92. // Now lets format our time string
  93. */
  94. // First clear the string
  95.  
  96. globaltime = "";
  97.  
  98. // Now we'll format our string this way: hours:minutes:seconds
  99. // Start with the hours, if below 0 then
  100. // Add a 0 at the beginning of the hours string
  101. if (globalhours < 10) {
  102. globaltime = "0" + globalhours;
  103. } else {
  104. globaltime = globalhours;
  105. }
  106. // Same thing for the minutes,
  107. // append minutes onto the time string
  108. if (globalminutes < 10) {
  109. globaltime = globaltime + ":0" + globalminutes;
  110. } else {
  111. globaltime = globaltime + ":" + globalminutes;
  112. }
  113. // Same thing for the seconds,
  114. // append the seconds onto the time string
  115.  
  116. /**
  117. * Before we set the time, let's set the weather
  118. */
  119.  
  120. // So first, check if the triggerWeatherChange count (same as seconds) is
  121. // between 1800 and 3540 (so if between 30 and 60 minutes)
  122. if (triggerWeatherChange <= 0) {
  123.  
  124. // Go through the weathers array
  125. for (local i = 0; i < Weathers.len(); i++) {
  126. // Check and compare current hour with the hours in array
  127. // So it checks if current hour is between HOUR_START and HOUR_END
  128. if (globalhours >= Weathers[i][0] && globalhours <= Weathers[i][1]) {
  129. // Select a random weather between start slot 2 and last slot
  130. local randWeather = Weathers[i][random(2, Weathers[i].len()-1)];
  131. // Set the random weather for all players
  132. setWeather( randWeather );
  133. // Change serverWeather string
  134. serverWeather = randWeather;
  135. // Loop through players and set their weather as well
  136. for (local p = 0; p < MAX_PLAYERS; p++) {
  137. if (isPlayerConnected(p)) {
  138. playerWeather[p] = serverWeather;
  139. }
  140. }
  141. // Reset the triggerWeatherChange count
  142. triggerWeatherChange = random(7*15, 15*15);
  143.  
  144. // Stop the for loop
  145. break;
  146. }
  147. }
  148. }
  149.  
  150.  
  151. //Now we come to setting the time and weather for the users
  152.  
  153. // Loop through players
  154. for (local i = 0; i < MAX_PLAYERS; i++) {
  155. // Must check if a player is connected
  156. if (isPlayerConnected(i)) {
  157. // First check if player does not have the current weather
  158. if (playerWeather[i] != serverWeather) {
  159. // Set weather in player variable
  160. playerWeather[i] = serverWeather;
  161.  
  162. // Set weather server wide again
  163. setWeather(serverWeather);
  164. }
  165.  
  166. // Then we trigger to set time on client side
  167. // We pass in the playerid, what event to call and what to
  168. // send to that event
  169. triggerClientEvent(i, "setTime", globaltime);
  170. }
  171. }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement