aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/world_environment.go (2145B)


      1 package main
      2 
      3 import (
      4 	"math"
      5 	"time"
      6 )
      7 
      8 type worldEnvironment struct {
      9 	gameSeconds  float64
     10 	clockStarted time.Time
     11 	weather      int
     12 	weatherUntil time.Time
     13 }
     14 
     15 func currentGameSeconds(seconds float64, started time.Time) float64 {
     16 	// Rubi-Ka has 27-hour days and advances 15 game seconds per real second.
     17 	return math.Mod(seconds+time.Since(started).Seconds()*15, 97200)
     18 }
     19 
     20 type environmentChange interface {
     21 	zoneCommand
     22 	apply(*worldEnvironment)
     23 }
     24 
     25 type worldEnvironmentUpdate struct {
     26 	change environmentChange
     27 	reply  chan []*zoneRuntime
     28 }
     29 
     30 // Called inside a world operation: environment changes serialize with each
     31 // other and mission/account creation. The directory publishes defaults before
     32 // returning existing owners, so a newly created zone cannot miss the change.
     33 func (world *worldRuntime) publishEnvironment(change environmentChange) error {
     34 	update := worldEnvironmentUpdate{change, make(chan []*zoneRuntime, 1)}
     35 	world.environment <- update
     36 	for _, owner := range <-update.reply {
     37 		if err := owner.observe(change); err != nil {
     38 			return err
     39 		}
     40 	}
     41 	return nil
     42 }
     43 
     44 type zoneGameTime struct {
     45 	seconds float64
     46 	started time.Time
     47 }
     48 
     49 func (change zoneGameTime) apply(state *worldEnvironment) {
     50 	state.gameSeconds, state.clockStarted = change.seconds, change.started
     51 }
     52 
     53 func (change zoneGameTime) execute(state *server) error {
     54 	state.gameSeconds, state.clockStarted = change.seconds, change.started
     55 	updates := make(map[*worldClient][][]byte)
     56 	for _, client := range state.clients {
     57 		if client.ready {
     58 			updates[client] = append(updates[client], gameTimePacket(client.id, change.seconds))
     59 		}
     60 	}
     61 	state.sendCombatUpdates(updates)
     62 	return nil
     63 }
     64 
     65 type zoneWeather struct {
     66 	weather int
     67 	until   time.Time
     68 }
     69 
     70 func (change zoneWeather) apply(state *worldEnvironment) {
     71 	state.weather, state.weatherUntil = change.weather, change.until
     72 }
     73 
     74 func (change zoneWeather) execute(state *server) error {
     75 	state.weather, state.weatherUntil, state.nextWeather = change.weather, change.until, time.Time{}
     76 	updates := make(map[*worldClient][][]byte)
     77 	state.weatherUpdates(time.Now(), updates)
     78 	state.sendCombatUpdates(updates)
     79 	return nil
     80 }