-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpaceMT.py
1187 lines (1137 loc) · 43.8 KB
/
SpaceMT.py
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
# An adaption of SpaceTrek done ages ago for ACTS Computing
# Version 1.0 - November 2021
# This version is planned to run on the AdaFruit MagTag.
# I have tried to keep all the "I/O" isolated, so it should be able to be moved
# to a different platform
# ToDo:
# *) Refactor to put collect 'utilities' in one section commands in another
# *) 'optimize' display refresh (make sure we only call it when needed)
# *) Start at a starbase
# *) Calculate rating/efficiency
# *) Game startup (get Sir/Madam, ship type)
# *) Game ending
# *) Invasion (swarm)
# *) Enemy moving
# **) After shooting at enemy.. if the enemy fires back it doesn't say so
# **) Redo doMove so it isn't re-entrant
# **) Buttons don't always work? (probably GC - need to figure out more of how to force GC)
# **) Why didn't docking bring power back up to 100%
# **) Efficiency is always negative?
# **) Cycle LEDS when waiting for a button (only if can figure out asyncio)
# 16) Cycle LEDS when waiting for the screen refresh
# **) SOS needs to be 'longer' (sleep between each tone)
# **) "victory" celebration!
# **) Setting shields.. when lowering.. Power ends up negative
# **) Efficency is way too small
# **) When launching trackers only show "around" that have an enemy in 'knowns'
# 22) Efficency needs more work
# **) Need to 'scroll' otherText. Create function to take a line
# and add it to an array.. if there are more than 9 lines, put 9 up
# and wait. waitForDisplay will need to collect the lines and put them
# into the correct area.
# **) Ask if they want to see instructions at the beginning (read from file)
# 25) Find/Destroy startbase (do we define shields, or just use 4/5 enemy in quadrant)
# 26) Damage/Fix/Repair stuff.
# **) Button width in getMove needs to be adjusted.
import time
import random
import board
import terminalio
import displayio
from adafruit_display_text import label
from adafruit_magtag.magtag import MagTag
import gc
import supervisor
# Setup Magtag "stuff"
magtag = MagTag(default_bg=0x0)
magtag.display.show(None)
import Globals
galaxy=Globals.Galaxy
status=Globals.Status
MAXENEMY=26
MAXSTARBASES=3
# galaxy.sectors will be filled with one of these
SECTOREMPTY=" "
SECTORSTAR="*"
SECTORBH="?"
SECTORSB="O"
SECTORENEMY="#"
SECTORME="+"
SECTORDOCKED="@"
# We need to be able to tell if a tracker is available or destroyed when we destroyed the enemy it was tracking
TRACKERAVAIL=-1
TRACKERDONE=-2
#Length of the buttons - this is a 'generic' left format string (do BUTTONLEN % "word")
BUTTONLEN="%-7s"
# The screen is divided into 3 areas
# 0) - The command display area at the bottom
# 1) - The status display area on the right
# 2) - The 'other' area for short/long scans, tracking info, etc.
mainGroup=displayio.Group()
commandGroup=displayio.Group()
cmdLabel=label.Label(terminalio.FONT, line_spacing=0.9,anchor_point=(0.0,0.0),anchored_position=(0,115),save_text=False)
commandGroup.append(cmdLabel)
mainGroup.append(commandGroup)
cgIdx=0
statusGroup=displayio.Group()
statusLabel=label.Label(terminalio.FONT, line_spacing=0.9, anchor_point = (0.0, 0.0), anchored_position = (211, 0),save_text=False)
statusGroup.append(statusLabel)
mainGroup.append(statusGroup)
sgIdx=1
otherGroup=displayio.Group()
otherLabel=label.Label(terminalio.FONT, line_spacing=0.9,anchor_point = (0.0, 0.0), anchored_position = (0, 0),save_text=False)
otherGroup.append(otherLabel)
mainGroup.append(otherGroup)
ogIdx=2
board.DISPLAY.show(mainGroup)
# A 'generic' function to space pad the commands
def spacePad(theWord):
return "%-7s" & theWord
# Update the status rows/information
def updateStatus():
# How many I should have shot = (starDate-origDate)/daysPerEnemy
# Actual/should have = efficiency
if status.origDate<status.starDate:
efficiency=100.0*status.enemyDown/((status.starDate-status.origDate)/status.daysPerEnemy)
if efficiency>200.0:
efficiency=200.0
else:
efficiency=0.0
print("daysPerEnemy,starDate,origDate",status.daysPerEnemy,status.starDate,status.origDate)
theText="Status :"
if galaxy.quadrants[galaxy.currentQ]&0o700 == 0:
theText+=" Green"
else:
theText+=" Red"
theText+="\nDate :{:6.1f}\n\
QQ:SS : {:02o}:{:02o}\n\
Power :{:6.1f}\n\
Shields:{:6.1f}\n\
Photons:{:6}\n\
Enemy :{:6}\n\
Killed :{:6}\n\
Rating :{:6.1f}\n\
Swarm :{:6.1f}".format(status.starDate,galaxy.currentQ,galaxy.currentS,status.energy,status.shields,status.photons,status.enemyCnt,status.enemyDown,efficiency,status.estInvasion)
statusLabel.text=theText
# Fill in the details of the current quadrant
# withPlayer=True will insert the player marker too
def fillSectors(withPlayer):
print("Fill Quadrant: {:02o}:{:02o} -> {:5o}".format(galaxy.currentQ,galaxy.currentS,galaxy.quadrants[galaxy.currentQ]))
galaxy.sectors=[SECTOREMPTY]*64
# Always start with the same seed so stars, black holes and base don't move
random.seed(galaxy.seeds[galaxy.currentQ])
for i in range(galaxy.quadrants[galaxy.currentQ]&0o7):
s=random.randint(0,0o77)
# Don't put it in a sector that already has a star
while galaxy.sectors[s]!=SECTOREMPTY:
s=random.randint(0,0o77)
galaxy.sectors[s]=SECTORSTAR
# Now we need to put in any black holes (because they never move)
if galaxy.quadrants[galaxy.currentQ]&0o20!=0:
s=random.randint(0,0o77)
while galaxy.sectors[s]!=SECTOREMPTY:
s=random.randint(0,0o77)
galaxy.sectors[s]=SECTORBH
# Finally a starbase (because they might get destroyed)
if galaxy.quadrants[galaxy.currentQ]&0o10!=0:
s=random.randint(0,0o77)
while galaxy.sectors[s]!=SECTOREMPTY:
s=random.randint(0,0o77)
galaxy.sectors[s]=SECTORSB
# Now for some enemys.
q=galaxy.currentQ<<6
for enemy in galaxy.enemys:
if enemy&0o7700 == q:
s=enemy&0o77 # Peal off the sector partition
galaxy.sectors[s]=SECTORENEMY
if withPlayer:
if galaxy.sectors[galaxy.currentS]==SECTOREMPTY:
galaxy.sectors[galaxy.currentS]=SECTORME
# Check to see if they docked with the starbase
elif galaxy.sectors[galaxy.currentS]==SECTORSB:
galaxy.sectors[galaxy.currentS]=SECTORDOCKED
else:
print("Ship sector already occupied: {:o}:{:o}".format(galaxy.currentQ,galaxy.currentS))
assert False
# Returns the points along a line from A to B
def trackShot(theFrom, theTo):
fy=theFrom>>3
fx=theFrom&0o07
ty=theTo>>3
tx=theTo&0o07
thePoints=[]
# Code adapted from https://github.com/encukou/bresenham
# Implementation of Bresenham's line drawing algorithm
#See en.wikipedia.org/wiki/Bresenham's_line_algorithm
dx = fx - tx
dy = fy - ty
xsign = 1 if dx > 0 else -1
ysign = 1 if dy > 0 else -1
dx = abs(dx)
dy = abs(dy)
if dx > dy:
xx, xy, yx, yy = xsign, 0, 0, ysign
else:
dx, dy = dy, dx
xx, xy, yx, yy = 0, ysign, xsign, 0
D = 2*dy - dx
y = 0
for x in range(dx + 1):
# yield tx + x*xx + y*yx, ty + x*xy + y*yy
thePoints.append((tx + x*xx + y*yx)+((ty + x*xy + y*yy)<<3))
print("{:02o} {:02o} {:02o}".format(((tx + x*xx + y*yx)+((ty + x*xy + y*yy)<<3)),(tx + x*xx + y*yx),(ty + x*xy + y*yy)))
if D >= 0:
y += 1
D -= 2*dx
D += 2*dy
return thePoints
# Display/wait for the next command choice
# This is really the main processing 'loop'
# NOTE: we don't clear the display before this.. the command processing should decide
def getCommand():
# 3 buttons = 3 commands at a time
# 1 button = 'More'
# Wait for them to press a button to pick a command
while True:
theAns=getButtonCommand(["Short","Long","Move","Photon","Phaser","Shields","Tracker","Clear"]
,"","More..")
cmd=theAns[1]
if cmd=="Short":
showShortScan()
if cmd=="Long":
showLongScan()
if cmd=="Move":
getMove()
if cmd=="Photon":
doFire("photon")
if cmd=="Phaser":
doFire("phaser")
if cmd=="Shields":
setShields()
if cmd=="Tracker":
doTrackers()
if cmd=="Clear":
clearKnowns()
# Let them clear out all the 'knowns' so they know what was scanned
def clearKnowns():
addOtherLine("Clear all known scans? "+status.theirTitle)
ans=getButtonCommand(["Yes"],"Cancel","")
if ans[1]=="Cancel":
addOtherLine("Scans retained. "+status.theirTitle)
else:
status.knowns=[" "]*64
addOtherLine("Scans cleared. "+status.theirTitle)
# Display the current trackers attached to enemy
# Left them launch one if they want
def doTrackers():
# First we display all the trackers currently deployed
addOtherLine("Current tracker reports:")
trackersFree=0
for aTracker in status.trackers:
if aTracker==TRACKERAVAIL:
# Count those available
trackersFree+=1
elif aTracker != TRACKERDONE:
# Not available, and not done - points to an enemy
addOtherLine("Enemy at: {:02o}:{:02o}\n".format(galaxy.enemys[aTracker]>>6,galaxy.enemys[aTracker]&0o77))
if trackersFree==0:
getButtonCommand([],"OK","")
return
# Create a list.. one as a string, and one with the numeric value
aList=[]
qList=[]
for qq in around(galaxy.currentQ):
if qq >= 0:
# We will only show quadrants where they saw enemy last time they did a long scan
if galaxy.knowns[qq][0]!=" " and galaxy.knowns[qq][0]!="0":
aList.append("{:02o}".format(qq))
qList.append(qq)
# They should be able to launch a tracker into the current quadrant
if galaxy.knowns[galaxy.currentQ][0]!=" " and galaxy.knowns[galaxy.currentQ][0]!="0":
aList.append("{:02o}".format(galaxy.currentQ))
qList.append(galaxy.currentQ)
if len(aList)==0:
addOtherLine("No known enemy to direct tracker to. {:}".format(status.theirTitle))
return
ans=getButtonCommand(aList,"Cancel","More..")
print("From Trackers",ans)
if ans[1]=="Cancel":
addOtherLine("No Trackers Launched! "+status.theirTitle)
return
# Get the quadrant that they launched into
qq=qList[ans[0]]<<6
# Pick a tracker we are launching
tidx=status.trackers.index(TRACKERAVAIL)
# Now find the first enemy in that quadrant
for idx in range(len(galaxy.enemys)):
if (galaxy.enemys[idx]&0o7700) == qq:
# Found one - make sure it isn't already being tracked
if not (idx in status.trackers):
# OK.. this one is not being tracker - find an available tracker
############### doesn't pick the right one #############
status.trackers[tidx]=idx
break # No need to look further
# If there aren't any enemy in that quadrant, the tracker self destructs
if status.trackers[tidx]==TRACKERAVAIL:
# Did not find any enemy to track
addOtherLine("No enemy in {} to track".format(aList[ans[0]]))
addOtherLine("Tracker self destructed")
status.trackers[tidx]=TRACKERDONE
else:
# Let them know the tracker is working
addOtherLine("Tracker reporting enemy at: {:02o}:{:02o}\n".format(galaxy.enemys[status.trackers[tidx]]>>6,galaxy.enemys[status.trackers[tidx]]&0o77))
# Set the shields
def setShields():
power=status.energy+status.shields
# Need to find out how much power they want to use
# Too complicated to do 3 or 4 digits via the buttons
# So we will give them 6 choices (2 at a time)
addOtherLine("Power available {:6.1f}".format(power))
addOtherLine("Set power to shields? {:}".format(status.theirTitle))
if power<60:
# One last shot?
increment=int(power/6.0)
elif power<600:
increment=int(power/60.0)*10
else:
increment=int(power/600.0)*100
eList=[]
for i in range(1,7):
eList.append(str(increment*i))
eAns=getButtonCommand(eList,"Cancel","More..")
print(eAns)
if eAns[1]=="Cancel":
addOtherLine("Shield change canceled! "+status.theirTitle)
return
status.shields=int(eList[eAns[0]])
status.energy=power-status.shields
print("Shields,Energy",status.shields,status.energy)
addOtherLine("Shields set to {:} {:}".format(status.shields,status.theirTitle))
updateStatus()
# Take care of firing a photon torpedo or a phaser bolt
def doFire (fireWhat):
if galaxy.quadrants[galaxy.currentQ]&0o700==0:
# Nobody to shoot at
addOtherLine("No enemy in this quadrant! "+status.theirTitle)
return
# For photons, we need to make sure we have one
if fireWhat=="photon":
if status.photons==0:
# No photons to fire
addOtherLine("No photon torpedos available! "+status.theirTitle)
return
else:
# Need to find out how much power they want to use
# Too complicated to do 3 or 4 digits via the buttons
# So we will give them 6 choices (2 at a time)
if status.energy<60:
# One last shot?
increment=int(status.energy/6.0)
elif status.energy<600:
increment=int(status.energy/60.0)*10
else:
increment=int(status.energy/600.0)*100
eList=[]
for i in range(1,7):
eList.append(str(increment*i))
addOtherLine("Specify power to phaser {:}".format(status.theirTitle))
eAns=getButtonCommand(eList,"Cancel","More..")
if eAns[1]=="Cancel":
addOtherLine("Firing orders canceled! "+status.theirTitle)
return
# Find all the enemy locations
# Rather than make them button in a sector via +-
# We will present them with a list of enemy in the quadrant
addOtherLine("Select fire coordinates. {:}".format(status.theirTitle))
guysHere=[]
pickList=[]
q=galaxy.currentQ<<6
for enemy in galaxy.enemys:
if enemy&0o7700 == q:
guysHere.append(enemy&0o77)
pickList.append("{:02o} ".format(enemy&0o77))
theAns=getButtonCommand(pickList,"Cancel","More..")
print("Enemy pick",theAns)
if theAns[1]=="Cancel":
addOtherLine("Firing orders canceled! "+status.theirTitle)
return
fy=galaxy.currentS>>3
fx=galaxy.currentS&0o07
ty=guysHere[theAns[0]]>>3
tx=guysHere[theAns[0]]&0o07
shotPoints=trackShot(galaxy.currentS,guysHere[theAns[0]])
fillSectors(True)
# Might need to reverse the list to get 'from' at 0
if shotPoints[0]==guysHere[theAns[0]]:
shotPoints.reverse()
print(shotPoints)
# We have fired.. even if we never hit anything
if fireWhat=="phaser":
# Even if it misses, we have used the power
status.power-=float(eList[eAns[0]])
else:
status.photons-=1
addOtherLine("Tracking shot."+status.theirTitle)
shotTracks=[]
for aPoint in shotPoints:
print("{:02o}->{}".format(aPoint,galaxy.sectors[aPoint]))
shotTracks.append("{:02o} ".format(aPoint))
# See if there is anything in the way
if galaxy.sectors[aPoint]==SECTORSTAR:
addOtherLine(" ".join(shotTracks))
addOtherLine("Shot impacted a star.")
# Any enemy left in the quadrant will definately fire back
enemyFire(True)
updateStatus()
break
elif galaxy.sectors[aPoint]==SECTORBH:
addOtherLine(" ".join(shotTracks))
addOtherLine("Shot swallowed by a black hole.")
# Any enemy left in the quadrant will definately fire back
enemyFire(True)
updateStatus()
break
elif galaxy.sectors[aPoint]==SECTORSB:
addOtherLine(" ".join(shotTracks))
addOtherLine("You just hit your starbase.")
# Any enemy left in the quadrant will definately fire back
enemyFire(True)
updateStatus()
break
elif galaxy.sectors[aPoint]==SECTORENEMY:
addOtherLine(" ".join(shotTracks))
#NOTE: we might end up 'accidentally' hitting an enemy
# or this is the last point (which is ok either way)
eidx=galaxy.enemys.index((galaxy.currentQ<<6)+aPoint)
if fireWhat=="phaser":
# Need to calculate hit power, and check the enemy shields
dist=calcDistance(galaxy.currentQ,galaxy.currentS,galaxy.currentQ,aPoint)
power=float(eList[eAns[0]])
power=power-0.5*(power-power/dist)
addOtherLine("{:6.1f} hit on enemy at {:02o}".format(power,aPoint))
if power<galaxy.enemyShields[eidx]:
galaxy.enemyShields[eidx]-=power
enemyFire(True)
updateStatus()
return
addOtherLine("Enemy destroyed")
# Now we need to find the enemy that was just destroyed.
# Also remove it from contents (update knowns too)
galaxy.quadrants[galaxy.currentQ]-=0o100
galaxy.knowns[galaxy.currentQ]=galaxy.quadrants[galaxy.currentQ]
for i in range(len(status.trackers)):
if status.trackers[i]>eidx:
# Backup one for all those past the one we will be removing
status.trackers[i]-=1
elif status.trackers[i]==eidx:
# The one pointing to this one is 'destroyed'
status.trackers[i]=TRACKERDONE
# Don't need to do anything for those less than the one we remove
status.enemyCnt-=1
status.enemyDown+=1
if status.enemyCnt==0:
addOtherLine("VICTORY!!!!")
addOtherLine("You defeated all the enemy!"+status.theirTitle)
updateStatus()
getButtonCommand(["Restart"],"","")
supervisor.reload()
# 'technically' this return will never get executed
return
galaxy.enemys.pop(eidx)
galaxy.enemyShields.pop(eidx)
# Any enemy left in the quadrant will definately fire back
enemyFire(True)
updateStatus()
break
return
# Display the short range scan
def showShortScan():
fillSectors(True)
# NOTE: This looks like a strange way of building up a string
# but Python doesn't like lots of string concationation
theRows=[" :0:1:2:3:4:5:6:7:\n"]
for i in range(8):
j=i*8
theRows.append("{}:{}:{}:{}:{}:{}:{}:{}:{}:{}\n".format(i,
galaxy.sectors[j],
galaxy.sectors[j+1],
galaxy.sectors[j+2],
galaxy.sectors[j+3],
galaxy.sectors[j+4],
galaxy.sectors[j+5],
galaxy.sectors[j+6],
galaxy.sectors[j+7],
i))
addOtherLine("".join(theRows))
# Generalized getting a command button
# theList = the complete list of buttons
# always = an optional command that will show on every set of 4 buttons
# page = the command that advances to the next set of items in theList
# (optional, and will always be 4th button)
def getButtonCommand(theList,always,page):
activeCnt=3 # We start with this zero based
blankEntry=BUTTONLEN % " "
cmdList=[blankEntry,blankEntry,blankEntry,blankEntry]
if page!="":
pageIdx=activeCnt
cmdList[activeCnt]=BUTTONLEN % page
activeCnt-=1
else:
# So no button ever 'finds' page
pageIdx=-100
if always!="":
cmdList[activeCnt]=BUTTONLEN % always
activeCnt-=1
# Make activeCnt= length of the available 'theList' locations
activeCnt+=1
beg=0
cnt=len(theList)
for i in range(activeCnt):
if i+beg<cnt:
cmdList[i]=BUTTONLEN % theList[i+beg]
else:
cmdList[i]=blankEntry
cmdLabel.text=" ".join(cmdList)
# Display the commands and loop for a button press
wait2refresh()
picked=-1
# Turn off all the LEDs
whichLED=0
magtag.peripherals.neopixels.fill((0,0,0))
# And loop until we gt a button press
while picked<0:
#magtag.peripherals.neopixels[whichLED]=(0,0,0)
#whichLED=(whichLED+1)&3
#magtag.peripherals.neopixels[whichLED]=(0,255,0)
if magtag.peripherals.button_a_pressed:
if cmdList[0]!=blankEntry:
picked=0
while magtag.peripherals.button_a_pressed: time.sleep(0.01)
if magtag.peripherals.button_b_pressed:
if cmdList[1]!=blankEntry:
picked=1
while magtag.peripherals.button_b_pressed: time.sleep(0.01)
if magtag.peripherals.button_c_pressed:
if cmdList[2]!=blankEntry:
picked=2
while magtag.peripherals.button_c_pressed: time.sleep(0.01)
if magtag.peripherals.button_d_pressed:
if cmdList[3]!=blankEntry:
picked=3
while magtag.peripherals.button_d_pressed: time.sleep(0.01)
if picked==pageIdx:
picked=-1 #So the while loop continues
beg+=activeCnt
if beg>=cnt:
beg=0
for i in range(activeCnt):
if i+beg<cnt:
cmdList[i]=BUTTONLEN % theList[i+beg]
else:
cmdList[i]=blankEntry
print(cmdList)
cmdLabel.text=" ".join(cmdList)
wait2refresh()
#magtag.peripherals.neopixels.fill((0,0,0))
return (picked+beg,cmdList[picked].rstrip())
# doMove - This is easier than actually getting the value
def doMove(newQ,newS):
print("Moving to: {:o}:{:o}".format(newQ,newS))
# We always can move to the new quadrant - need to see if we hit anything
galaxy.currentQ=newQ
fillSectors(False)
if galaxy.sectors[newS]==SECTORSB:
# Landed on the starbase - replenish energy, photons
# We add in the moveEnergy, because it will get taken out when we update
status.energy=status.theShip[2]+status.moveEnergy
status.photons=status.theShip[3]
# Reload any destroyed trackers
for i in range(len(status.trackers)):
if status.trackers[i]==TRACKERDONE:
status.trackers[i]=TRACKERAVAIL
status.shields=0
addOtherLine("Docked at Starbase:{:02o}:{:02o}".format(newQ,newS))
addOtherLine("Shields lowered.")
if galaxy.sectors[newS]==SECTORBH:
# Landed on a black hole - send them to a random quadrant
newQ=random.randint(0,0o77)
galaxy.currentQ=newQ
# Now find them an empty spot to land in
fillSectors(False)
newS=random.randint(0,0o77)
while galaxy.sectors[newS]!=SECTOREMPTY:
newS=random.randint(0,0o77)
addOtherLine("Black Hole bounce to:{:02o}:{:02o}".format(newQ,newS))
# And let the normal 'Empty sector' logic handle the rest
# We may be 'diverting' - so pick an available sector
keepers=[]
for x in around(newS):
if x>=0:
keepers.append(x)
x=random.randint(0,len(keepers)-1)
while galaxy.sectors[keepers[x]]!=SECTOREMPTY:
x=random.randint(0,len(keepers)-1)
print ("New x",x)
# This is an empty sector if we need to bounce
bounceTo=keepers[x]
if galaxy.sectors[newS]==SECTORSTAR:
# Landed on a star - Avoid that
newS=bounceTo
addOtherLine("Star bounce to:{:02o}:{:02o}".format(newQ,newS))
if galaxy.sectors[newS]==SECTORENEMY:
# Landed on an enemy - That's not good
newS=bounceTo
addOtherLine("Enemy bounce to:{:02o}:{:02o}".format(newQ,newS))
if galaxy.sectors[newS]==SECTOREMPTY:
# Simple - nothing there.. so just move them in
addOtherLine("Move to {:02o}:{:02o} complete.".format(newQ,newS))
galaxy.currentS=newS
status.energy-=status.moveEnergy
status.starDate+=status.moveDays
addOtherLine("Used:{:5.1f} Days {:4.1f} Power".format(status.moveDays,status.moveEnergy))
# Note.. enemy might still fire.. but SB shields will need to protect them
enemyMove(status.moveDays)
enemyFire(False)
updateStatus() #update the status based on the new location
return
# Move - pretty complicated
# Screen refresh isn't fast enough to use the buttons to change the coordinates
# So we do each digit separerately - put the value in binary in the neopixels
# And use the buttons to up/down forward/back and update the display when ever
# they 'advance' and accept the value
def getMove():
digits=[galaxy.currentQ>>3,galaxy.currentQ&0o07,galaxy.currentS>>3,galaxy.currentS&0o07]
digitIdx=0
cmdLabel.text=BUTTONLEN % "Warp"+BUTTONLEN % "+1"+BUTTONLEN % "-1"+BUTTONLEN % "Accept"
while True:
addOtherLine("{:} Current move to location is:\n{:1}{:1}:{:1}{:1}".format(status.theirTitle,digits[0],digits[1],digits[2],digits[3]))
blinkIndex(digitIdx)
thisOne=digits[digitIdx]
theAns=getValue(thisOne,0,7)
if theAns[0]==0:
# GO! - take care of moving them to the new location
addOtherLine("Requesting warp factor.")
addOtherLine("To location :{:1}{:1}:{:1}{:1}. {:}?".format(digits[0],digits[1],digits[2],digits[3],status.theirTitle))
cmdLabel.text=BUTTONLEN % "DoIt!"+BUTTONLEN % "+1"+BUTTONLEN % "-1"+BUTTONLEN % "Cancel"
theAns=getValue(status.theShip[1],0,status.theShip[1])
if theAns[0]==3:
addOtherLine("Move Canceled.")
wait2refresh()
return
newQ=(digits[0]<<3)+digits[1]
newS=(digits[2]<<3)+digits[3]
# Calculate energy and days here
# Any bounces will not be far enough to make a difference
dist=calcDistance(galaxy.currentQ, galaxy.currentS, newQ, newS)
warp=theAns[1]
status.moveEnergy=dist*(status.theShip[5]**warp)
status.moveDays=daysOrDistance("days",dist,warp)
# status.moveDays=dist*2.0/warp
print ("Move-> Dist: {:} Energy: {:} Days: {:}".format (dist,status.moveEnergy,status.moveDays))
if status.moveEnergy < status.energy:
# NOTE: We start the 'other' comments here, since doMove might bounce to new coordinates
addOtherLine("Move to: {:02o}:{:02o} At Warp:{:}\nEngaging! {:}".format(newQ,newS,warp,status.theirTitle))
doMove(newQ,newS)
else:
addOtherLine("Insufficient power to move that far.")
addOtherLine("Move Canceled. "+status.theirTitle)
status.moveEnergy=0
status.moveDays=0
# wait2refresh()
return
else:
# Next digit
digits[digitIdx]=theAns[1]
digitIdx+=1
if digitIdx>3:
digitIdx=0
wait2refresh()
# Display the long range scan and also the status
def showLongScan():
# First scan around our current quadrant
# The &00717 blanks four any black hole
for i in around(galaxy.currentQ):
if i>=0:
galaxy.knowns[i]="{0:03o}".format(galaxy.quadrants[i]&0o717)
# Make sure to update the current quadrant too
galaxy.knowns[galaxy.currentQ]="{0:03o}".format(galaxy.quadrants[galaxy.currentQ]&0o717)
addOtherLine(" :*0*:*1*:*2*:*3*:*4*:*5*:*6*:*7*:")
for i in range(8):
j=i*8
addOtherLine("{:}:{:}:".format(i,":".join(galaxy.knowns[j:j+8])))
# Move the enemy after their move.
# Enemy gets to move the same number of days at warp 4
# Enemy has 'infinate' energy - so always can move
def enemyMove(theDays):
# Check to see if the invastion starts (but we only want to do this once :)
if status.starDate>status.actualInvasion and status.actualInvasion>0.0:
status.actualInvasion=0.0
addOtherLine("{:}! The invasion has begun!".format(status.theirTitle))
addOtherLine("The enemy swarm has arrived.")
# We will need to use the current Q when we add enemy. So we need to be able to restore it
saveQ=galaxy.currentQ
print ("Invasion Started")
while len(galaxy.enemys)<MAXENEMY:
# Get a quadrant - Note use of currentQ so we can fill the sector and make sure they aren't on a star
galaxy.currentQ=random.randint(0,0o77)
# Make sure that there is room (will most likely be true)
# They CAN start in a SB now
while galaxy.quadrants[galaxy.currentQ]>0o500:
galaxy.currentQ=random.randint(0,0o77)
galaxy.quadrants[galaxy.currentQ]+=0o100
# Then find a free sector to put them in
fillSectors(False)
newS=random.randint(0,0o77)
while galaxy.sectors[newS]!=SECTOREMPTY:
newS=random.randint(0,0o77)
galaxy.enemys.append((galaxy.currentQ<<6)+newS)
# Enemy shields start at 200
galaxy.enemyShields.append(200)
i=len(galaxy.enemys)
print("Enemy(%2i) at %4o" % (i,galaxy.enemys[i-1]))
# Restore current Q
galaxy.currentQ=saveQ
status.enemyCnt=len(galaxy.enemys)
# So we can check to see if we are in their quadrant already
q=galaxy.currentQ<<6
for idx in range(len(galaxy.enemys)):
anEnemy=galaxy.enemys[idx]
if anEnemy&0o7700 != q:
# We only move if we are NOT in the current quadrant
if status.enemyType>1:
# Random move this time
toq=random.randint(0,63)
else:
# Otherwise, we move towards the player
toq=galaxy.currentQ
# Ignore sectors for now (we will try to keep the sector the same)
wantDist=calcDistance(anEnemy>>6,0,toq,0)
# Enemy always moves at warp 4 (and we assume they always have enough power)
canDist=daysOrDistance("Distance",theDays,4.0)
tx=toq>>3
ty=toq&0o7
if wantDist>canDist:
ratio=canDist/wantDist # We need to adjust the x/y distance by this amount
fx=anEnemy>>9
fy=(anEnemy&0o0700)>>6
qx=fx+round((tx-fx)*ratio)
qy=fy+round((ty-fy)*ratio)
toq=(qx<<3)+qy
# If we actually changed quadrants we need to make sure they move to an empty sector
if (anEnemy>>6) != toq:
# Only do the move if there is room in the 'toq'
# Max of 6 in any one quadrant (we could go 7, but 6 will be 'plenty'
if galaxy.quadrants[toq]&0o700 < 0o500:
# Need to save this so we can check for a free sector
saveQ=galaxy.currentQ
galaxy.currentQ=toq
fillSectors(False)
tos=anEnemy&0o77
while galaxy.sectors[tos]!=SECTOREMPTY:
tos=random.randint(0,63)
galaxy.quadrants[toq]+=0o100
galaxy.quadrants[anEnemy>>6]-=0o100
galaxy.enemys[idx]=tos+(toq<<6)
print("Ended: C:{:04o} F:{:04o} T:{:04o} W:{:}:{:}".format(q,anEnemy,galaxy.enemys[idx],tx,ty))
# Restore current quadrant
galaxy.currentQ=saveQ
return
# Called when ever we want to give the enemy a chance to fire
# poked=true when the ship lands on an enemy or they fire on an ememy
# 0=They attack, least likely to shoot
# 1=They attack, more likely to shoot
# 2=They move random, least likely to shoot
# 3=They move random, most likely to shoot
def enemyFire(poked):
if galaxy.quadrants[galaxy.currentQ]&0o700 ==0 :
# Nobody enemy here.. so we are done
return
# Otherwise.. find the one/ones here
q=galaxy.currentQ<<6
fillSectors(True)
for anEnemy in galaxy.enemys:
if anEnemy&0o7700 == q:
# Found one in this quadrant
print("{:04o} {:04o}".format(anEnemy,q))
odds=random.randint(0,100)
# Everybody will fire if odds <25 or they got poked
# The trigger happy enemy fire if the odds are <75
# print("P/O/T",poked,odds,status.enemyType)
if poked or odds<25 or ((status.enemyType==1 or status.enemyType==3) and odds<75):
shotPoints=trackShot(anEnemy&0o77,galaxy.currentS)
# Might need to reverse the list to get 'from' at 0
if shotPoints[0]!=anEnemy&0o77:
shotPoints.reverse()
print(shotPoints)
for aPoint in shotPoints:
print("{:02o} {:}".format(aPoint,galaxy.sectors[aPoint]))
if galaxy.sectors[aPoint]!=SECTOREMPTY:
if galaxy.sectors[aPoint]==SECTORDOCKED:
print("They hit a starbase")
# Starbase shields protect from all enemy fire (at least for now)
addOtherLine("Starbase deflected shot from {:02o}".format(shotPoints[0]))
continue
elif galaxy.sectors[aPoint]==SECTORME:
print("They hit the ship")
fy=galaxy.currentS>>3
fx=galaxy.currentS&0o07
ty=shotPoints[0]>>3
tx=shotPoints[0]&0o07
dist=(((fx-tx)**2)+((fy-ty)**2))**0.5
power=galaxy.enemyPhaser
power=power-0.5*(power-power/dist)
addOtherLine("{:6.1f} hit from enemy at {:02o}".format(power,shotPoints[0]))
if power>=status.shields:
addOtherLine("SHIP DESTROYED!")
addOtherLine("ALL HANDS ABANDON SHIP!")
magtag.peripherals.play_tone(2093, 0.10) # SOS
time.sleep(0.1)
magtag.peripherals.play_tone(2093, 0.10)
time.sleep(0.1)
magtag.peripherals.play_tone(2093, 0.10)
time.sleep(0.2)
magtag.peripherals.play_tone(2093, 0.20)
time.sleep(0.1)
magtag.peripherals.play_tone(2093, 0.20)
time.sleep(0.1)
magtag.peripherals.play_tone(2093, 0.20)
time.sleep(0.2)
magtag.peripherals.play_tone(2093, 0.10)
time.sleep(0.1)
magtag.peripherals.play_tone(2093, 0.10)
time.sleep(0.1)
magtag.peripherals.play_tone(2093, 0.10)
updateStatus()
getButtonCommand(["Restart"],"","")
supervisor.reload()
# 'technically' this return will never get executed
return
status.shields-=power
addOtherLine("Shields down to {:6.1f}".format(status.shields))
continue
elif aPoint==anEnemy&0o77:
# Skip 'me'
continue
else:
# Must have been a star, blackhole, or another enemy
break
return
###############################################
# Various 'helper' functions
###############################################
# value to lights
def value2lights(digit):
magtag.peripherals.neopixels.fill((0,0,0))
if digit&1==1:
magtag.peripherals.neopixels[0]=(255,255,255)
if digit&2==2:
magtag.peripherals.neopixels[1]=(255,255,255)
if digit&4==4:
magtag.peripherals.neopixels[2]=(255,255,255)
if digit&8==8:
magtag.peripherals.neopixels[3]=(255,255,255)
# Blink the position they are changing red for a second
# [0] is the right most neopixel
def blinkIndex(x):
magtag.peripherals.neopixel_disable = False
magtag.peripherals.neopixels.fill((0,0,0))
for i in range(5):
magtag.peripherals.neopixels[3-x]=(128,0,0)
time.sleep(0.1)
magtag.peripherals.neopixels[3-x]=(0,0,0)
time.sleep(0.1)
# getValue - generic get a value via the buttons
# The current value is displayed in the top LEDs, and the buttons change the value
# 'B' adds 1
# 'C' subtracts 1
# 'A' and 'D' return a tuple (button(0,4), value)
def getValue (value,min,max):
wait2refresh()
thisOne=value
value2lights(thisOne)
while True:
if magtag.peripherals.button_a_pressed:
while magtag.peripherals.button_a_pressed: time.sleep(0.01)
magtag.peripherals.neopixels.fill((0,0,0))
return (0,thisOne)
if magtag.peripherals.button_b_pressed:
# +1
thisOne+=1
# magtag.peripherals.play_tone(2093, 0.10)
if thisOne>max:
thisOne=max
value2lights(thisOne)
while magtag.peripherals.button_b_pressed: time.sleep(0.01)
if magtag.peripherals.button_c_pressed:
# -1
thisOne-=1
# magtag.peripherals.play_tone(1047, 0.10)
if thisOne<min:
thisOne=min
value2lights(thisOne)
while magtag.peripherals.button_c_pressed: time.sleep(0.01)
if magtag.peripherals.button_d_pressed:
while magtag.peripherals.button_d_pressed: time.sleep(0.01)
magtag.peripherals.neopixels.fill((0,0,0))
return (3,thisOne)
# Calculate either the days traveled for a distance
# Or the distance traveled for a number of days
# This really didn't need to be a function, but it keeps the formula in one place
def daysOrDistance(dod, theValue, warp):
if dod=="days":
# return theValue/(warp*5.0)
return theValue/(warp)
else:
# return theValue*5.0*warp
return theValue*warp
# days=((dist*2.0)/warp)/10.0
# days*10=(dist*2.0)/warp
# days*10*warp=dist*2.0
# days*10*warp/2.0=dist
# dist=days*10.0*warp/2.0
# dist/days=10.0*warp/2.0
# 1/days=(5.0*warp)/dist
# days = dist/(warp*5.0)
# The e-Ink needs to wait before a refresh
def wait2refresh():
if len(status.otherLines)>0:
# Collect all the 'other' lines we have been given
otherLabel.text="\n".join(status.otherLines)
# We have shown all these
status.otherLines=[]
# Otherwise, we leave what ever was there in
whichLED=0
counter=0
#magtag.peripherals.neopixels.fill((0,0,0))
#magtag.peripherals.neopixels[whichLED]=(255,0,0)
while board.DISPLAY.time_to_refresh>0.0:
counter+=1
if counter>1000:
#magtag.peripherals.neopixels[whichLED]=(0,0,0)
whichLED=(whichLED+1)&3
#magtag.peripherals.neopixels[whichLED]=(255,0,0)
counter=0
#magtag.peripherals.neopixels[whichLED]=(0,0,0)
board.DISPLAY.refresh()
# Calculate distance (in sectors) between two points
def calcDistance(tq,ts,fq,fs):
# We do the calculations in sectors
x=(((fq&0o07)-(tq&0o07))<<3)+(fs&0o7)-(ts&0o7)
y=((fq&0o70)-(tq&0o70))+(((fs&0o70)-(ts&0o70))>>3)
dist=((x**2)+(y**2))**0.5
# print("Distance: {:} From: {:02o}:{:02o} To: {:02o}:{:02o}".format(dist,fq,fs,tq,ts))
return dist
# Pick the player's title (Sir/Madam) from the list - returns the index
def getTitle():
addOtherLine("How should I address you?")
ans=getButtonCommand(["Sir","Madam"],"","")
return ans[1]
# Pick the player's ship from the list
def getShip():
theShips=(("Scout",15,2000,7,6,1.18),("Cruiser",8,3000,10,4,1.40),("Battleship",4,6000,20,5,1.96))
ans=(0,"")
while ans[1] != "Select":
addOtherLine("Which Ship would you like? {:}".format(status.theirTitle))
ans=getButtonCommand(["Scout","Cruiser","Battleship"],"","")
thisShip=ans[0]
addOtherLine("Select this ship? {:}".format(status.theirTitle))
addOtherLine("\
Type : {:>10}\n\
Warp : {:10}\n\
Power : {:10}\n\
Torpedos: {:10}\n\
Trackers: {:10}".format(theShips[thisShip][0],theShips[thisShip][1],theShips[thisShip][2],theShips[thisShip][3],theShips[thisShip][4]))
ans=getButtonCommand(["Select","Go back"],"","")
return theShips[thisShip]
# Around - returns a list with the coordinates 'around' a give coordinate
# -1 if that coordinate is not valid (off the edge)
def around(X):
# 0 1 2
# 3 x 4
# 5 6 7
# Frist assume everything ok
ans=[0]*8
ans[0]=X-9 # -0o11
ans[1]=X-8 # -0o10
ans[2]=X-7 # -0o10+0o01
ans[3]=X-1 # -0o01
ans[4]=X+1 # +0o01
ans[5]=X+7 # +0o10-0o01
ans[6]=X+8 # +0o10
ans[7]=X+9 # +0o11
r=X>>3
if r==0:
ans[0]=ans[1]=ans[2]=-1
if r==7:
ans[5]=ans[6]=ans[7]=-1