-
Notifications
You must be signed in to change notification settings - Fork 8
/
CivClone 0.2.monkey2
1946 lines (1834 loc) · 50.6 KB
/
CivClone 0.2.monkey2
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
#Import "<std>"
#Import "<mojo>"
Using std..
Using mojo..
' Here is how many tiles there are drawn on the screen.
' Currently tested from 16x16 up to 32x32
Global mystartmapwidth:Int=20
Global mystartmapheight:Int=20
Global blinkspeed:Int=5 ' lower is faster
Global turn:Int=1
Global activeunitmovesleft:Float=1
Global gamehasmovesleft:Bool=True
Global cityscreenopen:Bool=False
Global currentcityx:Int
Global currentcityy:Int
Global currentcitysize:Int
Global currentcityname:String
Global currentcityproduction:String
Global currentcityproductiontime:Int
'This variable is increased in the main loop
'if a key is pressed and this value is > 0 then this
'variable is set to 0 again.
Global keydelay:Int=0
Global mousedelay:Int=0
Class greybackground
Field greyimage:Image
Field greycanvas:Canvas
Field mapwidth:Int
Field mapheight:Int
Field tilewidth:Float
Field tileheight:Float
Field mapdepth:Int
'Field mapr:Int[][]
'Field mapg:Int[][]
'Field mapb:Int[][]
Field mapr:Int[,]
Field mapg:Int[,]
Field mapb:Int[,]
Method New(Width:float,Height:float,mapwidth:Float,mapheight:Float)
greyimage = New Image(Width,Height)
greycanvas = New Canvas(greyimage)
Self.mapwidth = mapwidth
Self.mapheight = mapheight
tilewidth = Width/Float(mapwidth)
tileheight = Height/Float(mapheight)
mapr = New Int[mapwidth,mapheight]
mapg = New Int[mapwidth,mapheight]
mapb = New Int[mapwidth,mapheight]
'mapr = New Int[mapwidth][]
'mapg = New Int[mapwidth][]
'mapb = New Int[mapwidth][]
'For Local i:= 0 Until mapwidth
' mapr[i] = New Int[mapheight]
' mapg[i] = New Int[mapheight]
' mapb[i] = New Int[mapheight]
' Next
addrectslayer()
brusheffect2()
smooth()
createimage(greycanvas)
End Method
Method brusheffect2()
For Local i:=0 Until (mapwidth*mapheight)
Local x1:Float=Rnd(0,mapwidth)
Local y1:Float=Rnd(0,mapheight)
Local angle:Int=Rnd(-180,180)
Local dist:Int=Rnd(3,5)
For Local iii:=0 Until 20
For Local ii:=0 Until dist
Local x4:Float=x1+Rnd(-5,5)
Local y4:Float=y1+Rnd(-5,5)
Local x2:Float=x4+Cos(angle)*1
Local y2:Float=y4+Sin(angle)*1
Local x3:Float=x4+Cos(angle)*2
Local y3:Float=y4+Sin(angle)*2
If x2>-1 And y2>-1 And x2<mapwidth And y2<mapheight
If x3>-1 And y3>-1 And x3<mapwidth And y3<mapheight
mapr[x3,y3] = mapr[x2,y2]
mapg[x3,y3] = mapg[x2,y2]
mapb[x3,y3] = mapb[x2,y2]
End If
End If
Next
Next
Next
End Method
Method smooth()
For Local i:=0 Until (mapwidth*mapheight)
Local x:Int=Rnd(0,mapwidth-1)
Local y:Int=Rnd(0,mapheight-1)
Local col1r:Int=mapr[x+1,y]
Local col1g:Int=mapg[x+1,y]
Local col1b:Int=mapb[x+1,y]
Local col2r:Int=mapr[x+1,y+1]
Local col2g:Int=mapg[x+1,y+1]
Local col2b:Int=mapb[x+1,y+1]
Local col3r:Int=mapr[x,y+1]
Local col3g:Int=mapg[x,y+1]
Local col3b:Int=mapb[x,y+1]
Local col4r:Int=(col1r+col2r+col3r)/3
Local col4g:Int=(col1g+col2g+col3g)/3
Local col4b:Int=(col1b+col2b+col3b)/3
mapr[x,y] = col4r
mapg[x,y] = col4g
mapb[x,y] = col4b
Next
End Method
Method addrectslayer()
For Local i:=0 Until (mapwidth*mapheight)
Local w:Int=Rnd(3,15)
Local h:Int=Rnd(3,15)
Local x:Int=Rnd(-3,mapwidth)
Local y:Int=Rnd(-3,mapheight)
Local colm:Int=Rnd(50)
Local colr:Int=205+colm
Local colg:Int=205+colm
Local colb:Int=205+colm
mapdrawrect(x,y,w,h,colr,colg,colb)
Next
End Method
Method mapdrawrect(x:Int,y:Int,w:Int,h:Int,colr:Int,colg:Int,colb:Int)
For Local y1:=y Until y+h
For Local x1:=x Until x+w
If x1>-1 And y1>-1 And x1<mapwidth And y1<mapheight
mapr[x1,y1] = colr
mapg[x1,y1] = colg
mapb[x1,y1] = colb
End If
Next
Next
End Method
Method draw(canvas:Canvas,x:Int=0,y:Int=0)
canvas.DrawImage(greyimage,0,0)
End Method
Method createimage(canvas:Canvas)
For Local y:=0 Until mapheight
For Local x:=0 Until mapwidth
Local colr:Float=mapr[x,y]
Local colg:Float=mapg[x,y]
Local colb:Float=mapb[x,y]
canvas.Color = New Color(255/colr,255/colg,255/colb)
canvas.DrawRect(x*tilewidth,y*tileheight,tilewidth+1,tileheight+1)
Next
Next
canvas.Flush()
End Method
Method distance:Int(x1:Int,y1:Int,x2:Int,y2:Int)
Return Abs(x2-x1)+Abs(y2-y1)
End Method
End Class
Class unituserinterface
Field Width:Int,Height:Int
Field arrowimage:Image
Field arrowcanvas:Canvas
Field ix:Int,iy:Int
Field undockx:Int,undocky:Int
Field undockw:Int,undockh:Int
Field dockx:Int,docky:Int
Field dockw:Int,dockh:Int
Field docked:Bool=false
Method New(Width:Int,Height:Int)
Self.Width = Width
Self.Height = Height
dockside("Left")
arrowimage = New Image(Width/10,Height/10)
arrowcanvas = New Canvas(arrowimage)
arrowimage.Handle = New Vec2f( .5,.5 )
makearrow(arrowcanvas)
End Method
Method update()
' the dock undock buttons controls
Local mx:Int,my:Int
If docked = False
If Mouse.ButtonReleased(MouseButton.Left) Or Touch.FingerReleased(0)
If Mouse.ButtonReleased(MouseButton.Left)
mx = Mouse.X
my = Mouse.Y
End If
If Touch.FingerReleased(0)
mx = Touch.FingerX(0)
my = Touch.FingerY(0)
End If
If rectsoverlap(mx,my,1,1,dockx,docky,dockw,dockh)
docked = True
mousedelay = 0
Return
End If
End If
End If
If docked = True
If Mouse.ButtonReleased(MouseButton.Left) Or Touch.FingerReleased(0)
If Mouse.ButtonReleased(MouseButton.Left)
mx = Mouse.X
my = Mouse.Y
End If
If Touch.FingerReleased(0)
mx = Touch.FingerX(0)
my = Touch.FingerY(0)
End If
If rectsoverlap(mx,my,1,1,undockx,undocky,undockw,undockh)
docked = False
mousedelay = 0
Return
End If
End If
End If
'unit movement controls (the arrows)
If docked = False
If Mouse.ButtonReleased(MouseButton.Left)
Local x:Int=ix-Width/20
Local y:Int=iy-Height/20
Local dx:Int,dy:Int
'find current active unit x and y position
For Local i:=Eachin myunit
If i.active = True
dx=i.x
dy=i.y
Exit
End If
Next
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x,y,Width/10,Height/10)
'Print "Up"+Millisecs()
myunitmethod.moveactiveunitto(dx,dy-1)
redrawgame()
mousedelay = 0
End If
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x,y+100,Width/10,Height/10)
'Print "Down"+Millisecs()
myunitmethod.moveactiveunitto(dx,dy+1)
redrawgame()
mousedelay = 0
End If
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x-50,y+50,Width/10,Height/10)
'Print "Left"+Millisecs()
myunitmethod.moveactiveunitto(dx-1,dy)
redrawgame()
mousedelay = 0
End If
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x+50,y+50,Width/10,Height/10)
'Print "Right"+Millisecs()
myunitmethod.moveactiveunitto(dx+1,dy)
redrawgame()
mousedelay = 0
End If
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x-45,y,Width/10,Height/10)
'Print "LeftUp"+Millisecs()
myunitmethod.moveactiveunitto(dx-1,dy-1)
redrawgame()
mousedelay = 0
End If
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x-45,y+100,Width/10,Height/10)
'Print "LeftDown"+Millisecs()
myunitmethod.moveactiveunitto(dx-1,dy+1)
redrawgame()
mousedelay = 0
End If
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x+45,y,Width/10,Height/10)
'Print "RightUp"+Millisecs()
myunitmethod.moveactiveunitto(dx+1,dy-1)
redrawgame()
mousedelay = 0
End If
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x+45,y+100,Width/10,Height/10)
'Print "RightDown"+Millisecs()
myunitmethod.moveactiveunitto(dx+1,dy+1)
redrawgame()
mousedelay = 0
End If
End If 'end if rectsoverlap
End If 'docked = false
If docked = False
' The unit commands. Taken directly from the controls class
' update there means update here (lazy)
If Mouse.ButtonReleased(MouseButton.Left)
Local x:Int=ix
Local y:Int=iy
'drawunitbutton(canvas,x+100,y,"R")
'drawunitbutton(canvas,x+100,y+32,"B")
'drawunitbutton(canvas,x+100,y+64,"F")
'drawunitbutton(canvas,x+100,y+96,"S")
'drawunitbutton(canvas,x-16,y+34,"E")
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x+100,y,32,32)
'Print "Build Road"
myunitmethod.buildroadatactiveunitpos()
myunitmethod.activateamovableunit()
mousedelay = 0
End If
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x+100,y+32,32,32)
'Print "Build City"
If myunitmethod.iscityatactiveunitpos() = True Then Return
myunitmethod.buildroadatactiveunitpos(False)
Local x:Int,y:Int
'get the active unit x and y coordinates
For Local i:=Eachin myunit
If i.active = True
x = i.x
y = i.y
i.deleteme = True
Exit
End If
Next
mycity.Add(New city(x,y))
myunitmethod.activateamovableunit()
mousedelay = 0
End If
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x+100,y+64,32,32)
'Print "Fortify"
myunitmethod.unitactivefortify()
myunitmethod.activateamovableunit()
mousedelay = 0
End If
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x+100,y+96,32,32)
'Print "Skip turn"
myunitmethod.activeunitskipturn()
myunitmethod.activateamovableunit()
mousedelay = 0
End If
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x-16,y+34,32,32)
'Print "End Turn"
'Update the buildlist of each city
For Local i:=Eachin mycity
i.turnend()
Next
'restore the moves of each unit
For Local i:=Eachin myunit
i.movesleft = i.originalmoves
i.active = False
Next
' set the game movement tip
gamehasmovesleft = True
'increase turn
turn+=1
' find a moveable unit
myunitmethod.activateamovableunit()
' set mouse delay so as not to double feaure click
mousedelay = 0
End If
End If
End If 'end if docked is false
End Method
Method dockside(side:String)
Select side
Case "Right"
ix = Width-150
iy = Height-200
undockx = ix + 100
undocky = iy + 160
undockw = 32
undockh = 20
dockx = ix + 100
docky = iy - 32
dockw = 32
dockh = 20
Case "Left"
ix = 150
iy = Height-200
undockx = ix - 140
undocky = iy + 160
undockw = 32
undockh = 20
dockx = ix - 140
docky = iy - 32
dockw = 32
dockh = 20
End Select
End Method
Method draw(canvas:Canvas)
If docked = False Then
Local x:Int=ix
Local y:Int=iy
'draw the unit commands buttons.
drawunitbutton(canvas,x+100,y,"R")
drawunitbutton(canvas,x+100,y+32,"B")
drawunitbutton(canvas,x+100,y+64,"F")
drawunitbutton(canvas,x+100,y+96,"S")
drawunitbutton(canvas,x-16,y+34,"E")
'draw the arrows
drawarrow(canvas,x,y,"Up")
drawarrow(canvas,x-50,y+50,"Left")
drawarrow(canvas,x+50,y+50,"Right")
drawarrow(canvas,x,y+100,"Down")
drawarrow(canvas,x-35,y,"LeftUp")
drawarrow(canvas,x-35,y+100,"LeftDown")
drawarrow(canvas,x+35,y,"RightUp")
drawarrow(canvas,x+35,y+100,"RightDown")
drawarrow2(canvas,x,y,"Up")
drawarrow2(canvas,x-50,y+50,"Left")
drawarrow2(canvas,x+50,y+50,"Right")
drawarrow2(canvas,x,y+100,"Down")
drawarrow2(canvas,x-45,y,"LeftUp")
drawarrow2(canvas,x-45,y+100,"LeftDown")
drawarrow2(canvas,x+45,y,"RightUp")
drawarrow2(canvas,x+45,y+100,"RightDown")
'draw the dock
canvas.Color = Color.White
canvas.DrawRect(dockx,docky,dockw,dockh)
canvas.Color = Color.Black
canvas.DrawLine(dockx,docky,dockx+dockw/2,docky+dockh)
canvas.DrawLine(dockx+dockw,docky,dockx+dockw/2,docky+dockh)
End If
' if we are docked
If docked = True
'draw the dock
canvas.Color = Color.White
canvas.DrawRect(undockx,undocky,undockw,undockh)
canvas.Color = Color.Black
canvas.DrawLine(undockx,undocky+undockh/2,undockx+undockw/2,undocky)
canvas.DrawLine(undockx+undockw,undocky+undockh/2,undockx+undockw/2,undocky)
End If
End Method
Method drawunitbutton(canvas:Canvas,x:Int,y:Int,t:String)
canvas.Color = Color.Red
canvas.DrawRect(x,y,32,32)
canvas.Color = Color.Black
canvas.DrawRect(x+2,y+2,32-4,32-4)
canvas.Color = Color.White
canvas.DrawText(t,x+16,y+16,.5,.5)
End Method
Method drawarrow(canvas:Canvas,x:Int,y:Int,d:String)
Local rotation:Float=0
Select d
Case "RightUp"
rotation=-Pi/1.4
Case "RightDown"
rotation=Pi/1.4
Case "LeftDown"
rotation=Pi/3.3
Case "LeftUp"
rotation=-Pi/3.3
Case "Up"
rotation=-Pi/2
Case "Down"
rotation=Pi/2
Case "Left"
rotation=0
Case "Right"
rotation=-Pi
End Select
canvas.Color = Color.White
canvas.DrawCircle(x,y,Width/20)
canvas.DrawImage(arrowimage,x,y,rotation)
end Method
Method drawarrow2(canvas:Canvas,x:Int,y:Int,d:String)
Local rotation:Float=0
Select d
Case "RightUp"
rotation=-Pi/1.4
Case "RightDown"
rotation=Pi/1.4
Case "LeftDown"
rotation=Pi/3.3
Case "LeftUp"
rotation=-Pi/3.3
Case "Up"
rotation=-Pi/2
Case "Down"
rotation=Pi/2
Case "Left"
rotation=0
Case "Right"
rotation=-Pi
End Select
canvas.Color = Color.Red
canvas.DrawImage(arrowimage,x,y,rotation)
end Method
Method makearrow(canvas:Canvas)
canvas.Clear(New Color(0,0,0,0))
Local pol:= New Float[14]
Local w:Float = Width/10
Local h:Float = Height/10
pol[0] = 0
pol[1] = h/2
pol[2] = w/3
pol[3] = h
pol[4] = w/3
pol[5] = h/1.5
pol[6] = w
pol[7] = h/1.5
pol[8] = w
pol[9] = h/3
pol[10] = w/3
pol[11] = 0+h/3
pol[12] = w/3
pol[13] = 0
canvas.Color = Color.Grey
canvas.DrawPoly(pol)
canvas.Flush()
End Method
Function rectsoverlap:Bool(x1:Int, y1:Int, w1:Int, h1:Int, x2:Int, y2:Int, w2:Int, h2:Int)
If x1 >= (x2 + w2) Or (x1 + w1) <= x2 Then Return False
If y1 >= (y2 + h2) Or (y1 + h1) <= y2 Then Return False
Return True
End Function
End Class
Class citycontrols
Method controls()
If Keyboard.KeyReleased(Key.Escape) Or Keyboard.KeyReleased(Key.Space)
cityscreenopen = False
keydelay = 0
End If
End Method
End Class
Class cityscreen
Field Width:Int,Height:Int
'the variables for the map of the city
Field tw:Float
Field th:Float
Field citymapx:Int,citymapy:Int
Field citymapw:Int,citymaph:Int
'variables for the production info
Field prodx:Int
Field prody:Int
Field prodw:Int
Field prodh:Int
Method New(Width:Int,Height:Int)
Self.Width = Width
Self.Height = Height
tw = myworld.tw
th = myworld.th
' fill the city map variables
citymapw = tw*5
citymaph = th*5
citymapx = Width/2-citymapw/2
citymapy = Height/2-citymaph/2
' fill the city production info variables
prodx = Width/1.5
prody = Height/1.5
prodw = Width/3.3
prodh = Height/3.3
End Method
Method draw(canvas:Canvas)
canvas.Color = Color.Black
canvas.DrawRect(0,0,Width,Height)
canvas.Color = Color.White
mygreybackground.draw(canvas)
drawcitymap(canvas)
drawproductioninfo(canvas)
canvas.Color = Color.White
canvas.DrawText("Press Space to Exit",Width/2,Height-20,.5,.5)
End Method
Method drawproductioninfo(canvas:Canvas)
canvas.Color = Color.White
canvas.DrawRect(prodx-2,prody-2,prodw+4,prodh+4)
canvas.Color = Color.Black
canvas.DrawRect(prodx,prody,prodw,prodh)
canvas.Color = Color.White
canvas.DrawText("Current Production :",prodx+10,prody+10)
canvas.DrawText(currentcityproduction,prodx+10,prody+30)
canvas.DrawText(currentcityproductiontime+" Turns Left",prodx+10,prody+50)
End Method
'Here we use the buffer image of the map
'we draw it so the city we selected is in the center.
Method drawcitymap(canvas:Canvas)
'
' Draw a border around the city map
Local rec:Recti<Int>
rec.X = citymapx-2
rec.Y = citymapy-2
rec.Size = New Vec2i(citymapw+4,citymaph+4)
canvas.Scissor = rec
canvas.Clear(Color.White)
' Draw the inside of the city map
rec = New Recti<Int>
rec.X = citymapx
rec.Y = citymapy
rec.Size = New Vec2i(citymapw,citymaph)
canvas.Scissor = rec
canvas.Clear(Color.Black)
' Draw the city map
rec = New Recti<Int>
rec.X = citymapx
rec.Y = citymapy
rec.Size = New Vec2i(citymapw,citymaph)
canvas.Scissor = rec
' Get the location to draw the buffer map image with.
Local offsetx:Int=(((myworld.mw*myworld.tw)/2)-(currentcityx*myworld.tw))-myworld.tw/2
Local offsety:Int=(((myworld.mh*myworld.th)/2)-(currentcityy*myworld.th))-myworld.th/2
'Print sx
canvas.Color = Color.White
canvas.DrawImage(myworld.image,offsetx,offsety)
' Draw the city.
mycitymethod.drawcity(canvas,citymapx+2*myworld.tw,citymapy+2*myworld.th,myworld.tw,myworld.th,currentcitysize,currentcityname)
'Restore scissor area
rec = New Recti<Int>
rec.X = 0
rec.Y = 0
rec.Size = New Vec2i(Width,Height)
canvas.Scissor = rec
End Method
End Class
' Controls like mouse pressed and keyboard
Class controls
'fortify unit (f key)
Method fortifyunit()
If Keyboard.KeyReleased(Key.F)
myunitmethod.unitactivefortify()
myunitmethod.activateamovableunit()
End If
End Method
'If press on city then open city sceen
Method opencityscreen()
If Keyboard.KeyDown(Key.LeftShift) = False
If Mouse.ButtonReleased(MouseButton.Left)
If mycitymethod.hascityatmousepos()
currentcityx = Mouse.X / myworld.tw
currentcityy = Mouse.Y / myworld.th
currentcityname = mycitymethod.getcitynameat(currentcityx,currentcityy)
currentcitysize = mycitymethod.getcitysizeat(currentcityx,currentcityy)
currentcityproduction = mycitymethod.getproductionat(currentcityx,currentcityy)
currentcityproductiontime = mycitymethod.getproductiontimeat(currentcityx,currentcityy)
cityscreenopen = True
End If
End If
End If
End Method
'unit skip turn (space)
Method activeunitskipturn()
If Keyboard.KeyReleased(Key.Space)
myunitmethod.activeunitskipturn()
myunitmethod.activateamovableunit()
End If
End Method
' build a road
Method buildroad()
If Keyboard.KeyReleased(Key.R)
myunitmethod.buildroadatactiveunitpos()
'find next movable unit
myunitmethod.activateamovableunit()
End If
End Method
' End of turn
Method myendofturn()
If Keyboard.KeyReleased(Key.Enter) Or Mouse.ButtonReleased(MouseButton.Middle)
'Update the buildlist of each city
For Local i:=Eachin mycity
i.turnend()
Next
'restore the moves
For Local i:=Eachin myunit
i.movesleft = i.originalmoves
i.active = False
Next
'rest moves
gamehasmovesleft = true
'increase turn
turn+=1
' activate a moveable unit
myunitmethod.activateamovableunit()
End If
End Method
' if mouse on unit then activate unit
Method activateunit()
If Mouse.ButtonReleased(MouseButton.Left) = False Then Return
Local x:Int=Mouse.X / myworld.tw
Local y:Int=Mouse.Y / myworld.th
If myunitmethod.ismovableunitatpos(x,y) = False Then return
myunitmethod.unitsactivedisable()
myunitmethod.activatemovableunitatpos(x,y)
If x > myworld.mw/2 Then
myunituserinterface.dockside("Left")
Else
myunituserinterface.dockside("Right")
End If
'If Mouse.X > myworld.mw/2 Then
' myunituserinterface.dockside("Left")
' Else
' myunituserinterface.dockside("Right")
'End If
End Method
' if pressed b then build city at active unit
Method buildcity()
If Keyboard.KeyReleased(Key.B)
If myunitmethod.iscityatactiveunitpos() = true Then Return
' build a road there
myunitmethod.buildroadatactiveunitpos(False)
Local x:Int,y:Int
'get the active unit x and y coordinates
For Local i:=Eachin myunit
If i.active = True
x = i.x
y = i.y
i.deleteme = True
Exit
End If
Next
mycity.Add(New city(x,y))
myunitmethod.activateamovableunit()
End If
End Method
' add a unit to the map (cheat)
Method addunit(canvas:Canvas,Width:Int,Height:Int)
If Keyboard.KeyDown(Key.LeftShift)
If Mouse.Y / myworld.th < myworld.mh-1
If Mouse.ButtonReleased(MouseButton.Left)
If myworld.map[Mouse.X/myworld.tw,Mouse.Y/myworld.th] > 5
myunit.Add(New unit(Mouse.X/myworld.tw,Mouse.Y/myworld.th))
myunitmethod.removefog(Mouse.X/myworld.tw,Mouse.Y/myworld.th)
If Mouse.X > Width/2 Then
myunituserinterface.dockside("Left")
Else
myunituserinterface.dockside("Right")
End If
redrawgame()
End If
End If
End If
End If
End Method
' add a unit to the map (cheat)
Method addunitat(x:Int,y:int)
If myworld.map[Mouse.X/myworld.tw,Mouse.Y/myworld.th] > 5
myunit.Add(New unit(x,y))
myunitmethod.removefog(x,y)
If x > myworld.mw/2 Then
myunituserinterface.dockside("Left")
Else
myunituserinterface.dockside("Right")
End If
redrawgame()
End If
End Method
Method moveunit(canvas:Canvas,Width:Int,Height:Int)
If Mouse.ButtonReleased(MouseButton.Right)
If Mouse.Y / myworld.th < myworld.mh-1
Local x:Int=Mouse.X / myworld.tw
Local y:Int=Mouse.Y / myworld.th
myunitmethod.moveactiveunitto(x,y)
'updatemapingame(canvas,Width,Height)
redrawgame()
End If
End If
End Method
End Class
Class city
Class production
Field name:String="Settler"
Field turns:Int=4
End Class
Field x:Int
Field y:Int
Field size:Int=1
Field deleteme:Bool=False
Field name:String
Field buildlist:Stack<production>
Method New(x:Int,y:Int)
If cityatpos(x,y) = True Then deleteme = True ; Return
buildlist = new Stack<production>
buildlist.Add(new production())
buildlist.Add(new production())
Self.x = x
Self.y = y
name = randomcityname()
myunitmethod.removeactiveunit()
End Method
'Give the city a random name
Method randomcityname:String()
Local newname:String
Local firstname:String[] = New String[]( "New",
"Old",
"First")
local secondname:String[] = New String[]( "Berlin",
"Brussel",
"Madrid",
"Paris",
"Antwerp",
"Rotterdam",
"Rome",
"London",
"Milan",
"Vienna",
"Hamburg",
"Budapest",
"Warsaw",
"Barcelona",
"Munich",
"Prague",
"Sofia",
"Cologne",
"Amsterdam")
Local exitloop:Bool=False
Repeat
newname = firstname[Rnd(firstname.Length)] + " " + secondname[Rnd(secondname.Length)]
exitloop=True
For Local i:=Eachin mycity
If i.name = newname Then exitloop=False
Next
If exitloop = True Then Exit
Forever
Return newname
End Method
'return true if there is a city at the input coords
Method cityatpos:Bool(x:Int,y:Int)
For Local i:=Eachin mycity
If i.x = x And i.y = y Then Return True
Next
Return False
End Method
'draw the city
Method draw(canvas:Canvas)
Local mx:Int=x*myworld.tw
Local my:Int=y*myworld.th
Local tw:Int=myworld.tw
Local th:Int=myworld.th
' Local rec:Recti<Int>
' rec.X = mx-40
' rec.Y = my-40
' rec.Size = New Vec2i(tw+80,th+80)
' canvas.Scissor = rec
mycitymethod.drawcity(canvas,mx,my,tw,th,size,name)
End Method
Method turnend()
If buildlist.Length=0 Then Return
buildlist.Top.turns-=1
If buildlist.Top.turns=0
mycontrols.addunitat(x,y)
buildlist.Pop()
End If
End Method
End Class
'city methods
Class citymethod
Method getproductionat:String(x:int,y:Int)
For Local i:=Eachin mycity
If i.x = x And i.y = y
If i.buildlist.Length>0
Return i.buildlist.Top.name
End If
End If
Next
Return "No Production"
End Method
Method getproductiontimeat:int(x:int,y:int)
For Local i:=Eachin mycity
If i.x = x And i.y = y
If i.buildlist.Length>0
return i.buildlist.Top.turns
End If
End If
Next
Return -1
End Method
Method getcitynameat:String(x:Int,y:Int)
For Local i:=Eachin mycity
If i.x = x and i.y = y Then Return i.name
Next
Return ""
End Method
Method getcitysizeat:int(x:Int,y:Int)
For Local i:=Eachin mycity
If i.x = x and i.y = y Then Return i.size
Next
Return 0
End Method
Method hascityatmousepos:Bool()
Local x:Int=Mouse.X / myworld.tw
Local y:Int=Mouse.Y / myworld.th
For Local i:=Eachin mycity
If i.x = x And i.y = y
Return True
End If
Next
Return False
End Method
Method drawcity(canvas:Canvas,mx:Int,my:Int,tw:int,th:int,size:int,name:string)
canvas.Color = New Color(1,0,0)
canvas.DrawRect(mx,my,tw,th)
canvas.Color = New Color(1,1,1)
canvas.DrawRect(mx+4,my+4,tw-8,th-8)
canvas.Color = New Color(0,0,0)
canvas.DrawText(size,mx+tw/2,my+th/2,.5,.5)
canvas.Color = New Color(0,0,0)
canvas.DrawText(name,(mx+tw/2),(my),0.5,.8)
canvas.Color = New Color(1,1,1)
canvas.DrawText(name,(mx+tw/2)+1,(my)+1.2,0.5,.8)
End Method
End Class
' Methods to modify units
Class unitmethod
Method removefog(x:int,y:int)
Local lefttop:Bool=False
Local righttop:Bool=False
Local leftbottom:Bool=False
Local rightbottom:Bool=False
If x-2 >= 0 And y-2 >= 0 And myworld.fogmap[x-2,y-2] = True Then lefttop = True
If x+2 < myworld.mw And y-2 >=0 And myworld.fogmap[x+2,y-2] = True Then righttop = True
If x-2 >= 0 And y+2 <myworld.mh And myworld.fogmap[x-2,y+2] = True Then leftbottom = True
If x+2 < myworld.mw And y+2 < myworld.mh And myworld.fogmap[x+2,y+2] = True Then rightbottom = True
For Local y1:=y-2 To y+2
For Local x1:=x-2 To x+2
If x1>=0 And y1>=0 And x1<myworld.mw And y1<myworld.mh
myworld.fogmap[x1,y1] = False
End if
Next
Next
If lefttop = True Then myworld.fogmap[x-2,y-2] = True
If righttop = True Then myworld.fogmap[x+2,y-2] = true
If leftbottom = True Then myworld.fogmap[x-2,y+2] = True
If rightbottom = True Then myworld.fogmap[x+2,y+2] = True
End Method
'fortify the active unit
Method unitactivefortify()
For Local i:=Eachin myunit
If i.active = True
i.active = False
i.visible = True
i.movesleft = 0
i.fortify = True
Exit
End If
Next
End Method
' skip the turn of a unit (set its moves to 0)
Method activeunitskipturn()
For Local i:=Eachin myunit
If i.active = True
i.active = False
i.visible = True
i.movesleft = 0
Return
End If
Next
End Method
' returns if there is a city at the active unit its position
Method iscityatactiveunitpos:bool()
For Local i:=Eachin myunit
If i.active=True
For Local i2:=Eachin mycity
If i2.x = i.x And i2.y = i.y Then Return true
Next
End If
Next
Return False
End Method
' build a road at the active unit its position
Method buildroadatactiveunitpos(modifyunit:Bool=True)
For Local i:=Eachin myunit
If i.active = True And myworld.roadmap[i.x,i.y].hasroad = False
If modifyunit = True Then
i.active = False
i.movesleft = 0
i.visible = True
End If
myworld.roadmap[i.x,i.y].hasroad = True
' has road north