-
Notifications
You must be signed in to change notification settings - Fork 0
/
orbital-battle.nlogo
1775 lines (1470 loc) · 38.8 KB
/
orbital-battle.nlogo
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
globals [
xdrift
ydrift
thrust-amount
missile-thrust-amount
rotation-amount
game-score
]
breed [ships ship]
breed [planets planet]
breed [products product]
breed [missiles missile]
breed [harvesters harvester]
breed [shields shield]
breed [orbital-computers orbital-computer]
breed [courses course]
breed [autonavs autonav]
turtles-own [
xvel
yvel
]
courses-own [
owner
course-timer
]
products-own [
product-type
value
]
ships-own [
player?
fuel
ship-level
autonav?
orbital-computer?
]
missiles-own [
target
fuel
]
harvesters-own [
harvest-range
owner
]
shields-own [
owner
]
orbital-computers-own [
owner
]
autonavs-own [
owner
]
planets-own [
gravity
]
to setup
clear-all
reset-timer
set thrust-amount 0.0001
set missile-thrust-amount 0.0000001
set rotation-amount 15
set game-score 0
;create the player ship
create-ships 1 [
set color blue
set size 3
set heading 0
set player? true
set fuel 10
set label fuel
set ship-level 1
set autonav? false
set orbital-computer? false
setxy random-xcor random-ycor
]
create-computer-ship
;create the planet
create-planets number-of-planets [
set size 3
set shape "circle"
set gravity 0.00000001
;setxy random-xcor random-ycor
]
;If multiple planets, randomize their locations
if count planets > 1 [
ask planets [
setxy random-xcor random-ycor
]
]
reset-ticks
end
to go
let j 0
if not any? ships with [player? = true] [stop]
if count courses = 0 [
move-ships
move-products
move-missiles
move-harvesters
move-shields
move-orbital-computers
move-autonavs
update-scoreboard
if ticks mod 100 = 0 [
ask missiles [set fuel fuel - 1]
if random 1000 = 1 [
set j random 11 + 1
print j
if j <= 6 [launch-fuel]
if j = 7 [launch-weapons]
if j = 8 [launch-harvester]
if j = 9 [launch-shield]
if j = 10 [launch-orbital-computer]
if j = 11 [launch-autonav]
move-computer-ships
]
;check to see if all the ships have been killed off. If so, spawn a few more
if not any? ships with [player? = false] and random 500 = 1 [
repeat random 2 + 1 [
create-computer-ship
]
]
reset-ticks
]
]
if count courses > 0 [move-courses]
update-scoreboard
tick
end
to update-scoreboard
;score
ask patch 49 48 [set plabel (word "SCORE: " game-score)]
;Autonav computer
if any? ships with [player? = true and autonav? = true] [
ask patch -35 48 [set plabel "AUTONAV ONLINE"]
]
;orbital computer
if any? ships with [player? = true and orbital-computer? = true] [
ask patch -10 48 [set plabel "ORBITAL COMPUTER READY"]
]
;weapons
if any? ships with [player? = true and ship-level = 2] [
ask patch 10 48 [set plabel "WEAPONS ARMED"]
]
end
to create-computer-ship
;create the computer controlled ship
create-ships 1 [
set size 3
set shape "lander-level1"
set ship-level 1
set player? false
set fuel 10
set label fuel
set autonav? false
setxy random-xcor random-ycor
]
end
to move-ships
ask ships [
set label fuel
set xdrift 0
set ydrift 0
;first check to see if the ship has an autonav. if not, apply the affects of the planets gravity
if autonav? = false [
ask planets [
if xcor > [xcor] of myself [set xdrift xdrift + gravity]
if xcor < [xcor] of myself [set xdrift xdrift - gravity]
if ycor > [ycor] of myself [set ydrift ydrift + gravity]
if ycor < [ycor] of myself [set ydrift ydrift - gravity]
]
set xvel xvel + xdrift
set yvel yvel + ydrift
]
;autonav creates the effect of slowing down after thrust
if autonav? = true [
if xvel > 0 [set xvel xvel - 0.000000002]
if xvel < 0 [set xvel xvel + 0.000000002]
if yvel > 0 [set yvel yvel - 0.000000002]
if yvel < 0 [set yvel yvel + 0.000000002]
]
setxy (xcor + xvel) (ycor + yvel)
;check for edge of world
if round(xcor) = max-pxcor [
set xvel 0
set yvel 0
set xcor xcor - 1
]
if round(xcor) = min-pxcor [
set xvel 0
set yvel 0
set xcor xcor + 1
]
if round(ycor) = max-pycor [
set xvel 0
set yvel 0
set ycor ycor - 1
]
if round(ycor) = min-pycor [
set xvel 0
set yvel 0
set ycor ycor + 1
]
;check for nabing a product
if any? products in-radius 2 [
let j 5
;gain fuel based on value of product
if [product-type = "fuel"] of one-of products in-radius 2 [
set fuel fuel + [value] of one-of products in-radius 2
]
if [product-type = "weapons"] of one-of products in-radius 2 [
set ship-level 2
]
;die sequence
ask products in-radius 2 [
repeat 5 [
set size j
wait 0.1
set j j - 1
]
die
]
]
if any? harvesters in-radius 2 [
capture-harvester self one-of harvesters in-radius 2
]
if any? shields with [owner = 0] in-radius 2 [
capture-shield self one-of shields in-radius 2
]
if any? orbital-computers with [owner = 0] in-radius 2 [
capture-orbital-computer self one-of orbital-computers in-radius 2
]
if any? autonavs with [owner = 0] in-radius 2 [
capture-autonavs self one-of autonavs in-radius 2
]
;check for death
if player? = true and any? planets in-radius 1 [crash self]
if player? = true and any? other ships in-radius 1 [crash self]
if any? missiles with [target = myself] in-radius 3 [
ask missiles in-radius 3 [die]
crash self
]
;make sure the shape is correct based on level
ifelse player? = true [
if ship-level = 1 and timer > 0.3 [set shape "default"]
if ship-level = 2 and timer > 0.3 [set shape "ship-level2"]
][
if ship-level = 1 and timer > 0.3 [set shape "lander-level1"]
if ship-level = 2 and timer > 0.3 [set shape "lander-level2"]
]
]
end
to move-products
ask products [
set xdrift 0
set ydrift 0
ask planets [
if xcor > [xcor] of myself [set xdrift xdrift + gravity]
if xcor < [xcor] of myself [set xdrift xdrift - gravity]
if ycor > [ycor] of myself [set ydrift ydrift + gravity]
if ycor < [ycor] of myself [set ydrift ydrift - gravity]
]
set xvel xvel + xdrift
set yvel yvel + ydrift
setxy (xcor + xvel) (ycor + yvel)
;check for edge of world
if round(xcor) = max-pxcor [
set xvel 0
set yvel 0
set xcor xcor - 1
]
if round(xcor) = min-pxcor [
set xvel 0
set yvel 0
set xcor xcor + 1
]
if round(ycor) = max-pycor [
set xvel 0
set yvel 0
set ycor ycor - 1
]
if round(ycor) = min-pycor [
set xvel 0
set yvel 0
set ycor ycor + 1
]
if product-type = "fuel" [set label (word "Fuel: " value)]
if product-type = "weapons" [set label "Weapons"]
]
end
to move-computer-ships
;check for randomness of computer players
ask ships with [player? = false] [
;rotate
if random 10 >= 5 [
repeat random 5 + 1 [set heading heading + 15]
]
;thrust
if random 10 >= 5 [
repeat random 5 + 1 [
if fuel > 0 [
set xvel xvel + thrust-amount * dx
set yvel yvel + thrust-amount * dy
set fuel fuel - 1
]
]
]
;fire missile
if ship-level > 1 and fuel >= 10 and random 10 >= 7 [
launch-missile self
]
]
end
to move-missiles
ask missiles [
;if there's computer ships, point towards them
if any? ships with [player? = false] [
if target != nobody [set heading towards target]
if target = nobody [
set target min-one-of ships with [player? = false] [distance self]
]
;check for thrusting at target
if distance one-of ships with [player? = false] <= 20 [
missile-thrust self
]
]
;check fuel and set label (or die)
if fuel <= 0 [die]
if fuel < 50 [set label ""]
if fuel > 500 [set label "*"]
if fuel > 1000 [set label "**"]
if fuel > 1500 [set label "***"]
if fuel > 2000 [set label "****"]
set xdrift 0
set ydrift 0
;apply the affects of the planets gravity
ask planets [
if xcor > [xcor] of myself [set xdrift xdrift + gravity]
if xcor < [xcor] of myself [set xdrift xdrift - gravity]
if ycor > [ycor] of myself [set ydrift ydrift + gravity]
if ycor < [ycor] of myself [set ydrift ydrift - gravity]
]
set xvel xvel + xdrift
set yvel yvel + ydrift
setxy (xcor + xvel) (ycor + yvel)
;check for edge of world
if round(xcor) = max-pxcor [
set xvel 0
set yvel 0
set xcor xcor - 1
]
if round(xcor) = min-pxcor [
set xvel 0
set yvel 0
set xcor xcor + 1
]
if round(ycor) = max-pycor [
set xvel 0
set yvel 0
set ycor ycor - 1
]
if round(ycor) = min-pycor [
set xvel 0
set yvel 0
set ycor ycor + 1
]
]
end
to move-shields
ask shields with [owner = 0] [
set xdrift 0
set ydrift 0
ask planets [
if xcor > [xcor] of myself [set xdrift xdrift + gravity]
if xcor < [xcor] of myself [set xdrift xdrift - gravity]
if ycor > [ycor] of myself [set ydrift ydrift + gravity]
if ycor < [ycor] of myself [set ydrift ydrift - gravity]
]
set xvel xvel + xdrift
set yvel yvel + ydrift
setxy (xcor + xvel) (ycor + yvel)
;check for edge of world
if round(xcor) = max-pxcor [
set xvel 0
set yvel 0
set xcor xcor - 1
]
if round(xcor) = min-pxcor [
set xvel 0
set yvel 0
set xcor xcor + 1
]
if round(ycor) = max-pycor [
set xvel 0
set yvel 0
set ycor ycor - 1
]
if round(ycor) = min-pycor [
set xvel 0
set yvel 0
set ycor ycor + 1
]
]
ask shields with [owner != 0] [
move-to owner
set heading [heading] of owner
]
end
to move-orbital-computers
ask orbital-computers with [owner = 0] [
set xdrift 0
set ydrift 0
ask planets [
if xcor > [xcor] of myself [set xdrift xdrift + gravity]
if xcor < [xcor] of myself [set xdrift xdrift - gravity]
if ycor > [ycor] of myself [set ydrift ydrift + gravity]
if ycor < [ycor] of myself [set ydrift ydrift - gravity]
]
set xvel xvel + xdrift
set yvel yvel + ydrift
setxy (xcor + xvel) (ycor + yvel)
;check for edge of world
if round(xcor) = max-pxcor [
set xvel 0
set yvel 0
set xcor xcor - 1
]
if round(xcor) = min-pxcor [
set xvel 0
set yvel 0
set xcor xcor + 1
]
if round(ycor) = max-pycor [
set xvel 0
set yvel 0
set ycor ycor - 1
]
if round(ycor) = min-pycor [
set xvel 0
set yvel 0
set ycor ycor + 1
]
]
end
to move-autonavs
ask autonavs with [owner = 0] [
set xdrift 0
set ydrift 0
ask planets [
if xcor > [xcor] of myself [set xdrift xdrift + gravity]
if xcor < [xcor] of myself [set xdrift xdrift - gravity]
if ycor > [ycor] of myself [set ydrift ydrift + gravity]
if ycor < [ycor] of myself [set ydrift ydrift - gravity]
]
set xvel xvel + xdrift
set yvel yvel + ydrift
setxy (xcor + xvel) (ycor + yvel)
;check for edge of world
if round(xcor) = max-pxcor [
set xvel 0
set yvel 0
set xcor xcor - 1
]
if round(xcor) = min-pxcor [
set xvel 0
set yvel 0
set xcor xcor + 1
]
if round(ycor) = max-pycor [
set xvel 0
set yvel 0
set ycor ycor - 1
]
if round(ycor) = min-pycor [
set xvel 0
set yvel 0
set ycor ycor + 1
]
]
end
to move-harvesters
ask harvesters [
set xdrift 0
set ydrift 0
ask planets [
if xcor > [xcor] of myself [set xdrift xdrift + gravity]
if xcor < [xcor] of myself [set xdrift xdrift - gravity]
if ycor > [ycor] of myself [set ydrift ydrift + gravity]
if ycor < [ycor] of myself [set ydrift ydrift - gravity]
]
set xvel xvel + xdrift
set yvel yvel + ydrift
setxy (xcor + xvel) (ycor + yvel)
;check for edge of world
if round(xcor) = max-pxcor [
set xvel 0
set yvel 0
set xcor xcor - 1
]
if round(xcor) = min-pxcor [
set xvel 0
set yvel 0
set xcor xcor + 1
]
if round(ycor) = max-pycor [
set xvel 0
set yvel 0
set ycor ycor - 1
]
if round(ycor) = min-pycor [
set xvel 0
set yvel 0
set ycor ycor + 1
]
if any? products with [product-type = "fuel"] in-radius harvest-range and ([owner] of self != 0) [
;visual graphics of harvesters doing their thing
ask patches in-radius harvest-range [set pcolor 49]
ask products in-radius harvest-range [
;harvest fuel
if product-type = "fuel" [
ask [owner] of myself [
set fuel fuel + [value] of myself
]
]
;show graphics
let j 10
repeat 5 [
set size j
wait 0.1
set j j - 1
]
die
]
;reset patch colors
ask patches in-radius (harvest-range + 5) [set pcolor black]
]
]
end
to move-courses
ask courses with [course-timer > 0] [
set xdrift 0
set ydrift 0
ask planets [
if xcor > [xcor] of myself [set xdrift xdrift + gravity]
if xcor < [xcor] of myself [set xdrift xdrift - gravity]
if ycor > [ycor] of myself [set ydrift ydrift + gravity]
if ycor < [ycor] of myself [set ydrift ydrift - gravity]
]
set xvel xvel + xdrift
set yvel yvel + ydrift
setxy (xcor + xvel) (ycor + yvel)
;check for edge of world
if round(xcor) = max-pxcor [
set xvel 0
set yvel 0
set xcor xcor - 1
]
if round(xcor) = min-pxcor [
set xvel 0
set yvel 0
set xcor xcor + 1
]
if round(ycor) = max-pycor [
set xvel 0
set yvel 0
set ycor ycor - 1
]
if round(ycor) = min-pycor [
set xvel 0
set yvel 0
set ycor ycor + 1
]
set course-timer course-timer - 1
]
;kill courses with no fuel left
ask courses with [course-timer < 1] [die]
;clear drawing if no courses remain
if count courses = 0 [clear-drawing]
end
to calculate-courses
ifelse any? ships with [player? = true and fuel > 0 and orbital-computer? = true and count courses = 0] [
ask ships with [player? = true] [
set fuel fuel - 1
]
ask turtles with [breed != planets] [
ask patch-here [
sprout-courses 1 [
set shape "dot"
set course-timer 200000
set owner one-of turtles-here
set xvel [xvel] of owner
set yvel [yvel] of owner
set heading [heading] of owner
pen-down
]
]
]
][
user-message "You need an orbital computer and at least 1 fuel to calculate courses. Also, you cannot calculate course if you are already calculating courses."
]
end
to thrust
ask ships with [player? = true and fuel > 0] [
set xvel xvel + thrust-amount * dx
set yvel yvel + thrust-amount * dy
set fuel fuel - 1
if ship-level = 1 [set shape "ship-thrusting"]
if ship-level = 2 [set shape "ship-level2-thrusting"]
reset-timer
]
end
to missile-thrust [this-missile]
ask this-missile [
set xvel xvel + missile-thrust-amount * dx
set yvel yvel + missile-thrust-amount * dy
]
end
to rotate-clockwise
ask ships with [player? = true] [
set heading heading + rotation-amount
]
end
to rotate-counterclockwise
ask ships with [player? = true] [
set heading heading - rotation-amount
]
end
to launch-orbital-computer
if count orbital-computers with [owner = 0] <= 5 [
ask one-of planets [
ask patch-here [
sprout-orbital-computers 1 [
set xvel xvel + (random-float 0.001 + 0.0002) * dx
set yvel yvel + (random-float 0.001 + 0.0002) * dy
set heading random 360 + 1
set shape "orbital-computer"
set label "Orbital Computer"
set size 4
set color grey
set owner 0
]
]
]
]
end
to launch-autonav
if count autonavs with [owner = 0] = 0 [
ask one-of planets [
ask patch-here [
sprout-autonavs 1 [
set xvel xvel + (random-float 0.001 + 0.0002) * dx
set yvel yvel + (random-float 0.001 + 0.0002) * dy
set heading random 360 + 1
set shape "target"
set label "AutoNav"
set size 4
set color grey
set owner 0
]
]
]
]
end
to launch-shield
if count shields with [owner = 0] <= 5 [
ask one-of planets [
ask patch-here [
sprout-shields 1 [
set xvel xvel + (random-float 0.001 + 0.0002) * dx
set yvel yvel + (random-float 0.001 + 0.0002) * dy
set heading random 360 + 1
set shape "shield"
set label "Shield"
set size 4
set color grey
set owner 0
]
]
]
]
end
to launch-harvester
if count harvesters <= 5 [
ask one-of planets [
ask patch-here [
sprout-harvesters 1 [
set xvel xvel + (random-float 0.001 + 0.0002) * dx
set yvel yvel + (random-float 0.001 + 0.0002) * dy
set heading random 360 + 1
set shape "factory"
set size 3
set color grey
set harvest-range random 8 + 3
set owner 0
set label (word "Range: " harvest-range)
]
]
]
]
end
to launch-weapons
if count products <= 10 [
ask one-of planets [
ask patch-here [
sprout-products 1 [
set xvel xvel + (random-float 0.001 + 0.0002) * dx
set yvel yvel + (random-float 0.001 + 0.0002) * dy
set shape "dart"
set size 3
set color grey
set product-type "weapons"
set heading random 360 + 1
set label "Weapons"
]
]
]
]
end
to launch-fuel
if count products <= 10 [
ask one-of planets [
ask patch-here [
sprout-products 1 [
set xvel xvel + (random-float 0.001 + 0.0002) * dx
set yvel yvel + (random-float 0.001 + 0.0002) * dy
;launch a random product
let launched-product random 4 + 1
if launched-product = 1 [
set shape "cloud"
set size 3
set color blue
set product-type "fuel"
set heading random 360 + 1
set value 5
]
if launched-product = 2 [
set shape "cloud"
set size 3
set color green
set product-type "fuel"
set heading random 360 + 1
set value 10
]
if launched-product = 3 [
set shape "cloud"
set size 3
set color yellow
set product-type "fuel"
set heading random 360 + 1
set value 15
]
if launched-product = 3 [
set shape "cloud"
set size 3
set color orange
set product-type "fuel"
set heading random 360 + 1
set value 30
]
if launched-product = 4 [
set shape "cloud"
set size 3
set color red
set product-type "fuel"
set heading random 360 + 1
set value 50
]
set label value
]
]
]
]
end
to launch-missile [ship-firing-missile]
carefully [
ask ship-firing-missile [
ifelse fuel >= 10 and ship-level > 1 [
set fuel fuel - 10
ask patch-here [
sprout-missiles 1 [
set fuel 2500
set xvel xvel + (random-float 0.001 + 0.0001) * dx
set yvel yvel + (random-float 0.001 + 0.0001) * dy
set shape "dart"
set size 3
set color [color] of ship-firing-missile
set target one-of ships with [who != [who] of ship-firing-missile]
]
]
] [
user-message "You need weapons on your ship and at least 10 fuel to launch a missile."
]
]
][]
end
to capture-orbital-computer [ship-capturing-orbital-computer orbital-computer-being-captured]