aorecomp

Anarchy Online classic in the browser

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


server/shop_catalog.go (1511B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"math/rand/v2"
      6 	"time"
      7 )
      8 
      9 func shopQualityRange(entry gamedata.ShopItem) (uint32, uint32) {
     10 	definition := gamedata.ItemTemplates[entry.Template]
     11 	pair, exists := gamedata.ItemFamilies[entry.Template]
     12 	class := itemStat(definition, 76)
     13 	// Keep supplies, fixed-QL goods and the starter equipment core continuously available.
     14 	if !exists || entry.Quality <= 10 || (class != 1 && class != 2) {
     15 		return entry.Quality, entry.Quality
     16 	}
     17 	spread := max(uint32(1), entry.Quality/10)
     18 	return max(*gamedata.ItemTemplates[pair[0]].Quality, entry.Quality-spread), min(*gamedata.ItemTemplates[pair[1]].Quality, entry.Quality+spread)
     19 }
     20 
     21 func (server *server) shopCatalog(machine, template uint32, now time.Time) []gamedata.ShopItem {
     22 	// A shop shares one selection across players for each half-hour, without a background timer.
     23 	roll := rand.New(rand.NewPCG(uint64(machine), uint64(now.Unix()/1800)))
     24 	stock := gamedata.ShopStock[template]
     25 	result := make([]gamedata.ShopItem, 0, len(stock))
     26 	seen := make(map[gamedata.ShopItem]bool)
     27 	for _, entry := range stock {
     28 		low, high := shopQualityRange(entry)
     29 		if low != high {
     30 			if roll.IntN(4) == 0 {
     31 				continue
     32 			}
     33 			entry.Quality = low + uint32(roll.IntN(int(high-low+1)))
     34 		}
     35 		if !seen[entry] {
     36 			result = append(result, entry)
     37 			seen[entry] = true
     38 		}
     39 	}
     40 	// Even a one-item specialty assortment remains a usable shop.
     41 	if len(result) == 0 && len(stock) != 0 {
     42 		result = append(result, stock[0])
     43 	}
     44 	return result
     45 }