Compare commits
6 Commits
22c4718faf
...
v0.5.0
Author | SHA1 | Date | |
---|---|---|---|
c30aca1f31
|
|||
abb00e30c3
|
|||
65c01318f0
|
|||
2b788f517c
|
|||
8d1aa0141f
|
|||
74fac625f2
|
@ -101,6 +101,63 @@ func (b *BasicCard[C]) Drawn(_ *Player[C]) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RefundAction returns a func that can be used as an AfterOption, which returns
|
||||||
|
// the player's action point.
|
||||||
|
func RefundAction[C StatsCollection]() func(c Card[C], p *Player[C], option CardOption[C]) error {
|
||||||
|
return func(c Card[C], p *Player[C], option CardOption[C]) error {
|
||||||
|
p.ActionsRemaining++
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A PanelCard is a Card that takes its title and text from an InfoPanel,
|
||||||
|
// while options, urgency, and the post-option callback are specified
|
||||||
|
// (like a BasicCard). It never does anything in particular when drawn.
|
||||||
|
//
|
||||||
|
// Omitting all options yields an inactionable card, which can be displayed
|
||||||
|
// but not played. This can be useful for adding an info panel as a debug action.
|
||||||
|
type PanelCard[C StatsCollection] struct {
|
||||||
|
Panel InfoPanel[C]
|
||||||
|
IsUrgent bool
|
||||||
|
CardOptions []CardOption[C]
|
||||||
|
// AfterOption is given the card itself as its first argument.
|
||||||
|
AfterOption func(c Card[C], p *Player[C], option CardOption[C]) error
|
||||||
|
}
|
||||||
|
|
||||||
|
// Title implements Card.
|
||||||
|
func (c *PanelCard[C]) Title(p *Player[C]) Message {
|
||||||
|
return c.Panel.Title(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Urgent implements Card.
|
||||||
|
func (c *PanelCard[C]) Urgent(_ *Player[C]) bool {
|
||||||
|
return c.IsUrgent
|
||||||
|
}
|
||||||
|
|
||||||
|
// EventText implements Card.
|
||||||
|
func (c *PanelCard[C]) EventText(p *Player[C]) (Message, error) {
|
||||||
|
msgs, err := c.Panel.Info(p)
|
||||||
|
return MultiMessage(msgs), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Options implements Card.
|
||||||
|
func (c *PanelCard[C]) Options(_ *Player[C]) ([]CardOption[C], error) {
|
||||||
|
return c.CardOptions, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then implements Card.
|
||||||
|
func (c *PanelCard[C]) Then(p *Player[C], option CardOption[C]) error {
|
||||||
|
if c.AfterOption == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return c.AfterOption(c, p, option)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Drawn implements Card.
|
||||||
|
func (c *PanelCard[C]) Drawn(_ *Player[C]) bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// A BasicOption is a CardOption with fixed text, effects, and output.
|
// A BasicOption is a CardOption with fixed text, effects, and output.
|
||||||
// It's always enabled.
|
// It's always enabled.
|
||||||
type BasicOption[C StatsCollection] struct {
|
type BasicOption[C StatsCollection] struct {
|
||||||
@ -151,3 +208,17 @@ func (o *optionFunc[C]) Enact(p *Player[C]) (Message, error) {
|
|||||||
func (o *optionFunc[C]) Enabled(p *Player[C]) bool {
|
func (o *optionFunc[C]) Enabled(p *Player[C]) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OnlyDiscardFree returns a []CardOption[C] providing a single option, which
|
||||||
|
// returns the action point. It does not shuffle the card back into the deck
|
||||||
|
// or draw a replacement (consider the AfterFunc for that if needed). This
|
||||||
|
// can be used for cards that are displayable but not actionable, but show up
|
||||||
|
// as cards rather than permanent or debug actions for some reason.
|
||||||
|
func OnlyDiscardFree[C StatsCollection](msg Message) []CardOption[C] {
|
||||||
|
return []CardOption[C]{
|
||||||
|
OptionFunc(msg, func(p *Player[C]) (Message, error) {
|
||||||
|
p.ActionsRemaining++
|
||||||
|
return MsgStr("Okay."), nil
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
103
cardsim/debugging.go
Normal file
103
cardsim/debugging.go
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
package cardsim
|
||||||
|
|
||||||
|
// Named debug verbosity levels. Using the raw constants is fine too. This
|
||||||
|
// is roughly consistent with "standard" meanings for these debug levels.
|
||||||
|
const (
|
||||||
|
HideWarnings = -1
|
||||||
|
NotDebugging = 0
|
||||||
|
DebugWarning = 1
|
||||||
|
DebugInfo = 2
|
||||||
|
DebugDetail = 3
|
||||||
|
DebugFine = 4
|
||||||
|
DebugSuperfine = 5
|
||||||
|
)
|
||||||
|
|
||||||
|
// ActionCounterDebugCard constructs a BasicCard intended for use only as a
|
||||||
|
// Debug Action that tinkers with the player's action counter.
|
||||||
|
func ActionCounterDebugCard[C StatsCollection]() Card[C] {
|
||||||
|
return &BasicCard[C]{
|
||||||
|
CardTitle: MsgStr("Adjust Action Counter"),
|
||||||
|
CardText: MsgStr("Change the number of actions you have available this turn."),
|
||||||
|
CardOptions: []CardOption[C]{
|
||||||
|
&BasicOption[C]{
|
||||||
|
Text: MsgStr("Get an extra action."),
|
||||||
|
Effect: func(p *Player[C]) error {
|
||||||
|
p.ActionsRemaining += 2 // counteract the one this costs
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
Output: MsgStr("Gotten."),
|
||||||
|
},
|
||||||
|
&BasicOption[C]{
|
||||||
|
Text: MsgStr("Waste an action."),
|
||||||
|
Effect: func(p *Player[C]) error {
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
Output: MsgStr("Wasted."),
|
||||||
|
},
|
||||||
|
&BasicOption[C]{
|
||||||
|
Text: MsgStr("Get a thousand actions."),
|
||||||
|
Effect: func(p *Player[C]) error {
|
||||||
|
p.ActionsRemaining = 1000
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
Output: MsgStr("ActionsRemaining set to 1000."),
|
||||||
|
},
|
||||||
|
&BasicOption[C]{
|
||||||
|
Text: MsgStr("Go to exactly 1 action remaining."),
|
||||||
|
Effect: func(p *Player[C]) error {
|
||||||
|
p.ActionsRemaining = 1
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
Output: MsgStr("ActionsRemaining set to 1."),
|
||||||
|
},
|
||||||
|
&BasicOption[C]{
|
||||||
|
Text: MsgStr("End the turn. (Set actions to 0.)"),
|
||||||
|
Effect: func(p *Player[C]) error {
|
||||||
|
p.ActionsRemaining = 0
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
Output: MsgStr("ActionsRemaining zeroed out."),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// DebugModeCard constructs a BasicCard to change the player's debug level.
|
||||||
|
// It is intended for use only as a Debug Action.
|
||||||
|
func DebugModeCard[C StatsCollection]() Card[C] {
|
||||||
|
return &BasicCard[C]{
|
||||||
|
CardTitle: MsgStr("Change Debug Level"),
|
||||||
|
CardText: MsgStr("Adjust verbosity of output, or exit debug mode entirely (not recommended)."),
|
||||||
|
CardOptions: []CardOption[C]{
|
||||||
|
debugLevelOption[C]{1, "Enable debug mode. Show warnings."},
|
||||||
|
debugLevelOption[C]{2, "Enable debug mode. Show info messages."},
|
||||||
|
debugLevelOption[C]{3, "Enable debug mode. Show detailed messages."},
|
||||||
|
debugLevelOption[C]{4, "Enable debug mode. Show individual details of operations."},
|
||||||
|
debugLevelOption[C]{5, "Enable debug mode. Show every event in excruciating detail."},
|
||||||
|
debugLevelOption[C]{0, "NOT RECOMMENDED. Disable debugging (show warnings). IT CAN'T BE TURNED BACK ON."},
|
||||||
|
debugLevelOption[C]{-1, "NOT RECOMMENDED. Disable debugging (hide warnings). IT CAN'T BE TURNED BACK ON."},
|
||||||
|
},
|
||||||
|
AfterOption: RefundAction[C](),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type debugLevelOption[C StatsCollection] struct {
|
||||||
|
level int
|
||||||
|
description string
|
||||||
|
}
|
||||||
|
|
||||||
|
// OptionText implements CardOption[C].
|
||||||
|
func (d debugLevelOption[C]) OptionText(*Player[C]) (Message, error) {
|
||||||
|
return Msgf("Set debug level %d: %s", d.level, d.description), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enact implements CardOption[C].
|
||||||
|
func (d debugLevelOption[C]) Enact(p *Player[C]) (Message, error) {
|
||||||
|
p.DebugLevel = d.level
|
||||||
|
return Msgf("Debug level is now %d.", d.level), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enabled implements CardOption[C].
|
||||||
|
func (d debugLevelOption[C]) Enabled(p *Player[C]) bool {
|
||||||
|
return true
|
||||||
|
}
|
142
cardsim/deck.go
142
cardsim/deck.go
@ -279,3 +279,145 @@ func (d *Deck[C]) Strip(shouldRemove func(idx int, c Card[C]) bool) int {
|
|||||||
d.cards = Strip(d.cards, shouldRemove)
|
d.cards = Strip(d.cards, shouldRemove)
|
||||||
return origLen - d.Len()
|
return origLen - d.Len()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeckDebugger is a Card[C], intended for use only as a debug action, that
|
||||||
|
// lists the top 10 cards of the deck (without checking if they are drawable)
|
||||||
|
// and allows various sorts of deck manipulation for free. It can't be drawn.
|
||||||
|
type DeckDebugger[C StatsCollection] struct{}
|
||||||
|
|
||||||
|
// Title implements Card[C].
|
||||||
|
func (DeckDebugger[C]) Title(p *Player[C]) Message {
|
||||||
|
return MsgStr("Debug Mode Deck Controls")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Urgent implements Card[C] as used in permanent actions. It's always valid
|
||||||
|
// to use the deck debugger. Debug actions do not check urgency flags, but this
|
||||||
|
// marks itself as urgent-compatible just in case.
|
||||||
|
func (DeckDebugger[C]) Urgent(p *Player[C]) bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Drawn implements Card[C]. It can't be drawn.
|
||||||
|
func (DeckDebugger[C]) Drawn(p *Player[C]) bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// EventText implements Card[C]. It lists the top ten cards of the deck and
|
||||||
|
// a few deck-related and hand-related stats.
|
||||||
|
func (DeckDebugger[C]) EventText(p *Player[C]) (Message, error) {
|
||||||
|
var msgs []Message
|
||||||
|
|
||||||
|
msgs = append(msgs, Msgf("The Deck contains %d cards.", p.Deck.Len()))
|
||||||
|
if p.Deck.Len() > 0 {
|
||||||
|
portion := p.Deck.cards
|
||||||
|
msgs = append(msgs, nil)
|
||||||
|
topness := "All"
|
||||||
|
if p.Deck.Len() > 10 {
|
||||||
|
portion = p.Deck.cards[:10]
|
||||||
|
topness = "Top 10"
|
||||||
|
}
|
||||||
|
msgs = append(msgs, Msgf("%s cards in the Deck:", topness))
|
||||||
|
for i, c := range portion {
|
||||||
|
urgency := " "
|
||||||
|
if c.Urgent(p) {
|
||||||
|
urgency = "!"
|
||||||
|
}
|
||||||
|
msgs = append(msgs, Msgf(" %s %2d) %v", urgency, i+1, c.Title(p)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
msgs = append(msgs, nil)
|
||||||
|
msgs = append(msgs, Msgf("At the start of each turn, the Player draws to %d cards. The player has %d cards in hand.", p.HandLimit, len(p.Hand)))
|
||||||
|
return MultiMessage(msgs), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Options implements Card[C]. It offers many possible actions.
|
||||||
|
func (DeckDebugger[C]) Options(p *Player[C]) ([]CardOption[C], error) {
|
||||||
|
ret := []CardOption[C]{
|
||||||
|
&BasicOption[C]{
|
||||||
|
Text: MsgStr("Draw a card."),
|
||||||
|
Effect: func(p *Player[C]) error {
|
||||||
|
return p.Draw()
|
||||||
|
},
|
||||||
|
Output: MsgStr("Done."),
|
||||||
|
},
|
||||||
|
&BasicOption[C]{
|
||||||
|
Text: MsgStr("Shuffle the deck."),
|
||||||
|
Effect: func(p *Player[C]) error {
|
||||||
|
return p.Deck.Shuffle()
|
||||||
|
},
|
||||||
|
Output: MsgStr("Done."),
|
||||||
|
},
|
||||||
|
&BasicOption[C]{
|
||||||
|
Text: MsgStr("Shuffle top half."),
|
||||||
|
Effect: func(p *Player[C]) error {
|
||||||
|
return p.Deck.ShuffleTop(0.5)
|
||||||
|
},
|
||||||
|
Output: MsgStr("Done."),
|
||||||
|
},
|
||||||
|
&BasicOption[C]{
|
||||||
|
Text: MsgStr("Shuffle bottom half."),
|
||||||
|
Effect: func(p *Player[C]) error {
|
||||||
|
return p.Deck.ShuffleBottom(0.5)
|
||||||
|
},
|
||||||
|
Output: MsgStr("Done."),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, n := range []int{1, 3, 5, 10} {
|
||||||
|
if p.Deck.Len() <= n {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
// We don't want the functions we're creating to all share the same "n"
|
||||||
|
// field -- we want to create distinct functions that move distinct
|
||||||
|
// numbers of cards. For more information on what's going on here, see
|
||||||
|
// https://eli.thegreenplace.net/2019/go-internals-capturing-loop-variables-in-closures/
|
||||||
|
//
|
||||||
|
// For the curious, the Go developers don't like the gotcha that
|
||||||
|
// this "shadow variable with itself" workaround patches over either.
|
||||||
|
// Here's the guy who implemented apologizing for it and discussing
|
||||||
|
// changing it: https://github.com/golang/go/discussions/56010
|
||||||
|
//
|
||||||
|
// "Loop variables being per-loop instead of per-iteration is the only
|
||||||
|
// design decision I know of in Go that makes programs incorrect more
|
||||||
|
// often than it makes them correct." -- Russ Cox (rsc)
|
||||||
|
n := n
|
||||||
|
invN := p.Deck.Len() - n
|
||||||
|
ret = append(ret,
|
||||||
|
&BasicOption[C]{
|
||||||
|
Text: Msgf("Move the top %d card(s) to the bottom of the deck, in order.", n),
|
||||||
|
Effect: func(p *Player[C]) error {
|
||||||
|
p.Deck.cards = append(p.Deck.cards, p.Deck.cards[:n]...)
|
||||||
|
p.Deck.cards = p.Deck.cards[n:]
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
Output: MsgStr("Done."),
|
||||||
|
},
|
||||||
|
&BasicOption[C]{
|
||||||
|
Text: Msgf("Move the bottom %d card(s) to the top of the deck, in order.", n),
|
||||||
|
Effect: func(p *Player[C]) error {
|
||||||
|
p.Deck.cards = append(p.Deck.cards, p.Deck.cards[:invN]...)
|
||||||
|
p.Deck.cards = p.Deck.cards[invN:]
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
Output: MsgStr("Done."),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
if n > 1 {
|
||||||
|
ret = append(ret, &BasicOption[C]{
|
||||||
|
Text: Msgf("Shuffle the top %d card(s) of the deck.", n),
|
||||||
|
Effect: func(p *Player[C]) error {
|
||||||
|
return p.Deck.ShufflePart(0, n)
|
||||||
|
},
|
||||||
|
Output: MsgStr("Done."),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then implements Card[C]. It refunds the action point.
|
||||||
|
func (DeckDebugger[C]) Then(p *Player[C], o CardOption[C]) error {
|
||||||
|
p.ActionsRemaining++
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -35,6 +35,19 @@ func Msgf(f string, args ...any) Message {
|
|||||||
return stringMessage(fmt.Sprintf(f, args...))
|
return stringMessage(fmt.Sprintf(f, args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ErrorMessage returns a Message representing an Error.
|
||||||
|
// This is preferred over Msgf for errors, since future versions of the library
|
||||||
|
// may perform special message formatting for errors.
|
||||||
|
func ErrorMessage(e error) Message {
|
||||||
|
if e == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if IsSeriousError(e) {
|
||||||
|
return MultiMessage{MsgStr("SERIOUS ERROR:"), Msgf("%v", e)}
|
||||||
|
}
|
||||||
|
return MultiMessage{MsgStr("Warning:"), Msgf("%v", e)}
|
||||||
|
}
|
||||||
|
|
||||||
// A SpecialMessage is a specific, uniquely identifiable message.
|
// A SpecialMessage is a specific, uniquely identifiable message.
|
||||||
type SpecialMessage struct {
|
type SpecialMessage struct {
|
||||||
msg Message
|
msg Message
|
||||||
|
@ -8,14 +8,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrUncooperativeCards = errors.New("a milion cards refused to join the hand")
|
|
||||||
ErrInvalidCard = errors.New("invalid card specified")
|
ErrInvalidCard = errors.New("invalid card specified")
|
||||||
ErrInvalidChoice = errors.New("invalid choice specified")
|
ErrInvalidChoice = errors.New("invalid choice specified")
|
||||||
ErrNotUrgent = errors.New("action not urgent when urgent card is available")
|
ErrNotUrgent = errors.New("action not urgent when urgent card is available")
|
||||||
ErrNoActions = errors.New("no actions remaining")
|
ErrNoActions = errors.New("no actions remaining")
|
||||||
ErrNotDebugging = errors.New("this is a debug-only feature and you're not in debug mode")
|
ErrNotDebugging = errors.New("this is a debug-only feature and you're not in debug mode")
|
||||||
|
|
||||||
WarningStalemate = errors.New("no actions can be taken")
|
WarningStalemate = &Warning{errors.New("no actions can be taken")}
|
||||||
|
WarningUncoperativeCards = &Warning{errors.New("a milion cards refused to join the hand")}
|
||||||
)
|
)
|
||||||
|
|
||||||
// Player stores all gameplay state for one player at a specific point in time.
|
// Player stores all gameplay state for one player at a specific point in time.
|
||||||
@ -106,7 +106,7 @@ type Player[C StatsCollection] struct {
|
|||||||
PermanentActions []Card[C]
|
PermanentActions []Card[C]
|
||||||
|
|
||||||
// DebugActions are PermanentActions only available when the player is in
|
// DebugActions are PermanentActions only available when the player is in
|
||||||
// debug mode.
|
// debug mode. InitPlayer adds some standard debugging actions by default.
|
||||||
DebugActions []Card[C]
|
DebugActions []Card[C]
|
||||||
|
|
||||||
// InfoPanels lists informational views available to the player. The Prompt
|
// InfoPanels lists informational views available to the player. The Prompt
|
||||||
@ -175,6 +175,12 @@ func InitPlayer[C StatsCollection](stats C) *Player[C] {
|
|||||||
HandLimit: 1,
|
HandLimit: 1,
|
||||||
ActionsPerTurn: 1,
|
ActionsPerTurn: 1,
|
||||||
Rules: NewRuleCollection[C](),
|
Rules: NewRuleCollection[C](),
|
||||||
|
DebugActions: []Card[C]{
|
||||||
|
&DeckDebugger[C]{},
|
||||||
|
&PanelCard[C]{Panel: RuleDumper[C]{}},
|
||||||
|
ActionCounterDebugCard[C](),
|
||||||
|
DebugModeCard[C](),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -266,9 +272,9 @@ func (p *Player[C]) StartNextTurn() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Draw draws a card into the hand, informing the card that it has been drawn.
|
// Draw draws a card into the hand, informing the card that it has been drawn.
|
||||||
// If more than a million cards refuse to enter the hand, this crashes with
|
// If more than a million cards refuse to enter the hand, this gives up and
|
||||||
// ErrUncooperativeCards. If the deck does not have enough cards, this
|
// returns WarningUncooperativeCards. If the deck does not have enough cards,
|
||||||
// returns WarningTooFewCards.
|
// this returns WarningTooFewCards.
|
||||||
func (p *Player[C]) Draw() error {
|
func (p *Player[C]) Draw() error {
|
||||||
for attempts := 0; attempts < 1000000; attempts++ {
|
for attempts := 0; attempts < 1000000; attempts++ {
|
||||||
if p.Deck.Len() == 0 {
|
if p.Deck.Len() == 0 {
|
||||||
@ -280,13 +286,13 @@ func (p *Player[C]) Draw() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ErrUncooperativeCards
|
return WarningUncoperativeCards
|
||||||
}
|
}
|
||||||
|
|
||||||
// FillHand draws up to the hand limit, informing cards that they have been
|
// FillHand draws up to the hand limit, informing cards that they have been
|
||||||
// drawn. If more than a million cards refuse to enter the hand, this crashes
|
// drawn. If more than a million cards refuse to enter the hand, this gives up
|
||||||
// with ErrUncooperativeCards. If the deck does not have enough cards, this
|
// and returns WarningUncooperativeCards. If the deck does not have enough
|
||||||
// returns WarningTooFewCards.
|
// cards, this returns WarningTooFewCards.
|
||||||
func (p *Player[C]) FillHand() error {
|
func (p *Player[C]) FillHand() error {
|
||||||
var lastErr error
|
var lastErr error
|
||||||
for p.Deck.Len() > 0 && len(p.Hand) < p.HandLimit {
|
for p.Deck.Len() > 0 && len(p.Hand) < p.HandLimit {
|
||||||
@ -380,17 +386,14 @@ func (p *Player[C]) EnactCardUnchecked(cardIdx, choiceIdx int) (Message, error)
|
|||||||
|
|
||||||
ret, err := options[choiceIdx].Enact(p)
|
ret, err := options[choiceIdx].Enact(p)
|
||||||
errs.Add(err)
|
errs.Add(err)
|
||||||
if IsSeriousError(err) {
|
|
||||||
p.State = GameCrashed
|
|
||||||
return ret, errs.Emit()
|
|
||||||
}
|
|
||||||
|
|
||||||
err = card.Then(p, options[choiceIdx])
|
err = card.Then(p, options[choiceIdx])
|
||||||
errs.Add(err)
|
errs.Add(err)
|
||||||
|
|
||||||
|
err = errs.Emit()
|
||||||
if IsSeriousError(err) {
|
if IsSeriousError(err) {
|
||||||
p.State = GameCrashed
|
p.State = GameCrashed
|
||||||
}
|
}
|
||||||
return ret, errs.Emit()
|
return ret, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// EnactCard executes a card choice, removes it from the hand, and decrements
|
// EnactCard executes a card choice, removes it from the hand, and decrements
|
||||||
@ -464,17 +467,13 @@ func (p *Player[C]) enactActionUnchecked(actionSource []Card[C], actionIdx, choi
|
|||||||
|
|
||||||
ret, err := chosen.Enact(p)
|
ret, err := chosen.Enact(p)
|
||||||
errs.Add(err)
|
errs.Add(err)
|
||||||
if IsSeriousError(err) {
|
|
||||||
p.State = GameCrashed
|
|
||||||
return ret, errs.Emit()
|
|
||||||
}
|
|
||||||
|
|
||||||
err = card.Then(p, chosen)
|
err = card.Then(p, chosen)
|
||||||
errs.Add(err)
|
errs.Add(err)
|
||||||
if IsSeriousError(err) {
|
retErr := errs.Emit()
|
||||||
|
if IsSeriousError(retErr) {
|
||||||
p.State = GameCrashed
|
p.State = GameCrashed
|
||||||
}
|
}
|
||||||
return ret, errs.Emit()
|
return ret, retErr
|
||||||
}
|
}
|
||||||
|
|
||||||
// EnactPermanentAction executes a permanently-available card and decrements
|
// EnactPermanentAction executes a permanently-available card and decrements
|
||||||
@ -506,15 +505,11 @@ func (p *Player[C]) ReportError(e error) {
|
|||||||
if e == nil || p.DebugLevel < -1 {
|
if e == nil || p.DebugLevel < -1 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if p.DebugLevel < 0 && !IsSeriousError(e) {
|
minLvl := NotDebugging
|
||||||
return
|
|
||||||
}
|
|
||||||
p.ChapterBreak()
|
|
||||||
severity := "[Warning]"
|
|
||||||
if IsSeriousError(e) {
|
if IsSeriousError(e) {
|
||||||
severity = "[ERROR]"
|
minLvl = HideWarnings
|
||||||
}
|
}
|
||||||
p.TemporaryMessages = append(p.TemporaryMessages, Msgf("%s: %v", severity, e))
|
p.Debug(minLvl, ErrorMessage(e))
|
||||||
}
|
}
|
||||||
|
|
||||||
// CanAct returns whether the player has actions theoretically available.
|
// CanAct returns whether the player has actions theoretically available.
|
||||||
@ -528,6 +523,7 @@ func (p *Player[C]) Debug(minLevel int, msg Message) {
|
|||||||
if p.DebugLevel < minLevel || msg == nil {
|
if p.DebugLevel < minLevel || msg == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
p.ChapterBreak()
|
||||||
p.TemporaryMessages = append(p.TemporaryMessages, msg)
|
p.TemporaryMessages = append(p.TemporaryMessages, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,8 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
|
"github.com/kr/pretty"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A Rule implements an operation run on every game turn.
|
// A Rule implements an operation run on every game turn.
|
||||||
@ -402,3 +404,14 @@ func (r *RuleCollection[C]) applyDelayedUpdates() {
|
|||||||
r.RemoveID(id)
|
r.RemoveID(id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RuleDumper is an InfoPanel[C] that dumps all rules in P.
|
||||||
|
type RuleDumper[C StatsCollection] struct{}
|
||||||
|
|
||||||
|
func (RuleDumper[C]) Title(p *Player[C]) Message {
|
||||||
|
return MsgStr("Rule Dumper")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (RuleDumper[C]) Info(p *Player[C]) ([]Message, error) {
|
||||||
|
return []Message{Msgf("%# v", pretty.Formatter(p.Rules))}, nil
|
||||||
|
}
|
||||||
|
@ -145,3 +145,21 @@ func Strip[T any](slice []T, removeWhen func(idx int, t T) bool) []T {
|
|||||||
}
|
}
|
||||||
return slice[:to]
|
return slice[:to]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EnsureCapacity checks if `cap(slice)` is at least req. If so, it returns
|
||||||
|
// slice unchanged. Otherwise, it copies `slice` to a new slice that is at least
|
||||||
|
// capacity `req` (but may be larger) and returns the copy.
|
||||||
|
//
|
||||||
|
// It is reasonably efficient to use EnsureCapacity consecutively without
|
||||||
|
// regard for the final overall capacity that a specific slice will need to be.
|
||||||
|
func EnsureCapacity[T any](slice []T, req int) []T {
|
||||||
|
if cap(slice) >= req {
|
||||||
|
return slice
|
||||||
|
}
|
||||||
|
if req < 2*cap(slice) {
|
||||||
|
req = 2 * cap(slice)
|
||||||
|
}
|
||||||
|
ret := make([]T, len(slice), req)
|
||||||
|
copy(ret, slice)
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
@ -46,6 +46,10 @@ func RunSimpleTerminalUI[C StatsCollection](p *Player[C]) error {
|
|||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if err != nil {
|
||||||
|
display(ErrorMessage(err))
|
||||||
|
display(MsgStr(""))
|
||||||
|
}
|
||||||
display(msg)
|
display(msg)
|
||||||
wait()
|
wait()
|
||||||
}
|
}
|
||||||
@ -149,19 +153,19 @@ func pickNextAction[C StatsCollection](p *Player[C]) (actionType EnactableType,
|
|||||||
wait()
|
wait()
|
||||||
} else if i <= actionsOffset {
|
} else if i <= actionsOffset {
|
||||||
i = i - debugOffset - 1
|
i = i - debugOffset - 1
|
||||||
option, promptErr := promptCard(p, p.DebugActions[i])
|
option, promptErr := promptCard(p, p.DebugActions[i], DebugActionEnactable)
|
||||||
if option >= 0 || IsSeriousError(promptErr) {
|
if option >= 0 || IsSeriousError(promptErr) {
|
||||||
return DebugActionEnactable, i, option, promptErr
|
return DebugActionEnactable, i, option, promptErr
|
||||||
}
|
}
|
||||||
} else if i <= handOffset {
|
} else if i <= handOffset {
|
||||||
i = i - actionsOffset - 1
|
i = i - actionsOffset - 1
|
||||||
option, promptErr := promptCard(p, p.PermanentActions[i])
|
option, promptErr := promptCard(p, p.PermanentActions[i], PermanentActionEnactable)
|
||||||
if option >= 0 || IsSeriousError(promptErr) {
|
if option >= 0 || IsSeriousError(promptErr) {
|
||||||
return PermanentActionEnactable, i, option, promptErr
|
return PermanentActionEnactable, i, option, promptErr
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
i = i - handOffset - 1
|
i = i - handOffset - 1
|
||||||
option, promptErr := promptCard(p, p.Hand[i])
|
option, promptErr := promptCard(p, p.Hand[i], CardEnactable)
|
||||||
if option >= 0 || IsSeriousError(promptErr) {
|
if option >= 0 || IsSeriousError(promptErr) {
|
||||||
return CardEnactable, i, option, nil
|
return CardEnactable, i, option, nil
|
||||||
}
|
}
|
||||||
@ -281,10 +285,10 @@ func displayNumberedTitles[C StatsCollection, T Titled[C]](p *Player[C], cards [
|
|||||||
|
|
||||||
// promptCard asks the player to take an action on a card. Returns the option
|
// promptCard asks the player to take an action on a card. Returns the option
|
||||||
// they chose, or -1 if there was a serious error or they cancelled selection.
|
// they chose, or -1 if there was a serious error or they cancelled selection.
|
||||||
func promptCard[C StatsCollection](p *Player[C], card Card[C]) (optionIdx int, err error) {
|
func promptCard[C StatsCollection](p *Player[C], card Card[C], cardType EnactableType) (optionIdx int, err error) {
|
||||||
// Iterate until the player makes a valid choice.
|
// Iterate until the player makes a valid choice.
|
||||||
for {
|
for {
|
||||||
opts, valid, err := displayCard(p, card, true)
|
opts, valid, err := displayCard(p, card, cardType, true)
|
||||||
if IsSeriousError(err) {
|
if IsSeriousError(err) {
|
||||||
return -1, err
|
return -1, err
|
||||||
}
|
}
|
||||||
@ -322,11 +326,11 @@ func promptCard[C StatsCollection](p *Player[C], card Card[C]) (optionIdx int, e
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func displayCard[C StatsCollection](p *Player[C], card Card[C], canAct bool) ([]CardOption[C], bool, error) {
|
func displayCard[C StatsCollection](p *Player[C], card Card[C], cardType EnactableType, canAct bool) ([]CardOption[C], bool, error) {
|
||||||
cls()
|
cls()
|
||||||
t := card.Title(p).String()
|
t := card.Title(p).String()
|
||||||
urgent := card.Urgent(p)
|
urgent := card.Urgent(p)
|
||||||
if urgent {
|
if urgent && cardType == CardEnactable {
|
||||||
t = "[URGENT!] " + t
|
t = "[URGENT!] " + t
|
||||||
}
|
}
|
||||||
fmt.Println(t)
|
fmt.Println(t)
|
||||||
@ -342,7 +346,7 @@ func displayCard[C StatsCollection](p *Player[C], card Card[C], canAct bool) ([]
|
|||||||
fmt.Println()
|
fmt.Println()
|
||||||
fmt.Println(SectionBreak.String())
|
fmt.Println(SectionBreak.String())
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
if !urgent && p.HasUrgentCards() {
|
if !urgent && cardType != DebugActionEnactable && p.HasUrgentCards() {
|
||||||
fmt.Println("<You have more urgent matters to attend to! You cannot act on this right now.>")
|
fmt.Println("<You have more urgent matters to attend to! You cannot act on this right now.>")
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
canAct = false
|
canAct = false
|
||||||
@ -457,13 +461,13 @@ func actionsMode[C StatsCollection](p *Player[C], canAct bool) (actionType Enact
|
|||||||
} else if v <= dOff {
|
} else if v <= dOff {
|
||||||
v--
|
v--
|
||||||
if canAct {
|
if canAct {
|
||||||
optIdx, err := promptCard(p, p.DebugActions[v])
|
optIdx, err := promptCard(p, p.DebugActions[v], DebugActionEnactable)
|
||||||
errs.Add(err)
|
errs.Add(err)
|
||||||
if optIdx >= 0 || IsSeriousError(err) {
|
if optIdx >= 0 || IsSeriousError(err) {
|
||||||
return DebugActionEnactable, v, optIdx, errs.Emit()
|
return DebugActionEnactable, v, optIdx, errs.Emit()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
_, _, err := displayCard(p, p.DebugActions[v], false)
|
_, _, err := displayCard(p, p.DebugActions[v], DebugActionEnactable, false)
|
||||||
errs.Add(err)
|
errs.Add(err)
|
||||||
if IsSeriousError(err) {
|
if IsSeriousError(err) {
|
||||||
return DebugActionEnactable, -1, -1, errs.Emit()
|
return DebugActionEnactable, -1, -1, errs.Emit()
|
||||||
@ -473,13 +477,13 @@ func actionsMode[C StatsCollection](p *Player[C], canAct bool) (actionType Enact
|
|||||||
} else if v <= pOff {
|
} else if v <= pOff {
|
||||||
v = v - dOff - 1
|
v = v - dOff - 1
|
||||||
if canAct {
|
if canAct {
|
||||||
optIdx, err := promptCard(p, p.PermanentActions[v])
|
optIdx, err := promptCard(p, p.PermanentActions[v], PermanentActionEnactable)
|
||||||
errs.Add(err)
|
errs.Add(err)
|
||||||
if optIdx >= 0 || IsSeriousError(err) {
|
if optIdx >= 0 || IsSeriousError(err) {
|
||||||
return PermanentActionEnactable, v, optIdx, errs.Emit()
|
return PermanentActionEnactable, v, optIdx, errs.Emit()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
_, _, err := displayCard(p, p.PermanentActions[v], false)
|
_, _, err := displayCard(p, p.PermanentActions[v], PermanentActionEnactable, false)
|
||||||
errs.Add(err)
|
errs.Add(err)
|
||||||
if IsSeriousError(err) {
|
if IsSeriousError(err) {
|
||||||
return PermanentActionEnactable, -1, -1, errs.Emit()
|
return PermanentActionEnactable, -1, -1, errs.Emit()
|
||||||
@ -489,13 +493,13 @@ func actionsMode[C StatsCollection](p *Player[C], canAct bool) (actionType Enact
|
|||||||
} else {
|
} else {
|
||||||
v = v - pOff - 1
|
v = v - pOff - 1
|
||||||
if canAct {
|
if canAct {
|
||||||
optIdx, err := promptCard(p, p.Hand[v])
|
optIdx, err := promptCard(p, p.Hand[v], CardEnactable)
|
||||||
errs.Add(err)
|
errs.Add(err)
|
||||||
if optIdx >= 0 || IsSeriousError(err) {
|
if optIdx >= 0 || IsSeriousError(err) {
|
||||||
return CardEnactable, v, optIdx, errs.Emit()
|
return CardEnactable, v, optIdx, errs.Emit()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
_, _, err := displayCard(p, p.Hand[v], false)
|
_, _, err := displayCard(p, p.Hand[v], CardEnactable, false)
|
||||||
errs.Add(err)
|
errs.Add(err)
|
||||||
if IsSeriousError(err) {
|
if IsSeriousError(err) {
|
||||||
return CardEnactable, -1, -1, errs.Emit()
|
return CardEnactable, -1, -1, errs.Emit()
|
||||||
@ -552,21 +556,21 @@ func review[C StatsCollection](p *Player[C]) error {
|
|||||||
displayOnePanel(p, p.InfoPanels[i-1])
|
displayOnePanel(p, p.InfoPanels[i-1])
|
||||||
} else if i <= actionsOffset {
|
} else if i <= actionsOffset {
|
||||||
i = i - debugOffset - 1
|
i = i - debugOffset - 1
|
||||||
_, _, err := displayCard(p, p.DebugActions[i], false)
|
_, _, err := displayCard(p, p.DebugActions[i], DebugActionEnactable, false)
|
||||||
errs.Add(err)
|
errs.Add(err)
|
||||||
if IsSeriousError(err) {
|
if IsSeriousError(err) {
|
||||||
return errs.Emit()
|
return errs.Emit()
|
||||||
}
|
}
|
||||||
} else if i <= handOffset {
|
} else if i <= handOffset {
|
||||||
i = i - actionsOffset - 1
|
i = i - actionsOffset - 1
|
||||||
_, _, err := displayCard(p, p.PermanentActions[i], false)
|
_, _, err := displayCard(p, p.PermanentActions[i], PermanentActionEnactable, false)
|
||||||
errs.Add(err)
|
errs.Add(err)
|
||||||
if IsSeriousError(err) {
|
if IsSeriousError(err) {
|
||||||
return errs.Emit()
|
return errs.Emit()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
i = i - handOffset - 1
|
i = i - handOffset - 1
|
||||||
_, _, err := displayCard(p, p.Hand[i], false)
|
_, _, err := displayCard(p, p.Hand[i], CardEnactable, false)
|
||||||
errs.Add(err)
|
errs.Add(err)
|
||||||
if IsSeriousError(err) {
|
if IsSeriousError(err) {
|
||||||
return errs.Emit()
|
return errs.Emit()
|
||||||
|
@ -142,25 +142,3 @@ func installPermanentActions(pa *[]card) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func installDebugActions(pa *[]card) {
|
|
||||||
*pa = []card{
|
|
||||||
&cardsim.BasicCard[*SmokeTestCollection]{
|
|
||||||
CardTitle: cardsim.MsgStr("Draw a card"),
|
|
||||||
CardText: cardsim.MsgStr("Draw an extra card."),
|
|
||||||
CardOptions: []cardOption{
|
|
||||||
&cardsim.BasicOption[*SmokeTestCollection]{
|
|
||||||
Text: cardsim.MsgStr("Draw an extra card."),
|
|
||||||
Effect: func(p *player) error {
|
|
||||||
return p.Draw()
|
|
||||||
},
|
|
||||||
Output: cardsim.MsgStr("Drawn. Probably."),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
AfterOption: func(c card, p *player, option cardOption) error {
|
|
||||||
p.ActionsRemaining++
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -5,8 +5,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"git.chromaticdragon.app/kistaro/CardSimEngine/cardsim"
|
"git.chromaticdragon.app/kistaro/CardSimEngine/cardsim"
|
||||||
|
|
||||||
"github.com/kr/pretty"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -40,13 +38,11 @@ func main() {
|
|||||||
installRules(p.Rules)
|
installRules(p.Rules)
|
||||||
initDeck(p.Deck)
|
initDeck(p.Deck)
|
||||||
installPermanentActions(&p.PermanentActions)
|
installPermanentActions(&p.PermanentActions)
|
||||||
installDebugActions(&p.DebugActions)
|
|
||||||
p.InfoPanels = []cardsim.InfoPanel[*SmokeTestCollection]{
|
p.InfoPanels = []cardsim.InfoPanel[*SmokeTestCollection]{
|
||||||
&cardsim.BasicStatsPanel[*SmokeTestCollection]{
|
&cardsim.BasicStatsPanel[*SmokeTestCollection]{
|
||||||
Name: cardsim.MsgStr("Stats"),
|
Name: cardsim.MsgStr("Stats"),
|
||||||
Intro: cardsim.MsgStr("Hi! These are the smoke test stats."),
|
Intro: cardsim.MsgStr("Hi! These are the smoke test stats."),
|
||||||
},
|
},
|
||||||
ruledumper{},
|
|
||||||
}
|
}
|
||||||
p.Prompt = prompt{}
|
p.Prompt = prompt{}
|
||||||
p.DebugLevel = 5
|
p.DebugLevel = 5
|
||||||
@ -73,13 +69,3 @@ func (prompt) Info(p *cardsim.Player[*SmokeTestCollection]) ([]cardsim.Message,
|
|||||||
cardsim.Msgf("The current Number is %d. It tastes like %s.", p.Stats.Number.Value, p.Stats.Flavor.Value),
|
cardsim.Msgf("The current Number is %d. It tastes like %s.", p.Stats.Number.Value, p.Stats.Flavor.Value),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type ruledumper struct{}
|
|
||||||
|
|
||||||
func (ruledumper) Title(p *player) cardsim.Message {
|
|
||||||
return cardsim.MsgStr("Rule Dumper")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ruledumper) Info(p *player) ([]cardsim.Message, error) {
|
|
||||||
return []cardsim.Message{cardsim.Msgf("%# v", pretty.Formatter(p.Rules))}, nil
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user