-
Notifications
You must be signed in to change notification settings - Fork 8
/
Example - Ai cities on random map.monkey2
157 lines (151 loc) · 3.69 KB
/
Example - Ai cities on random map.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
#Import "<std>"
#Import "<mojo>"
Using std..
Using mojo..
Class MyWindow Extends Window
Field map:Int[,]
Field tw:Float,th:Float
Field mw:Int=32,mh:Int=32
Class city
Field x:Int,y:Int
Method New(x:Int,y:Int)
Self.x = x
Self.y = y
End Method
End Class
Field mycity:List<city>
Method New()
tw = Float(Width)/Float(mw)
th = Float(Height)/Float(mh)
map = New Int[mw,mh]
makemap()
mycity = New List<city>
makecountry(3)
End Method
Method OnRender( canvas:Canvas ) Override
App.RequestRender() ' Activate this method
drawmap(canvas)
' If we press the mouse on the map then execute
' the method. (print the returnvale to the console)
If Mouse.ButtonReleased(MouseButton.Left) Or Keyboard.KeyReleased(Key.Space)
SeedRnd(Millisecs())
map = New Int[mw,mh]
makemap()
mycity = New List<city>
makecountry(Rnd(1,10))
End If
'
canvas.Color = Color.Black
canvas.DrawText("Press the lmb or space for new country.",0,0)
'
' if key escape then quit
If Keyboard.KeyReleased(Key.Escape) Then App.Terminate()
End Method
'
' For the first ai player.
' does not check other cities
Method makecountry(numberofcities:Int)
Local citiesmade:Int=0
Local currentx:Int,currenty:Int
'
' Find starting position
Local exitloop:Bool=False
While exitloop = False
Local x:Int=Rnd(mw)
Local y:Int=Rnd(mh)
If map[x,y] > 5
currentx = x
currenty = y
exitloop = True
End If
Wend
' Add the first city to the list
mycity.Add(New city(currentx,currenty))
citiesmade = 1
exitloop=False
Local failcount:Int
Repeat
' find next city position
exitloop=False
While exitloop=False
' get a random coordinate
Local nx:Int=Rnd(mw),ny:Int=Rnd(mh)
'first check if the new position is not to close to a excisting city
Local check1:Bool=True
For Local i:=Eachin mycity
If distance(i.x,i.y,nx,ny)<5 Then check1 = False
Next
'check if the new position is near a existing city
Local check2:Bool=True
For Local i:=Eachin mycity
If distance(i.x,i.y,nx,ny) > 10 Then check2 = False
Next
If check1 = True And check2 = True
If map[nx,ny] > 5 'if the current tile is land
citiesmade += 1
mycity.Add(New city(nx,ny))
exitloop = True
End If
End If
failcount+=1
If failcount>1000 Then Return
Wend
'
If citiesmade = numberofcities Then Exit
Forever
End Method
' function that returns the distance between 2 2d points.
Function distance:Int(x1:Int,y1:Int,x2:Int,y2:Int)
Return Abs(x2-x1)+Abs(y2-y1)
End Function
' Create a random map (hill algorithm)
Method makemap()
Local exitloop:Bool=False
While exitloop=False
Local x1:Int=Rnd(-10,mw)
Local y1:Int=Rnd(-10,mh)
Local w:Int=Rnd(1,12)
Local h:Int=Rnd(1,10)
For Local y2:=y1 To y1+h
For Local x2:=x1 To x1+w
If x2>=0 And x2<mw And y2>=0 And y2<mh
map[x2,y2] = map[x2,y2] + 1
If map[x2,y2] > 46 Then exitloop = True
End If
Next
Next
Wend
For Local y:=0 Until mh
For Local x:=0 Until mw
map[x,y] = (10.0/46)*map[x,y]
Next
Next
End Method
' Draw the map on the canvas
Method drawmap(canvas:Canvas)
For Local y:=0 Until mh
For Local x:=0 Until mw
Local col:Float=0
col = Float(map[x,y])/20
If map[x,y]>5
canvas.Color = New Color(col,1,col)
Else
canvas.Color = New Color(col,col,1)
End If
canvas.DrawRect(x*tw,y*th,tw,th)
Next
Next
'draw the cities
For Local i:=Eachin mycity
canvas.Color = Color.Black
canvas.DrawRect(i.x*tw-1,i.y*th-1,tw+2,th+2)
canvas.Color = Color.White
canvas.DrawRect(i.x*tw,i.y*th,tw,th)
Next
End Method
End Class
Function Main()
New AppInstance
New MyWindow
App.Run()
End Function