2022-05-17 23:29:59 -04:00
|
|
|
package simulator
|
|
|
|
|
2022-05-19 15:18:03 -04:00
|
|
|
import (
|
|
|
|
"fmt"
|
2022-05-25 16:45:23 -04:00
|
|
|
"sort"
|
2022-05-24 18:26:34 -04:00
|
|
|
"strconv"
|
2022-05-19 15:18:03 -04:00
|
|
|
)
|
2022-05-17 23:29:59 -04:00
|
|
|
|
|
|
|
//Player is a player controlled mob
|
|
|
|
type Player struct {
|
2022-05-19 17:09:46 -04:00
|
|
|
Resources map[itemType]int
|
2022-05-24 21:12:35 -04:00
|
|
|
Craftables map[itemType]struct{}
|
2022-05-25 16:45:23 -04:00
|
|
|
Techs map[TechID]Tech
|
2022-05-19 15:18:03 -04:00
|
|
|
CurrentTile *Tile
|
2022-05-24 18:26:34 -04:00
|
|
|
log []string
|
|
|
|
logIndex int
|
2022-05-17 23:29:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
//NewPlayer initializes a player
|
|
|
|
func NewPlayer() *Player {
|
2022-05-25 16:45:23 -04:00
|
|
|
return &Player{Resources: make(map[itemType]int), Techs: make(map[TechID]Tech), Craftables: make(map[itemType]struct{})}
|
2022-05-17 23:29:59 -04:00
|
|
|
}
|
|
|
|
|
2022-05-25 17:42:05 -04:00
|
|
|
//AddItemByName adds the given amount of the item using the item name
|
|
|
|
func (p *Player) AddItemByName(name string, value int) {
|
|
|
|
obj := lookupByName(name)
|
|
|
|
if obj.Type() != emptyObject {
|
|
|
|
p.AddItem(obj.ID(), value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//AddItem adds the given amount of the item
|
|
|
|
func (p *Player) AddItem(i itemType, value int) {
|
|
|
|
if v, ok := p.Resources[i]; ok {
|
|
|
|
p.Resources[i] = v + value
|
|
|
|
} else {
|
|
|
|
p.Resources[i] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//DelItem removes the given ammount of the item
|
|
|
|
func (p *Player) DelItem(i itemType, value int) {
|
|
|
|
if v, ok := p.Resources[i]; ok {
|
|
|
|
p.Resources[i] = v - value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//DelItemByName removes the given ammount of the item using the item name
|
|
|
|
func (p *Player) DelItemByName(name string, value int) {
|
|
|
|
obj := lookupByName(name)
|
|
|
|
if obj.Type() != emptyObject {
|
|
|
|
p.DelItem(obj.ID(), value)
|
|
|
|
}
|
|
|
|
}
|
2022-05-17 23:29:59 -04:00
|
|
|
func (p *Player) String() string {
|
|
|
|
var res string
|
|
|
|
res += "Resources: \n"
|
2022-05-25 16:45:23 -04:00
|
|
|
var ress []int
|
|
|
|
for k, v := range p.Resources {
|
|
|
|
if v != 0 {
|
|
|
|
//res += fmt.Sprintf("%v: %v\n", GlobalItems[k].Describe(), v)
|
|
|
|
ress = append(ress, int(k))
|
2022-05-19 17:09:46 -04:00
|
|
|
}
|
2022-05-17 23:29:59 -04:00
|
|
|
}
|
2022-05-25 16:45:23 -04:00
|
|
|
sort.Ints(ress)
|
|
|
|
for _, k := range ress {
|
|
|
|
id := itemType(k)
|
2022-05-25 18:07:07 -04:00
|
|
|
res += fmt.Sprintf("%v: %v\n", GlobalItems[id].Describe(), p.Resources[id])
|
2022-05-25 16:45:23 -04:00
|
|
|
}
|
2022-05-19 15:18:03 -04:00
|
|
|
res += "\nLocation: \n"
|
|
|
|
if p.CurrentTile != nil {
|
|
|
|
res += p.CurrentTile.String()
|
|
|
|
}
|
2022-05-17 23:29:59 -04:00
|
|
|
return res
|
|
|
|
}
|
2022-05-24 18:26:34 -04:00
|
|
|
|
|
|
|
func (p *Player) research() {
|
2022-05-25 16:45:23 -04:00
|
|
|
for _, tech := range GlobalTechs {
|
|
|
|
if _, ok := p.Techs[tech.ID]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for _, v := range tech.Requires {
|
|
|
|
req := lookupByName(v.Name)
|
|
|
|
if p.Resources[req.ID()] >= v.Value {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if i == len(tech.Requires) {
|
|
|
|
p.Techs[tech.ID] = tech
|
|
|
|
for _, v := range tech.Unlocks {
|
|
|
|
itm := lookupByName(v)
|
|
|
|
if itm.Type() != emptyObject {
|
|
|
|
p.Craftables[itm.ID()] = struct{}{}
|
|
|
|
}
|
2022-05-24 18:26:34 -04:00
|
|
|
}
|
2022-05-25 16:45:23 -04:00
|
|
|
p.Announce(fmt.Sprintf("New Tech: %v", tech.DisplayName))
|
2022-05-24 18:26:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Announce adds an entry to a players log
|
|
|
|
func (p *Player) Announce(msg string) {
|
|
|
|
p.logIndex++
|
|
|
|
p.log = append(p.log, strconv.Itoa(p.logIndex)+" "+msg)
|
2022-05-25 17:52:42 -04:00
|
|
|
if len(p.log) > 4 {
|
2022-05-24 18:26:34 -04:00
|
|
|
p.log = p.log[1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Log returns the player log
|
|
|
|
func (p *Player) Log() string {
|
|
|
|
res := "Log:\n"
|
2022-05-25 17:52:42 -04:00
|
|
|
for i, v := range p.log {
|
|
|
|
res += v
|
|
|
|
if i < 3 {
|
|
|
|
res += "\n"
|
|
|
|
}
|
2022-05-24 18:26:34 -04:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|