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
|
implement Clientmod;
# bouncing balls demo. it uses tk and multiple processes to animate a
# number of balls bouncing around the screen. each ball has its own
# process; CPU time is doled out fairly to each process by using
# a central monitor loop.
include "sys.m";
sys: Sys;
include "draw.m";
draw: Draw;
Display, Point, Rect, Image: import draw;
include "tk.m";
tk: Tk;
include "tkclient.m";
tkclient: Tkclient;
include "math.m";
math: Math;
include "rand.m";
include "../client.m";
BALLSIZE: con 5;
ZERO: con 1e-6;
π: con Math->Pi;
Maxδ: con π / 4.0; # max bat angle deflection
Line: adt {
p, v: Realpoint;
s: real;
new: fn(p1, p2: Point): ref Line;
hittest: fn(l: self ref Line, p: Point): (Realpoint, real, real);
intersection: fn(b: self ref Line, p, v: Realpoint): (int, Realpoint, real, real);
point: fn(b: self ref Line, s: real): Point;
};
Realpoint: adt {
x, y: real;
};
cliquecmds := array[] of {
"canvas .c -bg black",
"bind .c <ButtonRelease-1> {send mouse 0 1 %x %y}",
"bind .c <ButtonRelease-2> {send mouse 0 2 %x %y}",
"bind .c <Button-1> {send mouse 1 1 %x %y}",
"bind .c <Button-2> {send mouse 1 2 %x %y}",
"bind . <Key-b> {send ucmd newball}",
"bind . <ButtonRelease-1> {focus .}",
"bind .Wm_t <ButtonRelease-1> +{focus .}",
"focus .",
"bind .c <Key-b> {send ucmd newball}",
"bind .c <Key-u> {grab release .c}",
"frame .f",
"button .f.b -text {Start} -command {send ucmd start}",
"button .f.n -text {New ball} -command {send ucmd newball}",
"pack .f.b .f.n -side left",
"pack .f -fill x",
"pack .c -fill both -expand 1",
"update",
};
Ballstate: adt {
owner: int; # index into member array
hitobs: ref Obstacle;
t0: int;
p, v: Realpoint;
speed: real;
};
Queue: adt {
h, t: list of T;
put: fn(q: self ref Queue, s: T);
get: fn(q: self ref Queue): T;
};
Obstacle: adt {
line: ref Line;
id: int;
isbat: int;
s1, s2: real;
srvid: int;
owner: int;
new: fn(id: int): ref Obstacle;
config: fn(b: self ref Obstacle);
};
Object: adt {
obstacle: ref Obstacle;
ballctl: chan of ref Ballstate;
};
Member: adt {
id: int;
colour: string;
};
win: ref Tk->Toplevel;
lines: list of ref Obstacle;
lineversion := 0;
memberid: int;
myturn: int;
stderr: ref Sys->FD;
timeoffset := 0;
objects: array of ref Object;
srvobjects: array of ref Obstacle; # all for lasthit...
members: array of ref Member;
CORNER: con 60;
INSET: con 20;
WIDTH: con 500;
HEIGHT: con 500;
bats: list of ref Obstacle;
mkball: chan of (int, chan of chan of ref Ballstate);
cliquefd: ref Sys->FD;
currentlydragging := -1;
Ballexit: ref Ballstate;
Noobs: ref Obstacle;
nomod(s: string)
{
sys->fprint(stderr, "bounce: cannot load %s: %r\n", s);
sys->raise("fail:bad module");
}
client(ctxt: ref Draw->Context, argv: list of string, nil: int)
{
sys = load Sys Sys->PATH;
stderr = sys->fildes(2);
draw = load Draw Draw->PATH;
math = load Math Math->PATH;
tk = load Tk Tk->PATH;
tkclient = load Tkclient Tkclient->PATH;
if (tkclient == nil)
nomod(Tkclient->PATH);
tkclient->init();
cliquefd = sys->fildes(0);
Ballexit = ref Ballstate;
Noobs = Obstacle.new(-1);
lines = tl lines; # XXX ahem.
if (len argv >= 3) # argv: modname mnt dir ...
membername = readfile(hd tl argv + "/name");
sys->pctl(Sys->NEWPGRP, nil);
wmctl: chan of string;
(win, wmctl) = tkclient->toplevel(ctxt.screen, nil, "Bounce", 0);
ucmd := chan of string;
tk->namechan(win, ucmd, "ucmd");
mouse := chan of string;
tk->namechan(win, mouse, "mouse");
for (i := 0; i < len cliquecmds; i++)
cmd(win, cliquecmds[i]);
cmd(win, ".c configure -width 500 -height 500");
cmd(win, ".c configure -width [.c cget -actwidth] -height [.c cget -actheight]");
imageinit();
mch := chan of (int, Point);
spawn mouseproc(mch);
mkball = chan of (int, chan of chan of ref Ballstate);
spawn monitor(mkball);
balls: list of chan of ref Ballstate;
spawn updateproc();
sys->sleep(500); # wait for things to calm down a little
cliquecmd("time " + string sys->millisec());
buts := 0;
for (;;) alt {
c := <-wmctl =>
if (c == "exit")
sys->write(cliquefd, array[0] of byte, 0);
tkclient->wmctl(win, c);
c := <-mouse =>
(nil, toks) := sys->tokenize(c, " ");
if ((hd toks)[0] == '1')
buts |= int hd tl toks;
else
buts &= ~int hd tl toks;
mch <-= (buts, Point(int hd tl tl toks, int hd tl tl tl toks));
c := <-ucmd =>
cliquecmd(c);
}
}
cliquecmd(s: string): int
{
if (sys->fprint(cliquefd, "%s\n", s) == -1) {
err := sys->sprint("%r");
notify(err);
sys->print("bounce: cmd error on '%s': %s\n", s, err);
return 0;
}
return 1;
}
updateproc()
{
wfd := sys->open("/prog/" + string sys->pctl(0, nil) + "/wait", Sys->OREAD);
spawn updateproc1();
buf := array[Sys->ATOMICIO] of byte;
n := sys->read(wfd, buf, len buf);
sys->print("updateproc process exited: %s\n", string buf[0:n]);
}
updateproc1()
{
buf := array[Sys->ATOMICIO] of byte;
while ((n := sys->read(cliquefd, buf, len buf)) > 0) {
(nil, lines) := sys->tokenize(string buf[0:n], "\n");
for (; lines != nil; lines = tl lines)
applyupdate(hd lines);
cmd(win, "update");
}
if (n < 0)
sys->fprint(stderr, "bounce: error reading updates: %r\n");
sys->fprint(stderr, "bounce: updateproc exiting\n");
}
UNKNOWN, BALL, OBSTACLE: con iota;
applyupdate(s: string)
{
# sys->print("bounce: got update %s\n", s);
(nt, toks) := sys->tokenize(s, " ");
case hd toks {
"create" =>
# create id parentid vis type
id := int hd tl toks;
if (id >= len objects) {
newobjects := array[id + 10] of ref Object;
newobjects[0:] = objects;
objects = newobjects;
}
objects[id] = ref Object;
"del" =>
# del parent start end objid...
for (toks = tl tl tl tl toks; toks != nil; toks = tl toks) {
id := int hd toks;
if (objects[id].obstacle != nil)
sys->fprint(stderr, "bounce: cannot delete obstructions yet\n");
else
objects[id].ballctl <-= Ballexit;
objects[id] = nil;
}
"set" =>
# set obj attr val
id := int hd tl toks;
attr := hd tl tl toks;
val := tl tl tl toks;
case attr {
"state" =>
# state lasthit owner p.x p.y v.x v.y s time
state := ref Ballstate;
(state.hitobs, val) = (srvobj(int hd val), tl val);
(state.owner, val) = (int hd val, tl val);
(state.p.x, val) = (real hd val, tl val);
(state.p.y, val) = (real hd val, tl val);
(state.v.x, val) = (real hd val, tl val);
(state.v.y, val) = (real hd val, tl val);
(state.speed, val) = (real hd val, tl val);
(state.t0, val) = (int hd val, tl val);
if (objects[id].ballctl == nil)
objects[id].ballctl = makeball(id, state);
else
objects[id].ballctl <-= state;
"pos" or "coords" or "owner" or "id" =>
if (objects[id].obstacle == nil)
objects[id].obstacle = Obstacle.new(id);
o := objects[id].obstacle;
case attr {
"pos" =>
(o.s1, val) = (real hd val, tl val);
(o.s2, val) = (real hd val, tl val);
o.isbat = 1;
"coords" =>
p1, p2: Point;
(p1.x, val) = (int hd val, tl val);
(p1.y, val) = (int hd val, tl val);
(p2.x, val) = (int hd val, tl val);
(p2.y, val) = (int hd val, tl val);
o.line = Line.new(p1, p2);
"owner" =>
o.owner = hd val;
if (o.owner == membername)
bats = o :: bats;
"id" =>
o.srvid = int hd val;
if (o.srvid >= len srvobjects) {
newobjects := array[id + 10] of ref Obstacle;
newobjects[0:] = srvobjects;
srvobjects = newobjects;
}
srvobjects[o.srvid] = o;
}
if (currentlydragging != id)
o.config();
"arenasize" =>
# arenasize w h
cmd(win, ".c configure -width " + hd val + " -height " + hd tl val);
* =>
if (len attr > 5 && attr[0:5] == "score") {
# scoreN val
n := int attr[5:];
w := ".f." + string n;
if (!tkexists(w)) {
cmd(win, "label " + w + "l -text '" + attr);
cmd(win, "label " + w + " -relief sunken -bd 5 -width 5w");
cmd(win, "pack " +w + "l " + w + " -side left");
}
cmd(win, w + " configure -text {" + hd val + "}");
} else if (len attr > 6 && attr[0:6] == "member") {
# memberN id colour
n := int attr[6:];
if (n >= len members) {
newmembers := array[n + 1] of ref Member;
newmembers[0:] = members;
members = newmembers;
}
p := members[n] = ref Member(int hd val, hd tl val);
cmd(win, ".c itemconfigure o" + string p.id + " -fill " + p.colour);
if (p.id == memberid)
myturn = n;
}
else
sys->fprint(stderr, "bounce: unknown attr '%s'\n", attr);
}
"time" =>
# time offset orig
now := sys->millisec();
time := int hd tl tl toks;
transit := now - time;
timeoffset = int hd tl toks - transit / 2;
sys->print("transit time %d, timeoffset: %d\n", transit, timeoffset);
* =>
sys->fprint(stderr, "chat: unknown update message '%s'\n", s);
}
}
tkexists(w: string): int
{
return tk->cmd(win, w + " cget -bd")[0] != '!';
}
srvobj(id: int): ref Obstacle
{
if (id < 0 || id >= len srvobjects || srvobjects[id] == nil)
return Noobs;
return srvobjects[id];
}
mouseproc(mch: chan of (int, Point))
{
procname("mouse");
for (;;) {
hitbat: ref Obstacle = nil;
minperp, hitdist: real;
(buts, p) := <-mch;
for (bl := bats; bl != nil; bl = tl bl) {
b := hd bl;
(normal, perp, dist) := b.line.hittest(p);
perp = abs(perp);
if ((hitbat == nil || perp < minperp) && (dist >= b.s1 && dist <= b.s2))
(hitbat, minperp, hitdist) = (b, perp, dist);
}
if (hitbat == nil || minperp > 30.0) {
while ((<-mch).t0)
;
continue;
}
offset := hitdist - hitbat.s1;
if (buts & 2)
(buts, p) = aim(mch, hitbat, p);
if (buts & 1)
drag(mch, hitbat, offset);
}
}
drag(mch: chan of (int, Point), hitbat: ref Obstacle, offset: real)
{
realtosrv := chan of string;
dummytosrv := chan of string;
tosrv := dummytosrv;
currevent := "";
currentlydragging = hitbat.id;
line := hitbat.line;
batlen := hitbat.s2 - hitbat.s1;
cvsorigin := Point(int cmd(win, ".c cget -actx"), int cmd(win, ".c cget -acty"));
spawn sendproc(realtosrv);
cmd(win, "grab set .c");
cmd(win, "focus .");
loop: for (;;) alt {
tosrv <-= currevent =>
tosrv = dummytosrv;
(buts, p) := <-mch =>
if (buts & 2)
(buts, p) = aim(mch, hitbat, p);
(v, perp, dist) := line.hittest(p);
dist -= offset;
# constrain bat and mouse positions
if (dist < 0.0 || dist + batlen > line.s) {
if (dist < 0.0) {
p = line.point(offset);
dist = 1.0;
} else {
p = line.point(line.s - batlen + offset);
dist = line.s - batlen;
}
p.x -= int (v.x * perp);
p.y -= int (v.y * perp);
win.image.display.cursorset(p.add(cvsorigin));
}
(hitbat.s1, hitbat.s2) = (dist, dist + batlen);
hitbat.config();
cmd(win, "update");
currevent = "bat " + string hitbat.s1;
tosrv = realtosrv;
if (!buts)
break loop;
}
cmd(win, "grab release .c");
realtosrv <-= nil;
currentlydragging = -1;
}
CHARGETIME: con 1000.0;
MAXCHARGE: con 50.0;
α: con 0.999; # decay in one millisecond
D: con 5;
aim(mch: chan of (int, Point), hitbat: ref Obstacle, p: Point): (int, Point)
{
cvsorigin := Point(int cmd(win, ".c cget -actx"), int cmd(win, ".c cget -acty"));
startms := ms := sys->millisec();
δ := Realpoint(0.0, 0.0);
line := hitbat.line;
charge := 0.0;
pivot := line.point((hitbat.s1 + hitbat.s2) / 2.0);
s1 := p2s(line.point(hitbat.s1));
s2 := p2s(line.point(hitbat.s2));
cmd(win, ".c create line 0 0 0 0 -tags wire -fill yellow");
ballid := makeballitem(-1, myturn);
bp, p2: Point;
buts := 2;
for (;;) {
v := makeunit(δ);
bp = pivot.add((int (v.x * charge), int (v.y * charge)));
cmd(win, ".c coords wire "+s1+" "+p2s(bp)+" "+s2);
ballmove(ballid, bp);
cmd(win, "update");
if ((buts & 2) == 0)
break;
(buts, p2) = <-mch;
now := sys->millisec();
fade := math->pow(α, real (now - ms));
charge = real (now - startms) * (MAXCHARGE / CHARGETIME);
if (charge > MAXCHARGE)
charge = MAXCHARGE;
ms = now;
dp := p2.sub(p);
δ.x = δ.x * fade + real dp.x;
δ.y = δ.y * fade + real dp.y;
mag := δ.x * δ.x + δ.y * δ.y;
if (dp.x != 0 || dp.y != 0)
win.image.display.cursorset(p.add(cvsorigin));
}
cmd(win, ".c delete wire " + ballid);
cmd(win, "update");
(δ.x, δ.y) = (-δ.x, -δ.y);
cliquecmd("newball " + string hitbat.id + " " +
p2s(bp) + " " + rp2s(makeunit(δ)) + " " + string (charge / 100.0));
return (buts, p2);
}
makeunit(v: Realpoint): Realpoint
{
mag := math->sqrt(v.x * v.x + v.y * v.y);
if (mag < ZERO)
return (1.0, 0.0);
return (v.x / mag, v.y / mag);
}
sendproc(tosrv: chan of string)
{
procname("send");
while ((ev := <-tosrv) != nil)
cliquecmd(ev);
}
makeball(id: int, state: ref Ballstate): chan of ref Ballstate
{
mkballreply := chan of chan of ref Ballstate;
mkball <-= (id, mkballreply);
ballctl := <-mkballreply;
ballctl <-= state;
return ballctl;
}
blankobstacle: Obstacle;
Obstacle.new(id: int): ref Obstacle
{
cmd(win, ".c create line 0 0 0 0 -width 3 -fill #aaaaaa" + " -tags l" + string id);
o := ref blankobstacle;
o.line = Line.new((0, 0), (0, 0));
o.id = id;
o.owner = -1;
o.srvid = -1;
lineversion++;
lines = o :: lines;
return o;
}
Obstacle.config(o: self ref Obstacle)
{
if (o.isbat) {
cmd(win, ".c coords l" + string o.id + " " +
p2s(o.line.point(o.s1)) + " " + p2s(o.line.point(o.s2)));
if (o.owner == memberid)
cmd(win, ".c itemconfigure l" + string o.id + " -fill red");
else
cmd(win, ".c itemconfigure l" + string o.id + " -fill white");
} else {
cmd(win, ".c coords l" + string o.id + " " +
p2s(o.line.point(0.0)) + " " + p2s(o.line.point(o.line.s)));
}
}
# make sure cpu time is handed to all ball processes fairly
# by passing a "token" around to each process in turn.
# each process does its work when it *hasn't* got its
# token but it can't go through two iterations without
# waiting its turn.
#
# new processes are created by sending on mkball.
# the channel sent back can be used to control the position
# and velocity of the ball and to destroy it.
monitor(mkball: chan of (int, chan of chan of ref Ballstate))
{
procname("mon");
procl, proc: list of (chan of ref Ballstate, chan of int);
rc := dummyrc := chan of int;
for (;;) {
alt {
(id, ch) := <-mkball =>
(newc, newrc) := (chan of ref Ballstate, chan of int);
procl = (newc, newrc) :: procl;
spawn animproc(id, newc, newrc);
ch <-= newc;
if (tl procl == nil) { # first ball
newc <-= nil;
rc = newrc;
proc = procl;
}
alive := <-rc => # got token.
if (!alive) {
# ball has exited: remove from list
newprocl: list of (chan of ref Ballstate, chan of int);
for (; procl != nil; procl = tl procl)
if ((hd procl).t1 != rc)
newprocl = hd procl :: newprocl;
procl = newprocl;
}
if ((proc = tl proc) == nil)
proc = procl;
if (proc == nil) {
rc = dummyrc;
} else {
c: chan of ref Ballstate;
(c, rc) = hd proc;
c <-= nil; # hand token to next process.
}
}
}
}
# buffer ball state commands, so at least balls we handle
# locally appear glitch free.
bufferproc(cmdch: chan of string)
{
procname("buffer");
buffer := ref Queue;
bufhd: string;
dummytosrv := chan of string;
realtosrv := chan of string;
spawn sendproc(realtosrv);
tosrv := dummytosrv;
for (;;) alt {
tosrv <-= bufhd =>
if ((bufhd = buffer.get()) == nil)
tosrv = dummytosrv;
s := <-cmdch =>
if (s == nil) {
# ignore other queued requests, as they're
# only state changes for a ball that's now been deleted.
realtosrv <-= nil;
exit;
}
buffer.put(s);
if (tosrv == dummytosrv) {
tosrv = realtosrv;
bufhd = buffer.get();
}
}
}
start: int;
# animate one ball. initial position and unit-velocity are
# given by p and v.
animproc(id: int, c: chan of ref Ballstate, rc: chan of int)
{
procname("anim");
while ((newstate := <-c) == nil)
rc <-= 1;
state := *newstate;
totaldist := 0.0; # distance ball has travelled from reference point to last intersection
ballid := makeballitem(id, state.owner);
smallcount := 0;
version := lineversion;
tosrv := chan of string;
start := sys->millisec();
spawn bufferproc(tosrv);
loop: for (;;) {
hitp: Realpoint;
dist := 1000000.0;
oldobs := state.hitobs;
hitt: real;
for (l := lines; l != nil; l = tl l) {
obs := hd l;
(ok, hp, hdist, t) := obs.line.intersection(state.p, state.v);
if (ok && hdist < dist && obs != oldobs && (smallcount < 10 || hdist > 1.5)) {
(hitp, state.hitobs, dist, hitt) = (hp, obs, hdist, t);
}
}
if (dist > 10000.0) {
sys->print("no intersection!\n");
state = ballexit(1, ballid, tosrv, c, rc);
totaldist = 0.0;
continue loop;
}
if (dist < 0.0001)
smallcount++;
else
smallcount = 0;
t0 := int (totaldist / state.speed) + state.t0 - timeoffset;
et := t0 + int (dist / state.speed);
t := sys->millisec() - t0;
dt := et - t0;
do {
s := real t * state.speed;
currp := Realpoint(state.p.x + s * state.v.x, state.p.y + s * state.v.y);
ballmove(ballid, (int currp.x, int currp.y));
cmd(win, "update");
if (lineversion > version) {
(state.p, state.hitobs, version) = (currp, oldobs, lineversion);
totaldist += s;
continue loop;
}
if ((newstate := <-c) != nil) {
if (newstate == Ballexit)
ballexit(0, ballid, tosrv, c, rc);
state = *newstate;
totaldist = 0.0;
continue loop;
}
rc <-= 1;
t = sys->millisec() - t0;
} while (t < dt);
totaldist += dist;
state.p = hitp;
hitobs := state.hitobs;
if (hitobs.isbat) {
if (hitobs.owner == memberid) {
if (hitt >= hitobs.s1 && hitt <= hitobs.s2)
state.v = batboing(hitobs, hitt, state.v);
tosrv <-= "state " +
string id +
" " + string hitobs.srvid +
" " + string state.owner +
" " + rp2s(state.p) + " " + rp2s(state.v) +
" " + string state.speed +
" " + string (sys->millisec() + timeoffset);
} else {
# wait for enlightenment
while ((newstate := <-c) == nil)
rc <-= 1;
if (newstate == Ballexit)
ballexit(0, ballid, tosrv, c, rc);
state = *newstate;
totaldist = 0.0;
}
} else if (hitobs.owner == memberid) {
# if line has an owner but isn't a bat, then it's
# a terminating line, so we inform server.
cliquecmd("lost " + string id);
state = ballexit(1, ballid, tosrv, c, rc);
totaldist = 0.0;
} else
state.v = boing(state.v, hitobs.line);
}
}
#ballmask: ref Image;
imageinit()
{
# displ := win.image.display;
# ballmask = displ.newimage(((0, 0), (BALLSIZE+1, BALLSIZE+1)), 0, 0, Draw->White);
# ballmask.draw(ballmask.r, displ.zeros, displ.ones, (0, 0));
# ballmask.fillellipse((BALLSIZE/2, BALLSIZE/2), BALLSIZE/2, BALLSIZE/2, displ.ones, (0, 0));
# End: con Draw->Endsquare;
# n := 5;
# θ := 0.0;
# δ := (2.0 * π) / real n;
# c := Point(BALLSIZE / 2, BALLSIZE / 2).sub((1, 1));
# r := real (BALLSIZE / 2);
# for (i := 0; i < n; i++) {
# p2 := Point(int (r * math->cos(θ)), int (r * math->sin(θ)));
# sys->print("drawing from %s to %s\n", p2s(c), p2s(p2.add(c)));
# ballmask.line(c, c.add(p2), End, End, 1, displ.ones, (0, 0));
# θ += δ;
# }
}
makeballitem(id, owner: int): string
{
displ := win.image.display;
return cmd(win, ".c create oval 0 0 1 1 -fill " + members[owner].colour +
" -tags o" + string owner);
}
ballmove(ballid: string, p: Point)
{
cmd(win, ".c coords " + ballid +
" " + string (p.x - BALLSIZE) +
" " + string (p.y - BALLSIZE) +
" " + string (p.x + BALLSIZE) +
" " + string (p.y + BALLSIZE));
}
ballexit(wait: int, ballid: string, tosrv: chan of string, c: chan of ref Ballstate, rc: chan of int): Ballstate
{
if (wait) {
while ((s := <-c) != Ballexit)
if (s == nil)
rc <-= 1;
else
return *s; # maybe we're not exiting, after all...
}
cmd(win, ".c delete " + ballid + ";update");
# cmd(win, "image delete " + ballid);
tosrv <-= nil;
<-c;
rc <-= 0; # inform monitor that we've gone
exit;
}
# thread-safe access to the Rand module
randgenproc(ch: chan of int)
{
procname("rand");
rand := load Rand Rand->PATH;
for (;;)
ch <-= rand->rand(16r7fffffff);
}
abs(x: real): real
{
if (x < 0.0)
return -x;
return x;
}
# bounce ball travelling in direction av off line b.
# return the new unit vector.
boing(av: Realpoint, b: ref Line): Realpoint
{
d := math->atan2(b.v.y, b.v.x) * 2.0 - math->atan2(av.y, av.x);
return (math->cos(d), math->sin(d));
}
# calculate how a bounce vector should be modified when
# hitting a bat. t gives the intersection point on the bat;
# ballv is the ball's vector.
batboing(bat: ref Obstacle, t: real, ballv: Realpoint): Realpoint
{
ballθ := math->atan2(ballv.y, ballv.x);
batθ := math->atan2(bat.line.v.y, bat.line.v.x);
φ := ballθ - batθ;
δ: real;
t -= bat.s1;
batlen := bat.s2 - bat.s1;
if (math->sin(φ) > 0.0)
δ = (t / batlen) * Maxδ * 2.0 - Maxδ;
else
δ = (t / batlen) * -Maxδ * 2.0 + Maxδ;
θ := math->atan2(bat.line.v.y, bat.line.v.x) * 2.0 - ballθ; # boing
θ += δ;
return (math->cos(θ), math->sin(θ));
}
Line.new(p1, p2: Point): ref Line
{
ln := ref Line;
ln.p = (real p1.x, real p1.y);
v := Realpoint(real (p2.x - p1.x), real (p2.y - p1.y));
ln.s = math->sqrt(v.x * v.x + v.y * v.y);
if (ln.s > ZERO)
ln.v = (v.x / ln.s, v.y / ln.s);
else
ln.v = (1.0, 0.0);
return ln;
}
# return normal from line, perpendicular distance from line and distance down line
Line.hittest(l: self ref Line, ip: Point): (Realpoint, real, real)
{
p := Realpoint(real ip.x, real ip.y);
v := Realpoint(-l.v.y, l.v.x);
(nil, nil, perp, ldist) := l.intersection(p, v);
return (v, perp, ldist);
}
Line.point(l: self ref Line, s: real): Point
{
return (int (l.p.x + s * l.v.x), int (l.p.y + s * l.v.y));
}
# compute the intersection of lines a and b.
# b is assumed to be fixed, and a is indefinitely long
# but doesn't extend backwards from its starting point.
# a is defined by the starting point p and the unit vector v.
# return whether it hit, the point at which it hit if so,
# the distance of the intersection point from p,
# and the distance of the intersection point from b.p.
Line.intersection(b: self ref Line, p, v: Realpoint): (int, Realpoint, real, real)
{
det := b.v.x * v.y - v.x * b.v.y;
if (det > -ZERO && det < ZERO)
return (0, (0.0, 0.0), 0.0, 0.0);
y21 := b.p.y - p.y;
x21 := b.p.x - p.x;
s := (b.v.x * y21 - b.v.y * x21) / det;
t := (v.x * y21 - v.y * x21) / det;
if (s < 0.0)
return (0, (0.0, 0.0), s, t);
hit := t >= 0.0 && t <= b.s;
hp: Realpoint;
if (hit)
hp = (p.x+v.x*s, p.y+v.y*s);
return (hit, hp, s, t);
}
cmd(top: ref Tk->Toplevel, s: string): string
{
e := tk->cmd(top, s);
if (e != nil && e[0] == '!')
sys->print("tk error %s on '%s'\n", e, s);
return e;
}
state2s(s: ref Ballstate): string
{
return sys->sprint("[hitobs:%d(id %d), t0: %d, p: %g %g; v: %g %g; s: %g",
s.hitobs.srvid, s.hitobs.id, s.t0, s.p.x, s.p.y, s.v.x, s.v.y, s.speed);
}
l2s(l: ref Line): string
{
return p2s(l.point(0.0)) + " " + p2s(l.point(l.s));
}
rp2s(rp: Realpoint): string
{
return string rp.x + " " + string rp.y;
}
p2s(p: Point): string
{
return string p.x + " " + string p.y;
}
notifypid := -1;
notify(s: string)
{
kill(notifypid);
sync := chan of int;
spawn notifyproc(s, sync);
notifypid = <-sync;
}
notifyproc(s: string, sync: chan of int)
{
procname("notify");
sync <-= sys->pctl(0, nil);
cmd(win, ".c delete notify");
id := cmd(win, ".c create text 0 0 -anchor nw -fill red -tags notify -text '" + s);
bbox := cmd(win, ".c bbox " + id);
cmd(win, ".c create rectangle " + bbox + " -fill #ffffaa -tags notify");
cmd(win, ".c raise " + id);
cmd(win, "update");
sys->sleep(750);
cmd(win, ".c delete notify");
cmd(win, "update");
notifypid = -1;
}
kill(pid: int)
{
if ((fd := sys->open("/prog/"+string pid+"/ctl", Sys->OWRITE)) != nil)
sys->write(fd, array of byte "kill", 4);
}
T: type string;
Queue.put(q: self ref Queue, s: T)
{
q.t = s :: q.t;
}
Queue.get(q: self ref Queue): T
{
s: T;
if(q.h == nil){
q.h = revlist(q.t);
q.t = nil;
}
if(q.h != nil){
s = hd q.h;
q.h = tl q.h;
}
return s;
}
revlist(ls: list of T) : list of T
{
rs: list of T;
for (; ls != nil; ls = tl ls)
rs = hd ls :: rs;
return rs;
}
procname(s: string)
{
# sys->procname(sys->procname(nil) + " " + s);
}
|