-
Notifications
You must be signed in to change notification settings - Fork 13
/
combat.go
169 lines (136 loc) · 3.32 KB
/
combat.go
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package combat
import (
"time"
"github.com/Cristofori/kmud/events"
"github.com/Cristofori/kmud/model"
"github.com/Cristofori/kmud/types"
"github.com/Cristofori/kmud/utils"
)
var combatInterval = 3 * time.Second
var combatMessages chan interface{}
type combatInfo struct {
Defender types.Character
Skill types.Skill
}
var fights map[types.Character]combatInfo
type combatStart struct {
Attacker types.Character
Defender types.Character
Skill types.Skill
}
type combatStop struct {
Attacker types.Character
}
type combatQuery struct {
Character types.Character
Ret chan bool
}
type combatTick struct{}
func StartFight(attacker types.Character, skill types.Skill, defender types.Character) {
combatMessages <- combatStart{Attacker: attacker, Defender: defender, Skill: skill}
}
func StopFight(attacker types.Character) {
combatMessages <- combatStop{Attacker: attacker}
}
func InCombat(character types.Character) bool {
query := combatQuery{Character: character, Ret: make(chan bool)}
combatMessages <- query
return <-query.Ret
}
func init() {
fights = map[types.Character]combatInfo{}
combatMessages = make(chan interface{}, 1)
go func() {
defer func() { recover() }()
throttler := utils.NewThrottler(combatInterval)
for {
throttler.Sync()
combatMessages <- combatTick{}
}
}()
go func() {
for message := range combatMessages {
Switch:
switch m := message.(type) {
case combatTick:
for a, info := range fights {
d := info.Defender
if a.GetRoomId() == d.GetRoomId() {
power := 0
skill := info.Skill
if skill == nil {
power = utils.Random(1, 10)
} else {
effects := model.GetEffects(skill.GetEffects())
variance := 0
for _, e := range effects {
power += e.GetPower()
variance += e.GetVariance()
}
power += utils.Random(-variance, variance)
}
d.Hit(power)
events.Broadcast(events.CombatEvent{Attacker: a, Defender: d, Skill: skill, Power: power})
if d.GetHitPoints() <= 0 {
Kill(d)
}
} else {
doCombatStop(a)
}
}
case combatStart:
oldInfo, found := fights[m.Attacker]
if m.Defender == oldInfo.Defender {
break
}
if found {
doCombatStop(m.Attacker)
}
fights[m.Attacker] = combatInfo{
Defender: m.Defender,
Skill: m.Skill,
}
events.Broadcast(events.CombatStartEvent{Attacker: m.Attacker, Defender: m.Defender})
case combatStop:
doCombatStop(m.Attacker)
case combatQuery:
_, found := fights[m.Character]
if found {
m.Ret <- true
} else {
for _, info := range fights {
if info.Defender == m.Character {
m.Ret <- true
break Switch
}
}
m.Ret <- false
}
default:
panic("Unhandled combat message")
}
}
}()
}
func Kill(char types.Character) {
clearCombat(char)
events.Broadcast(events.DeathEvent{Character: char})
}
func clearCombat(char types.Character) {
_, found := fights[char]
if found {
doCombatStop(char)
}
for a, info := range fights {
if info.Defender == char {
doCombatStop(a)
}
}
}
func doCombatStop(attacker types.Character) {
info := fights[attacker]
if info.Defender != nil {
delete(fights, attacker)
events.Broadcast(events.CombatStopEvent{Attacker: attacker, Defender: info.Defender})
}
}