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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#  Created by Scott Russell
#  Copyright (c) 2006,2007 Marcil Tech Group. All rights reserved.

FAIL_DIVERT_TO_HOME_FACTOR = 0.5

class Plane
        attr_accessor :angle 
        attr_accessor :acceleration
        attr_accessor :target_airport
        attr_accessor :landed_airport
        attr_accessor :landed
        attr_accessor :tail
        attr_accessor :passengers
        attr_accessor :position
        attr_accessor :routes
        attr_accessor :enabled
        attr_accessor :takeoff_time
        attr_accessor :total_time
        attr_accessor :total_cost
        attr_accessor :total_hops
        attr_accessor :total_pax
        attr_accessor :exclusive
        attr_accessor :fail
        attr_accessor :takeoff_in
        attr_accessor :plane_failures
        attr_accessor :home
        attr_accessor :bearing
        attr_accessor :dayschedule
        attr_accessor :velocity
        attr_accessor :short_cost, :long_cost, :mtbf
        attr_accessor :home, :destination

        def initialize(sim)
                @sim = sim
                @position = Position.new
                @home = nil
                @loadTime = 5
                @routes = {}
                @tail = ""
                @pax = 0
                @range = 0
                @speed = 0
                @cost = 0
                @mtbf=0
                @dayschedule=[0,0,0,0,0,0,0]
                reset
        end
        
        def reset
                @bearing = 0.0
                @landed_airport = @home
                @landed = true
                @position = @home ? @home.position.clone : Position.new 
                @target_airport = nil
                @passengers = []
                @angle = @acceleration = 0
                @velocity = 0
                @etwf = 0
                @last_airport = nil
                @flight_time = 0
                @total_time = 0
                @total_pax = 0
                @total_hops = 0
                @total_cost = 0
                @hops = []
                @fail = 0
                @plane_failures = 0
        end
        
        def latitude
                @position ? @position.latitude : 0.0
        end
        
        def latitude=(val)
                if @position.nil?
                        @position = Position.new(val, 0.0)
                else
                        @position.latitude = val
                end
        end

        def longitude
                @position ? @position.longitude : 0.0
        end
        
        def longitude=(val)
                if @position.nil?
                        @position = Position.new(0.0, val)
                else
                        @position.longitude = val
                end
        end

        def clear
                @passengers = []
        end

        def loadTime
                @loadTime
        end

        def loadTime=(value)
                @loadTime=value.to_i
        end

        def pax
                @pax
        end

        def pax=(value)
                @pax=value.to_i
        end

        def range
                @range
        end

        def range=(value)
                @range=value.to_f
        end

        def speed
                @speed
        end

        def speed=(value)
                @speed=value.to_f
        end

        def cost
                @cost
        end

        def cost=(value)
                @cost=value.to_f
        end

        def mtbf
                @mtbf
        end

        def mtbf=(value)
                @mtbf=value.to_i
        end

        def etwf
                @etwf
        end

        def etwf=(value)
                @etwf=value.to_i
        end

        def fly(target)
          if !arrive_at_target?
            move_toward target, @velocity
          else
            @position.latitude = target.position.latitude
            @position.longitude = target.position.longitude
          end
  end

        def move_toward(new_position, distance)
                @bearing = bearing_to new_position
                @position.move_to @bearing, distance
        end
        
        def target(airport)
                @sim.addLog "TARGET #{tail} #{airport}"
                @target_airport = airport
                set_bearing_to_airport airport
                @velocity = @speed/10
        end
        
        def bearing_to(airport)
                @position.bearing_to airport.position
        end

        def set_bearing_to_airport(airport)
                @bearing = bearing_to(airport)
        end

        def available?
                !@landed or (@takeoff_in <= 0)
        end

        def distance_to_target
                if @target_airport
                        return distance_to(@target_airport.position)
                end
                return 0
        end

        def distance_to_airport(airport)
                distance_to airport.position
        end

        def distance_to(new_position)
                @position.distance_to new_position 
        end

        def arrive_at_target?
                arrive?(@target_airport)
        end
        
        def arrive?(airport)
                distance_to(airport.position) < @velocity
        end

        def plane_stats
                paxlentot = 0
                waitlentot = 0
    total_cost_tot = 0
        
                @hops.each{|h|
                        time, lastairport, thisairport, distance, paxlength, waitinglength, total_time, total_cost, total_hops, total_pax = h
                        paxlentot += paxlength
                        waitlentot += waitinglength
                        total_cost_tot += total_cost
                }
        
                ret = "  #{@tail} (#{@home.name}) \t Pax: #{@passengers.length} DT: #{@plane_failures/60.0} hours COST: #{total_cost_tot}" 
                ret +=  "\n    Flight: #{sprintf("%6.3f",@total_time)} \tHops: #{@total_hops}\t Pax carried: #{@total_pax}"
                if @hops.length > 0
                        ret +=  "\n    Avg load: #{sprintf("%6.3f",paxlentot.to_f/@hops.length)} \tAvg queue: #{sprintf("%6.3f",waitlentot.to_f/@hops.length)} \n    Avg hop: #{sprintf("%6.3f",@total_time/@hops.length)} "
                else
                        ret +=  "\n    Avg load: 0 \tAvg queue: 0"
                end
        end

        def think()
                if fail > 0
                        @plane_failures += 6
                end
                
                if @landed
                        if @fail > 0
                                @fail = [0,@fail - 1].max
                                if @fail == 0
                                        transfer_all_from 
                                end
                        else
                                transfer_all_from
                                @takeoff_in = [0,@takeoff_in - 1].max
                                if @takeoff_in < 1
                                        @sim.dispatch(self)
                                        if self.target_airport
                                                takeoff
                                        end
                                end
                        end
                else
                        if @target_airport
                                if arrive?(@target_airport)
                                        @sim.addLog "DESCEND #{tail} to #{@target_airport}"
                                        land @target_airport
                                        @angle = 0
                                else
                                        fly(@target_airport)
                                        @etwf += 1/6.0 if @fail < 1
                                        if @etwf >= @mtbf
                                                plane_failure
                                        end
                                        move
                                end
                        else
                                @sim.dispatch(self)
                        end
                end
        end
        
        def plane_failure
                @sim.something_happened = true
                @sim.addLog "FAILURE #{tail} *********************************"
                @etwf = 0
                @fail = 50
                landing_sites=[]
                @sim.airports.each do  |a|
                        landing_sites << [@position.distance_to(a.position ), a]
                end
        
                site = landing_sites.sort[0]
                if site[1] != @home
                        dist = @position.distance_to(@home.position) * FAIL_DIVERT_TO_HOME_FACTOR
                        if dist < site[0]
                                airport = @home
                                @sim.addLog "FAILDIVERT #{tail} chose home instead (#{site[1].to_s} - #{site[0]}, #{dist})"
                        else
                                airport = site[1]
                                @sim.addLog "FAILDIVERT #{tail} close to #{airport}"
                        end
                else
                        airport = @home
                        @sim.addLog "FAILDIVERT #{tail} close to home (#{airport})"
                end

                target(airport)

                #@sim.running = false
        end

        def transfer_to()
                @sim.addLog "TRANSFER to #{@landed_airport} (#{@passengers.size})"
                @passengers.each {|pax|
                        if @landed_airport == pax.target
                                pax.unload(@landed_airport)
                                @passengers.delete pax
                                @sim.addLog "UNLOAD at #{@landed_airport} (#{pax.name})"
                        end
                }
                if @fail > 0
                        while @passengers.size > 0
                                pax = @passengers[0]
                                @sim.addLog "FAILUNLOAD at #{@landed_airport} (#{pax.name})"
                                pax.unload_failure(@landed_airport)
                                @passengers.delete pax                                
                        end
                        ret = ""
                        @passengers.each {|pax| ret += "(#{pax.name})"}
                        @sim.addLog "FAILUNLOADED at #{@landed_airport} (#{@passengers.size}) / #{ret}"
                end
        end
        
        def transfer_all_from
                while @landed_airport.waiting.length > 0
                        if room_left
                                transfer_from @landed_airport.waiting.sort{ |one, other| (other.age <=> one.age) }[0]
                        else
                                @sim.addLog "Plane full with #{@landed_airport.waiting.length} left to load."
                                break
                        end                
                end
        end
        
        def transfer_from(pax)
                if @landed_airport
                        @landed_airport.waiting.delete pax
                        @passengers << pax
                        pax.load(self)
                        @total_pax += 1
                        @sim.addLog "LOAD at #{@landed_airport} to #{pax.target} (#{pax.name})"
                end
        end
  
        def room_left 
                if @exclusive.to_i == 1 and passengers.length > 0
                        false
                else
                        passengers.length < pax
                end
        end
  
        def land(airport)
                @total_hops += 1
                if @last_airport
                        flight_time = (@sim.time - @takeoff_time) / 3600.0
                        @total_time = @total_time + flight_time
                        @total_cost = @total_time * @long_cost
                        @hops << [@sim.time, @last_airport, airport, @last_airport.distance_to(airport), @passengers.length, airport.waiting.length, @total_time, @total_cost, @total_hops, @total_pax]
                        @sim.addLog "LAND #{@tail} at #{airport} (#{flight_time})"
                else
                        @hops << [@sim.time, nil, airport, 0, @passengers.length, airport.waiting.length, @total_time, @total_cost, @total_hops, @total_pax]
                        @sim.addLog "LAND #{@tail} at #{airport}"
                end
                @last_airport = airport
                @landed_airport = airport
                transfer_to
                if @fail < 1
                        @takeoff_in = @loadTime
                end
                @position = airport.position.clone
                @velocity = 0
                @landed = true
                @target_airport = nil
        end

        def takeoff
                @sim.addLog "TAKEOFF #{@tail} from #{@landed_airport}"
                @landed = false
                @landed_airport = nil
                @takeoff_time = @sim.time
                @velocity = @speed/10
        end
        
        def move
                if !@landed and @angle != 0
                  wind = @sim.wind(@position)
                        cosA = Math.cos(@angle)
                        sinA = Math.sin(@angle)
                        @x -= (cosA * @velocity.to_f) + wind[0]
                        @y -= (sinA * @velocity.to_f) + wind[1]
                        #@sim.addLog "MOVE #{@x} #{@y} #{@angle} #{@velocity}"
                end
        end

        def sanity_check(sim, version)
                @sim = sim
                @dayschedule=[0.0,0,0,0,0,0] if @dayschedule.nil?
                @plane_failures = 0  if plane_failures.nil?
                @fail = 0 if @fail.nil?
                @loadTime = 5 if @loadTime.nil?
                @takeoff_in = @loadTime if @takeoff_in.nil?
                @hops = [] if @hops.nil?
                @passengers = [] if @passengers.nil?
                @total_time = 0 if @total_time.nil?
                @total_pax = 0 if @total_pax.nil?
                @total_hops = 0if @total_hops.nil?
        end

end