-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRemoteProcessClient.swift
executable file
·391 lines (345 loc) · 11.7 KB
/
RemoteProcessClient.swift
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
public enum MessageType: Int8, RawByteRepresentable {
case unknownMessage = 0
case gameOver
case authenticationToken
case teamSize
case protocolVersion
case gameContext
case playerContext
case moveMessage
}
public class RemoteProcessClient {
fileprivate let tc: TCPClient // просто для короткой запись, ибо писал в блокноте
fileprivate var previousFacilityById: [Int64: Facility] = [:]
fileprivate var previousFacilities: [Facility] = []
fileprivate var previousPlayerById: [Int64: Player] = [:]
fileprivate var previousPlayers: [Player] = []
fileprivate var terrainByCellXY: [[TerrainType]] = []
fileprivate var weatherByCellXY: [[WeatherType]] = []
public init(host: String, port: Int) {
tc = TCPClient(address: host, port: Int32(port))
if !tc.connect() {
c_exit("Can't connect to host: \(host) port: \(port)")
}
}
public func close() {
tc.close()
}
public func write(token: String) {
tc.write(enum: MessageType.authenticationToken)
tc.write(string: token)
}
public func write(protocolVersion: Int) {
tc.write(enum: MessageType.protocolVersion)
tc.write(int: protocolVersion)
}
public func readTeamSize() -> Int {
ensureMessageType(tc.readEnum()!, .teamSize)
return tc.readInt()
}
public func readGameContext() -> Game {
ensureMessageType(tc.readEnum()!, .gameContext)
return readModelGame()
}
public func readPlayerContext() -> PlayerContext? {
let messageType: MessageType = tc.readEnum()!
if messageType == .gameOver {
return nil
}
ensureMessageType(messageType, .playerContext)
return readModelPlayerContext()
}
public func write(move: Move) {
tc.write(enum: MessageType.moveMessage)
writeModel(move: move)
}
}
/// Функции чтения моделей
extension RemoteProcessClient {
fileprivate func readModelFacility() -> Facility {
let flag = tc.readByte()
if 0 == flag {
c_exit("Can't read facility")
}
if 127 == flag {
return previousFacilityById[tc.readLong()]!
}
let facility = Facility(
id: tc.readLong(),
type: tc.readEnum()!,
ownerPlayerId: tc.readLong(),
left: tc.readDouble(),
top: tc.readDouble(),
capturePoints: tc.readDouble(),
vehicleType: tc.readEnum()!,
productionProgress: tc.readInt()
)
previousFacilityById[facility.id] = facility;
return facility;
}
fileprivate func readModelFacilities() -> [Facility] {
let count = tc.readInt()
if count < 0 {
return previousFacilities
}
let facilities = (0..<count).map { _ in readModelFacility() }
previousFacilities = facilities
return facilities
}
fileprivate func readModelGame() -> Game {
if !tc.readBool() {
c_exit("Can't read game")
}
return Game(
randomSeed: tc.readLong(),
tickCount: tc.readInt(),
worldWidth: tc.readDouble(),
worldHeight: tc.readDouble(),
fogOfWarEnabled: tc.readBool(),
victoryScore: tc.readInt(),
facilityCaptureScore: tc.readInt(),
vehicleEliminationScore: tc.readInt(),
actionDetectionInterval: tc.readInt(),
baseActionCount: tc.readInt(),
additionalActionCountPerControlCenter: tc.readInt(),
maxUnitGroup: tc.readInt(),
terrainWeatherMapColumnCount: tc.readInt(),
terrainWeatherMapRowCount: tc.readInt(),
plainTerrainVisionFactor: tc.readDouble(),
plainTerrainStealthFactor: tc.readDouble(),
plainTerrainSpeedFactor: tc.readDouble(),
swampTerrainVisionFactor: tc.readDouble(),
swampTerrainStealthFactor: tc.readDouble(),
swampTerrainSpeedFactor: tc.readDouble(),
forestTerrainVisionFactor: tc.readDouble(),
forestTerrainStealthFactor: tc.readDouble(),
forestTerrainSpeedFactor: tc.readDouble(),
clearWeatherVisionFactor: tc.readDouble(),
clearWeatherStealthFactor: tc.readDouble(),
clearWeatherSpeedFactor: tc.readDouble(),
cloudWeatherVisionFactor: tc.readDouble(),
cloudWeatherStealthFactor: tc.readDouble(),
cloudWeatherSpeedFactor: tc.readDouble(),
rainWeatherVisionFactor: tc.readDouble(),
rainWeatherStealthFactor: tc.readDouble(),
rainWeatherSpeedFactor: tc.readDouble(),
vehicleRadius: tc.readDouble(),
tankDurability: tc.readInt(),
tankSpeed: tc.readDouble(),
tankVisionRange: tc.readDouble(),
tankGroundAttackRange: tc.readDouble(),
tankAerialAttackRange: tc.readDouble(),
tankGroundDamage: tc.readInt(),
tankAerialDamage: tc.readInt(),
tankGroundDefence: tc.readInt(),
tankAerialDefence: tc.readInt(),
tankAttackCooldownTicks: tc.readInt(),
tankProductionCost: tc.readInt(),
ifvDurability: tc.readInt(),
ifvSpeed: tc.readDouble(),
ifvVisionRange: tc.readDouble(),
ifvGroundAttackRange: tc.readDouble(),
ifvAerialAttackRange: tc.readDouble(),
ifvGroundDamage: tc.readInt(),
ifvAerialDamage: tc.readInt(),
ifvGroundDefence: tc.readInt(),
ifvAerialDefence: tc.readInt(),
ifvAttackCooldownTicks: tc.readInt(),
ifvProductionCost: tc.readInt(),
arrvDurability: tc.readInt(),
arrvSpeed: tc.readDouble(),
arrvVisionRange: tc.readDouble(),
arrvGroundDefence: tc.readInt(),
arrvAerialDefence: tc.readInt(),
arrvProductionCost: tc.readInt(),
arrvRepairRange: tc.readDouble(),
arrvRepairSpeed: tc.readDouble(),
helicopterDurability: tc.readInt(),
helicopterSpeed: tc.readDouble(),
helicopterVisionRange: tc.readDouble(),
helicopterGroundAttackRange: tc.readDouble(),
helicopterAerialAttackRange: tc.readDouble(),
helicopterGroundDamage: tc.readInt(),
helicopterAerialDamage: tc.readInt(),
helicopterGroundDefence: tc.readInt(),
helicopterAerialDefence: tc.readInt(),
helicopterAttackCooldownTicks: tc.readInt(),
helicopterProductionCost: tc.readInt(),
fighterDurability: tc.readInt(),
fighterSpeed: tc.readDouble(),
fighterVisionRange: tc.readDouble(),
fighterGroundAttackRange: tc.readDouble(),
fighterAerialAttackRange: tc.readDouble(),
fighterGroundDamage: tc.readInt(),
fighterAerialDamage: tc.readInt(),
fighterGroundDefence: tc.readInt(),
fighterAerialDefence: tc.readInt(),
fighterAttackCooldownTicks: tc.readInt(),
fighterProductionCost: tc.readInt(),
maxFacilityCapturePoints: tc.readDouble(),
facilityCapturePointsPerVehiclePerTick: tc.readDouble(),
facilityWidth: tc.readDouble(),
facilityHeight: tc.readDouble(),
baseTacticalNuclearStrikeCooldown: tc.readInt(),
tacticalNuclearStrikeCooldownDecreasePerControlCenter:tc.readInt(),
maxTacticalNuclearStrikeDamage: tc.readDouble(),
tacticalNuclearStrikeRadius: tc.readDouble(),
tacticalNuclearStrikeDelay: tc.readInt()
)
}
fileprivate func writeModel(move: Move) {
tc.write(bool: true)
tc.write(enum: move.action)
tc.write(int: move.group)
tc.write(double: move.left)
tc.write(double: move.top)
tc.write(double: move.right)
tc.write(double: move.bottom)
tc.write(double: move.x)
tc.write(double: move.y)
tc.write(double: move.angle)
tc.write(double: move.factor)
tc.write(double: move.maxSpeed)
tc.write(double: move.maxAngularSpeed)
tc.write(enum: move.vehicleType)
tc.write(long: move.facilityId)
tc.write(long:move.vehicleId);
}
fileprivate func readModelPlayer() -> Player {
let flag = tc.readByte()
if 0 == flag {
c_exit("Can't read facility")
}
if 127 == flag {
return previousPlayerById[tc.readLong()]!
}
let player = Player(
id: tc.readLong(),
me: tc.readBool(),
strategyCrashed: tc.readBool(),
score: tc.readInt(),
remainingActionCooldownTicks: tc.readInt(),
remainingNuclearStrikeCooldownTicks: tc.readInt(),
nextNuclearStrikeVehicleId: tc.readLong(),
nextNuclearStrikeTickIndex:tc.readInt(),
nextNuclearStrikeX: tc.readDouble(),
nextNuclearStrikeY: tc.readDouble()
)
previousPlayerById[player.id] = player
return player
}
fileprivate func readModelPlayers() -> [Player] {
let count = tc.readInt()
if count < 0 {
return previousPlayers
}
let players = (0..<count).map { _ in readModelPlayer() }
previousPlayers = players
return players
}
fileprivate func readModelPlayerContext() -> PlayerContext {
if !tc.readBool() {
c_exit("Can't read player context")
}
return PlayerContext(
player: readModelPlayer(),
world: readModelWorld()
)
}
fileprivate func readModelVehicle() -> Vehicle {
if !tc.readBool() {
c_exit("Can't read vehicle")
}
return Vehicle(
id: tc.readLong(),
x: tc.readDouble(),
y: tc.readDouble(),
radius: tc.readDouble(),
playerId: tc.readLong(),
durability: tc.readInt(),
maxDurability: tc.readInt(),
maxSpeed: tc.readDouble(),
visionRange: tc.readDouble(),
squaredVisionRange: tc.readDouble(),
groundAttackRange: tc.readDouble(),
squaredGroundAttackRange: tc.readDouble(),
aerialAttackRange: tc.readDouble(),
squaredAerialAttackRange: tc.readDouble(),
groundDamage: tc.readInt(),
aerialDamage: tc.readInt(),
groundDefence: tc.readInt(),
aerialDefence: tc.readInt(),
attackCooldownTicks: tc.readInt(),
remainingAttackCooldownTicks: tc.readInt(),
type: tc.readEnum()!,
aerial: tc.readBool(),
selected: tc.readBool(),
groups: tc.readInts()
)
}
fileprivate func readModelVehicles() -> [Vehicle] {
let count = tc.readInt()
if count < 0 {
c_exit("Can't read vehicles")
}
return (0..<count).map { _ in readModelVehicle() }
}
fileprivate func readModelVehicleUpdate() -> VehicleUpdate {
if !tc.readBool() {
c_exit("Can't read vehicle update")
}
return VehicleUpdate(
id: tc.readLong(),
x: tc.readDouble(),
y: tc.readDouble(),
durability: tc.readInt(),
remainingAttackCooldownTicks: tc.readInt(),
selected: tc.readBool(),
groups: tc.readInts()
)
}
fileprivate func readModelVehicleUpdates() -> [VehicleUpdate] {
let count = tc.readInt()
if count < 0 {
c_exit("Can't read vehicle update")
}
return (0..<count).map { _ in readModelVehicleUpdate() }
}
fileprivate func readModelWorld() -> World {
if !tc.readBool() {
c_exit("Can't read world")
}
func readModelTerranByCellXY() -> [[TerrainType]] {
if terrainByCellXY.isEmpty {
terrainByCellXY = tc.readEnums2D();
}
return terrainByCellXY
}
func readModelWeatherByCellXY() -> [[WeatherType]] {
if weatherByCellXY.isEmpty {
weatherByCellXY = tc.readEnums2D();
}
return weatherByCellXY
}
return World(
tickIndex: tc.readInt(),
tickCount: tc.readInt(),
width: tc.readDouble(),
height: tc.readDouble(),
players: readModelPlayers(),
newVehicles: readModelVehicles(),
vehicleUpdates: readModelVehicleUpdates(),
terrainByCellXY: readModelTerranByCellXY(),
weatherByCellXY: readModelWeatherByCellXY(),
facilities: readModelFacilities()
)
}
}
/// Вспомогательные функции
extension RemoteProcessClient {
fileprivate func ensureMessageType(_ actualType: MessageType, _ expectedType: MessageType) {
if actualType != expectedType {
c_exit("message type not equals: actual: \(actualType) expected: \(expectedType)")
}
}
}