-
Notifications
You must be signed in to change notification settings - Fork 0
/
move.go
144 lines (130 loc) · 3.02 KB
/
move.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
package main
import (
"fmt"
"math"
)
func domove(ramflag int, course int, p_time float64, speed float64) float64 {
var (
angle float64
x, y, dx, dy float64
ix, iy int
bigger float64
n int
dist float64
sectsize float64
xn float64
evtime float64
)
tracef("move: ramflag %d course %d time %.2f speed %.2f", ramflag, course, p_time, speed)
sectsize = NSECTS
/* initialize delta factors for move */
angle = float64(course) * 0.0174532925
if damaged(SINS) {
angle += param.navigcrud[1] * (franf() - 0.5)
} else if ship.sinsbad {
angle += param.navigcrud[0] * (franf() - 0.5)
}
dx = -math.Cos(angle)
dy = math.Sin(angle)
bigger = math.Abs(dx)
dist = math.Abs(dy)
if dist > bigger {
bigger = dist
}
dx /= bigger
dy /= bigger
/* check for long range tractor beams */
/**** TEMPORARY CODE == DEBUGGING ****/
evtime = now.eventptr[E_LRTB].date - now.date
if p_time > evtime && etc.nkling < 3 {
/* then we got a LRTB */
evtime += 0.005
p_time = evtime
} else {
evtime = -1.0e50
}
dist = p_time * speed
/* move within quadrant */
sect[ship.sectx][ship.secty] = EMPTY
x = float64(ship.sectx) + 0.5
y = float64(ship.secty) + 0.5
xn = NSECTS * dist * bigger
n = int(xn + 0.5)
tracef("move: dx = %.2f, dy = %.2f, xn = %.2f, n = %d", dx, dy, xn, n)
move.free = false
for i := 0; i < n; i++ {
x += dx
y += dy
ix = int(x)
iy = int(y)
if x < 0.0 || y < 0.0 || x >= sectsize || y >= sectsize {
/* enter new quadrant */
dx = float64(ship.quadx)*NSECTS + float64(ship.sectx) + dx*xn
dy = float64(ship.quady)*NSECTS + float64(ship.secty) + dy*xn
if dx < 0.0 {
ix = -1
} else {
ix = int(dx + 0.5)
}
if dy < 0.0 {
iy = -1
} else {
iy = int(dy + 0.5)
}
ship.sectx = int(x)
ship.secty = int(y)
compkldist(false)
move.newquad = 2
attack(false)
checkcond()
ship.quadx = ix / NSECTS
ship.quady = iy / NSECTS
ship.sectx = ix % NSECTS
ship.secty = iy % NSECTS
if ix < 0 || ship.quadx >= NQUADS || iy < 0 || ship.quady >= NQUADS {
if !damaged(COMPUTER) {
dumpme(false)
} else {
lose(L_NEGENB)
}
}
initquad(0)
n = 0
break
}
if sect[ix][iy] != EMPTY {
/* we just hit something */
if !damaged(COMPUTER) && ramflag <= 0 {
ix = int(x - dx)
iy = int(y - dy)
fmt.Printf("Computer reports navigation error; %s stopped at %d,%d\n", ship.shipname, ix, iy)
ship.energy -= int(float64(param.stopengy) * speed)
break
}
/* test for a black hole */
if sect[ix][iy] == HOLE {
/* get dumped elsewhere in the galaxy */
dumpme(true)
initquad(0)
n = 0
break
}
ram(ix, iy)
break
}
}
if n > 0 {
dx = float64(ship.sectx - ix)
dy = float64(ship.secty - iy)
dist = math.Sqrt(dx*dx+dy*dy) / NSECTS
p_time = dist / speed
if evtime > p_time {
p_time = evtime /* spring the LRTB trap */
}
ship.sectx = ix
ship.secty = iy
}
sect[ship.sectx][ship.secty] = ship.ship
compkldist(false)
return p_time
}