1
00:00:05,180 --> 00:00:10,485
Hi, welcome to this 4th
video in our final,

2
00:00:10,485 --> 00:00:12,360
5th week of this course.

3
00:00:12,360 --> 00:00:13,700
We are working
through the project

4
00:00:13,700 --> 00:00:15,820
of the wave function
collapse algorithm.

5
00:00:15,820 --> 00:00:19,240
We're going to be looking at
how do we collapse a cell.

6
00:00:19,240 --> 00:00:21,180
The idea of collapsing
a cell is going

7
00:00:21,180 --> 00:00:23,500
from a large
possibility space of

8
00:00:23,500 --> 00:00:26,960
maybe four or eight or 16
different possible options

9
00:00:26,960 --> 00:00:28,730
for a cell to a singular one.

10
00:00:28,730 --> 00:00:31,220
How we're going to do it?
We're going to be doing it by

11
00:00:31,220 --> 00:00:35,260
selecting out of all the
possible options that we have,

12
00:00:35,260 --> 00:00:37,280
the actual options
that have to do with

13
00:00:37,280 --> 00:00:39,770
the compatibility with
other adjacent tiles,

14
00:00:39,770 --> 00:00:41,420
we're going to pick
one at random.

15
00:00:41,420 --> 00:00:43,320
We're not really going
to be determining

16
00:00:43,320 --> 00:00:46,340
any criteria for picking
one of these cells.

17
00:00:46,340 --> 00:00:49,280
You could start giving
some criteria here,

18
00:00:49,280 --> 00:00:52,460
but we're going to be using
one at random as long as it

19
00:00:52,460 --> 00:00:54,460
matches the adjacencies of

20
00:00:54,460 --> 00:00:56,820
the tiles around it that
have been collapsed.

21
00:00:56,820 --> 00:00:58,900
Meaning that we only

22
00:00:58,900 --> 00:01:01,950
evaluate the possibilities
that actually work.

23
00:01:01,950 --> 00:01:03,990
Based on compatibility, we
can pick one at random.

24
00:01:03,990 --> 00:01:05,940
That's the beauty
of this algorithm.

25
00:01:05,940 --> 00:01:08,700
As long as that tile can exist,

26
00:01:08,700 --> 00:01:11,715
we can just collapse it
by a random selection.

27
00:01:11,715 --> 00:01:15,610
As you can see here,
this particular tile

28
00:01:15,610 --> 00:01:17,290
that we have represented
in this graphic

29
00:01:17,290 --> 00:01:19,010
has eight possible options.

30
00:01:19,010 --> 00:01:22,330
We pick one at random,
we make that the actual,

31
00:01:22,330 --> 00:01:24,010
we get rid of all
the other options

32
00:01:24,010 --> 00:01:26,230
and at that point that
cell is collapsed,

33
00:01:26,230 --> 00:01:28,050
we change its graphic and

34
00:01:28,050 --> 00:01:30,425
we will move later
into the propagation.

35
00:01:30,425 --> 00:01:32,490
We're getting close
to start really

36
00:01:32,490 --> 00:01:34,730
implementing the
algorithm as is intended.

37
00:01:34,730 --> 00:01:36,750
Let's jump into the code and

38
00:01:36,750 --> 00:01:39,160
write the collapsing
of the cell.

39
00:01:39,160 --> 00:01:41,350
Here we are, the collapsing of

40
00:01:41,350 --> 00:01:43,630
the cell will invite
us in a way to

41
00:01:43,630 --> 00:01:45,570
start really writing

42
00:01:45,570 --> 00:01:49,290
the wave function collapse
algorithm as such.

43
00:01:49,290 --> 00:01:51,120
We are here at the beginning,

44
00:01:51,120 --> 00:01:55,150
we're going to be working on
the environment area just

45
00:01:55,150 --> 00:01:57,330
after the run because
this is the part of

46
00:01:57,330 --> 00:01:59,835
the algorithm that
will run continuously.

47
00:01:59,835 --> 00:02:01,980
We're going to be defining

48
00:02:01,980 --> 00:02:09,105
the function
wave_function_collapse.

49
00:02:09,105 --> 00:02:13,755
We're going to do that here
with the self like that.

50
00:02:13,755 --> 00:02:17,680
This function we
want to run every,

51
00:02:18,500 --> 00:02:21,940
let's just run it every frame.

52
00:02:22,370 --> 00:02:25,720
Nice. The first thing

53
00:02:25,720 --> 00:02:28,140
that I would like to do
with this algorithm,

54
00:02:28,140 --> 00:02:30,620
we're going to separate the
wave function collapse.

55
00:02:30,620 --> 00:02:32,120
It's going to be the
general algorithm that is

56
00:02:32,120 --> 00:02:33,680
going to contain a
series of functions.

57
00:02:33,680 --> 00:02:35,720
The function that we're
going to be writing today,

58
00:02:35,720 --> 00:02:38,480
it's the collapse function.

59
00:02:38,480 --> 00:02:40,820
But here first I would like to

60
00:02:40,820 --> 00:02:43,660
identify the cell that
we're going to be

61
00:02:43,660 --> 00:02:49,080
first working on. Think
of the following.

62
00:02:49,080 --> 00:02:50,820
We want to identify what is

63
00:02:50,820 --> 00:02:53,620
the cell that we would
like to start with.

64
00:02:57,560 --> 00:03:00,000
Let's define a variable called

65
00:03:00,000 --> 00:03:03,800
the next_cell. We
can do that here.

66
00:03:03,800 --> 00:03:11,520
We could say self.next_ cell_x.

67
00:03:11,520 --> 00:03:14,670
We're going to say the first
one is going to be 15.

68
00:03:15,910 --> 00:03:18,930
This is an arbitrary number.

69
00:03:19,750 --> 00:03:24,450
Let's write that
correctly, next_cell.

70
00:03:28,640 --> 00:03:31,350
I'm going to be placing

71
00:03:31,350 --> 00:03:33,440
it roughly at the
center of the screen,

72
00:03:33,440 --> 00:03:36,440
so 15 and seven.

73
00:03:36,440 --> 00:03:43,910
I believe that our Canvas
we're working with 30 and 15.

74
00:03:43,910 --> 00:03:46,320
That's roughly in the middle.

75
00:03:46,420 --> 00:03:52,470
Here now we're going to
start writing the algorithm.

76
00:03:52,470 --> 00:03:53,960
Let's just start defining that.

77
00:03:53,960 --> 00:03:58,950
The way we're going to start
will be self.next_cell_x.

78
00:04:03,800 --> 00:04:08,500
This is the cell_x and y
we're going to be working on.

79
00:04:10,220 --> 00:04:14,620
We do want to do a first
check here and we want to

80
00:04:14,620 --> 00:04:19,085
check if cell_x is

81
00:04:19,085 --> 00:04:28,370
none or cell_y is
none, return none.

82
00:04:28,370 --> 00:04:30,660
We want to do this just

83
00:04:30,660 --> 00:04:33,180
because the algorithm
will run sequentially.

84
00:04:33,180 --> 00:04:38,000
At some point we will run out
of next cells to perform.

85
00:04:38,000 --> 00:04:39,840
We want to make
sure that we have

86
00:04:39,840 --> 00:04:41,695
some provisions for that.

87
00:04:41,695 --> 00:04:45,115
At this point we could
actually pass these cells.

88
00:04:45,115 --> 00:04:47,040
The first function to

89
00:04:47,040 --> 00:04:48,600
execute within this algorithm

90
00:04:48,600 --> 00:04:50,145
will be in the collapse cell.

91
00:04:50,145 --> 00:04:53,950
Let's call self.collapse_cell,

92
00:04:53,950 --> 00:04:56,830
which is a function we
haven't created yet,

93
00:04:56,830 --> 00:05:01,520
but we're going to
write it just now.

94
00:05:01,520 --> 00:05:05,045
Let's collapse those two cells.

95
00:05:05,045 --> 00:05:08,430
Let's define that function now,

96
00:05:08,440 --> 00:05:13,690
collapse_cell and for
this function we are

97
00:05:13,690 --> 00:05:18,795
going to start with self, x, y.

98
00:05:18,795 --> 00:05:20,610
That's the information
that is required,

99
00:05:20,610 --> 00:05:22,930
the arguments, which is

100
00:05:22,930 --> 00:05:25,350
the cells that we're
going to be collapsing.

101
00:05:25,350 --> 00:05:28,600
Let's just write possibilities.

102
00:05:28,600 --> 00:05:32,330
If we think about
the possibilities,

103
00:05:32,330 --> 00:05:37,600
we know that the cells variable
contains all the cells.

104
00:05:37,600 --> 00:05:42,315
It has x and y.

105
00:05:42,315 --> 00:05:45,150
We're basically
identifying the cell that

106
00:05:45,150 --> 00:05:48,090
we want to collapse.

107
00:05:48,090 --> 00:05:51,540
But we're calculating
its possibilities,

108
00:05:51,540 --> 00:05:54,100
meaning at this point
it will be four.

109
00:05:54,590 --> 00:06:00,820
The list of possible tiles
exists within the nested list.

110
00:06:00,820 --> 00:06:06,060
We have one of those and we
basically want to select

111
00:06:06,060 --> 00:06:08,480
the chosen tile
and it's going to

112
00:06:08,480 --> 00:06:11,805
be using the random
module choice.

113
00:06:11,805 --> 00:06:14,880
Choice is very good because
when you have a list

114
00:06:14,880 --> 00:06:18,320
and you want to select
random from that list,

115
00:06:18,320 --> 00:06:21,600
we can use this function

116
00:06:21,600 --> 00:06:24,120
random.choice that we pass

117
00:06:24,120 --> 00:06:27,780
the list and we'll return
one element of that list.

118
00:06:27,780 --> 00:06:31,930
Now we can actually say
that the self.cells

119
00:06:31,930 --> 00:06:41,320
[x] [y] is the chosen tile.

120
00:06:42,470 --> 00:06:47,320
We are basically going to say
get rid of everything else.

121
00:06:47,320 --> 00:06:49,710
If there's four options,

122
00:06:49,710 --> 00:06:51,650
make one of them collapse

123
00:06:51,650 --> 00:06:53,285
and select one of
them at random.

124
00:06:53,285 --> 00:06:55,290
That's it. At this point,

125
00:06:55,290 --> 00:06:56,670
we're not propagating, we're

126
00:06:56,670 --> 00:06:58,330
not kind of iterating
over the process.

127
00:06:58,330 --> 00:07:00,650
We're just saying, let's
start with a cell at random,

128
00:07:00,650 --> 00:07:02,690
which is the middle
one, 15 and seven.

129
00:07:02,690 --> 00:07:06,435
Pick that cell, see how
many possibilities it has.

130
00:07:06,435 --> 00:07:10,230
Store those, pick one
at random and now

131
00:07:10,230 --> 00:07:14,680
assign it's actual
value of the cell.

132
00:07:14,680 --> 00:07:15,940
At that point, the tiles,

133
00:07:15,940 --> 00:07:17,850
it's going to be
not four, but one.

134
00:07:17,850 --> 00:07:19,400
It's just going
to be a singular.

135
00:07:19,400 --> 00:07:22,695
We're collapsing that
cell effectively.

136
00:07:22,695 --> 00:07:26,535
Let's see if this
in fact is running.

137
00:07:26,535 --> 00:07:28,760
We're running into
an error here.

138
00:07:28,760 --> 00:07:30,960
Let's see what we
wrote incorrectly.

139
00:07:30,960 --> 00:07:33,800
I think it might be that
we're missing an s here.

140
00:07:33,800 --> 00:07:37,930
Let's see if that's the
trick. There we go.

141
00:07:37,930 --> 00:07:39,920
What we can see here is that now

142
00:07:39,920 --> 00:07:41,220
we're actually invoking both

143
00:07:41,220 --> 00:07:43,660
of the different display

144
00:07:43,660 --> 00:07:45,000
functions that we
have for the cell.

145
00:07:45,000 --> 00:07:49,080
Like if the cells have a
large possibility space,

146
00:07:49,080 --> 00:07:51,310
we select the one that

147
00:07:51,310 --> 00:07:53,890
represents the number
of its entropy.

148
00:07:53,890 --> 00:07:55,470
If it's been collapsed,

149
00:07:55,470 --> 00:07:57,670
we show that cell has been

150
00:07:57,670 --> 00:07:59,170
collapsed and it has

151
00:07:59,170 --> 00:08:01,280
a representation of
its compatibility.

152
00:08:01,280 --> 00:08:03,970
We're good. We're going
to leave this video here.

153
00:08:03,970 --> 00:08:06,490
We have in fact
collapsed one cell.

154
00:08:06,490 --> 00:08:08,130
We need to start
learning how do we

155
00:08:08,130 --> 00:08:11,060
propagate this algorithm
to the adjacent cells.

156
00:08:11,060 --> 00:08:12,490
We're going to be seeing that

157
00:08:12,490 --> 00:08:15,280
in the video to come.
I'll see you then.