func (t *TimeService) Start() { if !t.isRunning { // Calculate in-game time from real world time // this way we always have the right in-game time t.calculateTimeFromSystem() // Generate random starting weather t.generateWeather() t.isRunning = true t.run() log.Info("TimeService", "Start", "TimeService started") } }
func (m *Map) processTiles() { for { row, ok := <-m.processChan if !ok { break } // Get or create tile tile := m.getOrAddTile(row.X, row.Y, row.Z) // If the tile is new set extra data if tile.IsNew { //Set tile db id tile.DbId = row.Idtile // Set blocking tile.Blocking = row.Movement // Link location to tile // if location, found := g_locations.Get(row.Idlocation); found { // tile.Location = location // } // Check if we have a tile event id. If so, do something with it // if row.IdtileEvent > 0 { // if row.Eventtype == pulogic.EVENTTYPE_TELEPORT { // destination_x, _ := strconv.Atoi(row.Param1) // destination_y, _ := strconv.Atoi(row.Param2) // destination_z, _ := strconv.Atoi(row.Param3) // // destination := pos.NewPositionFrom(destination_x, destination_y, destination_z) // teleport := NewTeleport(destination) // // tile.AddEvent(teleport) // } // } tile.IsNew = false } // Add layer to tile if IS_DEBUG { log.Info("Map", "processTiles", "Current layer, sprite, idtilelayer: %d, %d, %d", row.Layer, row.Sprite, row.IdtileLayer) } tile.AddLayer(row.Layer, row.Sprite, row.IdtileLayer, false) } m.processExitChan <- true }
func (m *Map) LoadTiles() bool { start := time.Now().UnixNano() var allTiles []TileRow //Init the tile id to 1 g_newTileId = 1 err := g_orm.SetTable("tile").Join("INNER", "tile_layer", "tile_layer.tileid = tile.idtile").Join(" LEFT", "tile_events", "tile_events.idtile_events = tile.idtile_event").OrderBy("tile.idtile DESC").FindAll(&allTiles) if err != nil { log.Error("map", "loadTiles", "Error while loading tiles: %v", err.Error()) return false } log.Info("Map", "loadTiles", "%d tiles fetched in %dms", len(allTiles), (time.Now().UnixNano()-start)/1e6) if len(allTiles) > 0 { log.Verbose("Map", "loadTiles", "Processing tiles with %d goroutines", m.numOfProcessRoutines) // Start process goroutine for i := 1; i <= m.numOfProcessRoutines; i++ { go m.processTiles() } // Send rows to channel for key, row := range allTiles { //First tile has highest ID if key == 0 { g_newTileId = (row.Idtile + 1) log.Verbose("Map", "loadTiles", "Determined next tile ID: %d", g_newTileId) } m.processChan <- row } // Close channel so the process goroutine(s) will shutdown close(m.processChan) } return true }
func (t *TimeService) run() { if t.isRunning { // In-game clock runs twice as fast as real time go func() { time.Sleep(time.Second * 30) t.run() }() currentMin := t.Minutes if currentMin == 59 { currentMin = 0 currentHour := t.Hour if currentHour == 23 { currentHour = 0 t.incrementDay() } else { currentHour++ } t.Hour = currentHour } else { currentMin++ } t.Minutes = currentMin weatherUpdate := SYS_TIME() - t.lastWeatherUpdate if weatherUpdate > 3600000 { t.generateWeather() t.lastWeatherUpdate = SYS_TIME() } } else { log.Info("TimeService", "Run", "TimeServie stopped") } }
func (t *TimeService) generateWeather() { weather := t.forcedWeather if weather == 9 { weather = rand.Intn(10) } switch weather { case 0: t.Weather = WEATHER_NORMAL case 1: t.Weather = WEATHER_RAIN case 2: t.Weather = WEATHER_HAIL case 3: t.Weather = WEATHER_FOG case 4: t.Weather = WEATHER_SANDSTORM default: t.Weather = WEATHER_NORMAL } log.Info("TimeService", "Weather", "INFO: Weather type changed to: %v", t.WeatherToStr()) }
func (t *TimeService) Stop() { if t.isRunning { log.Info("TimeService", "Stop", "Stopping TimeService") t.isRunning = false } }
func (t *TimeService) calculateTimeFromSystem() { currentTime := time.Now() currentDay := currentTime.Weekday() currentHour := currentTime.Hour() currentMinute := currentTime.Minute() if currentDay == time.Monday { if currentHour < 12 { t.Day = int(time.Monday) } else { t.Day = int(time.Tuesday) } } else if currentDay == time.Tuesday { if currentHour < 12 { t.Day = int(time.Wednesday) } else { t.Day = int(time.Thursday) } } else if currentDay == time.Wednesday { if currentHour < 12 { t.Day = int(time.Friday) } else { t.Day = int(time.Saturday) } } else if currentDay == time.Thursday { if currentHour < 12 { t.Day = int(time.Sunday) } else { t.Day = int(time.Monday) } } else if currentDay == time.Friday { if currentHour < 12 { t.Day = int(time.Tuesday) } else { t.Day = int(time.Wednesday) } } else if currentDay == time.Saturday { if currentHour < 12 { t.Day = int(time.Thursday) } else { t.Day = int(time.Friday) } } else { if currentHour < 12 { t.Day = int(time.Saturday) } else { t.Day = int(time.Sunday) } } if currentHour >= 12 { currentHour = currentHour - 12 } t.Hour = currentHour * 2 if currentMinute >= 30 { currentMinute = currentMinute - 30 // 30 min real world time, increment in-game hour by 1 t.Hour++ } t.Minutes = currentMinute * 2 log.Info("TimeService", "INFO", "Current time is %v @ %d:%d", Days[t.Day], t.Hour, t.Minutes) }