aboutsummaryrefslogtreecommitdiff
path: root/internal/game/game.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/game/game.go')
-rw-r--r--internal/game/game.go66
1 files changed, 20 insertions, 46 deletions
diff --git a/internal/game/game.go b/internal/game/game.go
index b9b54ba..a6b64cc 100644
--- a/internal/game/game.go
+++ b/internal/game/game.go
@@ -26,13 +26,6 @@ const (
ClassUnknown
)
-type QueuedCommand struct {
- Session *net.Session
- Command string
- Args string
- Timestamp time.Time
-}
-
type Game struct {
World *world.World
ObjectStore *object.ObjectStore
@@ -43,34 +36,23 @@ type Game struct {
CourseStore *CourseStore
Hub *net.Hub
Ticks *engine.Engine
- WorldFlags map[string]any
+ Flags *FlagStore
ColorConfig *config.ColorsConfig
DataDir string
ValidationConfig config.ValidationConfig
restTimers map[string]uint64
charsMu sync.Mutex
loggedInChars map[string]*net.Session
- freeQueue map[string][]QueuedCommand
- activeQueue map[string]*QueuedCommand
- consumeQueue map[string]*QueuedCommand
+ queue *CommandQueue
pendingDepletions []pendingDepletion
guardWatchTimers map[string]int
farmTickCounter int
hackingStates map[string]*hacking.Session
- safespotMu sync.Mutex
- safespotStates map[string]*SafespotState
+ safespot *SafespotManager
enterMu sync.Mutex
enterSeqs map[string]*enterSeq
}
-type SafespotState struct {
- Active bool
- ObjectDefID string
- ObjectIndex int
- RoomID int
- HideCountdown int
-}
-
func New(dataDir string, colorConfig *config.ColorsConfig, valConfig config.ValidationConfig) *Game {
g := &Game{
World: world.New(dataDir),
@@ -81,23 +63,22 @@ func New(dataDir string, colorConfig *config.ColorsConfig, valConfig config.Vali
CraftIndex: NewCraftIndex(),
CourseStore: NewCourseStore(dataDir),
Ticks: engine.New(),
- WorldFlags: make(map[string]any),
+ Flags: NewFlagStore(),
ColorConfig: colorConfig,
DataDir: dataDir,
ValidationConfig: valConfig,
restTimers: make(map[string]uint64),
loggedInChars: make(map[string]*net.Session),
- freeQueue: make(map[string][]QueuedCommand),
- activeQueue: make(map[string]*QueuedCommand),
- consumeQueue: make(map[string]*QueuedCommand),
+ queue: NewCommandQueue(),
pendingDepletions: nil,
guardWatchTimers: make(map[string]int),
hackingStates: make(map[string]*hacking.Session),
- safespotStates: make(map[string]*SafespotState),
+ safespot: NewSafespotManager(),
enterSeqs: make(map[string]*enterSeq),
}
g.CourseStore.LoadAll()
g.LoadMods()
+ g.LoadTechs()
g.buildCraftIndex()
g.ValidateAndLog()
return g
@@ -113,12 +94,8 @@ func (g *Game) SetHub(hub *net.Hub) {
delete(g.loggedInChars, p.Name)
g.charsMu.Unlock()
delete(g.hackingStates, p.Name)
- g.safespotMu.Lock()
- if ss, ok := g.safespotStates[p.Name]; ok {
- g.safespotMu.Unlock()
- g.forceLeaveSafespot(sess, p, ss, "")
- } else {
- g.safespotMu.Unlock()
+ if ss, ok := g.safespot.Get(p.Name); ok {
+ g.forceLeaveSafespot(sess, p, &ss, "")
}
}
})
@@ -234,7 +211,7 @@ func (g *Game) handleGameCommand(sess *net.Session, input string) {
}
if class == ClassFree {
- g.freeQueue[p.Name] = append(g.freeQueue[p.Name], QueuedCommand{
+ g.queue.EnqueueFree(p.Name, QueuedCommand{
Session: sess,
Command: cmd,
Args: strings.Join(parts[1:], " "),
@@ -252,12 +229,12 @@ func (g *Game) handleGameCommand(sess *net.Session, input string) {
return
}
- g.activeQueue[p.Name] = &QueuedCommand{
+ g.queue.EnqueueActive(p.Name, QueuedCommand{
Session: sess,
Command: cmd,
Args: strings.Join(parts[1:], " "),
Timestamp: time.Now(),
- }
+ })
g.cancelRest(p.Name)
if !p.OptionBool("queue_silently") {
sess.WriteLine(fmt.Sprintf("You prepare to %s.", cmd))
@@ -280,11 +257,13 @@ func (g *Game) ProcessQueuedCommands() {
if !ok {
p.ActionState = nil
} else {
- switch as.Type {
+ switch as.Type {
case ActionGathering, ActionCombating, ActionUsing, ActionTalking,
- ActionBurning, ActionStoking, ActionResting, ActionWalking, ActionProducing,
- ActionStealing, ActionPlanting, ActionHarvesting, ActionRaking, ActionWatering, ActionCuring,
- ActionTraversing:
+ ActionBurning, ActionStoking, ActionSearching, ActionIdentifying,
+ ActionHacking, ActionHiding, ActionWorking,
+ ActionResting, ActionWalking, ActionProducing,
+ ActionStealing, ActionPlanting, ActionHarvesting, ActionRaking, ActionWatering, ActionCuring,
+ ActionTraversing:
default:
if as.Type != ActionMoving || p.MoveTicks <= 0 {
p.ActionState = nil
@@ -297,7 +276,7 @@ func (g *Game) ProcessQueuedCommands() {
p.BackgroundActionState = nil
}
- cmds := g.freeQueue[p.Name]
+ cmds := g.queue.DrainFree(p.Name)
for _, qc := range cmds {
g.cancelRest(p.Name)
raw := qc.Command
@@ -312,7 +291,6 @@ func (g *Game) ProcessQueuedCommands() {
g.writePrompt(qc.Session)
}
}
- delete(g.freeQueue, p.Name)
if len(p.WalkSequence) > 0 {
g.advanceWalk(sess, p)
@@ -322,10 +300,7 @@ func (g *Game) ProcessQueuedCommands() {
}
}
- var actives []QueuedCommand
- for _, qc := range g.activeQueue {
- actives = append(actives, *qc)
- }
+ actives := g.queue.DrainActive()
sort.Slice(actives, func(i, j int) bool {
return actives[i].Timestamp.Before(actives[j].Timestamp)
})
@@ -354,7 +329,6 @@ func (g *Game) ProcessQueuedCommands() {
g.writePrompt(qc.Session)
}
}
- g.activeQueue = make(map[string]*QueuedCommand)
g.flushPendingDepletions()
}