Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////////////////////////////////////////////////////////////// CHANGE WEATHER FUNCTION (somewhere is LOCAL error after PLAYER spawn)
- // First three will hold our time
- local globalhours = 12;
- local globalminutes = 0;
- // This will hold a formated version of time
- // which will be sent to client with triggerClientEvent
- local globaltime = "12:00";
- // Holds our current weather
- local serverWeather = "DT_RTRclear_day_noon";
- // This holds the weather that the player has
- // If this does not match with serverWeather then we set the weather again.
- local playerWeather = array(MAX_PLAYERS, 0);
- // In playerConnect, set the playerWeather variable
- function playerConnect(playerid) {
- // Just set it to an empty string
- playerWeather[playerid] <- "";
- }
- addEventHandler("onPlayerConnect", playerConnect);
- /**
- * This array contains all the weathers and are ordered to fit the hour
- * Layout:
- * [HOUR_START, HOUR_END, ["LIST", "OF", "WEATHERS", ...] ]
- *
- * So if time is between HOUR_START and HOUR_END, then it will pick one of the
- * weathers that exist in the same slot.
- * You can recheck and reorder the weathers, if you have atleast one weather in
- * a slot then it should not break, since the code checks how many weathers the
- * slot contains.
- */
- local Weathers = [
- // This is between the hours of 00:00 and 02:00 (night)
- [0, 2, "DT_RTRclear_day_early_morn1", "DT_RTRfoggy_day_early_morn1", "DT_RTRrainy_day_early_morn", "DT_RTRclear_day_early_morn1"],
- // This is between the hours of 03:00 and 05:00 (night / early morning)
- [3, 5, "DT_RTRclear_day_early_morn2", "DT_RTRfoggy_day_morning", "DT_RTRrainy_day_morning", "DT_RTRclear_day_early_morn2"],
- // This is between the hours of 06:00 and 08:00 (morning) etc.
- [6, 8, "DT_RTRclear_day_morning", "DT_RTRfoggy_day_noon", "DT_RTRrainy_day_noon", "DT_RTRclear_day_morning"],
- [9, 11, "DT_RTRclear_day_noon", "DT_RTRfoggy_day_afternoon", "DT_RTRrainy_day_afternoon", "DT_RTRclear_day_noon"],
- [12, 14, "DT_RTRclear_day_afternoon", "DT_RTRfoggy_day_late_afternoon", "DT_RTRrainy_day_late_afternoon", "DT_RTRclear_day_afternoon"],
- [15, 17, "DT_RTRclear_day_late_afternoon", "DT_RTRfoggy_day_evening", "DT_RTRrainy_day_evening", "DT_RTRclear_day_late_afternoon"],
- [18, 20, "DT_RTRclear_day_evening", "DT_RTRclear_day_late_even", "DT_RTRfoggy_day_late_even", "DT_RTRrainy_day_late_even"],
- [21, 23, "DT_RTRclear_day_night", "DT_RTRfoggy_day_night", "DT_RTRrainy_day_night", "DT_RTRclear_day_night"]];
- // With this we check when its time to change the weather,
- // pretty much random but at the same time fits the hour
- local triggerWeatherChange = 10;
- // In scriptInit, create a timer to call Time function
- function scriptInit() {
- // Create timer to call function Time with duration of 1 second indefinitely
- timer(Time, 2000, -1);
- }
- addEventHandler("onScriptInit", scriptInit);
- // Our timer function, called from scriptInit
- function Time() {
- // This increments slowly until we do a check
- // whether it's time to change weather or not
- triggerWeatherChange--;
- //log("Weather change in " + triggerWeatherChange);
- // Increment minutes
- globalminutes++;
- // When seconds go above 60,
- // increment minutes and reset seconds
- // Same for minutes but increment hours
- // and reset minutes
- if (globalminutes >= 60) {
- globalhours++;
- globalminutes = 0;
- }
- // Almost the same for hours but
- // resets both hours and minutes
- if (globalhours >= 24) {
- globalhours = 0;
- globalminutes = 0;
- }
- /**
- // Now lets format our time string
- */
- // First clear the string
- globaltime = "";
- // Now we'll format our string this way: hours:minutes:seconds
- // Start with the hours, if below 0 then
- // Add a 0 at the beginning of the hours string
- if (globalhours < 10) {
- globaltime = "0" + globalhours;
- } else {
- globaltime = globalhours;
- }
- // Same thing for the minutes,
- // append minutes onto the time string
- if (globalminutes < 10) {
- globaltime = globaltime + ":0" + globalminutes;
- } else {
- globaltime = globaltime + ":" + globalminutes;
- }
- // Same thing for the seconds,
- // append the seconds onto the time string
- /**
- * Before we set the time, let's set the weather
- */
- // So first, check if the triggerWeatherChange count (same as seconds) is
- // between 1800 and 3540 (so if between 30 and 60 minutes)
- if (triggerWeatherChange <= 0) {
- // Go through the weathers array
- for (local i = 0; i < Weathers.len(); i++) {
- // Check and compare current hour with the hours in array
- // So it checks if current hour is between HOUR_START and HOUR_END
- if (globalhours >= Weathers[i][0] && globalhours <= Weathers[i][1]) {
- // Select a random weather between start slot 2 and last slot
- local randWeather = Weathers[i][random(2, Weathers[i].len()-1)];
- // Set the random weather for all players
- setWeather( randWeather );
- // Change serverWeather string
- serverWeather = randWeather;
- // Loop through players and set their weather as well
- for (local p = 0; p < MAX_PLAYERS; p++) {
- if (isPlayerConnected(p)) {
- playerWeather[p] = serverWeather;
- }
- }
- // Reset the triggerWeatherChange count
- triggerWeatherChange = random(7*15, 15*15);
- // Stop the for loop
- break;
- }
- }
- }
- //Now we come to setting the time and weather for the users
- // Loop through players
- for (local i = 0; i < MAX_PLAYERS; i++) {
- // Must check if a player is connected
- if (isPlayerConnected(i)) {
- // First check if player does not have the current weather
- if (playerWeather[i] != serverWeather) {
- // Set weather in player variable
- playerWeather[i] = serverWeather;
- // Set weather server wide again
- setWeather(serverWeather);
- }
- // Then we trigger to set time on client side
- // We pass in the playerid, what event to call and what to
- // send to that event
- triggerClientEvent(i, "setTime", globaltime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement