aboutsummaryrefslogtreecommitdiff
path: root/internal/game/tick.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/game/tick.go')
-rw-r--r--internal/game/tick.go141
1 files changed, 141 insertions, 0 deletions
diff --git a/internal/game/tick.go b/internal/game/tick.go
new file mode 100644
index 0000000..9595668
--- /dev/null
+++ b/internal/game/tick.go
@@ -0,0 +1,141 @@
+package game
+
+import (
+ "fmt"
+ "math/rand"
+
+ "thirdcollapse/internal/combat"
+ "thirdcollapse/internal/player"
+)
+
+func (g *Game) DisconnectTick() {
+ if g.Hub == nil {
+ return
+ }
+ for _, sess := range g.Hub.AllSessions() {
+ if !sess.Disconnecting {
+ continue
+ }
+ p, ok := sess.Player.(*player.Player)
+ if !ok {
+ g.Hub.Remove(sess)
+ continue
+ }
+ if combat.GetCombat(p.Name) != nil {
+ continue
+ }
+ sess.DisconnectTicks--
+ if sess.DisconnectTicks <= 0 {
+ g.AccountStore.SaveCharacter(p)
+ g.Hub.HardRemove(sess)
+ }
+ }
+}
+
+func (g *Game) RegenTick() {
+ if g.Hub == nil {
+ return
+ }
+ for _, sess := range g.Hub.AllSessions() {
+ p, ok := sess.Player.(*player.Player)
+ if !ok {
+ continue
+ }
+ if p.HP <= 0 || p.HP >= p.MaxHP() {
+ p.RegenerateTick = 0
+ continue
+ }
+ p.RegenerateTick--
+ if p.RegenerateTick <= 0 {
+ p.HP++
+ if p.HP >= p.MaxHP() {
+ p.HP = p.MaxHP()
+ p.RegenerateTick = 0
+ } else {
+ p.RegenerateTick = 100
+ }
+ }
+ }
+}
+
+func (g *Game) WanderTick() {
+ if g.Hub == nil {
+ return
+ }
+
+ type moveEvent struct {
+ name string
+ fromRoom int
+ toRoom int
+ level int
+ }
+
+ var moves []moveEvent
+
+ // Mob wandering
+ for _, inst := range g.MobStore.AllInstances() {
+ if inst.HP <= 0 || len(inst.WanderRooms) == 0 || inst.WanderInterval <= 0 {
+ continue
+ }
+ if combat.IsMobInCombat(inst.InstanceID) {
+ continue
+ }
+ inst.WanderTickCounter++
+ if inst.WanderTickCounter >= inst.WanderInterval {
+ inst.WanderTickCounter = 0
+ toRoom := inst.WanderRooms[rand.Intn(len(inst.WanderRooms))]
+ if toRoom == inst.RoomID {
+ continue
+ }
+ moves = append(moves, moveEvent{
+ name: mobDisplayName(inst, false),
+ fromRoom: inst.RoomID,
+ toRoom: toRoom,
+ level: mobCombatLevel(inst),
+ })
+ inst.RoomID = toRoom
+ }
+ }
+
+ // Object wandering (fishing spots, etc.)
+ g.World.TickObjWander()
+ objMoves := g.World.FlushObjMoves()
+ for _, om := range objMoves {
+ for _, sess := range g.Hub.PlayersInRoom(om.FromRoom) {
+ if p, ok := sess.Player.(*player.Player); ok && p.Action != nil {
+ if p.Action.TargetID == om.DefID {
+ g.CancelAction(p)
+ }
+ }
+ }
+ moves = append(moves, moveEvent{
+ name: "a " + om.Name,
+ fromRoom: om.FromRoom,
+ toRoom: om.ToRoom,
+ level: 0,
+ })
+ }
+
+ for _, m := range moves {
+ for _, sess := range g.Hub.AllSessions() {
+ p, ok := sess.Player.(*player.Player)
+ if !ok {
+ continue
+ }
+ if p.RoomID == m.fromRoom && p.Toggles["mobleave"] {
+ if m.level > 0 {
+ sess.WriteLine(fmt.Sprintf("\n%s (level %d) leaves.", m.name, m.level))
+ } else {
+ sess.WriteLine(fmt.Sprintf("\n%s moves away.", m.name))
+ }
+ }
+ if p.RoomID == m.toRoom && p.Toggles["mobenter"] {
+ if m.level > 0 {
+ sess.WriteLine(fmt.Sprintf("\n%s (level %d) enters.", m.name, m.level))
+ } else {
+ sess.WriteLine(fmt.Sprintf("\n%s drifts in.", m.name))
+ }
+ }
+ }
+ }
+}