This repository has been archived by the owner on Jun 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
2103 lines (1769 loc) · 54.8 KB
/
Form1.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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using LoUOptimize2.Properties;
namespace LoUOptimize2
{
public partial class MainForm : Form
{
City city;
Button activeButton;
bool keepWorking;
public MainForm()
{
InitializeComponent();
propertyGridOptimization.SelectedObject = OptimizationSettings.Instance;
activeButton = null;
city = new City(false);
propertyGridCity.SelectedObject = city;
city.CalculateScore();
Icon = Resources.Icon;
pnlProgress.Visible = false;
}
private void pnlCity_Paint(object sender, PaintEventArgs e)
{
if (city.WaterCity)
e.Graphics.DrawImage(Resources.town_bg_water, pnlCity.ClientRectangle);
else
e.Graphics.DrawImage(Resources.town_bg, pnlCity.ClientRectangle);
Image tile;
Rectangle rect = new Rectangle();
for (int y = 0; y < 21; y++)
{
for (int x = 0; x < 21; x++)
{
tile = null;
switch (city.Tile[x, y])
{
case TileType.Stone: tile = Resources.stone; break;
case TileType.Wood: tile = Resources.forest; break;
case TileType.Iron: tile = Resources.iron; break;
case TileType.Lake: tile = Resources.lake; break;
case TileType.WoodcuttersHut: tile = Resources.building_hut; break;
case TileType.Quarry: tile = Resources.building_stone_quarry; break;
case TileType.Farm: tile = Resources.building_farm; break;
case TileType.Cottage: tile = Resources.building_cottage; break;
case TileType.Marketplace: tile = Resources.building_market_place; break;
case TileType.IronMine: tile = Resources.building_mine; break;
case TileType.Sawmill: tile = Resources.building_lumber_mill; break;
case TileType.Mill: tile = Resources.building_mill; break;
case TileType.Hideout: tile = Resources.building_hideout; break;
case TileType.Stonemason: tile = Resources.building_stonecutter; break;
case TileType.Foundry: tile = Resources.building_iron_furnace; break;
case TileType.TownHall: tile = Resources.building_townhall; break;
case TileType.Townhouse: tile = Resources.building_townhouse; break;
case TileType.Barracks: tile = Resources.building_barracks; break;
case TileType.CityGuardHouse: tile = Resources.building_cityguard_house; break;
case TileType.TrainingGround: tile = Resources.building_casern; break;
case TileType.Stable: tile = Resources.building_stables; break;
case TileType.Workshop: tile = Resources.building_weapon_factory; break;
case TileType.Shipyard: tile = Resources.building_shipyard; break;
case TileType.Warehouse: tile = Resources.building_storage; break;
case TileType.Castle: tile = Resources.building_stronghold; break;
case TileType.Harbor: tile = Resources.building_harbour; break;
case TileType.MoonglowTower: tile = Resources.building_mage_tower; break;
case TileType.TrinsicTemple: tile = Resources.building_temple; break;
case TileType.OldWoodcuttersHut: tile = Resources.building_hut_old; break;
case TileType.OldQuarry: tile = Resources.building_stone_quarry_old; break;
case TileType.OldIronMine: tile = Resources.building_mine_old; break;
case TileType.OldFarm: tile = Resources.building_farm_old; break;
case TileType.Empty:
{
bool haveFarm = false;
for (int v = -1; v <= 1; v++)
{
for (int u = -1; u <= 1; u++)
{
if ((x + u < 0) || (x + u >= 21) || (y + v < 0) || (y + v >= 21))
continue;
if ((city.Tile[x + u, y + v] == TileType.Farm) || (city.Tile[x + u, y + v] == TileType.OldFarm))
{
haveFarm = true;
break;
}
}
if (haveFarm)
{
tile = Resources.farmland;
break;
}
}
}
break;
}
if (tile != null)
{
rect.X = x * 45 + 40;
rect.Y = y * 28 + 10;
rect.Width = 64;
rect.Height = 64;
e.Graphics.DrawImage(tile, rect);
}
if (city.TileLocked[x, y])
{
rect.X = x * 45 + 50;
rect.Y = y * 28 + 25;
rect.Width = 12;
rect.Height = 16;
e.Graphics.DrawImage(Resources.padlock, rect);
}
}
}
}
private void tileButton_Click(object sender, EventArgs e)
{
if (activeButton != null)
activeButton.FlatStyle = FlatStyle.Standard;
activeButton = sender as Button;
activeButton.FlatStyle = FlatStyle.Flat;
}
private void pnlCity_MouseClick(object sender, MouseEventArgs e)
{
Point location = new Point(
(e.X - 40) / 45,
(e.Y - 20) / 28
);
if ((location.X < 0) || (location.X >= 21) || (location.Y < 0) || (location.Y >= 21))
return;
bool checkZone = false;
for (int i = 0; i < 5; i++)
{
if (city.CheckZone(location.X, location.Y, i))
{
checkZone = true;
break;
}
}
if (!checkZone)
return;
switch (city.Tile[location.X, location.Y])
{
case TileType.Wall:
case TileType.TownHall:
return;
}
if (e.Button == MouseButtons.Left)
{
if (activeButton == null)
return;
TileType tileType = (TileType)Enum.Parse(typeof(TileType), activeButton.Tag as string, true);
switch (tileType)
{
case TileType.Harbor:
case TileType.Shipyard:
switch (city.Tile[location.X, location.Y])
{
case TileType.Harbor:
case TileType.Shipyard:
case TileType.WaterEmpty:
break;
default:
return;
}
break;
case TileType.Empty:
switch (city.Tile[location.X, location.Y])
{
case TileType.Harbor:
case TileType.Shipyard:
case TileType.WaterEmpty:
tileType = TileType.WaterEmpty;
break;
default:
break;
}
break;
default:
switch (city.Tile[location.X, location.Y])
{
case TileType.Harbor:
case TileType.Shipyard:
case TileType.WaterEmpty:
return;
}
break;
}
city.Tile[location.X, location.Y] = tileType;
}
else if (e.Button == MouseButtons.Right)
{
city.TileLocked[location.X, location.Y] ^= true;
}
Rectangle dirtyRect = new Rectangle(
location.X * 45 + 35 - 64,
location.Y * 28 + 10 - 64,
64 * 3,
64 * 3
);
city.CalculateScore();
pnlCity.Invalidate(dirtyRect);
propertyGridCity.SelectedObject = city;
}
private void btnPaste_Click(object sender, EventArgs e)
{
City oldCity = city.Clone();
city.PutShareString(Clipboard.GetText());
city.CalculateScore();
Array.Copy(oldCity.TileLocked, city.TileLocked, oldCity.TileLocked.Length);
pnlCity.Invalidate();
}
private void btnCopy_Click(object sender, EventArgs e)
{
Clipboard.SetText(city.GetShareString());
}
private void btnRun_Click(object sender, EventArgs e)
{
if (pnlCity.Enabled)
{
pnlCity.Enabled = false;
btnPaste.Enabled = false;
btnRun.Text = "Stop";
btnCopy.Enabled = false;
btnNew.Enabled = false;
btnSave.Enabled = false;
btnLoad.Enabled = false;
btnLockCenter.Enabled = false;
btnLockNW.Enabled = false;
btnLockNE.Enabled = false;
btnLockSW.Enabled = false;
btnLockSE.Enabled = false;
propertyGridOptimization.Enabled = false;
if (!OptimizationSettings.Instance.PrepareUsableTiles())
{
MessageBox.Show(
"The optimizer has no tiles it can use. Please specify at least one resource to maximize or ensure minimum production or storage for.",
this.Text,
MessageBoxButtons.OK,
MessageBoxIcon.Asterisk
);
pnlCity.Enabled = true;
btnPaste.Enabled = true;
btnRun.Text = "Run";
btnCopy.Enabled = true;
btnNew.Enabled = true;
btnSave.Enabled = true;
btnLoad.Enabled = true;
btnLockCenter.Enabled = true;
btnLockNW.Enabled = true;
btnLockNE.Enabled = true;
btnLockSW.Enabled = true;
btnLockSE.Enabled = true;
propertyGridOptimization.Enabled = true;
return;
}
city.CalculateScore();
float initialScore = city.Score;
float settledScore = city.Score;
int settledGenerations = -1;
int generation = 0;
txtBestScore.Text = "0";
lblInitialScore.Text = initialScore.ToString();
pbarGenerations.Maximum = OptimizationSettings.Instance.MaxGenerations;
pbarGenerations.Value = 0;
pnlProgress.Visible = true;
List<City> population = new List<City>();
for (int i = 0; i < OptimizationSettings.Instance.PopulationSize / 5; i++)
population.Add(city.Clone());
while (population.Count < OptimizationSettings.Instance.PopulationSize)
population.Add(city.Mutate());
keepWorking = true;
while ((generation < OptimizationSettings.Instance.MaxGenerations) && keepWorking)
{
population.Sort();
population.RemoveRange(
OptimizationSettings.Instance.PopulationSize,
population.Count - OptimizationSettings.Instance.PopulationSize
);
City fittest = population[0];
txtBestScore.Text = fittest.Score.ToString();
pbarGenerations.Value = generation;
lblGeneration.Text = generation.ToString();
if (OptimizationSettings.Instance.AllowScoreSettling)
{
if (fittest.Score > settledScore)
{
settledScore = fittest.Score;
settledGenerations = -1;
}
else
{
settledGenerations++;
if (settledGenerations >= 1000)
break;
}
}
List<City> breedPool = new List<City>();
for (int i = 0; i < OptimizationSettings.Instance.PopulationSize / 10; i++)
{
breedPool.Add(population[0]);
population.RemoveAt(0);
}
float[] accumulatedScores = new float[population.Count];
accumulatedScores[0] = population[0].Score;
for (int i = 1; i < population.Count; i++)
accumulatedScores[i] = population[i].Score + accumulatedScores[i - 1];
float maxAccuScore = accumulatedScores[population.Count - 1];
while (breedPool.Count < (OptimizationSettings.Instance.PopulationSize / 2))
{
float picker = (float)OptimizationSettings.Random.NextDouble() * maxAccuScore;
for (int i = 0; i < population.Count; i++)
{
if (accumulatedScores[i] >= picker)
{
breedPool.Add(population[i]);
population.RemoveAt(i);
break;
}
}
}
population.Clear();
while (breedPool.Count > 1)
{
int pick;
City parent1, parent2;
pick = OptimizationSettings.Random.Next(breedPool.Count);
parent1 = breedPool[pick];
breedPool.RemoveAt(pick);
pick = OptimizationSettings.Random.Next(breedPool.Count);
parent2 = breedPool[pick];
breedPool.RemoveAt(pick);
City[] children = parent1.Crossover(parent2);
population.Add(children[0]);
population.Add(children[1]);
population.Add(parent1);
population.Add(parent2);
}
population.AddRange(breedPool);
if (!population.Contains(fittest))
population.Add(fittest);
generation++;
Application.DoEvents();
}
if (keepWorking)
{
population.Sort();
#region Restore unnecessarily destroyed resources
for (int y = 0; y < City.CityHeight; y++)
{
for (int x = 0; x < City.CityWidth; x++)
{
if (population[0].Tile[x, y] == TileType.Empty)
{
bool haveFarm = false;
for (int v = -1; v <= 1; v++)
{
for (int u = -1; u <= 1; u++)
{
if ((x + u < 0) || (x + u >= City.CityWidth) || (y + v < 0) || (y + v >= City.CityHeight))
continue;
if ((population[0].Tile[x + u, y + v] == TileType.Farm) || (population[0].Tile[x + u, y + v] == TileType.OldFarm))
{
haveFarm = true;
break;
}
}
if (haveFarm)
break;
}
if (!haveFarm)
{
switch (city.Tile[x, y])
{
case TileType.Wood:
case TileType.Stone:
case TileType.Iron:
case TileType.Lake:
population[0].Tile[x, y] = city.Tile[x, y];
break;
default:
break;
}
}
}
}
}
#endregion
city = population[0];
propertyGridCity.SelectedObject = city;
pnlCity.Invalidate();
}
pnlCity.Enabled = true;
btnPaste.Enabled = true;
btnRun.Text = "Run";
btnCopy.Enabled = true;
btnNew.Enabled = true;
btnSave.Enabled = true;
btnLoad.Enabled = true;
btnLockCenter.Enabled = true;
btnLockNW.Enabled = true;
btnLockNE.Enabled = true;
btnLockSW.Enabled = true;
btnLockSE.Enabled = true;
propertyGridOptimization.Enabled = true;
pnlProgress.Visible = false;
keepWorking = false;
}
else
keepWorking = false;
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
}
private void btnLockNW_Click(object sender, EventArgs e)
{
for (int y = 0; y < 21; y++)
{
for (int x = 0; x < 21; x++)
{
if (city.CheckZone(x, y, 1))
city.TileLocked[x, y] ^= true;
}
}
pnlCity.Invalidate();
}
private void btnLockNE_Click(object sender, EventArgs e)
{
for (int y = 0; y < 21; y++)
{
for (int x = 0; x < 21; x++)
{
if (city.CheckZone(x, y, 2))
city.TileLocked[x, y] ^= true;
}
}
pnlCity.Invalidate();
}
private void btnLockCenter_Click(object sender, EventArgs e)
{
for (int y = 0; y < 21; y++)
{
for (int x = 0; x < 21; x++)
{
if (city.CheckZone(x, y, 0))
city.TileLocked[x, y] ^= true;
}
}
pnlCity.Invalidate();
}
private void btnLockSW_Click(object sender, EventArgs e)
{
for (int y = 0; y < 21; y++)
{
for (int x = 0; x < 21; x++)
{
if (city.CheckZone(x, y, 3))
city.TileLocked[x, y] ^= true;
}
}
pnlCity.Invalidate();
}
private void btnLockSE_Click(object sender, EventArgs e)
{
for (int y = 0; y < 21; y++)
{
for (int x = 0; x < 21; x++)
{
if (city.CheckZone(x, y, 4))
city.TileLocked[x, y] ^= true;
}
}
pnlCity.Invalidate();
}
private void btnSave_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.DefaultExt = "*.txt";
sfd.Filter = "Text File (*.txt)|*.txt";
if (sfd.ShowDialog() == DialogResult.OK)
{
SaveFile(sfd.FileName);
}
}
private void btnLoad_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.DefaultExt = "*.txt";
ofd.Filter = "Text File (*.txt)|*.txt";
if (ofd.ShowDialog() == DialogResult.OK)
{
LoadFile(ofd.FileName);
}
}
private void LoadFile(string fileName)
{
StreamReader reader = new StreamReader(fileName);
OptimizationSettings.Instance.WoodProductionMaximize = bool.Parse(reader.ReadLine());
OptimizationSettings.Instance.WoodProductionTarget = float.Parse(reader.ReadLine());
OptimizationSettings.Instance.WoodStorageTarget = int.Parse(reader.ReadLine());
OptimizationSettings.Instance.StoneProductionMaximize = bool.Parse(reader.ReadLine());
OptimizationSettings.Instance.StoneProductionTarget = float.Parse(reader.ReadLine());
OptimizationSettings.Instance.StoneStorageTarget = int.Parse(reader.ReadLine());
OptimizationSettings.Instance.IronProductionMaximize = bool.Parse(reader.ReadLine());
OptimizationSettings.Instance.IronProductionTarget = float.Parse(reader.ReadLine());
OptimizationSettings.Instance.IronStorageTarget = int.Parse(reader.ReadLine());
OptimizationSettings.Instance.FoodProductionMaximize = bool.Parse(reader.ReadLine());
OptimizationSettings.Instance.FoodProductionTarget = float.Parse(reader.ReadLine());
OptimizationSettings.Instance.FoodStorageTarget = int.Parse(reader.ReadLine());
OptimizationSettings.Instance.ProductionTargetPenalty = float.Parse(reader.ReadLine());
OptimizationSettings.Instance.StorageTargetPenalty = float.Parse(reader.ReadLine());
OptimizationSettings.Instance.EqualizationPenalty = float.Parse(reader.ReadLine());
OptimizationSettings.Instance.PopulationSize = int.Parse(reader.ReadLine());
OptimizationSettings.Instance.MaxGenerations = int.Parse(reader.ReadLine());
OptimizationSettings.Instance.AllowScoreSettling = bool.Parse(reader.ReadLine());
city.PutShareString(reader.ReadLine());
for (int y = 0; y < 21; y++)
{
for (int x = 0; x < 21; x++)
{
city.TileLocked[x, y] = bool.Parse(reader.ReadLine());
}
}
reader.Close();
city.CalculateScore();
propertyGridCity.SelectedObject = city;
pnlCity.Invalidate();
}
private void SaveFile(string fileName)
{
StreamWriter writer = new StreamWriter(fileName, false);
writer.WriteLine(OptimizationSettings.Instance.WoodProductionMaximize);
writer.WriteLine(OptimizationSettings.Instance.WoodProductionTarget);
writer.WriteLine(OptimizationSettings.Instance.WoodStorageTarget);
writer.WriteLine(OptimizationSettings.Instance.StoneProductionMaximize);
writer.WriteLine(OptimizationSettings.Instance.StoneProductionTarget);
writer.WriteLine(OptimizationSettings.Instance.StoneStorageTarget);
writer.WriteLine(OptimizationSettings.Instance.IronProductionMaximize);
writer.WriteLine(OptimizationSettings.Instance.IronProductionTarget);
writer.WriteLine(OptimizationSettings.Instance.IronStorageTarget);
writer.WriteLine(OptimizationSettings.Instance.FoodProductionMaximize);
writer.WriteLine(OptimizationSettings.Instance.FoodProductionTarget);
writer.WriteLine(OptimizationSettings.Instance.FoodStorageTarget);
writer.WriteLine(OptimizationSettings.Instance.ProductionTargetPenalty);
writer.WriteLine(OptimizationSettings.Instance.StorageTargetPenalty);
writer.WriteLine(OptimizationSettings.Instance.EqualizationPenalty);
writer.WriteLine(OptimizationSettings.Instance.PopulationSize);
writer.WriteLine(OptimizationSettings.Instance.MaxGenerations);
writer.WriteLine(OptimizationSettings.Instance.AllowScoreSettling);
writer.WriteLine(city.GetShareString());
for (int y = 0; y < 21; y++)
{
for (int x = 0; x < 21; x++)
{
writer.WriteLine(city.TileLocked[x, y]);
}
}
writer.Close();
}
private void btnNew_Click(object sender, EventArgs e)
{
switch (MessageBox.Show("Water city?", "New City", MessageBoxButtons.YesNoCancel))
{
case DialogResult.Yes:
city = new City(true);
break;
case DialogResult.No:
city = new City(false);
break;
default:
break;
}
city.CalculateScore();
propertyGridCity.SelectedObject = city;
pnlCity.Invalidate();
}
}
public class DoubleBufferedPanel : Panel
{
public DoubleBufferedPanel()
: base()
{
DoubleBuffered = true;
}
}
public enum TileType : int
{
None,
Empty,
WaterEmpty,
Wall,
TownHall,
Cottage,
WoodcuttersHut,
Quarry,
IronMine,
Farm,
Wood,
Stone,
Iron,
Lake,
Sawmill,
Stonemason,
Foundry,
Mill,
Warehouse,
Marketplace,
Hideout,
Townhouse,
Barracks,
CityGuardHouse,
TrainingGround,
Stable,
Workshop,
Shipyard,
Castle,
Harbor,
MoonglowTower,
TrinsicTemple,
OldWoodcuttersHut,
OldQuarry,
OldIronMine,
OldFarm
}
public class City: IComparable<City>
{
TileType[,] tile;
bool[,] tileLocked;
float score;
float woodProduction, stoneProduction, ironProduction, foodProduction, goldProduction;
int woodStorage, stoneStorage, ironStorage, foodStorage;
float woodStorageCapacity, stoneStorageCapacity, ironStorageCapacity, foodStorageCapacity;
int buildings = 0;
bool waterCity;
int armySize;
float cityGuardHouseRecruitment, workshopRecruitment, trainingGroundRecruitment, shipyardRecruitment, stableRecruitment, trinsicTempleRecruitment, moonglowTowerRecruitment;
float hideoutCapacity;
int carts, ships;
float constructionSpeed;
public const int CityWidth = 21;
public const int CityHeight = 21;
[Browsable(false)]
public TileType[,] Tile { get { return tile; } }
[Browsable(false)]
public bool[,] TileLocked { get { return tileLocked; } }
public float Score { get { return score; } }
public float WoodProduction { get { return woodProduction; } }
public float StoneProduction { get { return stoneProduction; } }
public float IronProduction { get { return ironProduction; } }
public float FoodProduction { get { return foodProduction; } }
public float GoldProduction { get { return goldProduction; } }
[Browsable(false)]
public float WoodStorage { get { return woodStorage; } }
[Browsable(false)]
public float StoneStorage { get { return stoneStorage; } }
[Browsable(false)]
public float IronStorage { get { return ironStorage; } }
[Browsable(false)]
public float FoodStorage { get { return foodStorage; } }
public float WoodStorageCapacity { get { return woodStorageCapacity; } }
public float StoneStorageCapacity { get { return stoneStorageCapacity; } }
public float IronStorageCapacity { get { return ironStorageCapacity; } }
public float FoodStorageCapacity { get { return foodStorageCapacity; } }
public int Buildings { get { return buildings; } }
public bool WaterCity { get { return waterCity; } }
public int ArmySize { get { return armySize; } }
public float CityGuardHouseRecruitment { get { return cityGuardHouseRecruitment; } }
public float WorkshopRecruitment { get { return workshopRecruitment; } }
public float TrainingGroundRecruitment { get { return trainingGroundRecruitment; } }
public float ShipyardRecruitment { get { return shipyardRecruitment; } }
public float StableRecruitment { get { return stableRecruitment; } }
public float TrinsicTempleRecruitment { get { return trinsicTempleRecruitment; } }
public float MoonglowTowerRecruitment { get { return moonglowTowerRecruitment; } }
public float HideoutCapacity { get { return hideoutCapacity; } }
public int Carts { get { return carts; } }
public int Ships { get { return ships; } }
public float ConstructionSpeed { get { return constructionSpeed; } }
public City(bool waterCity)
{
tile = new TileType[CityWidth, CityHeight];
tileLocked = new bool[CityWidth, CityHeight];
if (waterCity)
PutShareString("[ShareString.1.3];########################-------#-------#####--------#--------###---------#---------##---------#---------##------#######------##-----##-----##-----##----##-------##----##----#---------#----##----#---------#----#######----T----#######----#---------#----##----#---------#----##----##-------##----##-----##-----##-----##------#######--__--##---------#----_##_-##---------#----_###_###--------#-----_#######-------#------_########################");
else
PutShareString("[ShareString.1.3]:########################-------#-------#####--------#--------###---------#---------##---------#---------##------#######------##-----##-----##-----##----##-------##----##----#---------#----##----#---------#----#######----T----#######----#---------#----##----#---------#----##----##-------##----##-----##-----##-----##------#######------##---------#---------##---------#---------###--------#--------#####-------#-------########################");
this.waterCity = waterCity;
}
public City Clone()
{
City newCity = new City(this.waterCity);
Array.Copy(this.tile, newCity.tile, this.tile.Length);
Array.Copy(this.tileLocked, newCity.tileLocked, this.tileLocked.Length);
newCity.score = this.score;
newCity.woodProduction = this.woodProduction;
newCity.woodStorage = this.woodStorage;
newCity.stoneProduction = this.stoneProduction;
newCity.stoneStorage = this.stoneStorage;
newCity.ironProduction = this.ironProduction;
newCity.ironStorage = this.ironStorage;
newCity.foodProduction = this.foodProduction;
newCity.foodStorage = this.foodStorage;
newCity.goldProduction = this.goldProduction;
newCity.buildings = this.buildings;
return newCity;
}
public int CompareTo(City other)
{
return other.score.CompareTo(this.score);
}
public override string ToString()
{
return score.ToString() +
" / W: " + woodProduction.ToString() + " (" + woodStorage.ToString() + ")" +
" S: " + stoneProduction.ToString() + " (" + stoneStorage.ToString() + ")" +
" I: " + ironProduction.ToString() + " (" + ironStorage.ToString() + ")" +
" F: " + foodProduction.ToString() + " (" + foodStorage.ToString() + ")" +
" G: " + goldProduction.ToString() +
" (" + buildings.ToString() + " buildings)";
}
private TileType[] GetNeighbors(int x, int y)
{
TileType[] neighbors = new TileType[8];
int xu, yv;
int n = 0;
for (int v = -1; v <= 1; v++)
{
for (int u = -1; u <= 1; u++)
{
xu = x + u;
yv = y + v;
if ((xu < 0) || (xu >= CityWidth) || (yv < 0) || (yv >= CityHeight))
continue;
if ((u == 0) && (v == 0))
continue;
neighbors[n] = this.tile[xu, yv];
n++;
}
}
while (n < 8)
{
neighbors[n] = TileType.None;
n++;
}
return neighbors;
}
public void CalculateScore()
{
score = 0;
woodProduction = 300.0f;
woodStorage = 0;
woodStorageCapacity = 175000.0f;
stoneProduction = 0;
stoneStorage = 0;
stoneStorageCapacity = 175000.0f;
ironProduction = 0;
ironStorage = 0;
ironStorageCapacity = 175000.0f;
foodProduction = 0;
foodStorage = 0;
foodStorageCapacity = 175000.0f;
goldProduction = 0;
buildings = 0;
armySize = 0;
cityGuardHouseRecruitment = 1.0f;
workshopRecruitment = 1.0f;
trainingGroundRecruitment = 1.0f;
shipyardRecruitment = 1.0f;
stableRecruitment = 1.0f;
trinsicTempleRecruitment = 1.0f;
moonglowTowerRecruitment = 1.0f;
hideoutCapacity = 0.0f;
carts = 0;
ships = 0;
#region Calculate production rates
for (int y = 0; y < CityHeight; y++)
{
for (int x = 0; x < CityWidth; x++)
{
switch (tile[x, y])
{
case TileType.None:
case TileType.Empty:
case TileType.Wall:
case TileType.WaterEmpty:
case TileType.TownHall:
case TileType.Wood:
case TileType.Stone:
case TileType.Iron:
case TileType.Lake:
break;
default:
buildings++;
break;
}
switch (tile[x, y])
{
case TileType.WoodcuttersHut:
{
#region
TileType[] neighbors = GetNeighbors(x, y);
float manpower = 1.0f;
float resources = 1.0f;
float processing = 1.0f;
for (int i = 0; i < neighbors.Length; i++)
{
switch (neighbors[i])
{
case TileType.Cottage:
manpower += 0.3f;
break;
case TileType.Wood:
if (resources == 1.0f)
resources += 0.5f;
else
resources += 0.4f;
break;
case TileType.Sawmill:
processing = 1.75f;
break;
}
}
#endregion
woodProduction += 300.0f * (manpower * resources * processing);
}
break;
case TileType.Quarry:
{
#region
TileType[] neighbors = GetNeighbors(x, y);
float manpower = 1.0f;
float resources = 1.0f;
float processing = 1.0f;
for (int i = 0; i < neighbors.Length; i++)
{
switch (neighbors[i])
{
case TileType.Cottage:
manpower += 0.3f;
break;
case TileType.Stone:
if (resources == 1.0f)
resources += 0.5f;
else