/
client.rkt
1411 lines (1252 loc) · 57.6 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 ip port name
#:new-eventspace? (new-eventspace? #f)
#:gui? (gui? #t)
#:spacebox (sspace #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)
(server? #f)
(define key-for #f)
; #f for not entering anything
; 'name for entering name
; 'ip for entering ip address
(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? (box #f))
(define last-pbolt-time 0) ; space-time of last pbolt fire
(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 and goals
(define showsector? #f) ; tilde toggles showing the whole sector or the regular view
(define zerocenter (obj #f #f #f (posvel #f 0.0 0.0 #f #f #f #f)))
(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 (obj #f #f #f (posvel #f 0.0 0.0 #f #f #f #f)))
(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 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)))))
(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)
(timeit t1
(set! buttons '())
(set! sprites '())
(define cursordrawn #f)
(set! clickcmds #f)
(update-scale)
(set! center
(cond ((or (not ownspace) showsector?)
zerocenter)
((and my-stack center-follow?)
(get-center ownspace my-stack))
(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)))
(when (not ownspace)
; 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_BLACK))
(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 server-in-port 'disabled 'normal) 'start #f
0.0 150.0 200.0 50.0
(if server-in-port "Connecting..." "Connect")
(lambda (x y)
(connect!))))
(prepend! buttons startb)
)
)
(when ownspace
(timeit t2
(define p (findfid meid (space-players ownspace)))
(define fac (if p (player-faction p) #f))
(define ordertree (get-space-orders-for ownspace fac))
(define myshipid #f)
(when my-stack
(define rcship (if (player-rcid p)
(find-id ownspace ownspace (player-rcid p)) #f))
(define topship (or rcship (get-topship my-stack)))
(set! myshipid (ob-id topship)))
; background starts as fog of war gray
; put a black circle wherever we can see
(define fowlist
(for/list ((s (in-list (space-objects ownspace)))
#:when (and fac
(or (spaceship? s)
(spacesuit? s)
(probe? s))
((faction-check fac (ship-faction s)) . > . 0)))
(list (obj-x s) (obj-y s) (ship-radar s))))
(for ((f fowlist))
(define rad (caddr f))
(define-values (x y) (xy->screen (car f) (cadr f) center (get-scale)))
(prepend! sprites (sprite x y (sprite-idx csd 'circle) #:layer LAYER_FOW_BLACK
#:m (* rad (get-scale) (/ 50.0)))))
)
(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))
(timeit t4
(for ((o (in-list (space-objects ownspace)))
#:when (obj-alive? o))
(define fowa (if (obj-posvel o) (get-alpha (obj-x o) (obj-y o) fowlist) 1.0))
(when (fowa . > . 0)
(define spr (draw-object csd textr textsr center (get-scale) o ownspace myshipid
showtab fowa fac))
(prepend! sprites spr)))
)
; 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 p (car my-stack))
(define rcship (if (player-rcid p) (find-id ownspace ownspace (player-rcid p)) #f))
(define ship (get-ship my-stack))
(define topship (get-topship my-stack))
(when (and (unbox in-hangar?) (not (ship-hangar ship)))
; just to make sure, maybe our ship had a hangar and it went away?
(set-box! in-hangar? #f))
(cond
((unbox in-hangar?)
; hangar background
(define size (* 0.8 (min canon-width canon-height)))
(prepend! sprites (rect-filled csd 0.0 0.0 size size
#:layer LAYER_HANGAR_BACKGROUND
#:r 0 #:g 0 #:b 0
#:a 0.75))
(prepend! sprites (rect-outline csd 0.0 0.0 size size 3.5
#:layer LAYER_HANGAR_BACKGROUND))
; draw all the ships in the hangar
(define shipmax (+ 20 (* 2.0 (apply max 1.0 (map (lambda (s) (ship-w s 1.0))
(ship-hangar ship))))))
(define buf 8)
(define numfit (max 1 (inexact->exact (floor (/ size (+ shipmax buf))))))
(for ((s (in-list (ship-hangar ship)))
(i (in-naturals)))
(define x (+ (* -0.5 size)
(* (quotient i numfit) (+ shipmax buf))
(/ shipmax 2)))
(define y (+ (* -0.5 size)
(* (remainder i numfit) (+ shipmax buf))
(/ 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)))
(define w (ship-w s 1.0))
(prepend! sprites (draw-hp-bar s x y w csd LAYER_HANGAR 1.0))
(define b (button 'outline (ob-id s) #f
x y shipmax shipmax ""
(lambda (x y)
(send-commands (chmov meid (ob-id s) #f)))))
(prepend! buttons b)
(define players (find-all ownspace s player?))
(for ((i (modulo (debug-num) 10)))
(append! players (player -1 (~a "player" i) "fac1" -1 '() #f #f)))
(for ((p (in-list players))
(i (in-naturals)))
(prepend! sprites (text-sprite textr textsr (player-name p)
(+ x (/ shipmax 2) buf)
(+ y (- (/ shipmax 2)) (* i 20))
LAYER_UI)))))
((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_BACKGROUND 'circle
(/ (* 2.2 (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 (unbox 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 (button 'normal #\h #f (- (right) 206) (- (bottom) 28) 140 40
(~a "Hangar [h] " (length (ship-hangar ship)))
(lambda (x y)
(set-box! in-hangar? #t))))
(when (unbox in-hangar?)
(set-button-label! b "Exit Hangar [h]")
(set-button-f! b (lambda (x y) (set-box! in-hangar? #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
)
; 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 (not (ann-faction a))
(equal? (ann-faction a) fac))))
(when (ann-button? a)
(define ab (button 'normal (ob-id a) #f
(+ (left) (obj-x a)) (+ (top) (obj-y a))
(obj-dx a) (obj-dy a) (ann-txt a)
(lambda (k y) (send-commands (anncmd meid (ob-id a))))))
(prepend! buttons ab))
(when (ann-text? a)
(define z
(cond
((ann-text-life a)
(linear-fade (obj-age ownspace a)
(ann-text-life a)
(+ (ann-text-life a) MSG_FADE_TIME)))
(else 1.0)))
(define txts (string-split (ann-txt a) "\n"))
(for ((t (in-list txts))
(i (in-naturals)))
(prepend! sprites (text-sprite textr textsr t
(obj-x a) (+ (obj-y a) (* i 20))
LAYER_UI_TEXT z)))))
(when showtab
; list all players
(define str (format "Players (~a):" (length (space-players ownspace))))
(prepend! sprites (text-sprite textr textsr str
200 (+ (top) 80) LAYER_UI_TEXT))
(for ((p (in-list (space-players ownspace)))
(i (in-naturals)))
(define str (if (player-faction p)
(~a (player-name p) " " (player-faction p))
(player-name p)))
(prepend! sprites (text-sprite textr textsr str
200 (+ (top) 100 (* i 20)) LAYER_UI_TEXT))))
; 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
(/ 0.05 (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_TEXT 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? fac (ship-faction o))))))
(when (not fac)
(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-play 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) 70))
(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
)
((unbox 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) 28) 80 40 "Restart"
(lambda (x y)
(set! center-follow? #t) ; sector/ship centered
(send-commands (chmov meid #f #f)))))
(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 #f)))))
(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) 28) 160 40 "Exit to Mothership"
(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 rcship (if (player-rcid player)
(find-id ownspace ownspace (player-rcid player)) #f))
(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 (unbox in-hangar?)))
(define mypos (get-center ownspace my-stack))
(define-values (mx my) (obj->screen mypos 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 cmds (command meid (player-cmdlevel player) 'pbolt
(list a a
(pbolt-frac last-pbolt-time (space-time ownspace)))))
(set! clickcmds (lambda ()
(set! last-pbolt-time (space-time ownspace))
cmds))
(prepend! sprites (sprite (exact->inexact x) (exact->inexact y)
(sprite-idx csd 'target) #:layer LAYER_UI_TEXT
#:r 100 #:g 100 #:b 255 #:m 0.7))))))
(prepend! buttons
(button 'hidden #\tab #f 0 0 0 0 "Mission Info"
(lambda (k y) (set! showtab (not showtab))))
(button 'hidden #\` #f 0 0 0 0 "Show Sector"
(lambda (k y) (set! showsector? (not showsector?)))))
; messages
(when ownspace
(define max 10)
(define num 0)
(let loop ((l (space-objects ownspace)))
(when (and (not (null? l)) (num . < . max))
(when (message? (car l))
(define m (car l))
(set! num (+ num 1))
(define z (linear-fade (obj-age ownspace m) (/ MSG_FADE_TIME 2) MSG_FADE_TIME))
(prepend! sprites (text-sprite textr textsr (message-msg m)
(+ (left) 5.0) (+ (top) 100.0 (* num 20))
LAYER_UI_TEXT z)))
(loop (cdr l)))))
; debugging
(when (and sspace (unbox sspace))
(for ((o (space-objects (unbox sspace)))
#:when (ship? o))
(prepend! sprites
(obj-sprite o csd center (get-scale) LAYER_UI_TEXT 'circle
(/ 7.0 100 (get-scale)) 1.0 0.0 "pink"))))
)
) ; when ownspace
; normally quit with ctrl-q
(define quit-button
(button 'hidden #\q #t
0 0 0 0 "Quit"
(lambda (x y)
(define ans (message-box/custom "Quit?" "Done Playing?"
"Quit" "Keep Playing" #f
frame '(default=2)))
(when (equal? 1 ans)
(drop-connection "clicked exit")
(set! playing? #f)
(send frame show #f)))))
(when (or (not ownspace)
(not my-stack)
(spacesuit? (get-ship my-stack)))
; if we are not in the game, show the button
(set-button-draw! quit-button 'normal)
(set-button-x! quit-button (- (right) 48))
(set-button-y! quit-button (- (bottom) 28))
(set-button-width! quit-button 80)
(set-button-height! quit-button 40))
(prepend! buttons quit-button)
(timeit t7
(send canvas set-cursor (make-object cursor% (if cursordrawn 'blank 'arrow)))
; framerate
(when ((length frames) . > . 1)
(define start (list-ref frames (- (length frames) 1)))
(define end (first frames))
(define span (/ (- end start) 1000))
(define txt (format "FPS: ~a" (round (/ (- (length frames) 1) span))))
(prepend! sprites (text-sprite textr textsr txt (- (right) 70) (+ (top) 30) LAYER_UI)))
; network issues?
(define ma (apply max 0 aheads))
(when (ma . > . AHEAD_THRESHOLD)
(define txt (format "Ahead: ~a" ma))
(prepend! sprites (text-sprite textr textsr txt (- (right) 200) (top) LAYER_UI)))
(prepend! sprites (button-sprites csd textr buttons
(if ownspace (space-time ownspace) 0)
holding pressed))
(define-values (dmgx dmgy)
(cond (my-stack
(define ship (get-ship (reverse my-stack)))
(define d (ship-dmgfx ship))
(values (random-between (- d) d)
(random-between (- d) d)))
(else
(values 0.0 0.0))))
(define width (+ canon-width dmgx))
(define height (+ canon-height dmgy))
(define layers (for/vector ((i LAYER_NUM)) (layer width height)))
)
(timeit t8
(define rgb (if ownspace 80 0))
(define r (render layers '() sprites #:r rgb #:g rgb #:b rgb))
)
(timeit t9
(r (fl->fx canon-width) (fl->fx canon-height) dc)
)
;(outputtime "client render"
; (if ownspace (space-time ownspace) #f)
; t1 t2 t3 t4 t5 t6 t7 t8 #;t9)
)
(define-values (left-inset top-inset) (get-display-left-top-inset))
(define-values (screen-w screen-h) (get-display-size #t))
;(printf "insets ~a ~a size ~a ~a\n" left-inset top-inset screen-w screen-h)
(define frame (new frame%
(label "Warp")))
(define (zoom-mouse z)
(define old-scale (get-scale))
(set-scale (* scale-play z) #:immediate? #t)
(when (not center-follow?)
; zoom around mouse pointer
(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))
(define-values (ox oy) (canon->space center old-scale x y))
(define-values (mx my) (canon->space center (get-scale) x y))
; adjust centerxy so ox,oy will display at mx,my
(set-posvel-x! (obj-posvel centerxy) (- (obj-x centerxy) (- mx ox)))
(set-posvel-y! (obj-posvel centerxy) (- (obj-y centerxy) (- my oy)))))
(define my-canvas%
(class canvas%
(super-new)
(define/override (on-size w h)
(printf "canvas on-size ~a ~a\n" w h)
(when (odd? w) (set! w (- w 1)))
(when (odd? h) (set! h (- h 1)))
(set-canon-width! (fx->fl w))
(set-canon-height! (fx->fl h))
(set! render (gl:stage-draw/dc csd
(fl->fx canon-width)
(fl->fx canon-height)
LAYER_NUM)))
(define/override (on-event event)
(case (send event get-event-type)
((left-down)
(click this event))
((left-up)
(define h (for/first ((h (in-list holding))
#:when (equal? 'mouse (hold-held h)))
h))
(when h ((hold-frelease h))) ; run the release function
(set! holding (remove h holding))) ; cancel hold
((right-down)
(when (not showsector?)
(set! dragstate "start")
(set! dragxypx (cons (send event get-x) (send event get-y)))))
((motion)
(when (and (send event dragging?) (send event get-right-down))
(when (or (and (equal? dragstate "start")
(or ((abs (- (send event get-x) (car dragxypx))) . > . 3)
((abs (- (send event get-y) (cdr dragxypx))) . > . 3)))
(equal? dragstate "drag"))
(set! dragstate "drag")
(set! center-follow? #f)
(define dx (/ (- (send event get-x) (car dragxypx)) (get-scale)))
(define dy (/ (- (send event get-y) (cdr dragxypx)) (get-scale)))
(set-posvel-x! (obj-posvel centerxy) (- (obj-x centerxy) dx))
(set-posvel-y! (obj-posvel centerxy) (+ (obj-y centerxy) dy))
(set! dragxypx (cons (send event get-x) (send event get-y))))))
((right-up)
(set! dragstate "none"))
))
(define/override (on-char event)
(define kc (send event get-key-code))
(define b (key-button? buttons kc (send event get-control-down)))
(define h (for/first ((h (in-list holding))
#:when (equal? kc (hold-key h)))
h))
;(printf "on-char ~v ~v ~v\n" kc b h)
(cond
((not kc)
;(printf "got #f for on-char get-key-code\n")
)
(h
; repeated keypress from holding the key down, drop it
)
((equal? kc 'release)
(set! kc (send event get-key-release-code))
(define h (for/first ((h (in-list holding))
#:when (equal? kc (hold-key h)))
h))
(when h
; released the key being held
((hold-frelease h)) ; run the release function
(set! holding (remove h holding)))) ; cancel the hold
(b
((button-f b) kc #f)
(cond
((holdbutton? b)
(prepend! holding (hold kc kc (holdbutton-frelease b)))
(when ((length holding) . > . 5)
(printf "holding ~a\n" (length holding))))
(else
(press! kc))))
(key-for
(define val (if (equal? key-for 'name) name ip))
(define escape? #f)
(case kc
((escape)
(set! escape? #t))
((#\backspace #\rubout)
(when ((string-length val) . > . 0)
(set! val (substring val 0 (- (string-length val) 1)))))
(else
(when (and (char? kc)
(member kc *ALL-ASCII*))
(set! val (string-append val (string kc))))))
(set! val (substring val 0 (min (string-length val) 30)))
(if (equal? key-for 'name)
(set! name val)
(set! ip val))
(when escape?
(set! key-for #f)))
(else
(case kc
((wheel-up)
(when (and ownspace (not showsector?))
(zoom-mouse 1.05)))
((wheel-down)
(when (and ownspace (not showsector?))
(zoom-mouse (/ 1.0 1.05))))
((#\f)
(when (send event get-control-down)
(send frame fullscreen (not (send frame is-fullscreened?)))))
((#\0)
(debug-num (+ 1 (debug-num))))
#;((#\d)
(when ownspace
(define cmds '())
(for ((s (in-list (space-objects ownspace)))
#:when (spaceship? s))
(append! cmds (list (chdam (ob-id s) 10 #f))))
#;(when my-stack
(define s (get-ship my-stack))
(append! cmds (dmg-ship s 20 (- pi/2))))
(send-commands cmds)))
#;((#\m)
(when ownspace
(send-commands (message (next-id) (space-time ownspace) #t #f
(~a "message " (space-time ownspace))))))
#;((#\u)
(when ownspace
(send-commands (chadd (random-upgrade (space-time ownspace)
(posvel -1 0 0 0
(random 100)
(random 100) 0)) #f))))
))))
))
(define glconfig (new gl-config%))
(send glconfig set-legacy? #f)
(set! canvas
(new my-canvas%
(parent frame)
; start with a min-width and min-height so the frame shows it all
; this is reset after the frame is shown
(min-width (fl->fx canon-width))
(min-height (fl->fx canon-height))
(paint-callback draw-screen)
(gl-config glconfig)
(style '(no-autoclear gl))))
(define sd (make-sprite-db))
(let ()
(add-sprite!/value sd 'button-normal
(inset (filled-rectangle 100 50 #:color "gray"
#:border-color "white" #:border-width 2) 1))
(add-sprite!/value sd 'button-outline
(inset (rectangle 100 50 #:border-color "gray" #:border-width 2) 1))
(add-sprite!/value sd 'button-disabled
(inset (filled-rectangle 100 50 #:color "black"
#:border-color "gray" #:border-width 2) 1))
(add-sprite!/value sd 'button-normal-circle
(inset (filled-ellipse 100 100 #:color "gray"
#:border-color "white" #:border-width 2) 1))
(add-sprite!/value sd 'button-disabled-circle
(inset (filled-ellipse 100 100 #:color "black"
#:border-color "gray" #:border-width 2) 1))
(add-sprite!/value sd 'dmgbutton-normal
(inset (rectangle 100 50 #:border-color "black" #:border-width 2) 1))
(add-sprite!/value sd 'dmgbutton-fill
(inset (filled-rectangle 100 50 #:color "black"
#:border-color "black" #:border-width 0) 2))
; used to draw lines
; we need multiple because:
; - scaling up causes fading at the edges
; - scaling down too much causes some kind of visual ringing
(add-sprite!/value sd '10x10
(colorize (filled-rectangle 10 10) "black"))
(add-sprite!/value sd '100x10
(colorize (filled-rectangle 100 10) "black"))
(add-sprite!/value sd '1000x10
(colorize (filled-rectangle 1000 10) "black"))
; used to fill rectangles
(add-sprite!/value sd '100x100
(colorize (filled-rectangle 100 100) "black"))
(add-sprite!/value sd '1000x100
(colorize (filled-rectangle 1000 100) "black"))
(add-sprite!/value sd '1000x1000