forked from Pakz001/Monkey2examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CivClone 0.6.monkey2
3767 lines (3549 loc) · 99 KB
/
CivClone 0.6.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..
Global version:String="v0.6"
'Texture quality
Global texturequality:String="Low" 'High , Medium and Low
' 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 currentcityfarms:Int
Global currentcitymines:Int
Global currentcitybarracks:Bool
Global currentcitywalls:Bool
Global currentcitysize:Int
Global currentcityname:String
Global currentcityfood:Int
Global currentcityresources:Int
Global currentcityproduction:String
Global currentcityproductiontime:Int
Global currentcityharbor:Bool=False
Global currentcitycoastal:Bool=False
' This variable is used to give a weight to
' the wait feature. This way we can cycle though
' the list of waiting units.
Global mywaitweight:Int=0
'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
' This class is a* pathfinder.
' path is a list that hold the x and y coordinates
' of the path. The data in the path list is a class
' called pathnode and contain the x and y variables.
'
Class pathfinder
Class openlist
Field x:Int
Field y:Int
Field f:Int
Field g:Int
Field h:Int
Field px:Int
Field py:Int
Method New( x:Int=0,y:Int=0,f:Int=0,
g:Int=0,h:Int=0,px:Int=0,py:Int=0)
Self.x=x
Self.y=y
Self.f=f
Self.g=g
Self.h=h
Self.px=px
Self.py=py
End Method
End Class
Class closedlist
Field x:Int
Field y:Int
Field px:Int
Field py:Int
Method New(x:Int,y:Int,px:Int,py:Int)
Self.x = x
Self.y = y
Self.px = px
Self.py = py
End Method
End Class
Class pathnode
Field x:Int
Field y:Int
Method New(x:Int,y:Int)
Self.x = x
Self.y = y
End Method
End Class
Field cl:List<closedlist>
Field path:List<pathnode>
Field ol:List<openlist>
' -1 is impassable
Field map:Int[,]
Field olmap:Int[,]
Field clmap:Int[,]
Field mapwidth:Int
Field mapheight:Int
' these are the start and end coordinates
Field sx:Int,sy:Int,ex:Int,ey:int
'for debugging
Field tilewidth:Float,tileheight:Float
Method New(newmap:Int[,])
' If the inputted array is at zero and zero then return
If newmap.GetSize(0) = 0 Or newmap.GetSize(1) = 0 Then
Print "array in pathfinder class width and or height at zero..."
Return
End If
' get the mapwidth and mapheight
Self.mapwidth = newmap.GetSize(0)
Self.mapheight = newmap.GetSize(1)
' copy the input array into this class its map array
map = new Int[mapwidth,mapheight]
For Local y:=0 Until mapheight
For Local x:=0 Until mapwidth
map[x,y] = newmap[x,y]
Next
Next
' For debugging / testing
'
tilewidth = 10
tileheight = 10
'
path = New List<pathnode>
End Method
' turn water tiles into impassable tiles
Method mapforlandunits()
For Local y:=0 Until mapheight
For Local x:=0 Until mapwidth
If map[x,y] <= 5 Then map[x,y] = -1
Next
Next
End Method
'turn land tiles into impassable tiles
Method mapforseaunits()
For Local y:=0 Until mapheight
For Local x:=0 Until mapwidth
If map[x,y] > 5 Then map[x,y] = -1
Next
Next
End Method
' This is the a* pathfinder method.
' sx and sy = start x and y
' ex and ey = end coordinates
'
Method findpath:Bool(sx:Int,sy:Int,ex:Int,ey:Int)
' Store the start and end coordinates
' locally in the class
Self.ex = ex
Self.ey = ey
Self.ex = ex
Self.ey = ey
' if start and end is same then message that and return
If sx = ex And sy = ey Then
Print "Start coordinates same as end coordinates in findpath method in pathfinder class"
Return False
End If
' Reset/Create this class its arrays and lists
olmap = New Int[mapwidth,mapheight]
clmap = New Int[mapwidth,mapheight]
ol = New List<openlist>
cl = New List<closedlist>
path = New List<pathnode>
' Start pathfinding
ol.AddFirst(New openlist(sx,sy))
Local tx:Int
Local ty:Int
Local tf:Int
Local tg:Int
Local th:Int
Local tpx:Int
Local tpy:Int
Local newx:Int
Local newy:Int
Local lowestf:Int
olmap[sx,sy] = 1
While ol.Empty = False
lowestf = 100000
For Local i:=Eachin ol
If i.f < lowestf
lowestf = i.f
tx = i.x
ty = i.y
tf = i.f
tg = i.g
th = i.h
tpx = i.px
tpy = i.py
End If
Next
If tx = ex And ty = ey
cl.AddLast(New closedlist(tx,ty,tpx,tpy))
findpathback()
Return True
Else
removefromopenlist(tx,ty)
olmap[tx,ty] = 0
clmap[tx,ty] = 1
cl.AddLast(New closedlist(tx,ty,tpx,tpy))
For Local y:=-1 To 1
For Local x:=-1 To 1
newx = tx+x
newy = ty+y
If newx>=0 And newy>=0 And newx<mapwidth And newy<mapheight
If map[newx,newy] >= 0 '-1 is illegal
If olmap[newx,newy] = 0
If clmap[newx,newy] = 0
olmap[newx,newy] = 1
'
' gg is the go to cost. Modify the cost here.
' example: roads tile (5) in array cost 2
' while forrest tile (8) in array cost 8
' below it just adds the map int value and one (heightmap)
Local gg:Int
If myworld.roadmap[newx,newy].hasroad = True
gg = 1
else
gg = map[newx,newy]+1
End If
Local hh:Int = distance(newx,newy,ex,ey)
Local ff:Int = gg+hh
ol.AddLast(New openlist(newx,newy,ff,gg,hh,tx,ty))
End If
End If
End If
End If
Next
Next
End If
Wend
Return False
End Method
Method findpathback:Bool()
Local x:Int=ex
Local y:Int=ey
path.AddFirst(New pathnode(x,y))
Repeat
For Local i:=Eachin cl
If i.x = x And i.y = y
x = i.px
y = i.py
path.AddFirst(New pathnode(x,y))
End If
Next
If x = sx And y = sy Then Return True
Forever
Return False
End Method
Method removefromopenlist:Void(x1:Int,y1:Int)
For Local i:=Eachin ol
If i.x = x1 And i.y = y1
ol.Remove(i)
Exit
End If
Next
End Method
Function distance:Int(x1:Int,y1:Int,x2:Int,y2:Int)
Return Abs(x2-x1)+Abs(y2-y1)
End Function
' Debug test function/methods
' Draw the map to the screen
Method drawmap:Void(canvas:Canvas)
If mapwidth = 0 Or mapheight = 0
Print "Error in pathfinder class (drawmap) no width and height"
Return
End If
For Local y:=0 Until mapheight
For Local x:=0 Until mapwidth
If map[x,y]>=0
canvas.Color = New Color(0,(Float(map[x,y])*10)/255,0)
canvas.DrawRect(x*tilewidth,y*tileheight,tilewidth,tileheight)
End If
Next
Next
End Method
' draw the path to the screen
Method drawpath:Void(canvas:Canvas)
If path.Count() = 0 Then return
Local cnt:Int=1
For Local i:=Eachin path
canvas.Color = Color.Yellow
canvas.DrawOval(i.x*tilewidth,i.y*tileheight,4,4)
canvas.Color = Color.White
canvas.DrawText(cnt,i.x*tilewidth,i.y*tileheight)
cnt+=1
Next
End Method
End Class
' This class contains the code for displaying
' a unit window (left click on stacked unit)
'
'
Class unitview
'This is the list of units at x,y
Class unitlist
Field id:Int
Field movesleft:Float
Field name:String
Field seaunit:Bool
Field landunit:Bool
Field fortify:Bool
Field onboard:Bool
Method New()
End Method
End Class
Field myunitlist:Stack<unitlist>
' Is the unit view active
Field active:Bool=False
' Width and Height of the screen
Field Width:Int,Height:Int
' at which position are we currently
' displaying units
Field positionx:Int,positiony:Int
' some local variables
' tile width/height and map width
' and height
Field tw:Float,th:Float
Field mw:Float,mh:Float
'unit view window x,y,w,h
Field uvx:Int,uvy:Int
Field uvw:Int,uvh:Int
' Collumns
Field c1x:Int,c1y:Int
Field c2x:Int,c2y:Int
Field c3x:Int,c3y:Int
Field c4x:Int,c4y:Int
' activate all units button
Field activateallx:Int,activateally:Int
Field activateallw:Int,activateallh:Int
' fortify all units button
Field unitfortifyallx:Int,unitfortifyally:Int
Field unitfortifyallw:Int,unitfortifyallh:Int
' exit button
Field eruitx:Int,eruity:Int
Field eruitw:Int,eruith:Int
Field eruits:String
'initiate the unitview class
Method New(Width:Int,Height:Int)
' local screen width and height
Self.Width = Width
Self.Height = Height
' local tile and map width and height
tw = myworld.tw
th = myworld.th
mw = myworld.mw
mh = myworld.mh
' inititate list of units
myunitlist = New Stack<unitlist>
' set variable for unitviewwindow
uvw = 340
uvh = 300
uvx = Width / 2 - uvw / 2
uvy = 100
' Set varaibles for the collumns
c1x = uvx 'the name collumn
c2x = uvx+120 'the moves collumn
c3x = uvx+170 'the fortify collumn
c4x = uvx+240 'the type collumn
' temp populate the list
' myunitlist.Push(New unitlist())
' myunitlist.Top.name="Settlers"
' myunitlist.Top.movesleft = 1.5
' myunitlist.Top.landunit = True
' myunitlist.Top.fortify = False
' set up the variables for the exit knop
eruitx = uvx +5
eruity = uvy+uvh - 32
eruitw = 100
eruith = 20
eruits = "Exit"
End Method
' Call this to read all units at x,y
Method initiateat(x:Int,y:Int)
myunitview.active = True
myunitlist = New Stack<unitlist>
myunitmethod.unitsactivedisable()
For Local i:=Eachin myunit
If i.x = x And i.y = y
myunitlist.Push(New unitlist())
myunitlist.Top.id = i.id
myunitlist.Top.fortify = i.fortify
myunitlist.Top.movesleft = i.movesleft
myunitlist.Top.landunit = i.landunit
myunitlist.Top.seaunit = i.seaunit
myunitlist.Top.name = i.name
End If
Next
End Method
' write modifications to the units
Method updateunits()
For Local i:=Eachin myunitlist
for Local i2:=Eachin myunit
If i.id = i2.id
i2.fortify = i.fortify
End If
Next
Next
End Method
' Update the unitview (mouse and keys (interacts))
Method update()
' if not active then return
If active = False Then Return
If mousedelay < 20 Then Return
' Mouse in window
If Mouse.ButtonReleased(MouseButton.Left)
'if pressed in the list
If rectsoverlap(Mouse.X,Mouse.Y,1,1,uvx,uvy,uvw,uvh-32)
If myunitlist.Length
Local y1:Int=20
For Local i:=Eachin myunitlist
If rectsoverlap(Mouse.X,Mouse.Y,1,1,c1x,uvy+y1,100,20)
'Print i.name
If i.movesleft > .3
i.fortify = False
updateunits()
myunitmethod.activateamovableunitid(i.id)
active = False
mousedelay = 0
End If
End If
If rectsoverlap(Mouse.X,Mouse.Y,1,1,c3x,uvy+y1,50,20)
If i.fortify = True Then i.fortify = False Else i.fortify = true
End If
y1+=20
If y1 > uvh-70 Then exit
Next
End If
mousedelay = 0
End If
' If pressed on the exit button
If rectsoverlap(Mouse.X,Mouse.Y,1,1,eruitx,eruity,eruitw,eruith)
updateunits()
myunitmethod.activateamovableunit()
active = False
mousedelay = 0
End If
End If
End Method
' Draw the unitview to the canvas
Method draw(canvas:Canvas)
'if not active then return
If active = False Then Return
' Draw the window
' border
Local rec := New Recti<Int>
rec.X = 0
rec.Y = 0
rec.Size = New Vec2i(Width,Height)
canvas.Scissor = rec
canvas.Color = Color.White
canvas.DrawRect(uvx-2,uvy-2,uvw+4,uvh+4)
' inside
rec = New Recti<Int>
rec.X = uvx
rec.Y = uvy
rec.Size = New Vec2i(uvw,uvh)
canvas.Scissor = rec
canvas.Color = New Color(.5,.5,.5)
canvas.DrawImage(mygreybackground.greyimage,0,0)
'canvas.Clear(Color.Black)
' Draw the units
' window title
canvas.Color = Color.Black
canvas.DrawText("Unit",c1x,uvy)
canvas.DrawText("Moves",c2x,uvy)
canvas.DrawText("Fortified",c3x,uvy)
canvas.DrawText("Type",c4x,uvy)
' Draw the units list
If myunitlist.Length
Local y1:Int=0
For Local i:=Eachin myunitlist
Local s:String
Local y2:Int=uvy+20
Local flag:String=""
canvas.DrawText(i.name,c1x,y2+y1) 'name
flag = i.movesleft
flag = flag.Mid(0,3)
canvas.DrawText(flag,c2x,y2+y1) 'movesleft
If i.fortify = True Then flag="Yes" Else flag="No"
canvas.DrawText(flag,c3x,y2+y1)
If i.landunit Then flag = "Land" Else flag="Sea"
canvas.DrawText(flag,c4x,y2+y1)
y1+=20
If y1 > uvh-90 Then exit
Next
End If
' Draw the exit button
canvas.Color = Color.White
canvas.DrawRect(eruitx-2,eruity-2,eruitw+4,eruith+4)
canvas.Color = Color.Black
canvas.DrawRect(eruitx,eruity,eruitw,eruith)
canvas.Color = Color.White
canvas.DrawText(eruits,eruitx+eruitw/2,eruity+eruith/2,.5,.5)
' restore canvas scissor
rec = New Recti<Int>
rec.X = 0
rec.Y = 0
rec.Size = New Vec2i(Width,Height)
canvas.Scissor = rec
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 gamemessage
'This class contains the messages
Class message
Field tekst:String
Method New(tekst:String)
Self.tekst = tekst
End Method
End Class
'store the messages in a stack
Field mymessage:Stack<message>
Field Width:Int,Height:Int
Field time:Int 'count up to timeover means new message
Field timeover:Int=60'message dissapear time
Field currentmessage:String 'this contains the current message
Method New(Width:Int,Height:Int)
Self.Width = Width
Self.Height = Height
'create a message stack
mymessage = New Stack<message>
End Method
'Here we add a message to the system
Method pushmessage(tekst:String)
mymessage.Push(New message(tekst))
' time=timeover-5
End Method
'this method updates the messages
'removing old messages
Method update()
If mymessage.Length > 5 Then timeover = 20 Else timeover = 60
If mymessage.Length > 20 Then timeover = 5
time+=1
If time > timeover
time = 0
currentmessage = ""
If mymessage.Length
currentmessage = mymessage.Top.tekst
mymessage.Pop()
End If
End If
End Method
'Draw the messages to the canvas
Method drawmessage(canvas:Canvas)
If currentmessage = "" Then Return
Local mx:Float=Width/2-200
Local my:Float=10
canvas.PushMatrix()
canvas.Scale(1.2,1.2)
canvas.Color = Color.White
canvas.DrawRect(mx/1.2-2,my/1.2-2,400+4,20+4)
canvas.Color = Color.Grey
canvas.DrawRect(mx/1.2,my/1.2,400,20)
canvas.Color = Color.White
canvas.DrawText(currentmessage,(Width/2)/1.2,my/1.2+2,.5,0)
canvas.PopMatrix()
End Method
End Class
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(colr/255,colg/255,colb/255)
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
' buttons for the interface
Field brx:Int,bry:Int 'button R (road)
Field bbx:Int,bby:Int 'button B (build city)
Field bfx:Int,bfy:Int 'button F (fortify)
Field bsx:Int,bsy:Int 'button S (Skip turn)
Field bex:Int,bey:Int 'button E (End turn)
Field bwx:Int,bwy:Int 'button W (Wait)
' Width and height of the interface buttons
Field bw:Int = 32
Field bh:Int = 32
Method New(Width:Int,Height:Int)
'locally store width and height of the screen
Self.Width = Width
Self.Height = Height
'
'button R (button build road x and y)
brx = 100
bry = 0
'button B (button build city x and y)
bbx = 100
bby = 32
'button F (fortify unit button x and y)
bfx = 100
bfy = 64
'button S (button skip turn x and y)
bsx = 100
bsy = 96
'button E (button end turn x and y)
bex = -16
bey = 34
'button W (button wait x and y)
bwx = 100
bwy = 128
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()
If mousedelay < 20 Then return
' 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 'if the interface is visible
' 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
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x+brx,y+bry,bw,bh)
mousedelay = 0
'Print "Build Road"
If myunitmethod.activeunitissetler() = False Then Return
myunitmethod.buildroadatactiveunitpos()
myworld.updatedrawroads(myworld.roadcanvas)
myunitmethod.activateamovableunit()
End If
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x+bbx,y+bby,bw,bh)
mousedelay = 0
'Print "Build City"
If myunitmethod.activeunitissetler() = False Then Return
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))
myworld.updatedrawroads(myworld.roadcanvas)
myunitmethod.activateamovableunit()
End If
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x+bfx,y+bfy,bw,bh)
mousedelay = 0
'Print "Fortify"
myunitmethod.unitactivefortify()
myunitmethod.activateamovableunit()
End If
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x+bsx,y+bsy,bw,bh)
mousedelay = 0
'Print "Skip turn"
myunitmethod.activeunitskipturn()
myunitmethod.activateamovableunit()
End If
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x+bex,y+bey,bw,bh)
mousedelay = 0
'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
i.pathmove()
Next
' Update the fogmap
redrawgame()
' 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
End If
If rectsoverlap(Mouse.X,Mouse.Y,1,1,x+bwx,y+bwy,bw,bh)
mousedelay = 0
'Print "unit wait"
myunitmethod.unitactivewait()
myunitmethod.activateamovableunit()
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)
'draw in entire screen
Local rec := New Recti<Int>
rec.X = 0
rec.Y = 0
rec.Size = New Vec2i(Width,Height)
canvas.Scissor = rec
If docked = False Then 'if it is visible
Local x:Int=ix
Local y:Int=iy
drawunitbutton(canvas,x+brx,y+bry,"R")
drawunitbutton(canvas,x+bbx,y+bby,"B")
drawunitbutton(canvas,x+bfx,y+bfy,"F")
drawunitbutton(canvas,x+bsx,y+bsy,"S")
drawunitbutton(canvas,x+bex,y+bey,"E")