-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathExample - 2d roaming ai in random map.bb
More file actions
278 lines (257 loc) · 6.81 KB
/
Copy pathExample - 2d roaming ai in random map.bb
File metadata and controls
278 lines (257 loc) · 6.81 KB
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
Graphics 800,640,0,2
SetBuffer BackBuffer()
SeedRnd MilliSecs()
Global numagents%=20
Global mapwidth%=30
Global mapheight%=30
Global tilewidth%=Int(800/mapwidth)
Global tileheight%=Int(640/mapheight)
Dim map(mapwidth,mapheight)
SeedRnd MilliSecs()
Type agents
Field x#,y#
Field angle%
End Type
makegame
Global timer = CreateTimer(60)
While Not KeyHit(1)
WaitTimer timer
Cls
Color 255,255,255
;drawmap
If KeyHit(57) Then makegame
If counter>60*20 Then makegame : counter=0
counter=counter+1
drawmap()
update
draw
Color 255,255,255
Text 0,0,"Ray Casting roaming ai example."
Flip
Wend
End
Function update()
For ag.agents = Each agents
ag\x = ag\x + Cos(ag\angle)*3
ag\y = ag\y + Sin(ag\angle)*3
; here we check 64 pixels in the angle we are going into
; if there is a wall there then the obstr boolean is set
Local obstr=False
For i=0 To 64
Local x1=(ag\x)+Cos(ag\angle)*i
Local y1=(ag\y)+Sin(ag\angle)*i
Local x2%=x1/tilewidth
Local y2%=y1/tileheight
If x2>-1 And y2>-1 And x2<=mapwidth And y2<=mapheight
If map(x2,y2) > 0 Then
obstr = True
Exit
End If
End If
Next
; if obstructed then turn
If obstr = True
ag\angle = ag\angle + 16
End If
; random movement
If Rnd(10)<2 Then ag\angle = ag\angle + Rnd(-15,15)
; keep angle in check
If ag\angle>180 Then ag\angle = -180
If ag\angle<-180 Then ag\angle = 180
Next
End Function
Function draw()
For ag.agents = Each agents
Color 255,255,255
Oval ag\x,ag\y,10,10
Line ag\x+5,ag\y+5,ag\x+Cos(ag\angle)*64,ag\y+Sin(ag\angle)*64
Next
End Function
;Function drawmap()
; Color 200,200,200
; For y=0 To mapheight
; For x=0 To mapwidth
; If map(y,x) > 0
; Color map(y,x),200,200
; Rect x*tilewidth,y*tileheight,tilewidth,tileheight
; End If
; Next
; Next
;End Function
Function makemap(steps = 100)
Local aproved = False
While aproved = False
For y = 0 To mapheight
For x = 0 To mapwidth
map(x,y) = 0
Next
Next
x = mapwidth / 2
y = mapheight / 2
steps = Rand(500) + 500
For i=0 To steps
nstepf = False
While nstepf = False
dir = Rand(8)
Select dir
Case 1 : nx = x - 1 : ny = y - 1
Case 2 : ny = y - 1
Case 3 : nx = x + 1 : ny = y - 1
Case 4 : nx = x - 1
Case 5 : nx = x + 1
Case 6 : nx = x - 1 : ny = y + 1
Case 7 : ny = y + 1
Case 8 : nx = x + 1 : ny = y + 1
End Select
If nx < mapwidth-1 And nx > 0 And ny < mapheight-1 And ny > 0 Then
x = nx
y = ny
nstepf = True
End If
Wend
drawbrush(x,y)
Next
aproved = True
For y=0 To mapheight
If map(0,y) = 1 Then aproved = False
Next
For y=0 To mapheight
If map(mapwidth,y) = 1 Then aproved = False
Next
For x=0 To mapwidth
If map(x,0) = 1 Then aproved = False
Next
For x=0 To mapwidth
If map(x,mapheight) = 1 Then aproved = False
Next
For y=0 To mapheight
For x=mapwidth-7 To mapwidth
If map(x,y) = 1 Then aproved = False
Next
Next
hasone = False
For y=0 To mapheight
If map(mapwidth-8,y) = 1 Then hasone = True
Next
If hasone = False Then aproved = False
hasone = False
For y=0 To mapheight
If map(3,y) = 1 Then hasone = True
Next
If hasone = False Then aproved = False
hasone = False
For x=0 To mapwidth
If map(x,3) = 1 Then hasone = True
Next
If hasone = False Then aproved = False
hasone = False
For x=0 To mapwidth
If map(x,mapheight-3) = 1 Then hasone = True
Next
If hasone = False Then aproved = False
Wend
; turn the 0 into 1(blocked) and
; 1 into 0 (open)
; map() is a collision map sort of
For y = 0 To mapheight
For x = 0 To mapwidth
If map(x,y) = 1 Then map(x,y)=0 Else map(x,y)=1
Next
Next
End Function
; here we draw into the map to create the level
Function drawbrush(x,y)
x1 = x - 1
y1 = y - 1
If x1 > 0 And x1 < mapwidth And y1 > 0 And y1 < mapheight Then
map(x1,y1) = 1
End If
x1 = x
y1 = y - 1
If x1 > 0 And x1 < mapwidth And y1 > 0 And y1 < mapheight Then
map(x1,y1) = 1
End If
x1 = x + 1
y1 = y - 1
If x1 > 0 And x1 < mapwidth And y1 > 0 And y1 < mapheight Then
map(x1,y1) = 1
End If
x1 = x - 1
y1 = y
If x1 > 0 And x1 < mapwidth And y1 > 0 And y1 < mapheight Then
map(x1,y1) = 1
End If
x1 = x + 1
y1 = y
If x1 > 0 And x1 < mapwidth And y1 > 0 And y1 < mapheight Then
map(x1,y1) = 1
End If
x1 = x - 1
y1 = y + 1
If x1 > 0 And x1 < mapwidth And y1 > 0 And y1 < mapheight Then
map(x1,y1) = 1
End If
x1 = x
y1 = y + 1
If x1 > 0 And x1 < mapwidth And y1 > 0 And y1 < mapheight Then
map(x1,y1) = 1
End If
x1 = x + 1
y1 = y + 1
If x1 > 0 And x1 < mapwidth And y1 > 0 And y1 < mapheight Then
map(x1,y1) = 1
End If
End Function
Function drawmap()
Color 200,200,200
For y = 0 To mapheight
For x = 0 To mapwidth
If map(x,y) > 0 Then
Color map(x,y),200,200
;Rect x*16,y*16,16,16,True
Rect x*tilewidth,y*tileheight,tilewidth,tileheight
End If
Next
Next
End Function
Function makegame()
For y=0 To mapheight
For x=0 To mapwidth
map(x,y)=0
Next
Next
Delete Each agents
Select Rand(1,3)
Case 1
mapwidth=30
mapheight=30
Case 2
mapwidth=40
mapheight=40
Default
mapwidth=50
mapheight=50
End Select
Dim map(mapwidth,mapheight)
makemap()
numagents = Rand(2,20)
tilewidth=GraphicsWidth()/mapwidth
tileheight=GraphicsHeight()/mapheight
For i=0 To numagents
exitloop = False
While exitloop = False
x=Int(Rnd(mapwidth))
y=Int(Rnd(mapheight))
If map(x,y) = 0
ag = Create((x * tilewidth)+tilewidth/2, (y * tileheight)+tileheight/2 )
exitloop = True
End If
Wend
Next
End Function
Function Create(x#,y#)
ag.agents = New agents
ag\x = x
ag\y = y
ag\angle=Rnd(-180,180)
End Function