Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spells #36

Merged
merged 32 commits into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
0059176
split out spell casting into separate file
thorfour Sep 2, 2018
3c23a9c
updated state Cast func
thorfour Sep 2, 2018
ad65da1
added charm monsters
thorfour Sep 2, 2018
3ec468a
game->state plumbing in place to perform a cast projectile
thorfour Sep 4, 2018
f1cfe25
added spells constants
thorfour Sep 9, 2018
74c3972
added animation behavior (WIP)
thorfour Sep 9, 2018
1b57f4f
remove projectile after spell completion
thorfour Sep 9, 2018
03b6a7f
added bounds checking to projectile
thorfour Sep 9, 2018
3735a55
removed debug print
thorfour Sep 9, 2018
52af6d7
fixed allowing outer walls to be destroyed
thorfour Sep 9, 2018
20bc4fe
added killing of monsters
thorfour Sep 10, 2018
0d16b2b
added lit spell
thorfour Sep 22, 2018
86e4189
added sonic spear
Sep 29, 2018
177f4e2
added sle spell
Oct 1, 2018
6b0ea92
added webs
Oct 1, 2018
4a8efa0
added comments for implemented spells
Oct 1, 2018
747301e
added enl spell
Oct 1, 2018
e344c8a
added amulet of invisibility to map generation
Oct 1, 2018
741b49d
if cast returns nothing, continue on
Oct 1, 2018
6a3afe1
added cre - create monster
Oct 13, 2018
1ef6923
changed attackable interface to return dmg dealt
Oct 13, 2018
969d58e
converting glog to logrus
Oct 13, 2018
4b9ac8c
added pha spell
Oct 26, 2018
7eeb5e1
added bal and cld spells
Oct 26, 2018
7a47d69
added can and has spells
Oct 26, 2018
90a4703
added ckl spell
Oct 26, 2018
9b9d3c6
added ply spell
Oct 26, 2018
ecac150
all level 3 spells complete
Oct 26, 2018
1aab4eb
added drl spell
Oct 26, 2018
1b2fbc1
added fgr
Oct 26, 2018
15f2010
added sca and mfi
Oct 26, 2018
4a0d7cb
added tel spell
Oct 26, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions cmd/larn/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"runtime/debug"

"github.com/golang/glog"
log "github.com/sirupsen/logrus"
"github.com/thorfour/larn/pkg/game"
"github.com/thorfour/larn/pkg/game/data"
)
Expand All @@ -15,23 +16,28 @@ var (
)

func init() {
flag.Lookup("stderrthreshold").Value.Set("FATAL") // Only log fatal logs to stderr
flag.Parse()
logfile, err := ioutil.TempFile("", "larn.log")
if err != nil {
log.Errorf("unable to open log file: %v", err)
return
}

log.SetOutput(logfile)
}

func main() {
defer flushLogs() // To ensure logs are flushed
if err := game.New(&data.Settings{
Difficulty: *difficulty,
}).Start(); err != nil {
glog.Fatalf("game exited with error: %v", err)
log.WithField("error", err).Fatal("game exited with error")
}
}

func flushLogs() {
if r := recover(); r != nil {
fmt.Println("Larn encountered an error")
glog.Error(string(debug.Stack())) // Log the stack trace
log.Error(string(debug.Stack())) // Log the stack trace
}
glog.Flush()
}
4 changes: 2 additions & 2 deletions pkg/game/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"text/tabwriter"
"time"

"github.com/golang/glog"
termbox "github.com/nsf/termbox-go"
log "github.com/sirupsen/logrus"
"github.com/thorfour/larn/pkg/game/state/items"
)

Expand Down Expand Up @@ -105,7 +105,7 @@ func (g *Game) accountHandler(deposit bool) func(termbox.Event) {
case termbox.KeyEnter: // Deposit/Withdraw
n, err := strconv.Atoi(amt)
if err != nil {
glog.Errorf("unable to convert bank input to number: %s", amt)
log.WithField("amount", amt).Error("unable to convert bank input to number")
}
if deposit {
if g.currentState.C.Stats.Gold < uint(n) {
Expand Down
8 changes: 5 additions & 3 deletions pkg/game/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package game
import (
"fmt"

"github.com/golang/glog"
termbox "github.com/nsf/termbox-go"
log "github.com/sirupsen/logrus"
"github.com/thorfour/larn/pkg/game/state"
"github.com/thorfour/larn/pkg/io"
)
Expand Down Expand Up @@ -44,7 +44,10 @@ func infoBarGrid(s *state.State) [][]io.Runeable {
// overlay starts overlaying a map at the begging of the original map
func overlay(original, overlay [][]io.Runeable) [][]io.Runeable {
if len(overlay) > len(original) {
glog.Errorf("Overlay is longer than original %v > %v", len(overlay), len(original))
log.WithFields(log.Fields{
"overlay": len(overlay),
"original": len(original),
}).Error("overlay is longer than origional")
return original
}
for i := range overlay {
Expand All @@ -67,7 +70,6 @@ func cat(maps ...[][]io.Runeable) [][]io.Runeable {

// statusLog returns the status log that's displayed on the bottom
func statusLog(s *state.State) [][]io.Runeable {
glog.V(6).Infof("StatusLog: %v", s.StatLog[:logLength])
// Convert the status log to runes
return convert(s.StatLog[:logLength])
}
Expand Down
69 changes: 61 additions & 8 deletions pkg/game/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package game

import (
"fmt"
"time"

"github.com/golang/glog"
termbox "github.com/nsf/termbox-go"
log "github.com/sirupsen/logrus"
"github.com/thorfour/larn/pkg/game/data"
"github.com/thorfour/larn/pkg/game/state"
"github.com/thorfour/larn/pkg/game/state/items"
Expand Down Expand Up @@ -60,7 +61,7 @@ func saveFilePresent() (bool, string) {

// New initializes a game state
func New(s *data.Settings) *Game {
glog.V(1).Infof("Creating new game with %v difficulty", s.Difficulty)
log.WithField("difficulty", s.Difficulty).Info("creating new game")
g := new(Game)
g.settings = s
g.inputHandler = g.defaultHandler
Expand Down Expand Up @@ -281,15 +282,14 @@ func (g *Game) inventoryWrapper(callback func() func(termbox.Event)) func(termbo
g.inputHandler = callback()
g.render(display(g.currentState))
default:
glog.V(6).Infof("Receive invalid input: %s", string(e.Ch))
log.WithField("input", string(e.Ch)).Debug("recieved invalid input")
return
}
}
}

// itemAction is a subroutine for a player to interact with his inventory
func (g *Game) itemAction(a action) func(termbox.Event) {
glog.V(2).Infof("item action requested")

switch a {
case wieldAction:
Expand All @@ -311,7 +311,7 @@ func (g *Game) itemAction(a action) func(termbox.Event) {
case quaffAction:
g.currentState.Log("What do you want to quaff [space to view] ?")
default:
glog.Fatal("unknown item action %v", a)
log.WithField("action", a).Fatal("unknown item action")
}

g.render(display(g.currentState))
Expand Down Expand Up @@ -393,9 +393,24 @@ func (g *Game) cast() func(termbox.Event) {
default:
spell = append(spell, byte(e.Ch))
if len(spell) == 3 { // Spell complete
glog.V(2).Infof("Spell: %s", string(spell))
g.currentState.Cast(string(spell))
g.inputHandler = g.defaultHandler
log.WithField("spell", string(spell)).Info("cast")
callback, err := g.currentState.Cast(string(spell))
if err != nil {
g.currentState.Log(err.Error())
g.inputHandler = g.defaultHandler
g.render(display(g.currentState))
return
}

// If there was a callback func passed, that means the player is casting a projectile.
// Obtian the direction the player would like to cast it, before using the callback to render
// the animation
if callback != nil {
g.inputHandler = g.directionalSpellHandler(callback)
} else {
g.inputHandler = g.defaultHandler
g.render(display(g.currentState))
}
}
}
g.render(display(g.currentState))
Expand Down Expand Up @@ -457,3 +472,41 @@ func (g *Game) enterAction() func(termbox.Event) {
return g.defaultHandler
}
}

func (g *Game) directionalSpellHandler(cb func(types.Direction) bool) func(termbox.Event) {

g.currentState.Log("What Direction? ")
g.render(display(g.currentState))

return func(e termbox.Event) {
var d types.Direction
switch e.Ch {
case 'b':
d = types.DownLeft
case 'n':
d = types.DownRight
case 'y':
d = types.UpLeft
case 'u':
d = types.UpRight
case 'h':
d = types.Left
case 'k':
d = types.Up
case 'l':
d = types.Right
case 'j':
d = types.Down
default: // keep waiting for a valid direction to be entered
return
}

// Continue to call the callback function until animation complete
for cb(d) {
g.render(display(g.currentState))
time.Sleep(30 * time.Millisecond) // small sleep so user can see the spell animation
}
g.render(display(g.currentState))
g.inputHandler = g.defaultHandler
}
}
4 changes: 2 additions & 2 deletions pkg/game/lrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"strconv"
"time"

"github.com/golang/glog"
termbox "github.com/nsf/termbox-go"
log "github.com/sirupsen/logrus"
)

func tax(taxes int) string {
Expand Down Expand Up @@ -60,7 +60,7 @@ func (g *Game) payTaxesHandler() func(termbox.Event) {
case termbox.KeyEnter: // Execute payment
amount, err := strconv.Atoi(amt)
if err != nil {
glog.Errorf("unable to convert tax input to number: %s", amt)
log.WithField("amount", amt).Error("unable to convert tax input to number")
}
if g.currentState.C.Stats.Gold < uint(amount) {
g.renderSplash(lrsPage(g.currentState.Taxes, g.currentState.C.Stats.Gold) + "\n How much? " + amt + "\n You don't have that much.")
Expand Down
27 changes: 22 additions & 5 deletions pkg/game/state/character/character.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"math/rand"

"github.com/golang/glog"
termbox "github.com/nsf/termbox-go"
log "github.com/sirupsen/logrus"
"github.com/thorfour/larn/pkg/game/state/conditions"
"github.com/thorfour/larn/pkg/game/state/items"
"github.com/thorfour/larn/pkg/game/state/stats"
Expand Down Expand Up @@ -85,12 +85,12 @@ func (c *Character) Init(d int) {
w := items.GetNewWeapon(items.Dagger, 0)
w.Attribute = 0
if err := c.Wield(c.inv.AddItem(w, c.Stats)); err != nil {
glog.Fatal("Uanble to wield starting weapon")
log.Fatal("unable to wield starting weapon")
}
a := items.NewArmor(items.Leather, 0)
a.Attribute = 0
if err := c.Wear(c.inv.AddItem(a, c.Stats)); err != nil {
glog.Fatal("Unable to wear starting armor")
log.Fatal("unable to wear starting armor")
}
}
}
Expand Down Expand Up @@ -194,7 +194,7 @@ func (c *Character) item(e rune, a action) (items.Item, error) {
// Cast handles the bookkeeping for a character casting a spell
func (c *Character) Cast(s string) (*items.Spell, error) {
if c.Stats.Spells == 0 { // this should never happen, there's a guard before calls to this
glog.Error("Cast requested with no spells")
log.Error("Cast requested with no spells")
return nil, NoSpellsErr
}

Expand All @@ -213,7 +213,7 @@ func (c *Character) Cast(s string) (*items.Spell, error) {
}

// check if caster is high level enough to cast spell
if int(c.Stats.Level)*3+2 < spell.Level {
if int(c.Stats.Level)*3+2 < spell.Id {
return nil, Inexperienced
}

Expand Down Expand Up @@ -284,3 +284,20 @@ func (c *Character) Quaff(e rune) ([]string, items.PotionID, error) {
s, pid := i.(items.Quaffable).Quaff(c.Stats, c.Cond)
return s, pid, nil
}

// Wielding returns the weapon the character is currently wielding
func (c *Character) Wielding() items.Item {
return c.inv.Item(c.inv.weapon)
}

// CarryingSpecial returns the special item if found in chars inventory
func (c *Character) CarryingSpecial(t items.SpecialType) *items.Special {
for _, item := range c.inv.inv {
if i, ok := item.(*items.Special); ok {
if i.Type == t {
return i
}
}
}
return nil
}
8 changes: 4 additions & 4 deletions pkg/game/state/character/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"sort"
"strings"

"github.com/golang/glog"
log "github.com/sirupsen/logrus"
"github.com/thorfour/larn/pkg/game/state/items"
"github.com/thorfour/larn/pkg/game/state/stats"
)
Expand Down Expand Up @@ -53,7 +53,7 @@ func (i *Inventory) List() []string {
return strings.Split(ret[i], " ")[0] < strings.Split(ret[j], " ")[0]
})

glog.V(4).Infof("Inventory: %v", ret)
log.WithField("items", ret).Debug("Inventory")

return ret
}
Expand Down Expand Up @@ -152,7 +152,7 @@ func (i *Inventory) TakeOff(_ rune, s *stats.Stats) (items.Armor, error) {

a, ok := i.inv[i.armor].(items.Armor)
if !ok {
glog.Errorf("not wearing armor: %s", a)
log.WithField("item", a).Error("not wearing armor")
}
i.armor = none
a.TakeOff(s)
Expand Down Expand Up @@ -183,7 +183,7 @@ func (i *Inventory) Disarm(_ rune, s *stats.Stats) (items.Weapon, error) {

w, ok := i.inv[i.weapon].(items.Weapon)
if !ok {
glog.Errorf("not wielding weapon: %s", w)
log.WithField("item", w).Error("not wielding weapon")
}
i.weapon = none
w.Disarm(s)
Expand Down
9 changes: 6 additions & 3 deletions pkg/game/state/conditions/conditions.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package conditions

import "github.com/golang/glog"

type condition int

const (
Expand Down Expand Up @@ -35,6 +33,12 @@ const (
Invisiblity
// CharmMonsters monsters are more likely to be charmed
CharmMonsters
// Cancellation TODO
Cancellation
// HasteSelf increases character speed
HasteSelf
// ScareMonster makes them scared of you
ScareMonster
)

// ActiveConditions represents all active conditions a character might have
Expand Down Expand Up @@ -86,7 +90,6 @@ func (a ActiveConditions) Remove(c condition) {
// Add an active condition with the given decay function
func (a ActiveConditions) Add(c condition, dur int, decay func()) {
a.active[c] = func(refresh int) {
glog.V(6).Infof("Decay %s: %v", c, dur)
if refresh != 0 { // refresh instead of decay
dur += refresh
return
Expand Down
4 changes: 2 additions & 2 deletions pkg/game/state/items/potion.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"math/rand"

"github.com/golang/glog"
log "github.com/sirupsen/logrus"
"github.com/thorfour/larn/pkg/game/state/conditions"
"github.com/thorfour/larn/pkg/game/state/stats"
)
Expand Down Expand Up @@ -288,7 +288,7 @@ func (p *Potion) Quaff(s *stats.Stats, a *conditions.ActiveConditions) ([]string
a.Refresh(conditions.SeeInvisible, rand.Intn(1000)+401, nil)
l = append(l, "You feel your vision sharpen")
default:
glog.Error("unknown potion consumed: %v", p.ID)
log.WithField("id", p.ID).Error("unknown potion consumed")
}

return l, p.ID
Expand Down
Loading