-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup.mjs
112 lines (88 loc) · 2.58 KB
/
group.mjs
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
'use strict'
import TickCache from '/user/utils/tickCache'
import Sorting from '/user/utils/sorting'
class Group {
constructor(team) {
this.team = team
this.leader = null
this._members = []
}
get members() {
if (!TickCache.has(`members-${this.team}`)) {
for (const creep of this._members) {
if (creep.isDead) {
this.delete(creep)
}
}
TickCache.add(`members-${this.team}`)
}
return this._members
}
get isEmpty() {
return this._members.length === 0
}
get wounded() {
const wounded = this.members
.filter(i => i.isWounded)
.sort(Sorting.byHits())
return wounded
}
add(creep) {
this._members.push(creep)
creep.group = this
if (!this.leader) {
this.leader = creep
console.log('leader', this.leader)
}
}
delete(creep) {
const index = this._members.indexOf(creep)
if (index != -1) {
this._members.splice(index, 1)
}
creep.group = null
if (this.leader === creep) {
this.leader = this._members[0]
console.log('leader', this.leader)
}
}
update() {
if (this.isEmpty) return
const members = this.members
const leader = this.leader
const target = this.targetDefinition
const goal = this.goalDefinition
const alertRange = this.alertRange
const isSpreadTooHigh = this.isSpreadTooHigh
for (const creep of members) {
creep.alertRange = alertRange
creep.target = target[creep.role.toString()]
if (creep.target) {
// TODO: is this needed? why?
creep.goal = creep.target
} else if (isSpreadTooHigh) {
if (!creep.inRangeTo(leader, 2)) {
creep.goal = leader
} else {
creep.goal = creep
}
} else {
creep.goal = goal[creep.role.toString()]
}
creep.update()
}
}
get isSpreadTooHigh() {
return this.spread > 8
}
get spread() {
const leader = this.leader
const sorted = this.members.sort(Sorting.byRangeTo(leader, Sorting.DESC))
const range = leader.getRangeTo(sorted[0])
return range
}
positionReached(position) {
return this.members.some(i => i.standsOn(position))
}
}
export default Group