-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
ship.dm
295 lines (253 loc) · 9.28 KB
/
ship.dm
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
var/global/const/OVERMAP_SPEED_CONSTANT = (1 SECOND)
#define SHIP_MOVE_RESOLUTION 0.00001
#define MOVING(speed) abs(speed) >= min_speed
#define SANITIZE_SPEED(speed) SIGN(speed) * clamp(abs(speed), 0, max_speed)
#define CHANGE_SPEED_BY(speed_var, v_diff) \
v_diff = SANITIZE_SPEED(v_diff);\
if(!MOVING(speed_var + v_diff)) \
{speed_var = 0};\
else \
{speed_var = SANITIZE_SPEED((speed_var + v_diff)/(1 + speed_var*v_diff/(max_speed ** 2)))}
// Uses Lorentzian dynamics to avoid going too fast.
#define SENSOR_COEFFICENT 1000
/obj/overmap/visitable/ship
name = "generic ship"
desc = "Space faring vessel."
icon_state = "ship"
requires_contact = TRUE
var/moving_state = "ship_moving"
var/list/consoles
var/list/sensors
var/vessel_mass = 10000 // tonnes, arbitrary number, affects acceleration provided by engines
var/vessel_size = SHIP_SIZE_LARGE // arbitrary number, affects how likely are we to evade meteors
var/max_speed = 1/(1 SECOND) // "speed of light" for the ship, in turfs/tick.
var/min_speed = 1/(2 MINUTES) // Below this, we round speed to 0 to avoid math errors.
var/max_autopilot = 1 / (10 SECONDS) // The maximum speed any attached helm can try to autopilot at.
var/list/speed = list(0,0) // speed in x,y direction
var/list/position = list(0,0) // position within a tile.
var/last_burn = 0 // worldtime when ship last acceleated
var/burn_delay = 1 SECOND // how often ship can do burns
var/fore_dir = NORTH // what dir ship flies towards for purpose of moving stars effect procs
/// How much it increases identification process each scan
var/base_sensor_visibility = 10
var/list/navigation_viewers // list of weakrefs to people viewing the overmap via this ship
var/list/engines = list()
var/engines_state = 0 //global on/off toggle for all engines
var/thrust_limit = 1 //global thrust limit for all engines, 0..1
var/halted = 0 //admin halt or other stop.
var/skill_needed = SKILL_TRAINED //piloting skill needed to steer it without going in random dir
var/operator_skill
/obj/overmap/visitable/ship/Initialize()
. = ..()
glide_size = world.icon_size
min_speed = round(min_speed, SHIP_MOVE_RESOLUTION)
max_speed = round(max_speed, SHIP_MOVE_RESOLUTION)
SSshuttle.ships += src
START_PROCESSING(SSobj, src)
base_sensor_visibility = initial(base_sensor_visibility) + round(sqrt(vessel_mass/SENSOR_COEFFICENT),1)
/obj/overmap/visitable/ship/Destroy()
STOP_PROCESSING(SSobj, src)
SSshuttle.ships -= src
if(length(consoles))
for(var/obj/machinery/computer/ship/machine in consoles)
if(machine.linked == src)
machine.linked = null
consoles = null
if (length(sensors))
for (var/obj/machinery/shipsensors/machine in sensors)
if (machine.linked == src)
machine.linked = null
sensors = null
. = ..()
/obj/overmap/visitable/ship/relaymove(mob/user, direction, accel_limit)
accelerate(direction, accel_limit)
update_operator_skill(user)
/**
* Updates `operator_skill` to match the current user's skill level, or to null if no user is provided.
* Will skip observers to avoid allowing unintended external influences on flight.
*/
/obj/overmap/visitable/ship/proc/update_operator_skill(mob/user)
if (isobserver(user))
return
operator_skill = user?.get_skill_value(SKILL_PILOT)
/obj/overmap/visitable/ship/proc/is_still()
return !MOVING(speed[1]) && !MOVING(speed[2])
/obj/overmap/visitable/ship/get_scan_data(mob/user)
. = ..()
var/list/extra_data = list("Mass: [vessel_mass] tons.")
if(!is_still())
extra_data += "Heading: [get_heading_angle()], speed [get_speed() * 1000]"
if(instant_contact)
extra_data += "<b>It is broadcasting a distress signal.</b>"
. += jointext(extra_data, "<br>")
//Projected acceleration based on information from engines
/obj/overmap/visitable/ship/proc/get_acceleration()
return round(get_total_thrust()/get_vessel_mass(), SHIP_MOVE_RESOLUTION)
//Does actual burn and returns the resulting acceleration
/obj/overmap/visitable/ship/proc/get_burn_acceleration()
return round(burn() / get_vessel_mass(), SHIP_MOVE_RESOLUTION)
/obj/overmap/visitable/ship/proc/get_vessel_mass()
. = vessel_mass
for(var/obj/overmap/visitable/ship/ship in src)
. += ship.get_vessel_mass()
/obj/overmap/visitable/ship/proc/get_speed()
return round(sqrt(speed[1] ** 2 + speed[2] ** 2), SHIP_MOVE_RESOLUTION)
/obj/overmap/visitable/ship/proc/get_heading()
var/res = 0
if(MOVING(speed[1]))
if(speed[1] > 0)
res |= EAST
else
res |= WEST
if(MOVING(speed[2]))
if(speed[2] > 0)
res |= NORTH
else
res |= SOUTH
return res
/obj/overmap/visitable/ship/proc/get_heading_angle()
var/res = 0
if (MOVING(speed[1]) || MOVING(speed[2]))
res = (round(Atan2(speed[1], -speed[2])) + 450)%360
return res
/obj/overmap/visitable/ship/proc/adjust_speed(n_x, n_y)
CHANGE_SPEED_BY(speed[1], n_x)
CHANGE_SPEED_BY(speed[2], n_y)
for(var/zz in map_z)
if(is_still())
toggle_move_stars(zz)
else
toggle_move_stars(zz, fore_dir)
update_icon()
/obj/overmap/visitable/ship/proc/get_brake_path()
if(!get_acceleration())
return INFINITY
if(is_still())
return 0
if(!burn_delay)
return 0
if(!get_speed())
return 0
var/num_burns = get_speed()/get_acceleration() + 2 //some padding in case acceleration drops form fuel usage
var/burns_per_grid = 1/ (burn_delay * get_speed())
return round(num_burns/burns_per_grid)
/obj/overmap/visitable/ship/proc/decelerate(accel_limit)
if ((!speed[1] && !speed[2]) || !can_burn())
return
last_burn = world.time
var/delta = accel_limit ? min(get_burn_acceleration(), accel_limit) : get_burn_acceleration()
var/mag = sqrt(speed[1] ** 2 + speed[2] ** 2)
if (delta >= mag)
adjust_speed(-speed[1], -speed[2])
else
adjust_speed(-(speed[1] * delta) / mag, -(speed[2] * delta) / mag)
/obj/overmap/visitable/ship/proc/accelerate(direction, accel_limit)
if (!direction || !can_burn())
return
last_burn = world.time
var/delta = accel_limit ? min(get_burn_acceleration(), accel_limit) : get_burn_acceleration()
var/dx = (direction & EAST) ? 1 : ((direction & WEST) ? -1 : 0)
var/dy = (direction & NORTH) ? 1 : ((direction & SOUTH) ? -1 : 0)
if (dx && dy)
dx *= 0.5
dy *= 0.5
adjust_speed(delta * dx, delta * dy)
/obj/overmap/visitable/ship/Process()
if(!halted && !is_still())
var/list/deltas = list(0,0)
for(var/i = 1 to 2)
if(MOVING(speed[i]))
position[i] += speed[i] * OVERMAP_SPEED_CONSTANT
if(position[i] < 0)
deltas[i] = ceil(position[i])
else if(position[i] > 0)
deltas[i] = floor(position[i])
if(deltas[i] != 0)
position[i] -= deltas[i]
position[i] += (deltas[i] > 0) ? -1 : 1
update_icon()
var/turf/newloc = locate(x + deltas[1], y + deltas[2], z)
if(newloc && loc != newloc)
Move(newloc)
handle_wraparound()
sensor_visibility = min(round(base_sensor_visibility + get_speed_sensor_increase(), 1), 100)
/obj/overmap/visitable/ship/on_update_icon()
pixel_x = position[1] * (world.icon_size/2)
pixel_y = position[2] * (world.icon_size/2)
if(!is_still())
icon_state = moving_state
dir = get_heading()
else
icon_state = initial(icon_state)
for(var/obj/machinery/computer/ship/machine in consoles)
if(machine.z in map_z)
for(var/weakref/W in machine.viewers)
var/mob/M = W.resolve()
if(istype(M) && M.client)
M.client.pixel_x = pixel_x
M.client.pixel_y = pixel_y
..()
/obj/overmap/visitable/ship/proc/burn()
for(var/datum/ship_engine/E in engines)
. += E.burn()
/obj/overmap/visitable/ship/proc/get_total_thrust()
for(var/datum/ship_engine/E in engines)
. += E.get_thrust()
/obj/overmap/visitable/ship/proc/can_burn()
if(halted)
return 0
if (world.time < last_burn + burn_delay)
return 0
for(var/datum/ship_engine/E in engines)
. |= E.can_burn()
//deciseconds to next step
/obj/overmap/visitable/ship/proc/ETA()
. = INFINITY
for(var/i = 1 to 2)
if(MOVING(speed[i]))
. = min(., ((speed[i] > 0 ? 1 : -1) - position[i]) / speed[i])
. = max(ceil(.),0)
/obj/overmap/visitable/ship/proc/handle_wraparound()
var/nx = x
var/ny = y
var/low_edge = 1
var/high_edge = GLOB.using_map.overmap_size - 1
if((dir & WEST) && x == low_edge)
nx = high_edge
else if((dir & EAST) && x == high_edge)
nx = low_edge
if((dir & SOUTH) && y == low_edge)
ny = high_edge
else if((dir & NORTH) && y == high_edge)
ny = low_edge
if((x == nx) && (y == ny))
return //we're not flying off anywhere
var/turf/T = locate(nx,ny,z)
if(T)
forceMove(T)
/obj/overmap/visitable/ship/proc/halt()
adjust_speed(-speed[1], -speed[2])
halted = 1
/obj/overmap/visitable/ship/proc/unhalt()
if(!SSshuttle.overmap_halted)
halted = 0
/obj/overmap/visitable/ship/Bump(atom/A)
if(istype(A,/turf/unsimulated/map/edge))
handle_wraparound()
..()
/obj/overmap/visitable/ship/proc/get_helm_skill()//delete this mover operator skill to overmap obj
return operator_skill
/obj/overmap/visitable/ship/populate_sector_objects()
..()
for(var/obj/machinery/computer/ship/S in SSmachines.machinery)
S.attempt_hook_up(src)
for(var/datum/ship_engine/E in ship_engines)
if(check_ownership(E.holder))
engines |= E
/obj/overmap/visitable/ship/proc/get_landed_info()
return "This ship cannot land."
/obj/overmap/visitable/ship/proc/get_speed_sensor_increase()
return min(get_speed() * 1000, 50) //Engines should never increase sensor visibility by more than 50.
#undef MOVING
#undef SANITIZE_SPEED
#undef CHANGE_SPEED_BY