aboutsummaryrefslogtreecommitdiff
path: root/internal/game/aggro.go
blob: 8f4da4256769c5949fa8cec1f688187950ccbddc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package game

import (
	"fmt"

	"thehouseoficarus/internal/combat"
	"thehouseoficarus/internal/engine"
	"thehouseoficarus/internal/net"
	"thehouseoficarus/internal/player"
)

func (g *Game) checkAggro(sess *net.Session) {
	p := sess.Player.(*player.Player)

	if combat.GetCombat(p.Name) != nil {
		return
	}

	playerLevel := p.CombatLevel()
	mobs := g.MobStore.MobsInRoom(p.RoomID)

	for _, mob := range mobs {
		if !mob.Aggressive || mob.HP <= 0 || mob.Protected {
			continue
		}
		if combat.IsMobInCombat(mob.InstanceID) {
			continue
		}
		mobLevel := mobCombatLevel(mob)
		if playerLevel > mobLevel*2 {
			continue
		}
		aggMob := mob
		g.Ticks.Subscribe(engine.ToTicks(1), func() bool {
			if combat.GetCombat(p.Name) != nil {
				return false
			}
			if aggMob.HP <= 0 || aggMob.RoomID != p.RoomID {
				return false
			}
			if combat.IsMobInCombat(aggMob.InstanceID) {
				return false
			}
			attacker := aggMob.Name
			if !aggMob.Unique {
				attacker = "The " + aggMob.Name
			}
			sess.WriteLine(fmt.Sprintf("\n%s attacks you!", g.colorize(sess, "mob_name", attacker)))
			g.startCombat(sess, p, aggMob)
			return false
		})
		break
	}
}