1
00:00:04,200 --> 00:00:07,080
Welcome to this new lesson.

2
00:00:07,080 --> 00:00:08,980
In this lesson, we're going to

3
00:00:08,980 --> 00:00:10,940
continue with what we left off.

4
00:00:10,940 --> 00:00:14,685
We're going to start learning
about grid operations.

5
00:00:14,685 --> 00:00:16,940
We introduced a little bit of

6
00:00:16,940 --> 00:00:19,940
this idea in the
previous lesson.

7
00:00:19,940 --> 00:00:22,160
We started talking
about how in a grid,

8
00:00:22,160 --> 00:00:25,840
an index position in a grid
has adjacent or neighbors,

9
00:00:25,840 --> 00:00:29,370
if you want, entities
or neighboring cells.

10
00:00:29,370 --> 00:00:32,215
Let's take this
example, for instance.

11
00:00:32,215 --> 00:00:36,140
The position that we have
here in the red spot,

12
00:00:36,140 --> 00:00:40,230
which is in the
coordinates 2 and 3,

13
00:00:40,230 --> 00:00:42,955
that doesn't mean that
it is in fact the index

14
00:00:42,955 --> 00:00:45,425
2 and 3 as we saw last lesson.

15
00:00:45,425 --> 00:00:48,905
But this position has neighbors,

16
00:00:48,905 --> 00:00:51,530
and we will consider
these neighbors.

17
00:00:51,530 --> 00:00:53,530
The first order neighbors
are going to be

18
00:00:53,530 --> 00:00:56,835
the direct adjacent entities.

19
00:00:56,835 --> 00:00:59,930
Depending on the data
structure we're using,

20
00:00:59,930 --> 00:01:02,390
minus one in this direction,

21
00:01:02,390 --> 00:01:04,665
plus one in this direction,

22
00:01:04,665 --> 00:01:08,790
we will actually
need to be able to

23
00:01:08,790 --> 00:01:13,580
locate the position
of those neighbors.

24
00:01:13,580 --> 00:01:18,420
We can also think of neighbors
of a more extended order.

25
00:01:18,420 --> 00:01:21,460
The certain operations that
we might want to do that

26
00:01:21,460 --> 00:01:25,470
involve a whole
range of neighbors,

27
00:01:25,470 --> 00:01:27,600
and you can continue
this thought to

28
00:01:27,600 --> 00:01:30,225
even much larger range

29
00:01:30,225 --> 00:01:33,210
of neighbors like
second rows and so on.

30
00:01:33,230 --> 00:01:37,220
But these are some of
the first concepts

31
00:01:37,220 --> 00:01:38,540
of neighboring cells that

32
00:01:38,540 --> 00:01:40,255
we will actually
start identifying.

33
00:01:40,255 --> 00:01:42,360
What we're going to
start doing here,

34
00:01:42,360 --> 00:01:44,980
we're going to create
an entity like

35
00:01:44,980 --> 00:01:49,020
this red dot here that
we're going to call walker.

36
00:01:49,020 --> 00:01:51,320
We're going to just
start in a position.

37
00:01:51,320 --> 00:01:53,960
We are going to
identify a position.

38
00:01:53,960 --> 00:01:56,975
It could be a random, but
somewhere in the grid.

39
00:01:56,975 --> 00:02:01,040
Instead of creating a new
entity at a random position,

40
00:02:01,040 --> 00:02:03,530
we're going to ask
it to, let's say,

41
00:02:03,530 --> 00:02:05,670
delete a white rectangle,

42
00:02:05,670 --> 00:02:06,990
which is exactly what we were

43
00:02:06,990 --> 00:02:09,490
doing in our previous example,

44
00:02:09,490 --> 00:02:12,565
but adjacent to its
current location.

45
00:02:12,565 --> 00:02:17,230
How would it be if we
have a worker that starts

46
00:02:17,230 --> 00:02:19,490
eating up the color
but gradually moves

47
00:02:19,490 --> 00:02:22,635
through the grid
in adjacent cells?

48
00:02:22,635 --> 00:02:25,290
That we can do those
operations just by

49
00:02:25,290 --> 00:02:27,810
understanding where
the unit is located

50
00:02:27,810 --> 00:02:33,495
and how would we move back
and forth within the grid.

51
00:02:33,495 --> 00:02:36,300
Let's just get
started with them.

52
00:02:36,300 --> 00:02:39,440
As I mentioned, we
are going to continue

53
00:02:39,440 --> 00:02:42,620
working with our script.

54
00:02:42,620 --> 00:02:45,880
We're going to be basically
using the same script,

55
00:02:46,280 --> 00:02:48,540
but let's just create

56
00:02:48,540 --> 00:02:51,100
a variable that will
represent our worker.

57
00:02:51,100 --> 00:02:53,040
Up here, we're going to identify

58
00:02:53,040 --> 00:02:55,080
a variable which is going to be

59
00:02:55,080 --> 00:03:04,335
an integer and we'll call
it integer current x.

60
00:03:04,335 --> 00:03:07,410
We're going to start
with a zero and

61
00:03:07,410 --> 00:03:11,895
an integer current y,

62
00:03:11,895 --> 00:03:14,055
that is also going to be a zero.

63
00:03:14,055 --> 00:03:16,670
If you want to make
a comment here,

64
00:03:16,670 --> 00:03:20,255
this entity, let's
call this the walker.

65
00:03:20,255 --> 00:03:23,410
These two variables are going to

66
00:03:23,410 --> 00:03:26,830
represent where our unit

67
00:03:26,830 --> 00:03:29,580
that will move around
will be located.

68
00:03:29,580 --> 00:03:32,030
That's what we need
to do at the level

69
00:03:32,030 --> 00:03:35,650
of the global variables.

70
00:03:35,650 --> 00:03:41,110
Let's bring those variables
in here with global.

71
00:03:46,700 --> 00:03:49,270
Now that we have
those variables here,

72
00:03:49,270 --> 00:03:51,130
let's just double check
that this is all working.

73
00:03:51,130 --> 00:03:54,710
That's fine. This is where
we left off last week.

74
00:03:54,710 --> 00:03:59,410
Instead of creating
a random movement,

75
00:03:59,410 --> 00:04:01,685
we're going to delete that,

76
00:04:01,685 --> 00:04:03,840
we don't need that anymore.

77
00:04:07,370 --> 00:04:11,990
Let's just get rid of that
so that it's less confusing.

78
00:04:13,250 --> 00:04:19,310
We are going to
define one position,

79
00:04:19,310 --> 00:04:23,570
let's start with something
like a 20 and a 30,

80
00:04:23,570 --> 00:04:26,690
which is going to be
an arbitrary position

81
00:04:26,690 --> 00:04:28,490
where we want our
workers to start.

82
00:04:28,490 --> 00:04:31,550
This is going to be defined
in index coordinates,

83
00:04:31,550 --> 00:04:34,770
so we need to make sure that
we multiply this value by

84
00:04:34,770 --> 00:04:36,330
its cell size if we

85
00:04:36,330 --> 00:04:39,290
want to convert it into
actual coordinates.

86
00:04:39,480 --> 00:04:42,370
Let's go ahead and
remove some of

87
00:04:42,370 --> 00:04:45,810
the code that we left
from last lesson.

88
00:04:45,810 --> 00:04:50,110
We don't really need to do
the random index anymore,

89
00:04:50,110 --> 00:04:55,660
we can also get rid of
this part down here.

90
00:04:56,020 --> 00:05:02,140
But the new position x
and the new position y,

91
00:05:02,140 --> 00:05:05,185
which identifies the
position of the rectangle.

92
00:05:05,185 --> 00:05:08,740
This time it's going
to be identified

93
00:05:08,740 --> 00:05:12,445
by the walker variable.

94
00:05:12,445 --> 00:05:18,805
Let's do current index
times cell size.

95
00:05:18,805 --> 00:05:21,700
We have to do this just because
we want to convert index

96
00:05:21,700 --> 00:05:31,375
into a natural coordinate

97
00:05:31,375 --> 00:05:33,890
within the world.

98
00:05:34,020 --> 00:05:42,550
Let's see that.

99
00:05:42,550 --> 00:05:45,910
We have this information

100
00:05:45,910 --> 00:05:50,630
here and then we
have this 20 and 30.

101
00:05:51,120 --> 00:05:53,365
Is this the rectangle?

102
00:05:53,365 --> 00:05:55,075
Let's just double check

103
00:05:55,075 --> 00:05:57,580
if we want to be more
explicit with this,

104
00:05:57,580 --> 00:06:04,480
we could actually do an
ellipse and let's do red.

105
00:06:04,480 --> 00:06:10,400
We can see if we
identify its position.

106
00:06:12,270 --> 00:06:14,800
I find it.

107
00:06:14,800 --> 00:06:17,330
I guess the colors are pretty.

108
00:06:20,700 --> 00:06:24,655
We wouldn't find it because
we have an error here.

109
00:06:24,655 --> 00:06:28,195
Let's see. Oh, there we go.

110
00:06:28,195 --> 00:06:32,000
Here is this red circle.

111
00:06:34,290 --> 00:06:41,560
We see that it's in fact
located in the corner,

112
00:06:41,560 --> 00:06:47,770
the ellipse actually draws
itself from the center.

113
00:06:47,770 --> 00:06:50,360
Let's just do a rectangle again.

114
00:06:51,750 --> 00:06:54,415
Here we go. That is
the red rectangle.

115
00:06:54,415 --> 00:06:56,290
If we want it to be,
again, more explicit,

116
00:06:56,290 --> 00:06:59,350
we can do it black or let's
just, let's see if black,

117
00:06:59,350 --> 00:07:02,560
it's a bit more
visible. There we go.

118
00:07:02,560 --> 00:07:06,500
It's a black pixel.

119
00:07:07,740 --> 00:07:10,360
We are going to do it as

120
00:07:10,360 --> 00:07:12,265
we did before with
the white color.

121
00:07:12,265 --> 00:07:15,340
I think the white color
works quite nicely.

122
00:07:15,340 --> 00:07:18,925
What we want to do now
is that now that we

123
00:07:18,925 --> 00:07:23,050
have the rectangle in

124
00:07:23,050 --> 00:07:26,770
the grid being determined by

125
00:07:26,770 --> 00:07:31,375
this current index or
the integer current x,

126
00:07:31,375 --> 00:07:33,680
which is the variable
for the worker,

127
00:07:33,710 --> 00:07:36,045
so the worker variable here,

128
00:07:36,045 --> 00:07:37,665
what we want to do,
let's do a comment here.

129
00:07:37,665 --> 00:07:45,090
What we want to do is
make the worker move

130
00:07:45,090 --> 00:07:53,050
randomly in x or y.

131
00:07:53,050 --> 00:07:56,935
The way we would do that
is saying that the integer

132
00:07:56,935 --> 00:08:02,245
x could get a plus or minus.

133
00:08:02,245 --> 00:08:05,530
Let's add to that value
which is currently 20.

134
00:08:05,530 --> 00:08:09,320
Let's add a random
value of minus 1 and 1.

135
00:08:10,680 --> 00:08:13,940
Random.rangrange.

136
00:08:16,860 --> 00:08:20,950
We will add plus one or
minus one at random.

137
00:08:20,950 --> 00:08:26,390
Let's do the same thing for y.

138
00:08:27,120 --> 00:08:29,320
At any given in

139
00:08:29,320 --> 00:08:32,275
every frame because we're
doing this within the draw,

140
00:08:32,275 --> 00:08:36,610
we will add one in
x and one in y.

141
00:08:36,610 --> 00:08:38,125
There's going to be
some errors here.

142
00:08:38,125 --> 00:08:38,470
They are going to be

143
00:08:38,470 --> 00:08:39,790
some exceptions that
we're going to run into.

144
00:08:39,790 --> 00:08:45,985
But let's see if this
starts to make sense.

145
00:08:45,985 --> 00:08:48,190
Again, we're running
into an error.

146
00:08:48,190 --> 00:08:50,810
Let's double check what's wrong.

147
00:08:53,550 --> 00:08:57,730
Random range is going to
give us floating point.

148
00:08:57,730 --> 00:09:00,020
Let's run in.

149
00:09:02,100 --> 00:09:07,480
I think I misspelled that
it should be rang int.

150
00:09:07,480 --> 00:09:10,210
We want to make sure
that we keep the

151
00:09:10,210 --> 00:09:14,440
random to give us
integer values.

152
00:09:14,440 --> 00:09:16,690
The rang range could
be able to give us

153
00:09:16,690 --> 00:09:19,090
a floating point and
that would actually make

154
00:09:19,090 --> 00:09:24,280
our system not really work
the way we're intending.

155
00:09:24,280 --> 00:09:26,440
Let's just create a
random integer which is a

156
00:09:26,440 --> 00:09:29,440
minus 1, 0 or 1.

157
00:09:29,440 --> 00:09:34,735
Let's see if that actually
works. We have that working.

158
00:09:34,735 --> 00:09:37,705
We have this little entity.

159
00:09:37,705 --> 00:09:40,960
I call it an entity, but
in fact it's just a number

160
00:09:40,960 --> 00:09:46,450
that is randomly going up and
down, positive or negative,

161
00:09:46,450 --> 00:09:51,600
until it reaches an
H condition which

162
00:09:51,600 --> 00:09:55,170
should be something that

163
00:09:55,170 --> 00:09:58,990
we want to avoid because we
run into an error there.

164
00:09:59,450 --> 00:10:04,545
Let's see how we can
actually do some exceptions.

165
00:10:04,545 --> 00:10:06,690
Now we actually have a walker.

166
00:10:06,690 --> 00:10:09,420
We are identifying
that we can actually

167
00:10:09,420 --> 00:10:12,290
create one unit to move

168
00:10:12,290 --> 00:10:15,150
plus or minus within
this grade just by

169
00:10:15,150 --> 00:10:18,745
adding a random number to it.

170
00:10:18,745 --> 00:10:20,720
But here we could also say,

171
00:10:20,720 --> 00:10:22,625
there's an exception,

172
00:10:22,625 --> 00:10:25,790
there's an if this entity or

173
00:10:25,790 --> 00:10:31,080
the coordinate in x
is smaller than zero.

174
00:10:39,270 --> 00:10:41,485
Let's just do it this way.

175
00:10:41,485 --> 00:10:49,805
If the index in x is
smaller than zero,

176
00:10:49,805 --> 00:10:54,260
let's just make it
remain in zero.

177
00:10:54,260 --> 00:10:56,930
So we're going to make sure

178
00:10:56,930 --> 00:11:00,750
that it never goes below zero.

179
00:11:00,910 --> 00:11:05,600
In a similar way we could
actually say that for y.

180
00:11:05,600 --> 00:11:08,120
So let's just copy
paste that statement.

181
00:11:08,120 --> 00:11:12,800
That would fix that
our unit does not

182
00:11:12,800 --> 00:11:18,455
go outside on the left
or on the top right,

183
00:11:18,455 --> 00:11:21,620
because those coordinates are

184
00:11:21,620 --> 00:11:24,005
defined by the zero boundary.

185
00:11:24,005 --> 00:11:26,000
We're covering for two of

186
00:11:26,000 --> 00:11:28,860
the four boundaries
of the screen.

187
00:11:28,870 --> 00:11:32,750
We could also identify

188
00:11:32,750 --> 00:11:35,525
the maximum number which

189
00:11:35,525 --> 00:11:40,640
is basically the
resolution in x.

190
00:11:40,640 --> 00:11:42,155
So let's just do

191
00:11:42,155 --> 00:11:48,080
if this is for in the
uppermost boundary.

192
00:11:48,080 --> 00:12:01,100
If it's bigger or equal
than the resolution in x,

193
00:12:01,870 --> 00:12:10,140
we will keep it equal
to the resolution in x.

194
00:12:12,130 --> 00:12:14,690
What we're saying is that if

195
00:12:14,690 --> 00:12:16,100
we reach that number which is

196
00:12:16,100 --> 00:12:19,640
100 in the x, we reach 100.

197
00:12:19,640 --> 00:12:21,830
Make sure that you
never go beyond 100.

198
00:12:21,830 --> 00:12:23,690
Stay with 100.

199
00:12:23,690 --> 00:12:27,170
No matter what plus and minus
equation is happening here,

200
00:12:27,170 --> 00:12:31,025
you cannot go further from 100.

201
00:12:31,025 --> 00:12:35,850
We will do the same thing
for the boundary in y.

202
00:12:36,640 --> 00:12:41,730
Let's make sure to type
those numbers correctly.

203
00:12:46,870 --> 00:12:49,310
I like adding comments here,

204
00:12:49,310 --> 00:12:53,010
because what we're doing
here is boundaries.

205
00:12:56,890 --> 00:13:00,810
Sorry, boundaries. There we go.

206
00:13:04,540 --> 00:13:07,190
So it's not running.
We're having an error.

207
00:13:07,190 --> 00:13:11,370
Let's just find that error
before we wrap it up here.

208
00:13:16,540 --> 00:13:20,210
What I like doing when I
find some of these errors is

209
00:13:20,210 --> 00:13:25,865
to comment out the lines that
I just added. So let's see.

210
00:13:25,865 --> 00:13:29,465
The error is not
running at that point.

211
00:13:29,465 --> 00:13:35,915
Is it here? It's not
there either. Is it here?

212
00:13:35,915 --> 00:13:38,240
That's the line.
So this line has

213
00:13:38,240 --> 00:13:43,385
an error. I can see it here.

214
00:13:43,385 --> 00:13:49,110
Instead of resolution
in x, I typed res_x.

215
00:13:50,530 --> 00:13:53,690
So that's why it's always good

216
00:13:53,690 --> 00:13:58,280
to double click on
your variables.

217
00:13:58,280 --> 00:14:01,700
I'm terrible at
remembering sometimes

218
00:14:01,700 --> 00:14:05,105
the name of the variables.
Here we have it.

219
00:14:05,105 --> 00:14:08,150
Now we can see that
whenever the walker reaches

220
00:14:08,150 --> 00:14:11,210
the lower boundary condition,
it will stay there.

221
00:14:11,210 --> 00:14:14,555
It will never go
below that boundary.

222
00:14:14,555 --> 00:14:16,565
It's still random.

223
00:14:16,565 --> 00:14:19,220
Every time that you
run the script,

224
00:14:19,220 --> 00:14:21,080
it will run differently.

225
00:14:21,080 --> 00:14:23,550
You will get a
different pattern.

226
00:14:24,100 --> 00:14:27,020
This is actually a form
of Brownian motion.

227
00:14:27,020 --> 00:14:30,620
It's a form of behavior
that is starting to

228
00:14:30,620 --> 00:14:35,240
pick adjacent locations
next to the entity,

229
00:14:35,240 --> 00:14:37,670
and it's moving to one
of those locations.

230
00:14:37,670 --> 00:14:40,655
We're actually creating
this random walker idea.

231
00:14:40,655 --> 00:14:44,390
There's a lot more
interesting layers

232
00:14:44,390 --> 00:14:47,510
of behavior that we could
start adding to this.

233
00:14:47,510 --> 00:14:49,970
It basically operates
as how do we

234
00:14:49,970 --> 00:14:52,475
understand the information
of the grid that we're in,

235
00:14:52,475 --> 00:14:54,530
and how do we actually
start moving to

236
00:14:54,530 --> 00:14:58,325
an adjacent tile
or adjacent cell.

237
00:14:58,325 --> 00:15:00,350
It gets a lot more
interesting as we start

238
00:15:00,350 --> 00:15:03,290
introducing data like the grid

239
00:15:03,290 --> 00:15:05,900
itself could be a
form of data that

240
00:15:05,900 --> 00:15:10,670
we use in order to take
decisions on where to move next.

241
00:15:10,670 --> 00:15:12,530
I'm going to leave this lesson

242
00:15:12,530 --> 00:15:15,420
here and I'll see you
in the next video.