-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathranged.go
400 lines (383 loc) · 13.5 KB
/
ranged.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
Copyright (c) 2018, Tomasz "VedVid" Nowakowski
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package main
import (
blt "bearlibterminal"
"errors"
"fmt"
"sort"
)
func (c *Creature) Look(b Board, o Objects, cs Creatures) {
/* Look is method of Creature (that is supposed to be player).
It has to take Board, "global" Objects and Creatures as arguments,
because function PrintVector need to call RenderAll function.
At first, Look creates new para-vector, with player coords as
starting point, and dynamic end position.
Then ComputeVector checks what tiles are present
between Start and End, and adds their coords to vector values.
Line from Vector is drawn, then game waits for player input,
that will change position of "looking" cursors.
Loop breaks with Escape, Space or Enter input. */
startX, startY := c.X, c.Y
targetX, targetY := startX, startY
msg := ""
i := false
for {
vec, err := NewVector(startX, startY, targetX, targetY)
if err != nil {
fmt.Println(err)
}
_ = ComputeVector(vec)
_, _, _, _ = ValidateVector(vec, b, cs, o)
PrintVector(vec, VectorWhyInspect, VectorColorNeutral, VectorColorNeutral, b, o, cs)
if b[targetX][targetY].Explored == true {
if IsInFOV(b, c.X, c.Y, targetX, targetY) == true {
s := GetAllStringsFromTile(targetX, targetY, b, cs, o)
msg = FormatLookingMessage(s, true)
} else {
// Skip monsters if tile is out of c's field of view.
s := GetAllStringsFromTile(targetX, targetY, b, nil, o)
msg = FormatLookingMessage(s, false)
}
} else {
msg = "You don't know what is here."
}
PrintLookingMessage(msg, i)
key := ReadInput()
if key == blt.TK_ESCAPE || key == blt.TK_ENTER || key == blt.TK_SPACE {
break
}
CursorMovement(&targetX, &targetY, key)
i = true
}
}
func PrintLookingMessage(s string, b bool) {
/* Function PrintLookingMessage takes string (message) and bool ("is it
a first iteration?") as arguments.
It is used to provide dynamic printing looking message:
player do not need to confirm target to see what is it, but messages
will not flood message log. */
if RuneCountInBltString(s) > LogSizeX {
s = "Several items are lying here."
}
l := len(MsgBuf)
if s != "" {
switch {
case l == 0:
AddMessage(s)
case l >= MaxMessageBuffer:
RemoveLastMessage()
AddMessage(s)
case l > 0 && l < MaxMessageBuffer:
if b == true {
RemoveLastMessage()
}
AddMessage(s)
}
}
}
func FormatLookingMessage(s []string, fov bool) string {
/* FormatLookingMessage is function that takes slice of strings as argument
and returns string.
Player "see" things in his fov, and "recalls" out of his fov.
It is used to format Look() messages properly.
If slice is empty, it return empty tile message.
If slice contains only one item, it creates simplest message.
If slice is longer, it starts to format message - but it is
explicitly visible in function body. */
const inFov = "see"
const outFov = "recall"
txt := ""
if fov == true {
txt = inFov
} else {
txt = outFov
}
if len(s) == 0 {
return "There is nothing here."
}
if len(s) == 1 {
return "You " + txt + " " + s[0] + " here."
}
msg := "You " + txt + " "
for i, v := range s {
if i < len(s)-2 { // Regular items.
msg = msg + v + ", "
} else if i == len(s)-1-1 { // One-before-last item.
msg = msg + v + " and "
} else { // Last item.
msg = msg + v + " here."
}
}
return msg
}
func (c *Creature) Target(b Board, o *Objects, cs Creatures) bool {
/* Target is method of Creature, that takes game map, objects, and
creatures as arguments. Returns bool that serves as indicator if
action took some time or not.
This method is "the big one", general, for handling targeting.
In short, player starts targetting, line is drawn from player
to monster, then function waits for input (confirmation - "fire",
breaking the loop, or continuing).
Explicitly:
- creates list of all potential targets in fov
* tries to automatically last target, but
* if fails, it targets the nearest enemy
- draws line between source (receiver) and target (coords)
* creates new vector
* checks if it is valid - monsterHit should not be nil
* prints brensenham's line (ie so-called "vector")
- waits for player input
* if player cancels, function ends
* if player confirms, valley is shoot (in target, or empty space)
* if valley is shot in empty space, vector is extrapolated to check
if it will hit any target
* player can switch between targets as well; it targets
next target automatically; at first, only monsters that are
valid target (ie clean shot is possible), then monsters that
are in range and fov, but line of shot is not clear
* in other cases, game will try to move cursor; invalid input
is ignored */
turnSpent := false
var target *Creature
targets := c.FindTargets(FOVLength, b, cs, *o)
if LastTarget != nil && LastTarget != c &&
IsInFOV(b, c.X, c.Y, LastTarget.X, LastTarget.Y) == true {
target = LastTarget
} else {
var err error
target, err = c.FindTarget(targets)
if err != nil {
fmt.Println(err)
}
}
targetX, targetY := target.X, target.Y
i := false
for {
vec, err := NewVector(c.X, c.Y, targetX, targetY)
if err != nil {
fmt.Println(err)
}
_ = ComputeVector(vec)
valid, _, monsterHit, _ := ValidateVector(vec, b, targets, *o)
PrintVector(vec, VectorWhyTarget, VectorColorGood, VectorColorBad, b, *o, cs)
if monsterHit != nil {
cName := "[color=" + monsterHit.Color + "]" + monsterHit.Name + "[/color]"
msg := "There is " + cName + " here."
PrintLookingMessage(msg, i)
}
key := ReadInput()
if key == blt.TK_ESCAPE {
break
}
if key == blt.TK_F {
monsterAimed := FindMonsterByXY(targetX, targetY, cs)
if monsterAimed != nil && monsterAimed != c && monsterAimed.HPCurrent > 0 && valid == true {
LastTarget = monsterAimed
c.AttackTarget(monsterAimed, o)
} else {
if monsterAimed == c {
break // Do not hurt yourself.
}
if monsterHit != nil {
if monsterHit.HPCurrent > 0 {
LastTarget = monsterHit
c.AttackTarget(monsterHit, o)
}
} else {
vx, vy := FindVectorDirection(vec)
v := ExtrapolateVector(vec, vx, vy)
_, _, monsterHitIndirectly, _ := ValidateVector(v, b, targets, *o)
if monsterHitIndirectly != nil {
c.AttackTarget(monsterHitIndirectly, o)
}
}
}
turnSpent = true
break
} else if key == blt.TK_TAB {
i = true
monster := FindMonsterByXY(targetX, targetY, cs)
if monster != nil {
target = NextTarget(monster, targets)
} else {
target = NextTarget(target, targets)
}
targetX, targetY = target.X, target.Y
continue // Switch target
}
CursorMovement(&targetX, &targetY, key)
i = true
}
return turnSpent
}
func CursorMovement(x, y *int, key int) {
/* CursorMovement is function that takes pointers to coords, and
int-based user input. It uses MoveCursor function to
modify original values. */
switch key {
case blt.TK_UP, blt.TK_KP_8, blt.TK_K, blt.TK_W:
MoveCursor(x, y, 0, -1)
case blt.TK_RIGHT, blt.TK_KP_6, blt.TK_L, blt.TK_D:
MoveCursor(x, y, 1, 0)
case blt.TK_DOWN, blt.TK_KP_2, blt.TK_J, blt.TK_X:
MoveCursor(x, y, 0, 1)
case blt.TK_LEFT, blt.TK_KP_4, blt.TK_H, blt.TK_A:
MoveCursor(x, y, -1, 0)
case blt.TK_HOME, blt.TK_KP_7, blt.TK_Y, blt.TK_Q:
MoveCursor(x, y, -1, -1)
case blt.TK_PAGEUP, blt.TK_KP_9, blt.TK_U, blt.TK_E:
MoveCursor(x, y, 1, -1)
case blt.TK_END, blt.TK_KP_1, blt.TK_B, blt.TK_Z:
MoveCursor(x, y, -1, 1)
case blt.TK_PAGEDOWN, blt.TK_KP_3, blt.TK_N, blt.TK_C:
MoveCursor(x, y, 1, 1)
}
}
func MoveCursor(x, y *int, dx, dy int) {
/* Function MoveCursor takes pointers to coords, and
two other ints as direction indicators.
It adds direction to coordinate, checks if it is in
map bounds, and modifies original values accordingly.
This function is called by CursorMovement. */
newX, newY := *x+dx, *y+dy
if newX < 0 || newX >= MapSizeX {
newX = *x
}
if newY < 0 || newY >= MapSizeY {
newY = *y
}
*x, *y = newX, newY
}
func (c *Creature) FindTargets(length int, b Board, cs Creatures, o Objects) Creatures {
/* FindTargets is method of Creature that takes several arguments:
length (that is supposed to be max range of attack), and: map, creatures,
objects. Returns list of creatures.
At first, method creates list of all monsters im c's field of view.
Then, this list is divided to two: first, with all "valid" targets
(clean (without obstacles) line between c and target) and second,
with all other monsters that remains in fov.
Both slices are sorted by distance from receiver, then merged.
It is necessary for autotarget feature - switching between targets
player will start from the nearest valid target, to the farthest valid target;
THEN, it will start to target "invalid" targets - again,
from nearest to farthest one. */
targets := c.MonstersInFov(b, cs)
targetable, unreachable := c.MonstersInRange(b, targets, o, length)
sort.Slice(targetable, func(i, j int) bool {
return targetable[i].DistanceBetweenCreatures(c) <
targetable[j].DistanceBetweenCreatures(c)
})
sort.Slice(unreachable, func(i, j int) bool {
return unreachable[i].DistanceBetweenCreatures(c) <
unreachable[j].DistanceBetweenCreatures(c)
})
targets = nil
targets = append(targets, targetable...)
targets = append(targets, unreachable...)
return targets
}
func (c *Creature) FindTarget(targets Creatures) (*Creature, error) {
/* FindTarget is method of Creature that takes Creatures as arguments.
It returns specific Creature and error.
"targets" is supposed to be slice of Creature in player's fov,
sorted as explained in FindTargets docstring.
If this slice is empty, the target is set to receiver. If not,
it tries to target lastly targeted Creature. If it is not possible,
it targets first element of slice, and marks it as LastTarget.
This method throws an error if it can not find any target,
even including receiver. */
var target *Creature
if len(targets) == 0 {
target = c
} else {
if LastTarget != nil && CreatureIsInSlice(LastTarget, targets) {
target = LastTarget
} else {
target = targets[0]
LastTarget = target
}
}
var err error
if target == nil {
txt := TargetNilError(c, targets)
err = errors.New("Could not find target, even the 'self' one." + txt)
}
return target, err
}
func NextTarget(target *Creature, targets Creatures) *Creature {
/* Function NextTarget takes specific creature (target) and slice of creatures
(targets) as arguments. It tries to find the *next* target (used
with switching between targets, for example using Tab key).
At the end, it returns the next creature. */
i, _ := FindCreatureIndex(target, targets)
var t *Creature
length := len(targets)
if length > i+1 {
t = targets[i+1]
} else if length == 0 {
t = target
} else {
t = targets[0]
}
return t
}
func (c *Creature) MonstersInRange(b Board, cs Creatures, o Objects,
length int) (Creatures, Creatures) {
/* MonstersInRange is method of Creature. It takes global map, Creatures
and Objects, and length (range indicator) as its arguments. It returns
two slices - one with monsters that are in range, and one with
monsters out of range.
At first, two empty slices are created, then function starts iterating
through Creatures from argument. It creates new vector from source (c)
to target, adds monster to proper slice. It also validates vector
(ie, won't add monster hidden behind wall) and skips all dead monsters. */
var inRange = Creatures{}
var outOfRange = Creatures{}
for i, v := range cs {
vec, err := NewVector(c.X, c.Y, v.X, v.Y)
if err != nil {
fmt.Println(err)
}
if ComputeVector(vec) <= length+1 { // "+1" is necessary due Vector values.
valid, _, _, _ := ValidateVector(vec, b, cs, o)
if cs[i].HPCurrent <= 0 {
continue
}
if valid == true {
inRange = append(inRange, cs[i])
} else {
outOfRange = append(outOfRange, cs[i])
}
}
}
return inRange, outOfRange
}
func ZeroLastTarget(c *Creature) {
/* LastTarget is global variable (will be incorporated into
player struct in future). Function ZeroLastTarget changes
last target to nil, is last target matches creature
passed as argument. */
if LastTarget == c {
LastTarget = nil
}
}