This repository was archived by the owner on May 10, 2018. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathBrush.cs
More file actions
257 lines (211 loc) · 10.5 KB
/
Copy pathBrush.cs
File metadata and controls
257 lines (211 loc) · 10.5 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
// $Id: foo.cpp 2979 2006-01-10 00:00:04Z sommer $
//
// Cobble - A simple SuperTux level editor
// Copyright (C) 2006 Christoph Sommer <supertux@2006.expires.deltadevelopment.de>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace Cobble {
class Brush {
protected TileRepository tileRepository;
protected List<int[,]> id_matrices = new List<int[,]>();
protected List<bool[,]> solid_matrices = new List<bool[,]>();
//protected int[,] getIdMatrix(int tx, int ty) {
// return new int[3, 3] {
// { getTileAt(tx-1,ty-1), getTileAt(tx+0,ty-1), getTileAt(tx+1,ty-1) },
// { getTileAt(tx-1,ty+0), getTileAt(tx+0,ty+0), getTileAt(tx+1,ty+0) },
// { getTileAt(tx-1,ty+1), getTileAt(tx+0,ty+1), getTileAt(tx+1,ty+1) }
// };
//}
public Brush(TileRepository tileRepository) {
this.tileRepository = tileRepository;
}
public int Length {
get {
return id_matrices.Count;
}
}
public void saveToFile(string fname) {
FileStream fs = new FileStream(fname, FileMode.Create);
TextWriter tw = new StreamWriter(fs);
foreach (int[,] m1 in id_matrices) {
tw.WriteLine("" + m1[0, 0] + "," + m1[0, 1] + "," + m1[0, 2] + "," + m1[1, 0] + "," + m1[1, 1] + "," + m1[1, 2] + "," + m1[2, 0] + "," + m1[2, 1] + "," + m1[2, 2] + "");
}
tw.Close();
fs.Close();
}
public static Brush loadFromFile(TileRepository tileRepository, string fname) {
TileRepository tr = tileRepository;
Brush brush = new Brush(tileRepository);
FileStream fs = new FileStream(fname, FileMode.Open);
TextReader trd = new StreamReader(fs);
try {
string s;
while ((s = trd.ReadLine()) != null) {
string[] v = s.Split(',');
if (v.Length < 9) continue;
int[,] ids = new int[3, 3] {
{int.Parse(v[0]), int.Parse(v[1]), int.Parse(v[2])},
{int.Parse(v[3]), int.Parse(v[4]), int.Parse(v[5])},
{int.Parse(v[6]), int.Parse(v[7]), int.Parse(v[8])}
};
bool[,] sols = new bool[3, 3] {
{tr.isSolid[int.Parse(v[0])], tr.isSolid[int.Parse(v[1])], tr.isSolid[int.Parse(v[2])]},
{tr.isSolid[int.Parse(v[3])], tr.isSolid[int.Parse(v[4])], tr.isSolid[int.Parse(v[5])]},
{tr.isSolid[int.Parse(v[6])], tr.isSolid[int.Parse(v[7])], tr.isSolid[int.Parse(v[8])]}
};
brush.id_matrices.Add(ids);
brush.solid_matrices.Add(sols);
}
}
catch (IOException) {
}
trd.Close();
fs.Close();
return brush;
}
protected bool equal(int[,] m1, int[,] m2) {
for (int px = 0; px < 3; px++) {
for (int py = 0; py < 3; py++) {
if (m1[px, py] != m2[px, py]) return false;
}
}
return true;
}
protected bool matrixExists(int[,] m1) {
foreach (int[,] m2 in id_matrices) {
if (equal(m1, m2)) return true;
}
return false;
}
//if (!careful) {
// if (!matrixExists(new int[3, 3] {
// { tm.getTileAt(tx-2,ty-1), m1[0, 0], m1[0, 1] },
// { getTileAt(tx-2,ty+0), m1[1, 0], m1[1, 1] },
// { getTileAt(tx-2,ty+1), m1[2, 0], m1[2, 1] }
// })) thisSimilarity -= 100;
// if (!matrixExists(new int[3, 3] {
// { m1[0, 1], m1[0, 1], getTileAt(tx+2,ty-1)},
// { m1[1, 1], m1[1, 2], getTileAt(tx+2,ty+0)},
// { m1[2, 1], m1[2, 2], getTileAt(tx+2,ty+1)}
// })) thisSimilarity -= 100;
//}
public void learn(Tilemap tilemap, int tx, int ty) {
Tilemap tm = tilemap;
TileRepository tr = tileRepository;
int[,] m1 = new int[3, 3] {
{ tm.getTileAt(tx-1,ty-1), tm.getTileAt(tx+0,ty-1), tm.getTileAt(tx+1,ty-1) },
{ tm.getTileAt(tx-1,ty+0), tm.getTileAt(tx+0,ty+0), tm.getTileAt(tx+1,ty+0) },
{ tm.getTileAt(tx-1,ty+1), tm.getTileAt(tx+0,ty+1), tm.getTileAt(tx+1,ty+1) }
};
bool[,] m2 = new bool[3, 3] {
{ tr.isSolid[tm.getTileAt(tx-1,ty-1)], tr.isSolid[tm.getTileAt(tx+0,ty-1)], tr.isSolid[tm.getTileAt(tx+1,ty-1)] },
{ tr.isSolid[tm.getTileAt(tx-1,ty+0)], tr.isSolid[tm.getTileAt(tx+0,ty+0)], tr.isSolid[tm.getTileAt(tx+1,ty+0)] },
{ tr.isSolid[tm.getTileAt(tx-1,ty+1)], tr.isSolid[tm.getTileAt(tx+0,ty+1)], tr.isSolid[tm.getTileAt(tx+1,ty+1)] }
};
if (!matrixExists(m1)) {
id_matrices.Add(m1);
solid_matrices.Add(m2);
}
}
public void learn(Tilemap tilemap) {
for (int tx = 0; tx < tilemap.width; tx++) {
for (int ty = 0; ty < tilemap.height; ty++) {
learn(tilemap, tx, ty);
}
}
}
public void forget(Tilemap tilemap, int tx, int ty) {
Tilemap tm = tilemap;
TileRepository tr = tileRepository;
int[,] m1 = new int[3, 3] {
{ tm.getTileAt(tx-1,ty-1), tm.getTileAt(tx+0,ty-1), tm.getTileAt(tx+1,ty-1) },
{ tm.getTileAt(tx-1,ty+0), tm.getTileAt(tx+0,ty+0), tm.getTileAt(tx+1,ty+0) },
{ tm.getTileAt(tx-1,ty+1), tm.getTileAt(tx+0,ty+1), tm.getTileAt(tx+1,ty+1) }
};
bool[,] m2 = new bool[3, 3] {
{ tr.isSolid[tm.getTileAt(tx-1,ty-1)], tr.isSolid[tm.getTileAt(tx+0,ty-1)], tr.isSolid[tm.getTileAt(tx+1,ty-1)] },
{ tr.isSolid[tm.getTileAt(tx-1,ty+0)], tr.isSolid[tm.getTileAt(tx+0,ty+0)], tr.isSolid[tm.getTileAt(tx+1,ty+0)] },
{ tr.isSolid[tm.getTileAt(tx-1,ty+1)], tr.isSolid[tm.getTileAt(tx+0,ty+1)], tr.isSolid[tm.getTileAt(tx+1,ty+1)] }
};
for (int i = 0; i < id_matrices.Count; i++) {
if (equal(m1, id_matrices[i])) {
id_matrices.RemoveAt(i);
solid_matrices.RemoveAt(i);
}
}
}
protected void draw(Tilemap tilemap, int tx, int ty, bool erase) {
Tilemap tm = tilemap;
TileRepository tr = tileRepository;
int[,] desiredIdPattern = new int[3, 3] {
{ tm.getTileAt(tx-1,ty-1), tm.getTileAt(tx+0,ty-1), tm.getTileAt(tx+1,ty-1) },
{ tm.getTileAt(tx-1,ty+0), tm.getTileAt(tx+0,ty+0), tm.getTileAt(tx+1,ty+0) },
{ tm.getTileAt(tx-1,ty+1), tm.getTileAt(tx+0,ty+1), tm.getTileAt(tx+1,ty+1) }
};
bool[,] desiredSolPattern = new bool[3, 3] {
{ tr.isSolid[tm.getTileAt(tx-1,ty-1)], tr.isSolid[tm.getTileAt(tx+0,ty-1)], tr.isSolid[tm.getTileAt(tx+1,ty-1)] },
{ tr.isSolid[tm.getTileAt(tx-1,ty+0)], !erase , tr.isSolid[tm.getTileAt(tx+1,ty+0)] },
{ tr.isSolid[tm.getTileAt(tx-1,ty+1)], tr.isSolid[tm.getTileAt(tx+0,ty+1)], tr.isSolid[tm.getTileAt(tx+1,ty+1)] }
};
float bestSimilarity = 0;
int[,] bestPattern = new int[3, 3] {
{ tm.getTileAt(tx-1,ty-1), tm.getTileAt(tx+0,ty-1), tm.getTileAt(tx+1,ty-1) },
{ tm.getTileAt(tx-1,ty+0), tm.getTileAt(tx+0,ty+0), tm.getTileAt(tx+1,ty+0) },
{ tm.getTileAt(tx-1,ty+1), tm.getTileAt(tx+0,ty+1), tm.getTileAt(tx+1,ty+1) }
};
for (int i = 0; i < id_matrices.Count; i++) {
int[,] m1 = id_matrices[i];
int[,] m2 = desiredIdPattern;
bool[,] m3 = solid_matrices[i];
bool[,] m4 = desiredSolPattern;
float thisSimilarity = 0;
for (int px = 0; px < 3; px++) {
for (int py = 0; py < 3; py++) {
if ((px == 1) && (py == 1)) {
if (tr.isSolid[m1[px, py]] == (!erase)) thisSimilarity += 1000;
continue;
}
if (m1[px, py] == m2[px, py]) thisSimilarity += 1;
if (m3[px, py] == m4[px, py]) thisSimilarity += 100;
}
}
if (thisSimilarity > bestSimilarity) { bestSimilarity = thisSimilarity; bestPattern = m1; }
}
if (bestSimilarity > 0) {
int[,] m1 = bestPattern;
tm.setTileAt(tx - 1, ty - 1, m1[0, 0]);
tm.setTileAt(tx + 0, ty - 1, m1[0, 1]);
tm.setTileAt(tx + 1, ty - 1, m1[0, 2]);
tm.setTileAt(tx - 1, ty + 0, m1[1, 0]);
tm.setTileAt(tx + 0, ty + 0, m1[1, 1]);
tm.setTileAt(tx + 1, ty + 0, m1[1, 2]);
tm.setTileAt(tx - 1, ty + 1, m1[2, 0]);
tm.setTileAt(tx + 0, ty + 1, m1[2, 1]);
tm.setTileAt(tx + 1, ty + 1, m1[2, 2]);
}
}
public void draw(Tilemap tilemap, int tx, int ty) {
draw(tilemap, tx, ty, false);
}
public void erase(Tilemap tilemap, int tx, int ty) {
draw(tilemap, tx, ty, true);
}
}
}