-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.rkt
1790 lines (1600 loc) · 73.4 KB
/
client.rkt
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#lang racket/gui
(require
racket/fixnum
mode-lambda
mode-lambda/static
mode-lambda/text
(prefix-in gl: mode-lambda/backend/gl)
pict
"defs.rkt"
"utils.rkt"
"change.rkt"
"physics.rkt"
"draw-utils.rkt"
"draw.rkt"
"pilot.rkt"
"pbolt.rkt"
"effect.rkt"
"ships.rkt"
"upgrade.rkt"
"dmg.rkt"
"order.rkt"
"missile.rkt"
"warp.rkt"
"explosion.rkt"
"plasma.rkt")
(provide start-client)
(define (start-client port
#:ip [ip #f]
#:name [name #f]
#:new-eventspace? (new-eventspace? #f)
#:gui? (gui? #t)
#:spacebox (sspace #f))
(server? #f)
(cond
(new-eventspace?
(thread
(lambda ()
(current-eventspace (make-eventspace))
(start-client* ip port name gui? sspace)
(yield 'wait))))
(else
(start-client* ip port name gui? sspace)
(yield 'wait))))
(define (start-client* ip port name gui? sspace)
(load-ships!)
(when (not ip)
(define prefip (get-preference 'warp:ip))
(if prefip
(set! ip prefip)
(set! ip "127.0.0.1")))
(when (not name)
(define prefname (get-preference 'warp:name))
(if prefname
(set! name prefname)
(set! name "Player")))
(define key-for #f)
; #f for not entering anything
; 'name for entering name
; 'ip for entering ip address
; set to #t while trying to connect to server
; - reset to #f when we fail to connect or disconnect
(define connecting? #f)
; shown on the intro screen, used for server connection problems
(define intromsg "Press ? for quick help")
(define help-screen? #f)
(define playing? #t)
(define server-in-port #f)
(define server-out-port #f)
(define server-in-t #f)
(define server-out-t #f)
(define meid #f) ; integer? or #f
(define ownspace #f)
(define my-stack #f)
(define buttons #f)
(define sprites #f)
(define active-mouse-tool (box #f))
(define in-hangar? #f)
(define hangshipid #f) ; id of a ship in the hangar we are maybe boarding or scrapping
(define last-pbolt-time 0) ; space-time of last pbolt fire
; if current faction is different from this, reset our view
(define last-faction #f)
(define pressed '())
; list of press structs from recently pressed buttons
(define (press! key)
(prepend! pressed (press key (current-milliseconds))))
(define holding '())
; if you are holding a key/mouse button (from a holdbutton?)
; this is a list of holds structs
(player-cleanup-client!
(lambda (pid)
(when (equal? pid meid)
(set! pressed '())
(for ((h (in-list holding)))
((hold-frelease h)))
(set! holding '()))))
; if the mouse is over something (other than a button) that when clicked will
; do some action, clickcmds is a lambda that returns a list of commands we send
(define clickcmds #f)
(define frames '()) ; list of last few frame times
(define aheads '()) ; list of last few ahead calculations
(define last-update-time #f) ; space-time of last update we got from server
(define showtab #f) ; tab toggles an overlay showing players on ships and orders
(define showplayers #f) ; 'p' toggles showing all players playing currently
(define showsector? #f) ; tilde toggles showing the whole sector or the regular view
(define zerocenter (pvobj 0.0 0.0))
(define center #f) ; updated each frame for the click handler and mouse cursor drawing
(define center-follow? #t) ; show player position in the center?
; when (not center-follow?)
; center of the screen when panning
(define centerxy (pvobj 0.0 0.0))
(define dragxypx '(0 . 0)) ; last xy of drag in pixels
(define dragstate "none") ; "none", "start", "drag"
(define scale-play 1.0) ; scale for zooming
(define first-scale #t)
; when a new scenario happens and you are not on a ship, set this to #t
; if you are put onto a ship and this is #t, set to #f and set scale-play to 1.0
; effectively, the "first" time you are put on a ship, zoom to normal scale
(define (min-scale)
(if ownspace
(min (/ canon-width (space-width ownspace) 1.25)
(/ canon-height (space-height ownspace) 1.25))
.01))
(define (max-scale) 30.0)
(define (set-scale z #:immediate? (imm? #f))
(define newz (clamp (min-scale) (max-scale) z))
(set! scale-play newz)
(when imm? (set! shown-scale scale-play)))
(define shown-scale scale-play) ; scale that is actually used
(define (get-scale) (if showsector? (min-scale) shown-scale))
(define (update-scale) ; move shown-scale towards the scale we want
(define diff (abs (- scale-play shown-scale)))
(if (shown-scale . < . scale-play)
(set! shown-scale (min scale-play (+ shown-scale (max 0.1 (* 0.4 diff)))))
(set! shown-scale (max scale-play (- shown-scale (max 0.1 (* 0.4 diff)))))))
(define frame #f)
(define canvas #f)
(when gui?
(define (in-button? buttons x y)
(for/first ((b (in-list buttons))
#:when (if (button-height b)
(and (<= (abs (- (button-x b) x)) (/ (button-width b) 2.0))
(<= (abs (- (button-y b) y)) (/ (button-height b) 2.0)))
(and (<= (sqrt (+ (* (- x (button-x b)) (- x (button-x b)))
(* (- y (button-y b)) (- y (button-y b)))))
(/ (button-width b) 2.0)))))
b))
(define (key-button? buttons key ctrl?)
(for/first ((b (in-list buttons))
#:when (and (equal? (button-key b) key)
(equal? (button-ctrl? b) ctrl?)
(not (member (button-draw b) '(disabled dmg)))))
b))
(define (click canvas event)
(define-values (x y) (screen->canon canvas (send event get-x) (send event get-y)))
(define b (in-button? buttons x y))
;(printf "click ~a ~a ~a\n" x y b)
(cond
(b
(when (not (member (button-draw b) '(disabled dmg)))
;(printf "clicked button ~v\nship is ~v\n" b (if my-stack (get-ship my-stack) #f))
((button-f b) (- x (button-x b)) (- y (button-y b)))
(cond
((holdbutton? b)
(prepend! holding (hold 'mouse (button-key b) (holdbutton-frelease b))))
(else
(press! (button-key b))))
))
(clickcmds
(send-commands (clickcmds #f)))))
(define (quit?)
(define ans (message-box/custom "Quit?" "Ready to Stop?"
"Quit" "Keep Playing" #f
frame '(default=2)))
(when (equal? 1 ans)
(drop-connection!)
(set! playing? #f)
(send frame show #f)))
(define (draw-screen canvas dc)
(define t1 0)
(define t2 0)
(define t3 0)
(define t4 0)
(define t5 0)
(define t6 0)
(define t7 0)
(define t8 0)
(define t9 0)
(set! buttons '())
(set! sprites '())
(define cursordrawn #f)
(set! clickcmds #f)
(define background-gray 0)
; changed to LAYER_MAP if we are inside a hangar or other ship
(define layer-ships LAYER_SHIPS)
(define layer-effects LAYER_EFFECTS)
; changed to LAYER_SHIPS and LAYER_EFFECTS if we are inside hangar or other ship
(define layer-hangar-back #f)
(define layer-hangar #f)
(update-scale)
(define shipcenter #f)
(define rcship #f)
(define topship #f)
(define myshipid #f)
(define me #f)
(define myfaction #f)
(when ownspace
(set! me (findfid meid (space-players ownspace)))
(set! myfaction (player-faction me))
(when (player-rcid me)
(set! rcship (find-id ownspace ownspace (player-rcid me)))))
(when my-stack
(set! topship (get-topship my-stack))
(set! shipcenter (or rcship topship))
(set! myshipid (ob-id shipcenter)))
(set! center
(cond ((or (not ownspace) showsector?)
zerocenter)
((and my-stack center-follow?)
shipcenter)
(center-follow?
zerocenter)
(else
centerxy)))
(when center-follow?
; record the center so we start from there if center-follow? becomes #f
(set-posvel-x! (obj-posvel centerxy) (obj-x center))
(set-posvel-y! (obj-posvel centerxy) (obj-y center)))
(cond
(help-screen?
; This screen shows quick help for the game
(define size (* 0.8 (min canon-width canon-height)))
(prepend! sprites (rect-outline csd 0.0 0.0 size size 3.5
LAYER_SHIPS))
(prepend! buttons (button 'normal 'back-to-game #f
0.0 (- (/ size 2.0) 33.0) 200.0 50.0
"Back to Game"
(lambda (x y)
(set! help-screen? #f))))
(define col (send the-color-database find-color "white"))
(prepend! sprites
(textr "Quick Help"
0.0 (+ (- (/ size 2.0)) 15.0) #:layer LAYER_UI
#:r (send col red)
#:g (send col green)
#:b (send col blue)))
(define help-lines
(list "ctrl-f toggles fullscreen"
"ctrl-q to quit"
""
"tab shows extra info (players on ships)"
"backtick toggles full map"
"p lists all players"
""
"r/t or mouse wheel zooms"
"right-click drag to pan view"
""
"a/w/d to fly"
"space or mouse to fire"))
(for ((t (in-list help-lines))
(i (in-naturals 2)))
(prepend! sprites
(text-sprite textr textsr t
(+ (- (/ size 2.0)) 8.0)
(+ (- (/ size 2.0)) 8.0 (* i 20))
LAYER_UI)))
)
((not ownspace)
(timeit t1
; This screen is where you type in your name and the server IP
;(define txt "O")
;(define-values (w h) (textsr txt))
;(for* ((i (in-range (left) (right) w))
; (j (in-range (top) (bottom) h)))
; (prepend! sprites (text-sprite textr textsr txt i j LAYER_UI)))
(prepend! sprites (sprite 0.0 0.0 (sprite-idx csd 'intro) #:layer LAYER_FOW_RADAR))
(define vtext (string-append "Version " (number->string VERSION)))
(define-values (w h) (textsr vtext))
(prepend! sprites
(text-sprite textr textsr vtext
(- (right) w 8) (+ (top) 4) LAYER_UI))
(prepend! sprites (text-sprite textr textsr "Name" -100.0 0.0 LAYER_UI))
(define name? (equal? key-for 'name))
(define nb (button (if name? 'disabled 'normal)
'name #f
0.0 35.0 200.0 26.0
(string-append name (if name? "_" ""))
(if name?
(lambda (x y) (void))
(lambda (x y) (set! key-for 'name)))))
(prepend! buttons nb)
(prepend! sprites (text-sprite textr textsr "Server IP" -100.0 55.0 LAYER_UI))
(define ip? (equal? key-for 'ip))
(define ipb (button (if ip? 'disabled 'normal)
'ip #f
0.0 90.0 200.0 26.0
(string-append ip (if ip? "_" ""))
(if ip?
(lambda (x y) (void))
(lambda (x y) (set! key-for 'ip)))))
(prepend! buttons ipb)
(define startb (button (if connecting? 'disabled 'normal) 'start #f
0.0 150.0 200.0 50.0
(if connecting? "Connecting..." "Connect")
(lambda (x y)
(put-preferences
(list 'warp:ip 'warp:name)
(list ip name))
(connect/async!))))
(prepend! buttons startb)
(define txts (string-split intromsg "\n"))
(for ((t (in-list txts))
(i (in-naturals)))
(prepend! sprites (text-sprite textr textsr t
-100.0 (+ 190.0 (* i 20)) LAYER_UI)))
))
(else
; we have ownspace
(timeit t2
(define ordertree (get-space-orders-for ownspace myfaction))
; background starts as fog of war gray
(set! background-gray 75)
(define fowlist '())
(cond
((equal? myfaction "Observer")
; sees everything via radar but later we special case so nothing ever disappears
(define r (* 2.0 (max (space-width ownspace)
(space-height ownspace))))
(define f (fow 0.0 0.0 r 0.0))
(prepend! fowlist f)
(define-values (x y) (xy->screen (fow-x f) (fow-y f) center (get-scale)))
; paint the whole map black including nebula
(prepend! sprites
(sprite x y (sprite-idx csd 'fow) #:layer LAYER_FOW_VISIBLE
#:m (* r (get-scale) (/ 50.0)))))
(else
(for ((s (in-list (space-objects ownspace))))
(cond
((and myfaction
(or (spaceship? s)
(spacesuit? s)
(probe? s))
((faction-check myfaction (ship-faction s)) . > . 0))
; for every friendly ship
(define radar (+ (ship-visible s) (* (obj-neb s) (- (ship-radar s) (ship-visible s)))))
(define f (fow (obj-x s) (obj-y s) radar (ship-visible s)))
(prepend! fowlist f)
(define-values (x y) (xy->screen (fow-x f) (fow-y f) center (get-scale)))
(prepend! sprites
(sprite x y (sprite-idx csd 'fow) #:layer LAYER_FOW_VISIBLE
#:m (* (fow-visible f) (get-scale) (/ 50.0))))
(prepend! sprites
(sprite x y (sprite-idx csd 'fow) #:layer LAYER_FOW_RADAR
#:m (* radar (get-scale) (/ 50.0))))
)
((nebula? s)
(define-values (x y) (xy->screen (obj-x s) (obj-y s) center (get-scale)))
(prepend! sprites
(sprite x y (sprite-idx csd 'fowneb)
#:layer LAYER_FOW_BLOCK
#:m (* (nebula-radius s) (get-scale) (/ 50.0))
#:r background-gray
#:g background-gray
#:b background-gray)))))))
)
(timeit t3
(prepend! sprites (draw-sector-lines csd center (get-scale) ownspace))
; map annotations
; order annotations
(when ordertree
(define a (+ 0.85 (* 0.15 (cycletri (space-time ownspace) 2000))))
(define bright (linear-color "blue" "blue" 1.0 a))
(define dim (linear-color "blue" "blue" 1.0 0.85))
(for-orders ordertree showtab
(lambda (ot depth highlight?)
(when (order? ot)
(for ((a (in-list (order-anns ot))))
(cond
((ann-circle? a)
(define col (if highlight? bright dim))
(prepend! sprites
(obj-sprite a csd center (get-scale) LAYER_MAP 'circle-outline
(/ (* 2.0 (ann-circle-radius a)) 104)
(send col alpha) 0.0 col))
(define-values (x y) (obj->screen a center (get-scale)))
(prepend! sprites (textr (ann-txt a)
x y #:layer LAYER_MAP #:a (send col alpha)
#:r (send col red)
#:g (send col green)
#:b (send col blue))))
((ann-ship? a)
(define st (find-stack ownspace ownspace (ann-ship-id a)))
(when st
(define s (get-topship st))
(define col (if highlight? bright dim))
(prepend! sprites (obj-sprite s csd center (get-scale)
LAYER_MAP 'target
(max (/ 35.0 110 (get-scale))
(/ (* 4.0 (ship-radius s)) 110))
(send col alpha) 0.0 col))))
(else
(error "don't know how to draw annotation ~v" a))))))))
)
;(set! sprites (cons (draw-background-stars csd center (get-scale) fowlist) sprites))
; draw stuff specific to the ship you are on
; - stacked if we are on a ship inside another ship
(timeit t5
(when my-stack
(define ship (get-ship my-stack))
(when (and in-hangar? (not (ship-hangar ship)))
; just to make sure, maybe our ship had a hangar and it went away?
(set! in-hangar? #f)
(set! hangshipid #f))
(when (or in-hangar? (not (ship-flying? ship)))
(set! layer-ships LAYER_MAP)
(set! layer-effects LAYER_MAP)
(set! layer-hangar-back LAYER_SHIPS)
(set! layer-hangar LAYER_EFFECTS))
(cond
(in-hangar?
(define factory (ship-tool ship 'factory))
(define hshipsw (map (lambda (s) (ship-w s 1.0))
(ship-hangar ship)))
(define fships (if factory
(cadr (tool-val factory))
(list)))
(define fshipsw (map (lambda (s) (ship-w s 1.0))
fships))
(define shipmax (+ 28 (* 2.0 (apply max 1.0 (append hshipsw fshipsw)))))
(define labelspace 20.0)
(define hw (max shipmax (* 0.5 canon-width)))
(define fh (+ shipmax labelspace))
(define hh (max shipmax (* 0.5 canon-height)))
(define hy (+ (/ fh 2.0) labelspace))
(define fy (- hy labelspace (/ hh 2.0) (/ fh 2.0) 10.0))
(when factory
(define fw (max hw (* shipmax (length fships))))
(prepend! sprites (rect-filled csd 0.0 fy fw fh
layer-hangar-back
#:r 0 #:g 0 #:b 0
#:a 0.75))
(define txt "Factory Materials ")
(prepend! sprites
(text-sprite textr textsr txt
(- (/ fw 2.0))
(+ fy (- (/ fh 2.0)) 2.0)
LAYER_UI))
(let-values (((w h) (textsr txt)))
(prepend! sprites
(text-sprite textr textsr
(number->string (car (tool-val factory)))
(+ (- (/ fw 2.0)) w)
(+ fy (- (/ fh 2.0)) 2.0)
LAYER_UI 1.0 (make-color 0 255 0 1.0))))
(for ((s (in-list fships))
(i (in-naturals)))
(define x (+ (* -0.5 fw) (* i shipmax) (/ shipmax 2)))
(define y (+ fy (/ labelspace 2.0)))
(define sym (string->symbol (ship-type s)))
(prepend! sprites (sprite x y (sprite-idx csd sym)
#:layer layer-hangar #:theta (- pi/2)
#:m (/ (exact->inexact (ship-sprite-size s))
(sprite-size csd sym))))
(prepend! sprites (draw-tool-icons csd textr textsr s x y shipmax layer-hangar))
(define afford? ((ship-price s) . <= . (car (tool-val factory))))
(prepend! sprites (textr (number->string (ship-price s))
x (+ y (/ shipmax 2) -16.0)
#:layer layer-hangar
#:r 255
#:g (if afford? 255 0)
#:b (if afford? 255 0)))
(define b (button 'outline (ob-id s) #f
x y (- shipmax 8.0) (- shipmax 8.0) ""
(lambda (x y)
(when afford?
(send-commands (command meid
(player-cmdlevel me)
'factory
(ob-id s)))))))
(prepend! buttons b))
)
; hangar background
(prepend! sprites (rect-filled csd 0.0 hy hw hh
layer-hangar-back
#:r 0 #:g 0 #:b 0
#:a 0.75))
(prepend! sprites (rect-outline csd 0.0 hy hw hh 3.5
layer-hangar-back))
(prepend! sprites
(text-sprite textr textsr "Hangar"
(- (/ hw 2.0))
(- hy (/ hh 2.0) labelspace 4.0)
LAYER_UI))
(define hangship (for/first ((s (in-list (ship-hangar ship)))
#:when (equal? hangshipid (ob-id s)))
s))
(when (not hangship)
; maybe the ship we were looking at left?
(set! hangshipid #f))
; draw all the ships in the hangar
(define num-in-row (max 1 (inexact->exact (floor (/ hw shipmax)))))
(for ((s (in-list (ship-hangar ship)))
(i (in-naturals)))
(define x (+ (* -0.5 hw)
(* (remainder i num-in-row) shipmax)
(/ shipmax 2)))
(define y (+ hy
(* -0.5 hh)
(* (quotient i num-in-row) shipmax)
(/ shipmax 2)))
(define sym (string->symbol (ship-type s)))
(prepend! sprites (sprite x y (sprite-idx csd sym)
#:layer layer-hangar #:theta (- pi/2)
#:m (/ (exact->inexact (ship-sprite-size s))
(sprite-size csd sym))
#:r (get-red ownspace s)))
(prepend! sprites (draw-tool-icons csd textr textsr s x y shipmax layer-hangar))
(define w (ship-w s 1.0))
(prepend! sprites (draw-hp-bar s x y w csd layer-hangar 1.0))
(when (and hangship (equal? hangshipid (ob-id s)))
; focused on this ship
(define by (- y (/ (- shipmax 40.0) 2.0) -4.0))
(define w 80.0)
(define scrapb (button (if (null? (ship-playerids s)) 'normal 'disabled)
'scrap #f
(- x (/ shipmax 2.0) (/ w 2.0) -4.0) by
w 40.0 "Scrap"
(lambda (x y)
(send-commands (command meid
(player-cmdlevel me)
'factory
(ob-id s))))))
(when (and factory (ship-price s))
(prepend! buttons scrapb))
(define boardb (button 'normal 'board #f
(+ x (/ shipmax 2.0) (/ w 2.0) -4.0) by
w 40.0 "Board"
(lambda (x y)
(send-commands (chmov meid (ob-id s) #f)))))
(prepend! buttons boardb)
(define players (find-all ownspace s player?))
(for ((i (modulo (debug-num) 10)))
(append! players (player -1 (~a "player" i) "fac1" -1 '() #f #f)))
(define tw 0.0)
(for ((p (in-list players))
(i (in-naturals)))
(define-values (pw ph) (textsr (player-name p)))
(set! tw (max tw pw))
(prepend! sprites (text-sprite textr textsr (player-name p)
(+ x (/ shipmax 2) 4.0)
(+ by 28.0 (* i 20))
LAYER_UI_TEXT)))
(when (tw . > . 0.0)
(set! tw (+ tw 16.0))
(define th (+ (* 20.0 (length players)) 16.0))
(prepend! buttons (button 'disabled 'players #f
(+ x (/ shipmax 2) (/ tw 2.0) -4.0)
(+ by 20.0 (/ th 2.0))
tw th ""
values)))
)
(when (and factory (ship-price s))
(prepend! sprites (textr (number->string (ship-price s))
x (+ y (/ shipmax 2) -16.0)
#:layer layer-hangar
#:r 255 #:g 255 #:b 255)))
(define b (button 'outline (ob-id s) #f
x y (- shipmax 8.0) (- shipmax 8.0) ""
(lambda (x y)
(if (equal? hangshipid (ob-id s))
(set! hangshipid #f)
(set! hangshipid (ob-id s))))))
(append! buttons b) ; put these behind the scrap and board buttons
)
; button to deselect any hangship if you click anywhere in the hangar
(define backb (button 'hidden 'hangar #f
0.0 hy hw hh ""
(lambda (x y)
(set! hangshipid #f))))
(append! buttons backb))
((not (ship-flying? ship))
; our ship is inside another
; draw black circle on top of topship
(prepend! sprites (obj-sprite topship csd center (get-scale)
layer-hangar-back 'circle
(/ (* 2.2 (+ 1.0 (ship-radius topship))) 100)
0.75 0.0 (make-color 0 0 0 1.0)))
; draw our ship inside black circle
(define-values (x y) (obj->screen topship center (get-scale)))
(define sym (string->symbol (ship-type ship)))
(prepend! sprites (sprite x y (sprite-idx csd sym)
#:layer layer-hangar #:theta (- pi/2)
#:m (* (get-scale)
(/ (ship-sprite-size ship)
(sprite-size csd sym)))
#:r (get-red ownspace ship)))
(define w (ship-w ship (get-scale)))
(prepend! sprites (draw-hp-bar ship x y w csd layer-hangar 1.0))))
; draw ship UI
(prepend! sprites (draw-ship-hp csd textr center (get-scale) my-stack))
; draw tool UI
(when (not in-hangar?)
(define tools (filter tool-visible? (ship-tools (or rcship ship))))
(for ((t (in-list tools)))
(define-values (bs ss)
(draw-tool-ui csd center (get-scale) ownspace meid (or rcship ship)
t my-stack send-commands active-mouse-tool last-pbolt-time))
(prepend! buttons bs)
(prepend! sprites ss)))
(when (and (not rcship) (ship-hangar ship))
(define b (holdbutton 'normal #\h #f (- (right) 206) (- (bottom) 28) 140 40
(~a "Hangar [h] " (length (ship-hangar ship)))
(lambda (x y)
(set! in-hangar? #t)
(set! hangshipid #f))
void))
(when in-hangar?
(set-button-label! b "Exit Hangar [h]")
(set-button-f! b (lambda (x y)
(set! in-hangar? #f)
(set! hangshipid #f))))
(prepend! buttons b))
; tool overlay
; XXX when we have dock on
;(dock? t)
;(when (dock-on t)
;(draw-docking csd center (get-scale) (get-space my-stack) my-stack)))
) ; when my-stack
)
(timeit t4
(for ((o (in-list (space-objects ownspace)))
#:when (and (obj-alive? o) (obj-posvel o)))
(define r (obj-radius ownspace o #t))
(when r
(define fowa (get-alpha o r fowlist))
(when (equal? myfaction "Observer")
(set! fowa (max fowa 0.5)))
(when (equal? (ob-id o) myshipid)
(set! fowa (max fowa 0.5)))
(when (fowa . > . 0)
(define spr (draw-object csd textr textsr center (get-scale) o ownspace myshipid
showtab fowa myfaction layer-ships layer-effects))
(prepend! sprites spr))))
)
; draw annotations that exist in canon space
(timeit t6
(for ((a (in-list (space-objects ownspace)))
#:when (and (ann? a)
(or showtab (not (ann-tab? a)))
(or (equal? (ann-faction a) #t)
(and CLIENT_SPECIAL? (not (ann-faction a)))
(equal? (ann-faction a) myfaction))))
(define-values (x y)
(case (obj-dr a)
((topleft)
(values (+ (left) (obj-x a)) (+ (top) (obj-y a))))
((center)
(values (obj-x a) (obj-y a)))
((space)
(xy->screen (obj-x a) (obj-y a) center (get-scale)))
(else
(error "ann had unrecognized posvel-dr"))))
(when (ann-button? a)
(define ab (button 'normal (ob-id a) #f
x y
(obj-dx a) (obj-dy a) (ann-txt a)
(lambda (k y) (send-commands (anncmd meid (ob-id a))))))
(prepend! buttons ab))
(when (ann-anim? a)
(define frame (quotient (obj-age ownspace a) (ann-anim-delay a)))
(define sym (anim-frame-sym (ann-txt a) frame))
(prepend! sprites (sprite x y (sprite-idx csd sym) #:layer LAYER_UI)))
(when (ann-text? a)
(define z
(cond
((ann-text-life a)
(linear-fade (obj-age ownspace a)
(ann-text-life a)
(ann-text-gone a)))
(else 1.0)))
(define txts (string-split (ann-txt a) "\n"))
(for ((t (in-list txts))
(i (in-naturals)))
(cond
((equal? 'space (obj-dr a))
(prepend! sprites (textr t x (+ y (* i 20))
#:layer LAYER_UI_TEXT
#:mx (get-scale) #:my (get-scale)
#:r 255 #:g 255 #:b 255 #:a z)))
(else
(prepend! sprites (text-sprite textr textsr t
x (+ y (* i 20))
LAYER_UI_TEXT z)))))))
(when showplayers
; list all players by faction
(define players (space-players ownspace))
(for ((i (modulo (debug-num) 10)))
(append! players (player -1 (~a "player" i) "team" -1 '() #f #f)))
(define factions '())
(define h (make-hash))
(for ((p (in-list players)))
(when (not (member (player-faction p) factions))
(set! factions (cons (player-faction p) factions)))
(hash-set! h (player-faction p)
(cons (player-name p) (hash-ref h (player-faction p) '()))))
(set! factions (sort factions string<?))
(define str (format "~a Players" (length players)))
(prepend! sprites (text-sprite textr textsr str
-200 (+ (top) 100) LAYER_UI))
(for (((fact names) (in-hash h))
(i (in-naturals)))
(prepend! sprites (text-sprite textr textsr fact
(+ -200 (* 150 i)) (+ (top) 100 30) LAYER_UI))
(for ((name names)
(k (in-naturals 1)))
(prepend! sprites (text-sprite textr textsr name
(+ -200 (* 150 i)) (+ (top) 100 40 (* k 20))
LAYER_MAP)))))
; draw orders
(define line 0)
(when ordertree
(define lefte (+ (left) 150))
(define tope (top))
(for-orders ordertree showtab
(lambda (ot depth highlight?)
(when showtab
(define color (send the-color-database find-color (if (ord-done? ot) "green" "red")))
(prepend! sprites (xy-sprite (+ lefte (* 10 depth) 5) (+ tope (* 20 line) 10)
csd (get-scale) LAYER_UI 'circle-small
(/ 0.5 (get-scale))
1.0 0.0 color)))
(define color (send the-color-database find-color (if highlight? "white" "gray")))
(define txt (ord-text ot))
(when (and (ordertime? ot) (string-contains? txt "~a"))
(define secleft (ceiling (/ (- (ordertime-subtotal ot)
(- (space-time ownspace) (ordertime-start ot)))
1000)))
(define-values (min sec) (quotient/remainder secleft 60))
(set! txt (format (ord-text ot)
(~a (~a min #:min-width 2 #:align 'right
#:pad-string "0")
":"
(~a sec #:min-width 2 #:align 'right
#:pad-string "0")))))
(prepend! sprites (text-sprite textr textsr txt
(+ lefte 12 (* 10 depth)) (+ tope (* 20 line))
LAYER_UI 1.0 color))
(set! line (+ line 1)))))
(when (not my-stack)
(define start-ships
(find-all ownspace ownspace (lambda (o)
(and (ship? o)
(ship-flying? o)
(ship-start o)
(equal? myfaction (ship-faction o))))))
(when (not myfaction)
(prepend! sprites (textr "Waiting for faction assignment..."
0.0 0.0 #:layer LAYER_UI
#:r 255 #:g 255 #:b 255)))
(for ((s (in-list start-ships))
(i (in-naturals)))
(define x (+ (left) 100 (* (remainder i 3) 250)))
(define y (+ (top) 30 (* (quotient i 3) 60)))
(define b (button 'normal (ob-id s) #f x y 200 30
(format "~a" (ship-name s))
(lambda (x y)
; leaving sector overview, so center on ship and reset scale
(set-scale 1.0)
(set! center-follow? #t)
(send-commands (chmov meid (ob-id s) #f)))))
(prepend! buttons b)))
; draw game UI
; zoom scale
(when (not showsector?)
(define zw 20)
(define zh 150)
(define zcx (- (right) 16 (/ zw 2)))
(define zcy (+ (top) 60 (/ zh 2)))
(prepend! sprites (sprite zcx (+ zcy (- (/ zh 2))) (sprite-idx csd '20x2)
#:layer LAYER_UI
#:r (send zoomcol red)
#:g (send zoomcol green)
#:b (send zoomcol blue)))
(prepend! sprites (sprite zcx (+ zcy (/ zh 2)) (sprite-idx csd '20x2)
#:layer LAYER_UI
#:r (send zoomcol red)
#:g (send zoomcol green)
#:b (send zoomcol blue)))
(prepend! sprites (sprite zcx zcy (sprite-idx csd '2x150)
#:layer LAYER_UI
#:r (send zoomcol red)
#:g (send zoomcol green)
#:b (send zoomcol blue)))
(define zfrac (/ (- (log scale-play) (log (min-scale)))
(- (log (max-scale)) (log (min-scale)))))
(prepend! sprites (sprite zcx (+ zcy (/ zh 2) (- (* zfrac zh)))
(sprite-idx csd '20x2)
#:layer LAYER_UI
#:r (send zoomcol red)
#:g (send zoomcol green)
#:b (send zoomcol blue)))
(define zbutton (button 'hidden 'zoom #f zcx zcy zw zh "Zoom"
(lambda (x y)
(define zfracy (/ (+ (/ zh 2) (- y)) zh))
(define z (exp (+ (log (min-scale))
(* zfracy (- (log (max-scale))
(log (min-scale)))))))
(set-scale z))))
(define zkeyb (button 'hidden #\r #f 0 0 0 0 "Zoom In"
(lambda (k y) (set-scale (* scale-play 1.1)))))
(define xkeyb (button 'hidden #\t #f 0 0 0 0 "Zoom Out"
(lambda (k y) (set-scale (/ scale-play 1.1)))))
(prepend! buttons zbutton zkeyb xkeyb))
; auto-center button
(define acby (+ (top) 60))
(define acbw 180)
(define acbh 40)
(cond
(showsector?
(prepend! buttons (button 'normal 'leave-sector-map #f 0.0 acby acbw acbh
"Leave Sector Map"
(lambda (x y) (set! showsector? #f)))))
((not center-follow?)
(prepend! buttons (button 'normal #\z #f 0.0 acby acbw acbh "Auto Center [z]"
(lambda (x y) (set! center-follow? #t))))))
(cond
((not my-stack)
; nothing to leave
)
(in-hangar?
; the hangar button turns into an exit button
)
((spacesuit? (get-ship my-stack))
; dying
(define b (button 'normal 'restart #f
0.0 (- (bottom) 124) 100 40 "Respawn"
(lambda (x y)
(set! center-follow? #t) ; sector/ship centered
(send-commands (chmov meid #f 'restart)))))
(prepend! buttons b))
((and (player-rcid (car my-stack))
(find-id ownspace ownspace (player-rcid (car my-stack))))
; remote controlling something
)
((ship-flying? (get-ship my-stack))
; jumping ship
(define b (button 'normal 'jump #f
(+ (left) 58) (+ (top) 28) 100 40 "Jump"
(lambda (x y)
(send-commands (chmov meid #f 'jump)))))
(prepend! buttons b))
(else
; leaving this ship into mothership
(define ms (cadr (get-ships my-stack)))
(define b (button 'normal 'escape #f
0.0 (- (bottom) 76) 200 40
(string-append "Exit to " (ship-name ms))
(lambda (x y)
(send-commands (chmov meid (ob-id ms) #f)))))
(prepend! buttons b)))
; draw mouse cursor and green corners
(when my-stack
(define player (car my-stack))
(define ship (or rcship (get-ship my-stack)))
(define-values (p mods) (get-current-mouse-state))
(define-values (wx wy) (send canvas screen->client
(+ (send p get-x) left-inset)
(+ (send p get-y) top-inset)))
(define-values (x y) (screen->canon canvas wx wy))
(when (and (not (in-button? buttons x y))
(not in-hangar?))
(define-values (mx my) (obj->screen shipcenter center (get-scale)))
(define a (angle-norm (atan0 (- my y) (- x mx))))
(cond
#;((or (and mt (find-id ownspace ownspace (mtube-mid mt)))
(and pt (find-id ownspace ownspace (ptube-pid pt)))
(and st (tool-online? st)))
(set! cursordrawn #t)
(set! clickcmds (command
(cond (mt (mtube-mid mt))
(pt
(define probe (find-id ownspace ownspace
(ptube-pid pt)))
(ob-id (findf steer?
(pod-tools (car (ship-pods probe))))))
(else (ob-id st)))
a))
(prepend! sprites (sprite (exact->inexact x) (exact->inexact y)
(sprite-idx csd 'arrowhead) #:layer LAYER_UI_TEXT
#:theta (- a) #:b 150)))
((and (equal? 'pbolt (unbox active-mouse-tool))
(ship-tool ship 'pbolt)
(ship-flying? ship)
(not (warping? ship)))
(set! cursordrawn #t)
(define t (space-time ownspace))
(define cmd (command meid (player-cmdlevel player) 'pbolt