-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathklmove.go
128 lines (120 loc) · 2.81 KB
/
klmove.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
package main
import "fmt"
func klmove(fl bool) {
var (
n int
k *kling
dx, dy float64
nextx, nexty int
lookx, looky int
motion int
fudgex, fudgey int
qx, qy int
bigger float64
i int
)
for n = 0; n < etc.nkling; n++ {
k = &etc.klingon[n]
i = 100
if fl {
i = 100 * (k.power / param.klingpwr)
}
flint := 0
if fl {
flint = 1
}
if float64(ranf(i)) >= param.moveprob[2*move.newquad+flint] {
continue
}
/* compute distance to move */
motion = ranf(75) - 25
motion = int(float64(motion) * (k.avgdist * param.movefac[2*move.newquad+flint]))
/* compute direction */
dx = float64(ship.sectx - k.x + ranf(3) - 1)
dy = float64(ship.secty - k.y + ranf(3) - 1)
bigger = dx
if dy > bigger {
bigger = dy
}
if bigger == 0.0 {
bigger = 1.0
}
dx = float64(dx)/float64(bigger) + 0.5
dy = float64(dy)/float64(bigger) + 0.5
if motion < 0 {
motion = -motion
dx = -dx
dy = -dy
}
fudgex, fudgey = 1, 1
/* try to move the klingon */
nextx = k.x
nexty = k.y
for ; motion > 0; motion-- {
lookx = int(float64(nextx) + dx)
looky = int(float64(nexty) + dy)
if lookx < 0 || lookx >= NSECTS || looky < 0 || looky >= NSECTS {
/* new quadrant */
qx = ship.quadx
qy = ship.quady
if lookx < 0 {
qx -= 1
} else if lookx >= NSECTS {
qx += 1
}
if looky < 0 {
qy -= 1
} else if looky >= NSECTS {
qy += 1
}
if qx < 0 || qx >= NQUADS || qy < 0 || qy >= NQUADS || quad[qx][qy].stars < 0 || quad[qx][qy].klings > MAXKLQUAD-1 {
break
}
if !damaged(SRSCAN) {
fmt.Printf("Klingon at %d,%d escapes to quadrant %d,%d\n",
k.x, k.y, qx, qy)
motion = quad[qx][qy].scanned
if motion >= 0 && motion < 1000 {
quad[qx][qy].scanned += 100
}
motion = quad[ship.quadx][ship.quady].scanned
if motion >= 0 && motion < 1000 {
quad[ship.quadx][ship.quady].scanned -= 100
}
}
sect[k.x][k.y] = EMPTY
quad[qx][qy].klings += 1
etc.nkling -= 1
*k = etc.klingon[etc.nkling]
quad[ship.quadx][ship.quady].klings -= 1
k = nil
break
}
if sect[lookx][looky] != EMPTY {
lookx = nextx + fudgex
if lookx < 0 || lookx >= NSECTS {
lookx = int(float64(nextx) + dx)
}
if sect[lookx][looky] != EMPTY {
fudgex = -fudgex
looky = nexty + fudgey
if looky < 0 || looky >= NSECTS || sect[lookx][looky] != EMPTY {
fudgey = -fudgey
break
}
}
}
nextx = lookx
nexty = looky
}
if k != nil && (k.x != nextx || k.y != nexty) {
if !damaged(SRSCAN) {
fmt.Printf("Klingon at %d,%d moves to %d,%d\n", k.x, k.y, nextx, nexty)
}
sect[k.x][k.y] = EMPTY
k.x, k.y = nextx, nexty
sect[nextx][nexty] = KLINGON
}
}
compkldist(false)
}