-
Notifications
You must be signed in to change notification settings - Fork 6
/
BoardDrawing.cs
533 lines (502 loc) · 17.6 KB
/
BoardDrawing.cs
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Noxico
{
public partial class Board : TokenCarrier
{
public void Clear(int biomeID)
{
this.Entities.Clear();
this.GetToken("biome").Value = biomeID;
this.GetToken("music").Text = BiomeData.Biomes[biomeID].Music;
var ground = TileDefinition.Find(BiomeData.Biomes[biomeID].GroundTile, true);
for (int row = 0; row < Height; row++)
{
for (int col = 0; col < Width; col++)
{
var def = ground;
if (Random.Flip() && def.Variants.Tokens.Count > 0)
{
var chance = Random.NextDouble();
var variants = def.Variants.Tokens.Where(v => v.Value < chance).ToArray();
if (variants.Length > 0)
def = TileDefinition.Find(variants.PickOne().Name);
}
SetTile(row, col, def);
}
}
}
public void Clear(string biomeName)
{
Clear(BiomeData.ByName(biomeName));
}
public void Clear()
{
Clear((int)this.GetToken("biome").Value);
}
public void ClearToWorld(WorldMapGenerator generator)
{
if (!this.HasToken("coordinate"))
return;
var coord = this.Coordinate;
var x = coord.X;
var y = coord.Y;
var biomeID = generator.RoughBiomeMap[y, x];
if (biomeID == -1)
return;
this.Entities.Clear();
this.Width = WorldMapGenerator.TileWidth;
this.Height = WorldMapGenerator.TileHeight;
this.Tilemap = new Tile[this.Width, this.Height];
this.Lightmap = new bool[this.Width, this.Height];
for (int row = 0; row < Height; row++)
for (int col = 0; col < Width; col++)
this.Tilemap[col, row] = new Tile();
this.GetToken("biome").Value = biomeID;
var worldMapX = x * Width;
var worldMapY = y * Height;
for (int row = 0; row < Height; row++)
{
for (int col = 0; col < Width; col++)
{
var b = generator.DetailedMap[worldMapY + row, worldMapX + col];
var h = generator.WaterMap[worldMapY + row, worldMapX + col];
this.Tilemap[col, row].Index = TileDefinition.Find(BiomeData.Biomes[b].GroundTile).Index;
this.Tilemap[col, row].Fluid = h == 1 ? BiomeData.WaterTypes[(int)Realm] : Fluids.Dry;
}
}
}
/// <summary>
/// Draws a line from one point to another.
/// </summary>
/// <param name="x1">The starting row.</param>
/// <param name="y1">The starting column.</param>
/// <param name="x2">The ending row.</param>
/// <param name="y2">The ending column.</param>
/// <param name="brush">A <see cref="Noxico.TileDefinition"/>, tile name as a string, or tile number, or a Lua callback function.</param>
/// <remarks>The callback function takes the tile number and TileDefinition at the current coordinates in the line and the coordinates, and must return a TileDef, string, or int.</remarks>
public void Line(int x1, int y1, int x2, int y2, object brush)
{
if (brush is TileDefinition)
brush = ((TileDefinition)brush).Index;
else if (brush is string)
brush = TileDefinition.Find((string)brush).Index;
if (brush is int)
{
foreach (var point in Toolkit.Line(x1, y1, x2, y2))
SetTile(point.X, point.Y, (int)brush);
}
else if (brush is Func<object, object, object, object, Neo.IronLua.LuaResult>)
{
var callback = (Func<object, object, object, object, Neo.IronLua.LuaResult>)brush;
foreach (var point in Toolkit.Line(x1, y1, x2, y2))
{
var tileHere = Tilemap[point.Y, point.Y];
brush = callback(tileHere.Index, tileHere.Definition, point.X, point.Y)[0];
if (brush is TileDefinition)
brush = ((TileDefinition)brush).Index;
else if (brush is string)
brush = TileDefinition.Find((string)brush).Index;
SetTile(point.X, point.Y, (int)brush);
}
}
}
/// <summary>
/// Replaces one tile with another across the board.
/// </summary>
/// <param name="replaceThis">A <see cref="Noxico.TileDefinition"/>, tile name as a string, or tile number, or a Lua callback function.</param>
/// <param name="withThis">A <see cref="Noxico.TileDefinition"/>, tile name as a string, or tile number, or a Lua callback function.</param>
/// <remarks>The callback functions takes the tile number and TileDefinition at the current coordinates in the line and the coordinates. One must return a boolean, the other a TileDef, string, or int.</remarks>
public void Replace(object replaceThis, object withThis)
{
if (replaceThis is TileDefinition)
replaceThis = ((TileDefinition)replaceThis).Index;
else if (replaceThis is string)
replaceThis = TileDefinition.Find((string)replaceThis).Index;
if (withThis is TileDefinition)
withThis = ((TileDefinition)withThis).Index;
else if (withThis is string)
withThis = TileDefinition.Find((string)withThis).Index;
for (var y = 0; y < Width; y++)
{
for (var x = 0; x < Height; x++)
{
if (replaceThis is int)
{
if (Tilemap[y, x].Index != (int)replaceThis)
continue;
}
else if (replaceThis is Func<object, object, object, object, Neo.IronLua.LuaResult>)
{
var callback = (Func<object, object, object, object, Neo.IronLua.LuaResult>)replaceThis;
var tileHere = Tilemap[y, x];
if (!(bool)callback(tileHere.Index, tileHere.Definition, x, y)[0])
continue;
}
var w = withThis;
if (w is Func<object, object, object, object, Neo.IronLua.LuaResult>)
{
var callback = (Func<object, object, object, object, Neo.IronLua.LuaResult>)withThis;
var tileHere = Tilemap[y, x];
w = callback(tileHere.Index, tileHere.Definition, x, y)[0];
}
SetTile(x, y, (int)w);
}
}
}
/// <summary>
/// Replaces every tile found in a floodfill with another.
/// </summary>
/// <param name="startX">The starting row.</param>
/// <param name="startY">The starting column.</param>
/// <param name="replaceThis">Null/nil, a <see cref="Noxico.TileDefinition"/>, tile name as a string, or tile number, or a Lua callback function.</param>
/// <param name="withThis">A <see cref="Noxico.TileDefinition"/>, tile name as a string, or tile number, or a Lua callback function.</param>
/// <param name="allowDiagonals">If true, flood in eight directions, passing through more gaps.</param>
/// <remarks>The callback functions takes the tile number and TileDefinition at the current coordinates in the line and the coordinates. One must return a boolean, the other a TileDef, string, or int. If a null/nil is given, the tile number at the starting coordinates is used.</remarks>
public void Floodfill(int startX, int startY, object replaceThis, object withThis, bool allowDiagonals)
{
if (replaceThis is TileDefinition)
replaceThis = ((TileDefinition)replaceThis).Index;
else if (replaceThis is string)
replaceThis = TileDefinition.Find((string)replaceThis).Index;
if (withThis is TileDefinition)
withThis = ((TileDefinition)withThis).Index;
else if (withThis is string)
withThis = TileDefinition.Find((string)withThis).Index;
if (replaceThis == null)
{
replaceThis = Tilemap[startY, startX].Index;
}
var stack = new Stack<Point>();
stack.Push(new Point(startX, startY));
while (stack.Count > 0)
{
var point = stack.Pop();
var x = point.X;
var y = point.Y;
if (x < 0 || y < 0 || x >= Height || y >= Width)
continue;
if (replaceThis is int)
{
if (Tilemap[y, x].Index != (int)replaceThis)
continue;
}
else if (replaceThis is Func<object, object, object, object, Neo.IronLua.LuaResult>)
{
var callback = (Func<object, object, object, object, Neo.IronLua.LuaResult>)replaceThis;
var tileHere = Tilemap[y, x];
if (!(bool)callback(tileHere.Index, tileHere.Definition, x, y)[0])
continue;
}
{
var w = withThis;
if (w is Func<object, object, object, object, Neo.IronLua.LuaResult>)
{
var callback = (Func<object, object, object, object, Neo.IronLua.LuaResult>)withThis;
var tileHere = Tilemap[y, x];
w = callback(tileHere.Index, tileHere.Definition, x, y)[0];
}
SetTile(x, y, (int)w);
stack.Push(new Point(x - 1, y));
stack.Push(new Point(x + 1, y));
stack.Push(new Point(x, y - 1));
stack.Push(new Point(x, y + 1));
if (allowDiagonals)
{
stack.Push(new Point(x - 1, y - 1));
stack.Push(new Point(x - 1, y + 1));
stack.Push(new Point(x + 1, y - 1));
stack.Push(new Point(x + 1, y + 1));
}
}
}
}
public void MergeBitmap(string fileName, string tiledefs)
{
var bitmap = Mix.GetBitmap(fileName);
var tileset = new Token();
tileset.AddSet(Mix.GetTokenTree(tiledefs, true));
var width = Width;
var height = Height;
if (width > bitmap.Width) width = bitmap.Width;
if (height > bitmap.Height) height = bitmap.Height;
for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++)
{
var color = bitmap.GetPixel(x, y);
if (color.Name == "ff000000" || color.A == 0)
continue;
var key = color.Name.Substring(2).ToUpperInvariant();
if (!tileset.HasToken(key))
continue;
var tile = tileset.GetToken(key);
//Keep the original tile, but drain it.
if (tile.Text == "drain")
{
this.Tilemap[x, y].Fluid = Fluids.Dry;
continue;
}
this.Tilemap[x, y].Index = TileDefinition.Find(tile.Text).Index;
if (tile.Text.StartsWith("doorway"))
{
var door = new Door()
{
XPosition = x,
YPosition = y,
ForegroundColor = this.Tilemap[x, y].Definition.Background,
BackgroundColor = this.Tilemap[x, y].Definition.Background.Darken(),
ID = "mergeBitmap_Door" + x + "_" + y,
ParentBoard = this,
Closed = tile.Text.EndsWith("Closed"),
Glyph = '+'
};
this.Entities.Add(door);
}
if (tile.HasToken("clutter"))
{
var nc = new Clutter()
{
XPosition = x,
YPosition = y,
ParentBoard = this
};
this.Entities.Add(nc);
var properties = tile.GetToken("clutter");
foreach (var property in properties.Tokens)
{
switch (property.Name)
{
case "id": nc.ID = property.Text; break;
case "name": nc.Name = property.Text; break;
case "desc": nc.Description = property.Text; break;
case "glyph": nc.Glyph = (int)property.Value; break;
case "fg":
if (property.Text.StartsWith('#'))
nc.ForegroundColor = Color.FromCSS(property.Text);
else
nc.ForegroundColor = Color.FromName(property.Text);
break;
case "bg":
if (property.Text.StartsWith('#'))
nc.BackgroundColor = Color.FromCSS(property.Text);
else
nc.BackgroundColor = Color.FromName(property.Text);
break;
case "block": nc.Blocking = true; break;
case "burns": nc.CanBurn = true; break;
}
}
}
if (tile.HasToken("unique"))
{
var unique = tile.GetToken("unique");
var newChar = new BoardChar(Character.GetUnique(unique.Text))
{
XPosition = x,
YPosition = y,
ParentBoard = this
};
this.Entities.Add(newChar);
this.EnsureUniqueEntities();
newChar.AssignScripts(unique.Text);
newChar.ReassignScripts();
}
if (!tile.HasToken("fluid"))
this.Tilemap[x, y].Fluid = Fluids.Dry;
else
this.Tilemap[x, y].Fluid = (Fluids)Enum.Parse(typeof(Fluids), tile.GetToken("fluid").Text, true);
}
}
this.ResolveVariableWalls();
}
public void AddClutter(int x1, int y1, int x2, int y2)
{
//TODO: Reimplement this? Is currently covered by Variants.
/*
if (y2 >= 50)
y2 = 49;
for (var x = x1; x < x2; x++)
{
for (var y = y1; y < y2; y++)
{
var biomeData = BiomeData.Biomes[Tilemap[x, y].Biome];
if (biomeData.Clutter == null)
continue;
foreach (var clutter in biomeData.Clutter)
{
if (Random.NextDouble() < clutter.Chance)
{
if (Tilemap[x, y].SolidToDryWalker) //TODO: add a bool appearsInWater to clutter.
continue;
Tilemap[x, y] = new Tile()
{
Character = clutter.Character,
Foreground = clutter.ForegroundColor,
Background = clutter.BackgroundColor == Color.Transparent ? Tilemap[x, y].Background : clutter.BackgroundColor,
CanBurn = clutter.CanBurn,
Wall = clutter.Wall,
Fence = clutter.Fence,
SpecialDescription = clutter.Description,
};
}
}
}
}
*/
}
public void AddClutter()
{
AddClutter(0, 0, this.Width - 1, this.Height - 1);
}
public void AddWater(List<Rectangle> safeZones)
{
//TODO: Tweak some more to prevent... ahum... water damage.
var biome = BiomeData.Biomes[(int)this.GetToken("biome").Value];
var water = BiomeData.Biomes[BiomeData.ByName(biome.Realm == Realms.Nox ? "Water" : "KoolAid")];
var points = new List<Point>();
var pointsPerZone = 4;
var threshold = 0.66f;
if (safeZones.Count == 1 && safeZones[0].Left == 0 && safeZones[0].Right == this.Width - 1)
{
safeZones.Clear();
pointsPerZone = 8;
var x = Random.Next(0, 40);
var y = Random.Next(0, 10);
safeZones.Add(new Rectangle() { Left = x, Top = y, Right = x + 30, Bottom = y + 14 });
threshold = 0.5f;
}
foreach (var zone in safeZones)
{
for (var i = 0; i < pointsPerZone; i++)
{
var x = Random.Next(zone.Left + 4, zone.Right - 4);
var y = Random.Next(zone.Top + 4, zone.Bottom - 4);
points.Add(new Point(x, y));
}
}
var bitmap = new float[Height, Width];
var sum = 0.0;
var radius = 0.4;
for (var y = 0; y < Height; y++)
{
for (var x = 0; x < Width; x++)
{
sum = 0;
foreach (var point in points)
{
sum += radius / Math.Sqrt((x - point.X) * (x - point.X) + (y - point.Y) * (y - point.Y));
}
if (sum > 1.0)
sum = 1.0;
if (sum < 0.0)
sum = 0.0;
bitmap[y, x] = (float)sum;
}
}
for (var y = 0; y < Height; y++)
{
for (var x = 0; x < Width; x++)
{
if (bitmap[y, x] < threshold || Tilemap[x, y].SolidToWalker)
continue;
Tilemap[x, y].Fluid = Realm == Realms.Nox ? Fluids.Water : Fluids.KoolAid;
/*
Tilemap[x, y] = new Tile()
{
Character = water.GroundGlyphs[Random.Next(water.GroundGlyphs.Length)],
Foreground = water.Color.Darken(), //.Darken(water.DarkenPlus + (Random.NextDouble() / water.DarkenDiv)),
Background = water.Color, //(water.DarkenPlus + (Random.NextDouble() / water.DarkenDiv)),
Water = true,
};
*/
}
}
}
public void AddWater()
{
AddWater(new List<Rectangle>() { new Rectangle() { Left = 0, Top = 0, Right = this.Width - 1, Bottom = this.Height - 1 } });
}
public void Drain()
{
for (var y = 0; y < Height; y++)
for (var x = 0; x < Width; x++)
Tilemap[x, y].Fluid = Fluids.Dry;
}
public void GenerateTown(bool rename, bool vendors, List<string> vendorTypes)
{
this.BoardType = BoardType.Town;
this.GetToken("encounters").Value = 0;
this.GetToken("encounters").Tokens.Clear();
var townGen = new TownGenerator();
townGen.Board = this;
var biome = BiomeData.Biomes[(int)this.GetToken("biome").Value];
var cultureName = biome.Cultures.PickOne();
townGen.Culture = Culture.Cultures[cultureName];
townGen.Create(biome);
townGen.ToTilemap(ref this.Tilemap);
townGen.ToSectorMap(this.Sectors);
this.Music = biome.Realm == Realms.Nox ? "set://Town" : "set://Dungeon";
this.AddToken("culture", 0, cultureName);
if (rename)
{
while (true)
{
var newName = Culture.GetName(townGen.Culture.TownName, Culture.NameType.Town);
if (NoxicoGame.Me.Boards.Find(b => b != null && b.Name == newName) == null)
{
this.Name = newName;
break;
}
}
//this.ID = string.Format("{0}x{1}-{2}", this.cx, y, this.Name.ToID());
}
if (vendors)
{
if (vendorTypes == null)
{
vendorTypes = new List<string>();
var lootData = Mix.GetTokenTree("loot.tml", true);
foreach (var filter in lootData.Where(t => t.Path("filter/vendorclass") != null).Select(t => t.Path("filter/vendorclass")))
if (!vendorTypes.Contains(filter.Text))
vendorTypes.Add(filter.Text);
}
var citizens = this.Entities.OfType<BoardChar>().Where(e => e.Character.Path("role/vendor") == null).ToList();
foreach (var vendorType in vendorTypes)
{
if (Random.Flip())
continue;
if (citizens.Count == 0) //Shouldn't happen, but who knows.
break;
var chosenCitizen = citizens.PickOne();
citizens.Remove(chosenCitizen);
var spouse = chosenCitizen.Character.Spouse;
if (spouse != null)
citizens.Remove(spouse.BoardChar);
var newVendor = chosenCitizen.Character;
var vendorStock = newVendor.GetToken("items");
newVendor.RemoveAll("role");
newVendor.AddToken("role").AddToken("vendor").AddToken("class", 0, vendorType);
newVendor.GetToken("money").Value = 1000 + (Random.Next(0, 20) * 50);
Program.WriteLine("*** {0} of {2} is now a {1} ***", newVendor.Name.ToString(true), vendorType, this.Name);
chosenCitizen.RestockVendor();
}
}
}
public void EnsureUniqueEntities()
{
foreach (var a in Entities)
{
foreach (var b in Entities)
{
if (a == b)
continue;
if (a.ID == b.ID)
b.ID += 'X';
}
}
}
}
}