Compare commits

..

No commits in common. "1eedfb50e8d587d5a9abf0ba63848ec46e5e2683" and "8ce6539c8aea42f39da2fb91cf7d32b69b773d49" have entirely different histories.

2 changed files with 43 additions and 296 deletions

View File

@ -23,7 +23,6 @@ var cards = []Card{
p.Stats.GovWarExpense.Value -= 0.02
return nil
},
CanDo: YesWeCan,
},
&BasicPolicy{
UnenactedDesc: cardsim.MsgStr(`Your head miner considers the matter worriedly. "Creches under military control? No. That would invite chaos. We need to dig deeper; we can have a peaceful, orderly society if we just get far enough away from surfacers."`),
@ -40,7 +39,6 @@ var cards = []Card{
p.Stats.GovBureaucracyExpense.Value -= 0.01
return nil
},
CanDo: YesWeCan,
},
&BasicPolicy{
UnenactedDesc: cardsim.MsgStr(`Your nursery director is incensed. "Creches under military control? Never! Let young kobolds play! In fact, cut the military just for suggesting this. The threats facing us are completely overstated."`),
@ -57,10 +55,9 @@ var cards = []Card{
p.Stats.GovWarExpense.Value += 0.02
return nil
},
CanDo: YesWeCan,
},
&VerbosePolicy{
Default: &BasicPolicy{
BasicPolicy: &BasicPolicy{
UnenactedDesc: cardsim.MsgStr("This isn't about a disaster and can probably be safely dismissed."),
Do: func(p *Player) (cardsim.Message, error) {
p.Stats.Kobolds.Value += 20
@ -70,24 +67,23 @@ var cards = []Card{
p.Stats.Kobolds.Value -= 20
return nil
},
CanDo: YesWeCan,
},
Variants: []Policy{
&BasicPolicy{
UnenactedDesc: cardsim.MsgStr("Rejecting this issue will also reject the military's natalist stance."),
Do: OverrideMsg(cardsim.MsgStr("Militant natalism has been reduced by policy.")),
Content: []DescResult{
{
Desc: cardsim.MsgStr("Rejecting this issue will also reject the military's natalist stance."),
Result: cardsim.MsgStr("Militant natalism has been reduced by policy."),
},
&BasicPolicy{
UnenactedDesc: cardsim.MsgStr(`"Dig deeper" pressures in your nation may be excessive.`),
Do: OverrideMsg(cardsim.MsgStr("Some of the lower depths are being abandoned.")),
{
Desc: cardsim.MsgStr(`"Dig deeper" pressures in your nation may be excessive.`),
Result: cardsim.MsgStr("Some of the lower depths are being abandoned."),
},
&BasicPolicy{
UnenactedDesc: cardsim.MsgStr("Near-surface patrols may need to be increased."),
Do: OverrideMsg(cardsim.MsgStr("More and better-armed hunting outposts are being established.")),
{
Desc: cardsim.MsgStr("Near-surface patrols may need to be increased."),
Result: cardsim.MsgStr("More and better-armed hunting outposts are being established."),
},
&BasicPolicy{
EnactedDesc: cardsim.MsgStr("This isn't about a disaster and can probably continue to be safely dismissed."),
Do: OverrideMsg(cardsim.MsgStr("Creche control doesn't shift that easily.")),
{
Desc: cardsim.MsgStr("This isn't about a disaster and can probably continue to be safely dismissed."),
Result: cardsim.MsgStr("Creche control doesn't shift that easily."),
},
},
},
@ -113,7 +109,6 @@ var cards = []Card{
p.Stats.GovBureaucracyExpense.Value -= 0.03
return nil
},
CanDo: YesWeCan,
},
&BasicPolicy{
UnenactedDesc: cardsim.MsgStr(`Your Minister of Education pulls you aside. "This is a good opportunity to head-hunt. Bureaucrats from many nations will be attending. There's got to be some who'll work for less than the ones we've got. There are some terribly threatened or impoverished communities among kobolds.`),
@ -128,8 +123,7 @@ var cards = []Card{
p.Stats.GovBureaucracyExpense.Value += 0.01
return nil
},
CanDo: YesWeCan,
// TODO(rakeela): This option should only be available if the fourth option isn't the status quo and bureaucratic expense is at least 0.02.
// This option should only be available if the fourth option isn't the status quo and bureaucratic expense is at least 0.02.
},
&BasicPolicy{
UnenactedDesc: cardsim.MsgStr(`Your Minister of Education greets you exuberantly. "We've trimmed the wages of the bureaucracy beautifully, but maybe we can scout some outright volunteers. Some nations are absolute disasters, after all.`),
@ -144,8 +138,7 @@ var cards = []Card{
p.Stats.GovBureaucracyExpense.Value += 0.02
return nil
},
CanDo: YesWeCan,
// TODO(rakeela): This option should only be available if the fourth option isn't the status quo and bureaucratic expense is less than 0.02.
// This option should only be available if the fourth option isn't the status quo and bureaucratic expense is less than 0.02.
},
&BasicPolicy{
UnenactedDesc: cardsim.MsgStr(`One of your non-bureaucrat friends has been in a foul temper. "This festival is everything that's wrong with our society. Life isn't about filling out forms. We ought to snub this festival and outright fire some bureaucrats."`),
@ -160,12 +153,10 @@ var cards = []Card{
p.Stats.GovBureaucracyExpense.Value += 0.04
return nil
},
CanDo: YesWeCan,
},
&VerbosePolicy{
Default: &BasicPolicy{
BasicPolicy: &BasicPolicy{
UnenactedDesc: cardsim.MsgStr("Some kobolds will attend the festival on their own, and we may face desertions for our lack of interest."),
EnactedDesc: cardsim.MsgStr("Some kobolds will attend the festival on their own. No change is expected."),
Do: func(p *Player) (cardsim.Message, error) {
p.Stats.Kobolds.Value -= 20
return cardsim.MsgStr("A festival of bureaucracy lured away a few kobolds to other nations."), nil
@ -174,27 +165,6 @@ var cards = []Card{
p.Stats.Kobolds.Value += 20
return nil
},
CanDo: YesWeCan,
},
Variants: []Policy{
nil,
&DisabledPolicy{cardsim.MsgStr("TODO(rakeela): write")},
&DisabledPolicy{cardsim.MsgStr("TODO(rakeela): write")},
&FuncPolicy{
OptionTextFunc: func(p *Player) (cardsim.Message, error) {
if p.Stats.GovBureaucracyExpense.Value >= -0.03 {
return cardsim.MsgStr("Permitting the festival will yield some immigration, but we'll be expected to rebuild our bureaucracy."), nil
}
return cardsim.MsgStr("Permitting the festival will yield some immigration."), nil
},
EnactFunc: func(p *Player) (cardsim.Message, error) {
if p.Stats.GovBureaucracyExpense.Value >= -0.03 {
return cardsim.MsgStr("A festival of bureaucracy just saw the nation's bureaucracy rebuilt."), ErrKeepMessage
}
return cardsim.MsgStr("A festival of bureaucracy brought in immigrants wondering at the nation's lack thereof."), ErrKeepMessage
},
},
nil, // remember, if using a BasicPolicy, this uses EnactedDesc
},
},
},

View File

@ -9,14 +9,6 @@ import (
var (
ErrOptionNotEnabled = errors.New("option not enabled")
ErrPolicyNotEnacted = errors.New("cannot unenact policy that is not enacted")
// ErrUnimplemented and ErrKeepMessaage are "non-errors". They are used
// as special signals that the result needs to be handled in a special way;
// VerbosePolicy uses these to decide when to use the Default instead.
// If these are returned in a context that does not know how to respond
// to them, then they're just errors.
ErrUnimplemented = errors.New("unimplemented policy element")
ErrKeepMessage = errors.New("use the default behavior but this message")
)
type Policy interface {
@ -108,7 +100,6 @@ func (s *SwitchingCard) Then(p *Player, o CardOption) error {
// BasicPolicy is a straightfoward implementation of Policy. If the currently
// enacted option is re-enacted, it refunds the player's action point.
type BasicPolicy struct {
Desc cardsim.Message
UnenactedDesc cardsim.Message
EnactedDesc cardsim.Message
NothingChanged cardsim.Message
@ -133,22 +124,13 @@ func (b *BasicPolicy) LastEnacted(_ int, p Policy) {
// OptionText implements CardOption.
func (b *BasicPolicy) OptionText(*Player) (cardsim.Message, error) {
if b.currentlyEnacted {
if b.EnactedDesc == nil {
return nil, ErrUnimplemented
}
return b.EnactedDesc, nil
}
if b.UnenactedDesc == nil {
return nil, ErrUnimplemented
}
return b.UnenactedDesc, nil
}
// Enact implements CardOption.
func (b *BasicPolicy) Enact(p *Player) (cardsim.Message, error) {
if b.Do == nil {
return nil, ErrUnimplemented
}
if b.currentlyEnacted {
p.ActionsRemaining++
if b.NothingChanged == nil {
@ -156,10 +138,10 @@ func (b *BasicPolicy) Enact(p *Player) (cardsim.Message, error) {
}
return b.NothingChanged, nil
}
if b.Enabled(p) {
return b.Do(p)
}
if b.CanDo != nil && !b.CanDo(p) {
return nil, ErrOptionNotEnabled
}
return b.Do(p)
}
// Unenact implements Policy.
@ -167,9 +149,6 @@ func (b *BasicPolicy) Unenact(p *Player) error {
if !b.currentlyEnacted {
return ErrPolicyNotEnacted
}
if b.Undo == nil {
return ErrUnimplemented
}
return b.Undo(p)
}
@ -179,7 +158,7 @@ func (b *BasicPolicy) Enabled(p *Player) bool {
return true
}
if b.CanDo == nil {
panic(ErrUnimplemented)
b.CanDo = YesWeCan
}
return b.CanDo(p)
}
@ -191,254 +170,52 @@ func (b *BasicPolicy) Is(p Policy) bool {
return false
}
// A VerbosePolicy is a group of related policies pretending to all be the same
// policy. Which policy is used is determined by what the previous policy for
// the card was (as reported via a call to LastEnacted):
//
// * If no policy has yet been enacted, use FirstTime.
// * If a policy has been enacted, use the Policy at the slot in Variants
// that corresponds to the slot (on the Card) of the currently-enacted policy.
// * If the policy retrieved in this way returns ErrUnimplemented, throw away
// its response and use Default instead. For Enabled, which does not have
// an error component to its return value, look for ErrUnimplemented as the
// argument to a Panic call, instead.
// * If the policy retrieved in this way returns ErrKeepMessage when Enact
// is called, it calls Default for the side effects but ignores its message,
// retaining the message from the original call. This is to avoid having to
// repeat the same Enact function except with different text each time.
// OptionText does this too, even though OptionText doesn't have side effects,
// so the same helper function can create a "constant message" callback
// that works the same for both fields so someone implementing a card won't
// accidentally fail to enact their policy's effects by using the wrong one
// in the wrong slot.
// A DescResuilt is descriptive text for an option and the text result of
// enacting that option when it was described this way.
type DescResult struct {
Desc cardsim.Message
Result cardsim.Message
}
// A VerbosePolicy is an extension to a BasicPolicy. It emits the BasicPolicy's
// `UnenactedDesc` and the message returned from Do only when no policy has ever
// been enacted for this card; otherwise, it looks up the description and result
// from a slice using the index of the last policy selected.
type VerbosePolicy struct {
Default Policy
FirstTime Policy
*BasicPolicy
lastIdx int
lastWasMe bool
lastEnacted Policy
Variants []Policy
Content []DescResult
}
func (v *VerbosePolicy) LastEnacted(i int, p Policy) {
v.lastIdx = i
v.lastEnacted = p
v.lastWasMe = v.Is(p)
// make sure we can just assume that there is a policy in this slot,
// inserting the default if there is none.
v.fillDefaults()
// Tell the potential candidate policy about this, too. Two special cases:
// * first time -- use first-time policy
// * lastWasMe -- tell the polcy that the last encated policy was itself,
// since it doesn't know it's wrapped in another policy and would not
// recognize itself as v
if i < 0 {
v.FirstTime.LastEnacted(i, p) // p should be nil here...
} else if v.lastWasMe {
v.Variants[i].LastEnacted(i, v.Variants[i])
} else {
v.Variants[i].LastEnacted(i, p)
}
// In case we need it, also prepare the Default for use.
if v.lastWasMe {
v.Default.LastEnacted(i, v.Default)
} else {
v.Default.LastEnacted(i, p)
}
v.BasicPolicy.currentlyEnacted = v.Is(p)
}
func (v *VerbosePolicy) fillDefaults() {
if v.FirstTime == nil {
v.FirstTime = v.Default
}
for len(v.Variants) <= v.lastIdx {
v.Variants = append(v.Variants, v.Default)
}
if v.lastIdx > 0 && v.Variants[v.lastIdx] == nil {
v.Variants[v.lastIdx] = v.Default
}
}
func (v *VerbosePolicy) OptionText(p *Player) (cardsim.Message, error) {
var msg cardsim.Message
var err error
func (v *VerbosePolicy) OptionText(*Player) (cardsim.Message, error) {
if v.lastIdx < 0 {
msg, err = v.FirstTime.OptionText(p)
} else {
msg, err = v.Variants[v.lastIdx].OptionText(p)
return v.BasicPolicy.UnenactedDesc, nil
}
if errors.Is(err, ErrKeepMessage) {
_, err = v.Default.OptionText(p)
} else if errors.Is(err, ErrUnimplemented) {
msg, err = v.Default.OptionText(p)
}
return msg, err
return v.Content[v.lastIdx].Desc, nil
}
func (v *VerbosePolicy) Enact(p *Player) (cardsim.Message, error) {
var msg cardsim.Message
var err error
if v.lastIdx < 0 {
msg, err = v.FirstTime.Enact(p)
} else {
msg, err = v.Variants[v.lastIdx].Enact(p)
}
if errors.Is(err, ErrKeepMessage) {
_, err = v.Default.Enact(p)
} else if errors.Is(err, ErrUnimplemented) {
msg, err = v.Default.Enact(p)
msg, err := v.BasicPolicy.Enact(p)
if v.lastIdx >= 0 {
msg = v.Content[v.lastIdx].Result
}
return msg, err
}
func (v *VerbosePolicy) Unenact(p *Player) error {
if !v.lastWasMe {
return ErrPolicyNotEnacted
}
var err error
if v.lastIdx < 0 {
err = v.FirstTime.Unenact(p)
} else {
err = v.Variants[v.lastIdx].Unenact(p)
}
if errors.Is(err, ErrUnimplemented) {
err = v.Default.Unenact(p)
}
return err
}
func (v *VerbosePolicy) Is(p Policy) bool {
if o, ok := p.(*VerbosePolicy); ok {
return o == v
return o == p
}
return false
}
func (v *VerbosePolicy) Enabled(p *Player) (result bool) {
// oops, enablement isn't designed to error out. so we have to use
// panic/recover for this.
defer func() {
if x := recover(); x != nil {
if e, ok := x.(error); ok {
if errors.Is(e, ErrUnimplemented) {
// Recover and use the Default to cover for the missing
// Enabled method.
result = v.Default.Enabled(p)
return
}
}
// Whatever we caught, it's not something we're actually ready to recover from.
panic(x)
}
}()
if v.lastIdx < 0 {
result = v.FirstTime.Enabled(p)
return
}
result = v.Variants[v.lastIdx].Enabled(p)
return
}
// FuncPolicy implements Policy by calling specified functions. If they're
// missing, it returns ErrUnimplemented. It handles Is itself. It also tracks
// LastEnacted data (last index, which policy, is policy self) itself.
type FuncPolicy struct {
OptionTextFunc func(*Player) (cardsim.Message, error)
EnactFunc func(*Player) (cardsim.Message, error)
EnabledFunc func(*Player) bool
UnenactFunc func(*Player) error
// These three fields are assigned by LastEnacted and typically should not
// be set in the initializer.
LastEnactedIdx int
LastEnactedPolicy Policy
WasEnactedLast bool
}
func (f *FuncPolicy) OptionText(p *Player) (cardsim.Message, error) {
if f.OptionTextFunc == nil {
return nil, ErrUnimplemented
}
return f.OptionTextFunc(p)
}
func (f *FuncPolicy) Enact(p *Player) (cardsim.Message, error) {
if f.EnactFunc == nil {
return nil, ErrUnimplemented
}
return f.EnactFunc(p)
}
func (f *FuncPolicy) Enabled(p *Player) bool {
if f.EnabledFunc == nil {
panic(ErrUnimplemented)
}
return f.EnabledFunc(p)
}
func (f *FuncPolicy) Unenact(p *Player) error {
if f.UnenactFunc == nil {
return ErrUnimplemented
}
return f.UnenactFunc(p)
}
func (f *FuncPolicy) LastEnacted(i int, p Policy) {
f.LastEnactedIdx = i
f.LastEnactedPolicy = p
f.WasEnactedLast = f.Is(p)
}
func (f *FuncPolicy) Is(p Policy) bool {
fp, ok := p.(*FuncPolicy)
return ok && (f == fp)
}
// A DisabledPolicy is never enabled.
type DisabledPolicy struct {
Msg cardsim.Message
}
func (d *DisabledPolicy) OptionText(p *Player) (cardsim.Message, error) {
return d.Msg, nil
}
func (d *DisabledPolicy) Enact(*Player) (cardsim.Message, error) {
return nil, ErrOptionNotEnabled
}
func (d *DisabledPolicy) Enabled(*Player) bool {
return false
}
func (d *DisabledPolicy) Unenact(*Player) error {
return ErrPolicyNotEnacted
}
func (d *DisabledPolicy) LastEnacted(int, Policy) {}
func (d *DisabledPolicy) Is(p Policy) bool {
dp, ok := p.(*DisabledPolicy)
return ok && (dp == d)
}
// ShuffleIntoBottomHalf is a common "what to do with the card after?" behavior.
func ShuffleIntoBottomHalf(c Card, p *Player, _ CardOption) error {
p.Deck.InsertRandomBottom(0.5, c)
return nil
}
// OverrideDefaultMsg returns a closure that returns the provided message and
// ErrKeepMessage. This can be used in a FuncPolicy to tell a VerbosePolicy
// to use its Default but keep the message written here, to avoid writing
// repetitive Enact funcs (and, for that matter, OptionText funcs, even though
// the underlying Default call should be unnecessary).
func OverrideMsg(m cardsim.Message) func(*Player) (cardsim.Message, error) {
return func(p *Player) (cardsim.Message, error) {
return m, ErrKeepMessage
}
}