2023-04-03 06:24:47 +00:00
package koboldsim
import (
2024-09-29 19:19:07 +00:00
"errors"
2024-09-29 17:27:15 +00:00
"fmt"
2024-09-29 19:19:07 +00:00
"reflect"
2024-09-29 17:27:15 +00:00
2023-04-03 06:24:47 +00:00
"git.chromaticdragon.app/kistaro/CardSimEngine/cardsim"
)
2024-09-27 23:04:17 +00:00
// KoboldMine is the state of a kobold mine.
2023-04-03 06:24:47 +00:00
type KoboldMine struct {
2023-04-05 03:23:32 +00:00
BasePopulation float64 ` cardsim:"stathidden" `
2024-09-29 16:29:50 +00:00
DragonCount float64 ` cardsim:"stat" cardsim_name:"Dragon Population" `
2023-04-03 06:24:47 +00:00
2024-09-28 04:56:13 +00:00
Mining float64 ` cardsim:"stathidden" cardsim_name:"Mining" `
Scavenging float64 ` cardsim:"stathidden" cardsim_name:"Scavenging" `
Alchemy float64 ` cardsim:"stathidden" cardsim_name:"Alchemy" `
Hospitality float64 ` cardsim:"stathidden" cardsim_name:"Hospitality" `
Agriculture float64 ` cardsim:"stathidden" cardsim_name:"Agriculture" `
Manufacturing float64 ` cardsim:"stathidden" cardsim_name:"Manufacturing" `
PlanarConnections float64 ` cardsim:"stathidden" cardsim_name:"Planar Connections" `
Publishing float64 ` cardsim:"stathidden" cardsim_name:"Publishing" `
Forestry float64 ` cardsim:"stathidden" cardsim_name:"Forestry" `
Finance float64 ` cardsim:"stathidden" cardsim_name:"Finance" `
Gadgetry float64 ` cardsim:"stathidden" cardsim_name:"Gadgetry" `
Fishing float64 ` cardsim:"stathidden" cardsim_name:"Fishing" `
Construction float64 ` cardsim:"stathidden" cardsim_name:"Construction" `
Propaganda float64 ` cardsim:"stathidden" cardsim_name:"Propaganda" `
Bureaucracy float64 ` cardsim:"stathidden" cardsim_name:"Bureaucracy" `
Militarism float64 ` cardsim:"stathidden" cardsim_name:"Militarism" `
Welfare float64 ` cardsim:"stathidden" cardsim_name:"Social Safety Net" `
Logistics float64 ` cardsim:"stathidden" cardsim_name:"Logistics" `
DragonSubs float64 ` cardsim:"stathidden" cardsim_name:"Dragon Subsidies" `
ResearchSubs float64 ` cardsim:"stathidden" cardsim_name:"Research" `
Education float64 ` cardsim:"stathidden" cardsim_name:"Education" `
Healthcare float64 ` cardsim:"stathidden" cardsim_name:"Healthcare" `
ForeignRelExpense float64 ` cardsim:"stathidden" cardsim_name:"Diplomatic Investment" `
ParksExpense float64 ` cardsim:"stathidden" cardsim_name:"City Beautification" `
Faith float64 ` cardsim:"stathidden" cardsim_name:"Faith" `
FoodSupply float64 ` cardsim:"stathidden" `
ForeignRelations float64 ` cardsim:"stathidden" `
HiddenRelPenalty float64 ` cardsim:"stathidden" ` // Lower is better.
Secrecy float64 ` cardsim:"stathidden" `
Rebellion float64 ` cardsim:"stathidden" `
Madness float64 ` cardsim:"stathidden" `
Cruelty float64 ` cardsim:"stat" `
2024-09-29 16:29:50 +00:00
Greed float64 ` cardsim:"stathidden" `
2024-09-28 04:56:13 +00:00
Gullibility float64 ` cardsim:"stathidden" `
Authoritarianism float64 ` cardsim:"stat" `
Taxation float64 ` cardsim:"stat" `
TaxEvasion float64 ` cardsim:"stat" `
Squalor float64 ` cardsim:"stat" `
// Idea for tax evasion. The corruption stat should increase tax evasion, while the squalor stat should reduce it. The link with corruption should be obvious, but for squalor: when the economy improves, more people have resources with which to attempt to protect their income. This is part of why rich people can evade taxes more effectively than poor people. It's not ALL lobbying advantage.
2023-05-23 18:30:09 +00:00
2023-04-03 06:24:47 +00:00
}
2023-04-05 03:23:32 +00:00
func ( k * KoboldMine ) Kobolds ( ) int64 {
2024-09-29 16:29:50 +00:00
return int64 ( ( k . BasePopulation * ( k . Healthcare + 1 ) ) * ( k . FoodSupply / 10 ) * ( 1 + k . TrueForeignRelations ( ) / 100 ) )
2023-04-05 03:23:32 +00:00
}
2024-09-28 04:56:13 +00:00
const (
2024-09-29 16:29:50 +00:00
MinFoodSupply = 18
MaxFoodSupply = 37
MinForeignRelations = - 11
MaxForeignRelations = 9
2024-09-28 04:56:13 +00:00
)
2023-04-05 03:23:32 +00:00
2024-09-28 04:56:13 +00:00
func ( k * KoboldMine ) DisplayedFoodSupply ( ) float64 {
return 100 * ( k . FoodSupply - MinFoodSupply ) / ( MaxFoodSupply - MinFoodSupply ) //This returns a Policy Implementation Percentage, so that the player can see how good they are at capturing food supply. When it becomes possible for Food Supply to hit 0 and end the game in famine, the minimum survivable food supply PIP should be calculated and returned to the player as a warned-against failure threshold.
2023-04-19 18:37:40 +00:00
}
2024-09-28 04:56:13 +00:00
//func (k *KoboldMine) StatObesity() float64 {
//return (100 / (2.3 + math.Exp(-0.04*(k.DisplayedFoodSupply()-37)))) + 100/(2.3+math.Exp(-0.04*(k.StatObesogenicity()-37)))
//}
2023-04-06 03:26:57 +00:00
func ( k * KoboldMine ) TrueForeignRelations ( ) float64 {
2024-09-28 04:56:13 +00:00
openness := 100 - clamp ( k . Secrecy , 0 , 100 )
effectiveRel := clamp ( k . ForeignRelations , - 100 , 100 ) - k . HiddenRelPenalty
return openness / 100 * effectiveRel
2023-04-06 03:26:57 +00:00
}
func ( k * KoboldMine ) DisplayedForeignRelations ( ) float64 {
2024-09-28 04:56:13 +00:00
return 100 * ( k . ForeignRelations - MinForeignRelations ) / ( MaxForeignRelations - MinForeignRelations )
2023-04-06 03:26:57 +00:00
}
func ( k * KoboldMine ) DisplayedSecrecy ( ) float64 {
2024-09-29 16:29:50 +00:00
return k . Secrecy
2023-04-05 19:29:23 +00:00
}
2023-05-14 03:05:07 +00:00
func ( k * KoboldMine ) StatChaos ( ) float64 {
2023-05-14 03:15:57 +00:00
return Mean ( k . Rebellion , k . Madness , k . Cruelty )
2023-05-14 03:05:07 +00:00
}
2023-05-23 18:30:09 +00:00
// This is the "crimes of chaos" stat for my crime calculations. It will also be displayed to the player as just Chaos. The player can see Cruelty, but not Madness or Rebellion. I'm concerned some players may think, "Oh, chaos is a good thing, I want more of that as long as it's not cruel!" In that case, they'll have mad, rebellious colonies that commit crimes against those perceived to be cruel...
func ( k * KoboldMine ) StatCorruption ( ) float64 {
return Mean ( k . Greed , k . Gullibility , k . Authoritarianism )
}
2024-09-29 16:29:50 +00:00
// This is the "crimes of cunning" stat for my crime calculations. It will also be displayed to the player as just Corruption. The player can see Authoritarianism, but they'll have to infer the importance of Greed and Gullibility.
2023-05-23 18:30:09 +00:00
2023-04-03 06:24:47 +00:00
func ( k * KoboldMine ) Stats ( ) [ ] cardsim . Stat {
stats := cardsim . ExtractStats ( k )
funcs := [ ] cardsim . Stat {
2023-04-06 03:26:57 +00:00
cardsim . StatFunc (
"Foreign Relations" ,
k . DisplayedForeignRelations ,
) ,
cardsim . StatFunc (
"Secrecy" ,
k . DisplayedSecrecy ,
) ,
2023-04-05 03:23:32 +00:00
cardsim . StatFunc (
"Food Supply" ,
k . DisplayedFoodSupply ,
) ,
cardsim . StatFunc (
"Kobolds" ,
k . Kobolds ,
) ,
2023-04-03 06:24:47 +00:00
}
stats = append ( stats , funcs ... )
2023-04-04 17:33:14 +00:00
// cardsim.SortStats(stats)
2023-04-03 06:24:47 +00:00
return stats
}
func NewKoboldMine ( ) * KoboldMine {
return & KoboldMine {
2024-09-28 04:56:13 +00:00
BasePopulation : 1025 ,
Mining : 0 ,
Scavenging : 0 ,
Alchemy : 0 ,
Hospitality : 0 ,
Agriculture : 0 ,
Manufacturing : 0 ,
PlanarConnections : 0 ,
Publishing : 0 ,
Forestry : 0 ,
Finance : 0 ,
Gadgetry : 0 ,
Fishing : 0 ,
Construction : 0 ,
Propaganda : 0 ,
Bureaucracy : 0 ,
Militarism : 0 ,
Welfare : 0 ,
Logistics : 0 ,
DragonSubs : 0 ,
ResearchSubs : 0 ,
Education : 0 ,
Healthcare : 0 ,
ForeignRelExpense : 0 ,
ParksExpense : 0 ,
Faith : 0 ,
FoodSupply : 20 ,
ForeignRelations : 0 ,
HiddenRelPenalty : 0 ,
Rebellion : 0 ,
Madness : 0 ,
Cruelty : 0 ,
Greed : 0 ,
Gullibility : 0 ,
Authoritarianism : 0 ,
Secrecy : 95 ,
2023-04-03 06:24:47 +00:00
}
}
2024-09-29 17:19:19 +00:00
2024-09-29 19:19:07 +00:00
// FieldLabel instances are strings exactly matching the name of an
// exported field in `KoboldMine`. These are used to map field names to
// amounts to change in `TablePolicy` instances, which are then looked
// up by name (via reflection) when adding the stat.
2024-09-29 17:19:19 +00:00
type FieldLabel string
const (
2024-09-29 18:14:31 +00:00
Authoritarianism FieldLabel = "Authoritarianism"
2024-09-29 17:27:15 +00:00
BasePopulation FieldLabel = "BasePopulation"
2024-09-29 18:14:31 +00:00
Bureaucracy FieldLabel = "Bureaucracy"
Construction FieldLabel = "Construction"
Cruelty FieldLabel = "Cruelty"
Education FieldLabel = "Education"
2024-09-29 17:27:15 +00:00
FoodSupply FieldLabel = "FoodSupply"
2024-09-29 17:19:19 +00:00
ForeignRelations FieldLabel = "ForeignRelations"
2024-09-29 18:14:31 +00:00
Gullibility FieldLabel = "Gullibility"
2024-09-29 17:27:15 +00:00
Madness FieldLabel = "Madness"
2024-09-29 18:14:31 +00:00
Manufacturing FieldLabel = "Manufacturing"
Mining FieldLabel = "Mining"
Militarism FieldLabel = "Militarism"
Rebellion FieldLabel = "Rebellion"
Scavenging FieldLabel = "Scavenging"
Secrecy FieldLabel = "Secrecy"
2024-09-29 17:19:19 +00:00
)
2024-09-29 19:19:07 +00:00
// ErrBadFieldLabel is an "error category" for all errors where a
// FieldLabel did not correctly name a Field that could be used in Add.
var ErrBadFieldLabel = errors . New ( "bad field label" )
// ErrNoFieldLabel is a kind of `ErrBadFieldLabel` used when no field
// of the target has the exact name specified in the FieldLabel. Check
// spelling and capitalization.
var ErrNoFieldLabel = fmt . Errorf ( "%w: field does not exist" , ErrBadFieldLabel )
// ErrFieldNotFloat is a kind of `ErrBadFieldLabel` used when it is not
// possible to read and assign a float to the named field. Is this the
// name of a calculated stat (a function) rather than a base stat?
var ErrFieldNotFloat = fmt . Errorf ( "%w: field type is not float" , ErrBadFieldLabel )
// ErrFieldSetPanic is a kind of `ErrBadFieldLabel` used when trying to
// set the value of a field panicked. The panic message should be
// preserved in the error to diagnose the problem. If the problem is
// that the field is unexported, capitalize the name (including inside
// the KoboldMine type). Any other issue is a more complicated bug,
// because all of them that aren't already caught by ErrFieldNotFloat
// imply something very unexpected happened with `k` itself (in `Add`).
var ErrFieldSetPanic = fmt . Errorf ( "%w: panic when setting" , ErrBadFieldLabel )
// Use a FieldLabel to add an amount to the matching field. If no such
// field can be found, the field is not exported, or the field is not
// of type float64, this returns an error ad does not change any values.
2024-09-29 19:41:49 +00:00
func ( k * KoboldMine ) Add ( which FieldLabel , amount float64 ) error {
2024-09-29 19:19:07 +00:00
kv := reflect . ValueOf ( k ) . Elem ( )
f := kv . FieldByName ( string ( which ) )
if ! f . IsValid ( ) {
return fmt . Errorf ( "cannot add %f to field %q: %w" , amount , which , ErrNoFieldLabel )
2024-09-29 17:19:19 +00:00
}
2024-09-29 19:19:07 +00:00
if ! f . CanFloat ( ) {
return fmt . Errorf ( "cannot add %f to field %q: %w" , amount , which , ErrFieldNotFloat )
}
2024-09-29 19:41:49 +00:00
if err := try ( func ( ) { f . SetFloat ( f . Float ( ) + amount ) } ) ; err != nil {
return fmt . Errorf ( "could not add %f to field %q: %w" , amount , which , err )
}
2024-09-29 19:19:07 +00:00
return nil
2024-09-29 17:19:19 +00:00
}