Skip to content

Commit

Permalink
refactor: extract intelligent kill-monster function
Browse files Browse the repository at this point in the history
  • Loading branch information
benknoble committed Sep 6, 2024
1 parent 05df852 commit 7608aad
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 31 deletions.
15 changes: 1 addition & 14 deletions gui/manager.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -283,20 +283,7 @@
(define (update-hp num proc)
(update-by-num num (monster-update-hp proc)))
(define (kill num)
(define g (box #f))
(<@ (state-@creatures s)
(λ (cs)
(define cs* (update-monster-groups
cs
k
(λ (mg)
(define new-mg ((monster-group-remove num) mg))
(set-box! g new-mg)
new-mg)
{~> 2> monster-group-first-monster}))
cs*))
(when (~> (g) unbox monster-group-monsters empty?)
((add-or-remove-monster-group s) `(remove ,(unbox g)))))
(kill-monster s k num))
(define (new num elite?)
(update (monster-group-add num elite? (@! @env))
(const num))
Expand Down
20 changes: 20 additions & 0 deletions manager/state.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
(-> monster-group? monster-group?)}
{(-> (or/c #f monster-number/c) monster-group? (or/c #f monster-number/c))}
(listof creature?))]
[kill-monster (-> state? any/c monster-number/c any)]
[update-all-players (-> (listof creature?) (-> player? player?) (listof creature?))]
[update-all-monster-groups (-> (listof creature?) (-> monster-group? monster-group?) (listof creature?))]
[update-player-name (-> state? (-> any/c string? any))]
Expand Down Expand Up @@ -437,6 +438,25 @@
e))
(map maybe-update-monster-group creatures))

(define (kill-monster s monster-group-id monster-number)
;; g records the result of monster-group-remove to avoid having to traverse
;; state-@creatures again to find out what to do; with more efficient data
;; structures, this would likely be unnecessary.
(define g (box #f))
(<@ (state-@creatures s)
(λ (cs)
(define cs* (update-monster-groups
cs
monster-group-id
(λ (mg)
(define new-mg ((monster-group-remove monster-number) mg))
(set-box! g new-mg)
new-mg)
{~> 2> monster-group-first-monster}))
cs*))
(when (~> (g) unbox monster-group-monsters empty?)
((add-or-remove-monster-group s) `(remove ,(unbox g)))))

(define (update-all-players creatures f)
(define (update-only-player c)
(match c
Expand Down
8 changes: 8 additions & 0 deletions scribblings/manager/state.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ When updating a monster-group, @racket[fn] can update the
@racket[monster-group*-active] number.
}

@defproc[(kill-monster [s state?] [monster-group-id any/c] [monster-number monster-number/c])
any]{
Composes @racket[monster-group-remove] with @racket[update-monster-groups], and
intelligently sets the current active number or removes the monster group if the
last monster is killed. Prefer this procedure to the lower-level primitives when
manipulating state to remove or kill monsters in monster groups.
}

@deftogether[(
@defproc[(update-all-players [creatures (listof creature?)]
[f (-> player? player?)])
Expand Down
20 changes: 3 additions & 17 deletions server.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@
(define (monster-action req what args)
(let/ec return
(match (cons what args)
['("kill") (kill-monster req)]
['("kill") (do-kill-monster req)]
['("hp" "-") (decrement-monster-hp req)]
['("hp" "+") (increment-monster-hp req)]
['("condition" "add") (add-monster-condition req)]
Expand Down Expand Up @@ -1000,22 +1000,8 @@
[(and c (not #f)) (do-player/id pid (const #f) (update-player-summon sid (summon-remove-condition c)))]
[_ (void)]))

(define/monster kill-monster (_r => [mgid num])
(do
(define g (box #f))
(<@ (state-@creatures (s))
(λ (cs)
(define cs* (update-monster-groups
cs
mgid
(λ (mg)
(define new-mg ((monster-group-remove num) mg))
(set-box! g new-mg)
new-mg)
{~> 2> monster-group-first-monster}))
cs*))
(when (~> (g) unbox monster-group-monsters empty?)
((add-or-remove-monster-group (s)) `(remove ,(unbox g))))))
(define/monster do-kill-monster (_r => [mgid num])
(do (kill-monster (s) mgid num)))

(define/monster decrement-monster-hp (_r => [mgid mn])
(do-monster-group/n mgid mn {switch [(not monster-dead?) (esc (monster-update-hp sub1))]}))
Expand Down

0 comments on commit 7608aad

Please sign in to comment.