-
Notifications
You must be signed in to change notification settings - Fork 1
/
station_dock_server.ks
162 lines (130 loc) · 4.32 KB
/
station_dock_server.ks
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
CLEARSCREEN.
PRINT "Vessel Comms Station Server Running.".
lock steering to v(0,0,0).
unlock steering.
local message_handlers is lexicon (
"DOCKWANTED", process_dockwanted@,
"UNKNOWN", process_unknown@
).
ag10 off. wait 0.
until ag10 {
WAIT UNTIL NOT SHIP:MESSAGES:EMPTY or ag10.
if not ag10 {
local msg is SHIP:MESSAGES:POP.
process_msg(msg).
wait 0.001.
}
}
function process_msg {
parameter msg.
local data is msg:content.
if not data:ISTYPE("LEXICON") or
not data:HASKEY("TYPE") or
not message_handlers:HASKEY(data["TYPE"])
{
message_handlers["UNKNOWN"]:call(msg).
}
else
{
message_handlers[data["TYPE"]]:call(msg).
}
}
function process_dockwanted {
parameter msg.
local data is msg:content.
print data["TYPE"] + ": " +msg:sender + ", nodetype: " + data["NODETYPE"].
local other_ves is msg:sender.
print "Checking to find a docking port of the requested type with a tag name.".
local found_part is 0.
local did_find is false.
for p in ship:parts {
if p:ISTYPE("DOCKINGPORT") and
p:NODETYPE = data["NODETYPE"] and
(p:STATE = "Disabled" or p:STATE = "Ready") and
p:tag <> "" {
set found_part to p.
set did_find to true.
}
}
if did_find {
local port_name is found_part:TAG.
local other_port_name is data["TAG"].
local other_port_list is other_ves:partstagged(other_port_name).
if other_port_list:LENGTH = 0 {
print "Client sent us a port name that's not there.".
} else {
local other_port is other_port_list[0].
other_ves:connection:sendMessage(LEXICON("TYPE", "DOCKALLOWED", "TAG", port_name)).
print "Found a suitable docking port: " + found_part.
if found_part:hasmodule("ModuleAnimateGeneric") {
local mod is found_part:getmodule("ModuleAnimateGeneric").
if mod:hasevent("open shield") {
mod:doevent("open shield").
print "Opening Shield for the port.".
}
if mod:hasevent("open") {
mod:doevent("open").
print "Opening Shield for the port.".
}
}
local light_name is port_name + "_light".
local indicator_lights is ship:partstagged(light_name).
light_change( indicator_lights, yellow, true ).
print "Orienting port to face client. Color will turn green when ready.".
found_part:CONTROLFROM().
lock steering to orient_to_client(found_part, other_ves).
wait 0.001.
wait 0.001.
wait until abs(steeringmanager:angleerror) < 1 and abs(steeringmanager:rollerror) < 1.
light_change( indicator_lights, green, true ).
set old_steering to orient_to_client(found_part, other_port).
lock steering to old_steering.
other_ves:connection:sendMessage(LEXICON("TYPE", "DOCKREADY", "TAG", port_name)).
print "Waiting for dock to happen.".
until found_part:STATE = "Docked (docker)" or
found_part:STATE = "Docked (dockee)" or
found_part:STATE = "Docked (same vessel)" {
wait 0.0001.
}
print "Dock Happened.".
light_change( indicator_lights, red, false ).
unlock steering.
set ship:control:neutralize to true.
}
} else {
print "Could not find a docking port of that size that had a name tag.".
print " - Refusing dock request.".
other_ves:connection:sendMessage(LEXICON("TYPE", "DOCKDENIED")).
}
}
function process_unknown {
parameter msg.
print "DEBUG: GOT UNKNOWN CLIENT REQUEST " + msg.
print "DEBUG: CONTENTS: " + msg:content.
}
function light_change {
parameter light_list, light_color, enable.
for L in light_list {
local has_kOS_light_mod is L:HASMODULE("kOSLightModule").
local has_light_mod is L:HASMODULE("ModuleLight").
if has_kOS_light_Mod {
local mod is L:GETMODULE("kOSLightModule").
mod:SETFIELD("LIGHT R", light_color:R).
mod:SETFIELD("LIGHT G", light_color:G).
mod:SETFIELD("LIGHT B", light_color:B).
}
if has_light_mod {
local mod is L:GETMODULE("ModuleLight").
if enable {
if (mod:HASEVENT("Lights On")) { mod:doevent("Lights On"). }
} else {
if (mod:HASEVENT("Lights Off")) { mod:doevent("Lights Off"). }
}
}
}
}
function orient_to_client {
parameter port, clientport.
local positionVec is clientport:position - port:position.
return positionVec.
}