ChainsBoy

[M2-Online] Server time & weather

May 3rd, 2016
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.08 KB | None | 0 0
  1. /*******************************************
  2.     CLIENT SIDE
  3. ********************************************/
  4.  
  5. local SCREEN = getScreenSize();
  6.  
  7. // Text that will be displayed
  8. local TIME = "12:00";
  9.  
  10. local TIME_X = 120.0;
  11. local TIME_Y = 32.0;
  12.  
  13.  
  14. addEventHandler( "onClientFrameRender", function ( post ) {
  15.     if( post ) {
  16.         // Draw the TIME on screen
  17.         dxDrawText(TIME, (SCREEN[0] - TIME_X).tofloat(), TIME_Y.tofloat(), 0xAAFFFFFF, true, "arial", 2.0);
  18.     }
  19. });
  20.  
  21. // Server sends time to this event
  22. addEventHandler("setTime", function (text) {
  23.     TIME = text.tostring();
  24. });
  25.  
  26.  
  27.  
  28.  
  29. /*******************************************
  30.     SERVER SIDE
  31. ********************************************/
  32.  
  33. // First three will hold our time
  34. GLOBAL_HOURS <- 12;
  35. GLOBAL_MINUTES <- 0;
  36. GLOBAL_SECONDS <- 0;
  37. // This will hold a formatted version of time, which we send to client with triggerClientEvent
  38. GLOBAL_TIME <- "12:00";
  39.  
  40. // Set the in-game time speed
  41. SERVER_TIME_SPEED <- 500;
  42. // Set if it's summer or winter on server
  43. SERVER_IS_SUMMER <- true;
  44. // Holds our current weather
  45. SERVER_WEATHER <- "DT_RTRclear_day_noon";
  46.  
  47. // This we use to check when to change weather again
  48. WEATHER_CHANGE_TRIGGER <- 10;
  49.  
  50.  
  51. /**
  52.  * This array contains all the weathers and are ordered to fit the hour
  53.  * Layout:
  54.  *      [HOUR_START, HOUR_END, ["LIST", "OF", "WEATHERS", ...] ]
  55.  *
  56.  * So if time is between HOUR_START and HOUR_END,
  57.  * then it will pick one of the weathers that are available in slot [2].
  58.  */
  59. WEATHERS <- {
  60.     SUMMER = [
  61.         // This is between the hours of 00:00 and 02:00 (night)
  62.         [0, 2, ["DT_RTRclear_day_nigh", "DT07part04night_bordel", "DTFreerideNight", "DT14part11", "DT11part05", "DT_RTRrainy_day_night", "DT10part03Subquest", "DT_RTRfoggy_day_night"] ],
  63.        
  64.         // This is between the hours of 03:00 and 05:00 (night / early morning)
  65.         [3, 5, ["DT_RTRclear_day_early_morn1", "DT_RTRfoggy_day_early_morn1", "DT_RTRrainy_day_early_morn"] ],
  66.        
  67.         // This is between the hours of 06:00 and 08:00 (morning) etc.
  68.         [6, 9, ["DT_RTRclear_day_early_morn2", "DT_RTRclear_day_morning", "DT_RTRrainy_day_morning", "DT_RTRfoggy_day_morning"] ],
  69.  
  70.         [10, 10, ["DTFreeRideDay", "DT06part03", "DTFreeRideDayRain", "DT11part01", "DT_RTRfoggy_day_noon"] ],
  71.         [11, 11, ["DT07part01fromprison", "DT13part01death", "DT09part1VitosFlat", "DT_RTRclear_day_noon", "DT06part01", "DT06part02", "DT11part02"] ],
  72.         [12, 12, ["DT07part02dereksubquest", "DT08part01cigarettesriver", "DT09part2MalteseFalcone", "DT14part1_6", "DT_RTRrainy_day_noon", "DT_RTRfoggy_day_afternoon"] ],
  73.         [13, 13, ["DT_RTRclear_day_afternoon", "DT10part02Roof", "DT09part3SlaughterHouseAfter", "DT_RTRrainy_day_afternoon", "DT_RTRfoggy_day_afternoon"] ],
  74.         [14, 15, ["DT09part4MalteseFalcone2", "DT08part02cigarettesmill", "DT12_part_all", "DT15", "DT15end", "DT15_interier"] ],
  75.         [16, 17, ["DT13part02", "DT_RTRclear_day_late_afternoon", "DT_RTRrainy_day_late_afternoon", "DT11part03", "DT_RTRfoggy_day_late_afternoon"] ],
  76.         [18, 18, ["DT08part03crazyhorse", "DT07part03prepadrestaurcie", "DT_RTRrainy_day_evening", "DT_RTRfoggy_day_late_afternoon"] ],
  77.         [19, 19, ["DT05part06Francesca", "DT10part03Evening", "DT14part7_10", "DT11part04", "DT_RTRfoggy_day_evening"] ],
  78.         [20, 23, ["DT_RTRclear_day_evening", "DT08part04subquestwarning", "DT_RTRclear_day_late_even", "DT_RTRrainy_day_late_even", "DT_RTRfoggy_day_late_even"] ],
  79.     ],
  80.  
  81.     WINTER = [
  82.         [0, 7, ["DTFreeRideNightSnow", "DT04part02"] ],
  83.         [8, 11, ["DT05part01JoesFlat", "DT03part01JoesFlat", "DTFreeRideDaySnow"] ],
  84.         [12, 13, ["DT05part02FreddysBar", "DTFreeRideDayWinter", "DT05part04Distillery", "DT04part01JoesFlat"] ],
  85.         [14, 15, ["DT02part01Railwaystation", "DT05part03HarrysGunshop", "DT05part05ElGreco"] ],
  86.         [16, 17, ["DT02part02JoesFlat", "DT02part04Giuseppe", "DT01part01sicily_svit", "DT03part02FreddysBar"] ],
  87.         [18, 20, ["DT05Distillery_inside", "DT02part05Derek", "DT02part03Charlie"] ],
  88.         [21, 23, ["DT02NewStart1", "DT03part03MariaAgnelo", "DT02NewStart2", "DT03part04PriceOffice", "DT01part02sicily"] ],
  89.     ]
  90. };
  91.  
  92.  
  93. // We use this to make sure all players have same weather
  94. // In case it bugs or alike and is unsycned
  95. playerWeather <- array(MAX_PLAYERS, 0);
  96.  
  97.  
  98. function scriptInit() {
  99.     log("Script initialized!");
  100.    
  101.     // Set summer state
  102.     setSummer(SERVER_IS_SUMMER);
  103.  
  104.     // Create timer to call function Time with duration from SERVER_TIME_SPEED
  105.     timer(Time, SERVER_TIME_SPEED, -1);
  106. }
  107. addEventHandler("onScriptInit", scriptInit);
  108.  
  109.  
  110.  
  111. function playerConnect(playerid, name, ip, serial) {
  112.     // Just set it to an empty string for now
  113.     playerWeather[playerid] = "";
  114. }
  115. addEventHandler("onPlayerConnect", playerConnect);
  116.  
  117.  
  118. // Need a random function to select a new weather
  119. function random (min = 0, max = RAND_MAX) {
  120.     srand((getTickCount() * rand()));
  121.     return (rand() % ((max.tointeger() + 1) - min.tointeger())) + min.tointeger();
  122. }
  123.  
  124.  
  125. // Our timer function, called from scriptInit
  126. function Time() {
  127.     // Decreases slowly
  128.     WEATHER_CHANGE_TRIGGER--;
  129.  
  130.     // Increment seconds
  131.     GLOBAL_SECONDS++;
  132.  
  133.     // When seconds go above 60, increment minutes and reset seconds
  134.     if(GLOBAL_SECONDS >= 60){
  135.         GLOBAL_MINUTES++;
  136.         GLOBAL_SECONDS = 0;
  137.     }
  138.     // Same for minutes but increment hours, and reset minutes
  139.     if (GLOBAL_MINUTES >= 60) {
  140.         GLOBAL_HOURS++;
  141.         GLOBAL_MINUTES = 0;
  142.     }
  143.     // Almost the same for hours but, resets both hours and minutes
  144.     if (GLOBAL_HOURS >= 24) {
  145.         GLOBAL_HOURS = 0;
  146.         GLOBAL_MINUTES = 0;
  147.     }
  148.  
  149.     // Use format to structure the time string, looks more cleaner than loads of if statements.
  150.     // "%02i" makes sure that at least two integers are present.  Result:  HH:MM
  151.     GLOBAL_TIME = format("%02i:%02i", GLOBAL_HOURS, GLOBAL_MINUTES);
  152.  
  153.  
  154.  
  155.     // First we check if weather count has reached 0
  156.     if (WEATHER_CHANGE_TRIGGER <= 0) {
  157.         // Get weather based on what season it is
  158.         local weathers = (SERVER_IS_SUMMER) ? WEATHERS.SUMMER : WEATHERS.WINTER;
  159.  
  160.         // Go through the weathers array
  161.         for (local i = 0; i < weathers.len(); i++) {
  162.             // Check and compare current hour with the hours in array
  163.             // So it checks if current hour is between HOUR_START and HOUR_END
  164.             if (GLOBAL_HOURS >= weathers[i][0] && GLOBAL_HOURS <= weathers[i][1]) {
  165.                 // Select a random weather from slot [2]
  166.                 local randWeather = weathers[i][2][random(0, weathers[i][2].len()-1)];
  167.                 // Set the random weather for all players
  168.                 setWeather(randWeather);
  169.                 // Change SERVER_WEATHER string
  170.                 SERVER_WEATHER = randWeather;
  171.                
  172.                 // Loop through players and set their playerWeather variable
  173.                 for (local p = 0; p < MAX_PLAYERS; p++) {
  174.                     if (isPlayerConnected(p)) {
  175.                         playerWeather[p] = SERVER_WEATHER;
  176.                     }
  177.                 }
  178.                 // Generate a new number when weather change will happen again
  179.                 // New count is between 20 and 60 in-game minutes.
  180.                 WEATHER_CHANGE_TRIGGER = random(20*60, 60*60);
  181.  
  182.                 // Break out of the loop
  183.                 break;
  184.             }
  185.         }
  186.     }
  187.  
  188.  
  189.  
  190.     // Loop through players
  191.     for (local i = 0; i < MAX_PLAYERS; i++) {
  192.         // Must check if a player is connected
  193.         if (isPlayerConnected(i)) {
  194.             // First check if player does not have the current weather
  195.             if (playerWeather[i] != SERVER_WEATHER) {
  196.                 // Set weather in playerWeather variable
  197.                 playerWeather[i] = SERVER_WEATHER;
  198.  
  199.                 // Set weather server wide again
  200.                 setWeather(SERVER_WEATHER);
  201.             }
  202.  
  203.             // Trigger the client event and send our GLOBAL_TIME there to display
  204.             triggerClientEvent(i, "setTime", GLOBAL_TIME);  
  205.         }
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment