server/admin_weather.go (2161B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "encoding/binary" 6 "math" 7 "net/http" 8 "strconv" 9 "time" 10 ) 11 12 // WeatherControlIIR_t::Read (kkr.dll 0x100dbe30) and the environment queue 13 // (0x100da960) define these fields; see docs/admin.md for the preset policy. 14 func weatherPacket(player *character, weather int) []byte { 15 body := make([]byte, 43) 16 binary.BigEndian.PutUint16(body, 1) 17 binary.BigEndian.PutUint32(body[2:], 4) 18 binary.BigEndian.PutUint16(body[6:], 1) 19 binary.BigEndian.PutUint32(body[8:], math.Float32bits(1000000)) 20 body[12] = []byte{3, 3, 0, 2, 1}[weather] 21 body[13] = 70 22 body[15] = 20 23 copy(body[20:26], []byte{255, 255, 255, 160, 170, 180}) 24 if weather == 4 { 25 body[26] = 25 26 } 27 origin := gamedata.Playfields[player.playfield].Origin 28 position := [3]float32{player.position[0] + float32(origin[0]), player.position[1], player.position[2] + float32(origin[1])} 29 for index, value := range position { 30 binary.BigEndian.PutUint32(body[27+index*4:], math.Float32bits(value)) 31 } 32 return n3Packet(0x0c5a5d6d, 50000, player.id, body) 33 } 34 35 func (state *server) setWeather(w http.ResponseWriter, r *http.Request) { 36 weather, err := strconv.Atoi(r.FormValue("weather")) 37 if err != nil || weather < 1 || weather > 4 { 38 http.Error(w, "Invalid weather", 400) 39 return 40 } 41 err = state.world.operate(func(coordinator *worldCoordinator) error { 42 return state.world.publishEnvironment(zoneWeather{weather, time.Now().Add(5 * time.Minute)}) 43 }) 44 if err != nil { 45 http.Error(w, err.Error(), http.StatusInternalServerError) 46 return 47 } 48 http.Redirect(w, r, state.baseURL+"admin", http.StatusSeeOther) 49 } 50 51 func (server *server) weatherUpdates(now time.Time, updates map[*worldClient][][]byte) { 52 // Let the client's ordinary weather resume when this event ends. 53 if !now.Before(server.weatherUntil) { 54 server.weather = 0 55 } 56 if server.weather == 0 || now.Before(server.nextWeather) { 57 return 58 } 59 server.nextWeather = now.Add(5 * time.Second) 60 for id, client := range server.clients { 61 if !client.ready || client.loading || !client.target { 62 continue 63 } 64 updates[client] = append(updates[client], weatherPacket(server.characters[id], server.weather)) 65 } 66 }