-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuildCity.cs
1510 lines (1241 loc) · 48.6 KB
/
BuildCity.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.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using QuickGraph;
using QuickGraph.Algorithms;
//This file contains probably the most important classes for both logic and visualization.
//BuildCity
//Job
//Army
//BuilCity is the sort of umbrella class in which multiple instances of 'Job' exist as well
//as one instance of 'Army'.
//The city grid logic is taken from a procedural city tutorial series: https://www.youtube.com/watch?v=sLtelfckEjc
//The street assets themselves are downlaoded from the Unity Asset Store, the package is called 'Simple Modular Street Kit'
public partial class BuildCity : MonoBehaviour
{
[SerializeField]
GUISkin _skin;
//the three masterlists of jobs
public List<Job> potentialJobs; //those that your could pursue, which time out after a while if not clicked on
public List<Job> currentJobs; // those you have clicked on and your army is pursuing
public List<Job> activeJobs; //job that are under construction
int proxyJobCount;
ProceduralBuilding pBuilding;
GameObject xStreets;
GameObject zStreets;
GameObject crossroad;
GameObject _grass;
GameObject _hq;
public MeshCollider globalHQ;
public Bounds cityBounds;
Material buildingMat;
GameObject mainController;
public int mapWidth;
public int mapHeight;
public Pixel[,] mapPixels;
public int buildingFootPrint;
public float multiplierSpacing;
float seed;
public int connected;
public float voxelSize = 2.0f;
public List<MeshCollider> voids;
public List<MeshCollider> voidsOther;
public List<Vector3Int> totalJobIndices;
public int allJobCount;
UndirectedGraph<SparseGrid.Voxel, TaggedEdge<SparseGrid.Voxel, SparseGrid.Face>> graph = null;
public float _revenue;
public Army army;
public int fleetCountIdle;
public int fleetCountActive;
public BuildCity(int cityWidth, int cityLength)
{
cityBounds = new Bounds();
mapWidth = cityWidth;
mapHeight = cityLength;
mapPixels = new Pixel[mapWidth, mapHeight];
buildingFootPrint = 12;
multiplierSpacing = 1.0f;
xStreets = Resources.Load("StreetX", typeof(GameObject)) as GameObject;
zStreets = Resources.Load("StreetZ", typeof(GameObject)) as GameObject;
crossroad = Resources.Load("CrossX", typeof(GameObject)) as GameObject;
_grass = Resources.Load("Grass", typeof(GameObject)) as GameObject;
_hq = Resources.Load("Capsule", typeof(GameObject)) as GameObject;
buildingMat = Resources.Load<Material>("Materials/_tiledBuilding");
totalJobIndices = new List<Vector3Int>();
proxyJobCount = 0;
_revenue = 0.0f;
allJobCount = 0;
fleetCountActive = 0;
fleetCountIdle = 0;
}
public void Awake()
{
mainController = GameObject.Find("MainController");
}
// Start is called before the first frame update
public void Start()
{
seed = 42; // I trust this number will yield the best possible version of my city
voids = new List<MeshCollider>();
voidsOther = new List<MeshCollider>();
potentialJobs = new List<Job>();
currentJobs = new List<Job>();
activeJobs = new List<Job>();
pBuilding = new ProceduralBuilding(buildingFootPrint, multiplierSpacing, this);
CityGen();
army = new Army(this);
//initiating one potential job between the 'WakeCity' coroutine
var j1 = new Job(this);
potentialJobs.Add(j1);
totalJobIndices.AddRange(j1.indices);
}
public IEnumerator WakeCity()
{
for (int i = 0; i < 120; i++)
{
bool canCreate = true;
float delay = (float)Random.Range(5, 15);
if (allJobCount < 6)
{
var a = new Job(this);
foreach (var j in potentialJobs)
foreach (var c in currentJobs)
if (a.name == j.name || a.name == c.name)
{
canCreate = false;
continue;
}
if (canCreate == true)
{
var _streets = a.streets;
var _target = a.targetBuilding;
var area = a.size;
potentialJobs.Add(a);
totalJobIndices.AddRange(a.indices);
}
}
yield return new WaitForSeconds(delay);
}
}
//This method is used to identify 'pixels' which require street light payments.
public List<BuildCity.Pixel> GetIntersectionPixels()
{
List<BuildCity.Pixel> output = new List<BuildCity.Pixel>();
for (int i = 0; i < mapHeight; i++)
{
for (int j = 0; j < mapWidth; j++)
{
var pix = mapPixels[i, j];
if (pix.Type == -3 || pix.Type == -2 || pix.Type == -1)
output.Add(pix);
}
}
return output;
}
public int getFleetSize()
{
var armyList = army.grid.GetVoxels().Where(v => v.On).ToList();
return armyList.Count;
}
public void UpdateFleetStatus()
{
fleetCountIdle = army.grid.GetVoxels().Where(v => v.On)
.Where(v => v.Idle == true).Count();
fleetCountActive = army.grid.GetVoxels().Where(v => v.On)
.Where(v => v.Idle == false).Count();
}
public int getFootPrint()
{
return buildingFootPrint;
}
public int getCityWidth()
{
return mapWidth;
}
public List<Job> UpdateAllJobs()
{
var allJobs = new List<Job>();
allJobs.AddRange(potentialJobs);
allJobs.AddRange(currentJobs);
return allJobs;
}
public void JobCount()
{
var count = potentialJobs.Count + currentJobs.Count;
allJobCount = count;
}
public List<GameObject> PickStreets(Pixel building, int index)
{
var streets = new List<GameObject>();
List<Pixel> closestStreets = FindClosestStreetPixels(building, index);
for (int i = 0; i < closestStreets.Count; i++)
{
if (closestStreets[i].IsActive)
{
var parent = closestStreets[i].DisplayBuilding;
var child = parent.transform.GetChild(0).gameObject;
streets.Add(child);
}
}
return streets;
}
public List<Pixel> neighborBlocks(BuildCity.Pixel active)
{
List<Pixel> outputBlocks = new List<Pixel>();
var neighbors = active.getAllNeighbors(mapPixels, mapWidth, mapHeight, active.Position.x, active.Position.z);
foreach (var n in neighbors)
if (!n.IsActive)
outputBlocks.Add(n);
return outputBlocks;
}
public GameObject PickBuilding(out BuildCity.Pixel plot, out int index)
{
Pixel buildingNew = new Pixel(this);
GameObject GO = null;
index = new int();
bool selected = false;
while (selected == false)
{
int r1 = Random.Range(0, mapHeight);
int r2 = Random.Range(0, mapWidth);
if (mapPixels[r1, r2].IsActive == false && mapPixels[r1, r2].HasStreet)
{
if (mapPixels[r1, r2].HQNotNeighbour(mapPixels, mapWidth, mapHeight, r1, r2))
{
var proxy = mapPixels[r1, r2].DisplayBuilding;
var proxyCount = proxy.transform.childCount;
if (proxyCount >= 2)
{
selected = true;
buildingNew = mapPixels[r1, r2];
}
}
}
}
plot = buildingNew;
var closestStreet = FindClosestStreetPixel(buildingNew);
var selectedGo = buildingNew.DisplayBuilding;
var goPos = buildingNew.WorldPosition;
var childCount = selectedGo.transform.childCount;
var tempGO = new List<GOScore>();
//gameobject within pixel
if (childCount > 3)
{
for (int i = 0; i < childCount; i++)
{
if (i != 0)
{
var bldg = selectedGo.transform.GetChild(i).gameObject;
var _index = bldg.transform.GetSiblingIndex();
var modPos = bldg.transform.localPosition + goPos;
var score = Vector3.Distance(modPos, closestStreet.WorldPosition);
tempGO.Add(new GOScore(bldg, score, _index));
}
}
var sortedTemp = tempGO.OrderBy(s => s.score).ToList();
var selTemp = sortedTemp[0].GO;
GO = selTemp;
index = sortedTemp[0].index;
}
else if (childCount == 3)
{
for (int i = 0; i < childCount; i++)
{
if (i != 0)
{
var bldg = selectedGo.transform.GetChild(i).gameObject;
var _index = bldg.transform.GetSiblingIndex();
var modPos = bldg.transform.localPosition + goPos;
var score = Vector3.Distance(modPos, closestStreet.WorldPosition);
tempGO.Add(new GOScore(bldg, score, _index));
}
}
var sortedTemp = tempGO.OrderBy(s => s.score).ToList();
var selTemp = sortedTemp[0].GO;
GO = selTemp;
index = sortedTemp[0].index;
}
return GO;
}
public Pixel FindClosestStreetPixel(Pixel activeBuilding)
{
Pixel outPix;
List<PixelScore> tempPixels = new List<PixelScore>();
Vector3Int activePos = activeBuilding.Position;
for (int i = 0; i < mapHeight; i++)
{
for (int j = 0; j < mapWidth; j++)
{
var pix = mapPixels[i, j];
//if is street
if (pix.IsActive)
{
var dist = Vector3.Distance(activePos, pix.Position);
var scored = new PixelScore(pix, dist);
tempPixels.Add(scored);
}
}
}
var sortedPix = tempPixels.OrderBy(d => d.getScore).ToList();
outPix = sortedPix[0].getPixel;
return outPix;
}
public List<Pixel> FindClosestStreetPixels(Pixel activeBuilding, int index)
{
List<Pixel> outPix = new List<Pixel>();
List<PixelScore> tempPixels = new List<PixelScore>();
var bldgChild = activeBuilding.DisplayBuilding.transform.GetChild(index);
var localPos = bldgChild.transform.localPosition;
var combinedPos = localPos + activeBuilding.WorldPosition;
Vector3 activePos = activeBuilding.Position;
for (int i = 0; i < mapHeight; i++)
{
for (int j = 0; j < mapWidth; j++)
{
var pix = mapPixels[i, j];
//if is street
if (pix.IsActive)
{
var dist = Vector3.Distance(combinedPos, pix.WorldPosition);
var scored = new PixelScore(pix, dist);
tempPixels.Add(scored);
}
}
}
var sortedPix = tempPixels.OrderBy(d => d.getScore).ToList();
List<PixelScore> selectedPix = new List<PixelScore>();
selectedPix = sortedPix.GetRange(0, 2);
for (int i = 0; i < selectedPix.Count; i++)
{
var pix = selectedPix[i].getPixel;
outPix.Add(pix);
}
return outPix;
}
public void FindAccessibleBuildings()
{
for (int i = 0; i < mapHeight; i++)
{
for (int j = 0; j < mapWidth; j++)
{
var activePix = mapPixels[i, j];
if (activePix.IsActive == false)
{
var neighbors = activePix.getOrthoNeighbors(mapPixels, mapWidth, mapHeight, i, j);
if (neighbors.Count > 0) activePix.HasStreet = true;
else activePix.HasStreet = false;
}
else
activePix.HasStreet = false;
}
}
}
public void CityGen()
{
mapPixels = new Pixel[mapWidth, mapHeight];
///gen map data
for (int h = 0; h < mapHeight; h++)
{
for (int w = 0; w < mapWidth; w++)
{
mapPixels[w, h] = new Pixel(this);
mapPixels[w, h].Type = (int)(Mathf.PerlinNoise(w / 10.0f + seed, h / 10.0f + seed) * 10);
mapPixels[w, h].IsActive = false;
mapPixels[w, h].HasStreet = false;
mapPixels[w, h].Position = new Vector3Int(w, h, 0);
mapPixels[w, h].Name = "BLDG " + w.ToString() + h.ToString();
}
}
//build streets
//street gen largely taken from tutorial
int x = Random.Range(1, 5);
for (int n = 0; n < mapHeight; n++)
{
for (int h = 0; h < mapWidth; h++)
{
if (x >= mapWidth)
x = (int)(x - (x / 2));
mapPixels[x, h].Type = -1;
mapPixels[x, h].IsActive = true;
mapPixels[x, h].HasStreet = false;
}
x += Random.Range(3, 10);
if (x > mapWidth) break;
}
int z = Random.Range(1, 5);
for (int n = 0; n < mapHeight; n++)
{
for (int w = 0; w < mapWidth; w++)
{
if (z >= mapHeight)
z = (int)(z - (z / 2));
if (mapPixels[w, z].Type == -1)
{
mapPixels[w, z].Type = -3;
mapPixels[w, z].IsActive = true;
mapPixels[w, z].HasStreet = false;
}
else
{
mapPixels[w, z].Type = -2;
mapPixels[w, z].IsActive = true;
mapPixels[w, z].HasStreet = false;
}
}
z += Random.Range(3, 10);
if (z >= mapHeight) break;
}
//Find buildings with street print
FindAccessibleBuildings();
bool hqAssigned = false;
while (hqAssigned == false)
{
var randomX = Random.Range(0, mapWidth);
var randomZ = Random.Range(0, mapHeight);
if (randomX < mapWidth && randomZ < mapHeight && randomX > 0 && randomZ > 0)
{
if (mapPixels[randomX, randomZ].HasStreet)
{
hqAssigned = true;
mapPixels[randomX, randomZ].Type = 11;
mapPixels[randomX, randomZ].IsActive = true;
mapPixels[randomX, randomZ].HasStreet = false;
}
}
}
Color lerpedColor = Color.gray;
//gen city
for (int h = 0; h < mapHeight; h++)
{
for (int w = 0; w < mapWidth; w++)
{
int result = mapPixels[w, h].Type;
var regPos = mapPixels[w, h].WorldPosition;
var origin = new Vector3(0, 0, 0);
float deltaMoveX = (mapWidth * buildingFootPrint / 2);
float deltaMoveY = (mapHeight * buildingFootPrint / 2);
Vector3 pos = new Vector3((w * buildingFootPrint) - deltaMoveX, 0, (h * buildingFootPrint) - deltaMoveY);
mapPixels[w, h].WorldPosition = pos;
if (result < -2)
{
GameObject _parent = new GameObject();
_parent.isStatic = true;
var localPos = pos + new Vector3(0, voxelSize * 1.0f, 0);
GameObject street = Instantiate(crossroad, localPos, crossroad.transform.rotation);
street.isStatic = true;
street.transform.SetParent(_parent.transform, false);
mapPixels[w, h].DisplayBuilding = _parent;
mapPixels[w, h].IsActive = true;
}
else if (result < -1)
{
GameObject _parent = new GameObject();
_parent.isStatic = true;
var localPos = pos + new Vector3(0, voxelSize * 1.0f, 0);
GameObject streetx = Instantiate(xStreets, localPos, xStreets.transform.rotation);
streetx.transform.SetParent(_parent.transform, false);
streetx.isStatic = true;
mapPixels[w, h].DisplayBuilding = _parent;
mapPixels[w, h].IsActive = true;
}
else if (result < 0)
{
GameObject _parent = new GameObject();
_parent.isStatic = true;
var localPos = pos + new Vector3(0, voxelSize *1.0f, 0);
GameObject streetz = Instantiate(zStreets, localPos, zStreets.transform.rotation);
streetz.isStatic = true;
streetz.transform.SetParent(_parent.transform, false);
mapPixels[w, h].DisplayBuilding = _parent;
mapPixels[w, h].IsActive = true;
}
else if (result < 1)
{
GameObject bldg = ChooseBuilding(45, 65, pos, lerpedColor);
mapPixels[w, h].DisplayBuilding = bldg;
}
else if (result < 2)
{
GameObject bldg = ChooseBuilding(30, 45, pos, lerpedColor);
mapPixels[w, h].DisplayBuilding = bldg;
}
else if (result < 4)
{
GameObject bldg = ChooseBuilding(20, 30, pos, lerpedColor);
mapPixels[w, h].DisplayBuilding = bldg;
}
else if (result < 5)
{
GameObject bldg = ChooseBuilding(15, 20, pos, lerpedColor);
mapPixels[w, h].DisplayBuilding = bldg;
}
else if (result < 6)
{
GameObject bldg = ChooseBuilding(10, 15, pos, lerpedColor);
mapPixels[w, h].DisplayBuilding = bldg;
}
//grass
else if (result < 7)
{
GameObject bldg = ChooseBuilding(6, 10, pos, lerpedColor);
mapPixels[w, h].DisplayBuilding = bldg;
}
else if (result < 10)
{
GameObject _parent = new GameObject();
GameObject grass = Instantiate(_grass, pos, Quaternion.identity);
grass.transform.SetParent(_parent.transform, false);
mapPixels[w, h].DisplayBuilding = _parent;
}
else if (result == 11)
{
GameObject _parent = new GameObject();
GameObject hq = Instantiate(_hq, pos + new Vector3(0, (_hq.transform.localScale.y * 0.5f) + (voxelSize * 0.5f), 0), Quaternion.identity);
hq.transform.SetParent(_parent.transform, false);
hq.GetComponent<MeshRenderer>().material.color = Color.black;
hq.GetComponent<MeshRenderer>().enabled = false;
mapPixels[w, h].DisplayBuilding = _parent;
mapPixels[w, h].IsActive = true;
globalHQ = hq.GetComponent<MeshCollider>();
}
}
}
getVoids();
setBounds();
}
public GameObject ChooseBuilding(int min, int max, Vector3 pos, Color color)
{
var rand = Random.Range(0, 4);
GameObject output = new GameObject();
if (rand == 0)
output = pBuilding.BuildingA(min, max, pos, buildingMat, color);
else if (rand == 1)
output = pBuilding.BuildingB(min, max, pos, buildingMat, color);
else if (rand == 2)
output = pBuilding.BuildingC(min, max, pos, buildingMat, color);
else if (rand == 3)
output = pBuilding.BuildingD(min, max, pos, buildingMat, color);
return output;
}
//check to see if each voxel has reached its target and update targets
//set voxels that have reached a target to 'active'/ 'working'
//make next index in indices the target for that job
public void UpdateJobTarget()
{
if (currentJobs.Count > 0)
{
foreach (var job in currentJobs)
{
if (job.indices.Count < 1) continue;
else
{
var activeVox = army.grid.GetVoxels().Where(v => v.On)
.Where(v => v.Job == job)/*.Where(v => v.Idle == false)*/;
foreach (var v in activeVox)
{
for (int i = 0; i < job.indices.Count; i++)
{
if (v.Index == job.indices[i])
{
v.MakeWork();
job.indices.RemoveAt(i);
for (int t = 0; t < totalJobIndices.Count; t++)
{
if (v.Index == totalJobIndices[t])
totalJobIndices.Remove(totalJobIndices[t]);
}
}
}
}
}
}
}
else
{
var activeVox = army.grid.GetVoxels().Where(v => v.On)
.Where(v => v.Job == null);
foreach(var a in activeVox)
{
if(a.Index == army.hqBestVacant)
{
a.MakeIdle();
}
}
}
}
// Update is called once per frame
public void Update()
{
JobHandler();//if job times out, voxel.job set to 'null', and voxels set to 'commute'
JobCount();
if (CheckCurrentJobs() == true)
{
RedistributeJobs();//every time new job is added or subtracted the army is redistributed to cover all targets
}
UpdateJobTarget();
}
public void RedistributeJobs()
{
IEnumerable <SparseGrid.Voxel> vox;
if (army.armyVacancyRate < 0.5f)
{
vox = army.grid.GetVoxels().Where(v => v.On).Where(v=>v.Job ==null).Where(v=>v.AtWork==false);
}
else
vox = army.grid.GetVoxels().Where(v => v.On).Where(v=>v.AtWork == false);
if (currentJobs.Count < 1)
{
foreach (var v in vox)
{
v.Job = null;
v.MakeCommute();
}
}
else
{
foreach (var job in currentJobs)
{
var jobSpecVox = vox.Take(job.indices.Count);
foreach (var j in jobSpecVox)
{
j.Job = job;
j.Job.indices = job.indices;
j.MakeCommute();
}
}
}
}
//determines whether there has been a change in the number of potential/active jobs
public bool CheckCurrentJobs()
{
bool worked = false;
if (currentJobs.Count != proxyJobCount)
{
proxyJobCount = currentJobs.Count;
worked = true;
}
else worked = false;
return worked;
}
public void JobHandler()
{
//handling potentialJobs
if(potentialJobs.Count>0)
{
for (int i = 0; i < potentialJobs.Count; i++)
{
var job = potentialJobs[i];
var jobStreets = job.streets;
var jobBuild = job.targetBuilding;
var indices = job.indices;
job.Update();
if (job.timeExpire <= job.timePassed)
{
totalJobIndices.RemoveRange(0, job.indices.Count);
potentialJobs.Remove(job);
foreach (var js in jobStreets)
js.GetComponent<MeshRenderer>().material.color = Color.white;
jobBuild.GetComponent<MeshRenderer>().material.color = Color.gray;
}
if (job.pursue)
{
currentJobs.Add(job);
potentialJobs.Remove(job);
}
if (job.abort)
{
var activeVox = army.grid.GetVoxels().Where(v => v.On)
.Where(v => v.Job == job);
foreach (var v in activeVox)
{
if (v.Job == job)
{
v.Job = null;
v.MakeCommute();
}
}
foreach(var j in job.indices)
{
totalJobIndices.Remove(j);
}
potentialJobs.Remove(job);
//color streets gray again
foreach (var js in jobStreets)
js.GetComponent<MeshRenderer>().material.color = Color.white;
//color building gray again
jobBuild.GetComponent<MeshRenderer>().material.color = Color.gray;
}
}
}
//handing currentJobs
if (currentJobs.Count > 0)
{
for (int i = 0; i < currentJobs.Count; i++)
{
var job = currentJobs[i];
var jobStreets = job.streets;
var jobBuild = job.targetBuilding;
var indices = job.indices;
job.Update();
foreach (var js in jobStreets)
js.GetComponent<MeshRenderer>().material.color = Color.green;
jobBuild.GetComponent<MeshRenderer>().material.color = Color.green;
if (job.completed)
{
var activeVox = army.grid.GetVoxels().Where(v => v.On)
.Where(v => v.Job == job);
foreach (var v in activeVox)
{
if (v.Job == job)
{
v.Job = null;
v.MakeCommute();
}
}
_revenue += job.payout;
currentJobs.Remove(job);
foreach (var js in jobStreets)
js.GetComponent<MeshRenderer>().material.color = Color.white;
jobBuild.GetComponent<MeshRenderer>().material.color = Color.gray;
}
if (job.abort)
{
var activeVox = army.grid.GetVoxels().Where(v => v.On)
.Where(v => v.Job == job);
foreach (var v in activeVox)
{
if (v.Job == job)
{
v.Job = null;
v.MakeCommute();
}
}
//totalJobIndices.RemoveRange(0, job.indices.Count);
foreach (var j in job.indices)
{
totalJobIndices.Remove(j);
}
currentJobs.Remove(job);
//color streets gray again
foreach (var js in jobStreets)
js.GetComponent<MeshRenderer>().material.color = Color.white;
//color building gray again
jobBuild.GetComponent<MeshRenderer>().material.color = Color.gray;
}
}
}
}
public void getVoids() // city voids - used to construct army sparse array
{
for (int i = 0; i < mapHeight; i++)
{
for (int j = 0; j < mapWidth; j++)
{
var go = mapPixels[i, j].DisplayBuilding;
var childCount = go.transform.childCount;
if (mapPixels[i, j].Type != 11)
{
if (childCount > 1)
{
for (int c = 0; c < childCount; c++)
{
//normal voids
if (c != 0)
{
var bldg = go.transform.GetChild(c).gameObject;
//bldg.transform.position = bldg.transform.position + new Vector3(0, voxelSize * 0.5f, 0);
var coll = bldg.GetComponent<MeshCollider>();
voids.Add(coll);
}
//big voids (used for optimized sparsgrid)
else
{
var bldg = go.transform.GetChild(0);
var coll = bldg.GetComponent<MeshCollider>();
voidsOther.Add(coll);
}
}
}
else
{
for (int c = 0; c < childCount; c++)
{
var bldg = go.transform.GetChild(c).gameObject;
var coll = bldg.GetComponent<MeshCollider>();
voids.Add(coll);
}
}
}
}
}
}
public void setBounds() //city bounds - used for army sparse array
{
var bbox = new Bounds();
foreach (var v in voids.Select(v => v.bounds))
bbox.Encapsulate(v);
var max = bbox.max;
var min = bbox.min + new Vector3(0, voxelSize, 0);
bbox.SetMinMax(min, max);
bbox.Expand(-voxelSize);
cityBounds = bbox;
}
}
public class Job
{
public BuildCity.Pixel plot;
UndirectedGraph<SparseGrid.Voxel, TaggedEdge<SparseGrid.Voxel, SparseGrid.Face>> _graph;
public float size;
public float payout;
public float timeReq;
public float timePassed;
public bool completed;
public bool timeUp;
public string name;
public int bldgIndex;
public List<GameObject> streets;
public GameObject targetBuilding;
public bool pursue;
public bool abort;
public float timeCreate;
public float timeExpire;
float reactClock;
public List<Vector3Int> indices;
public int initIndicesCount;
public BuildCity city;
//Generates structures to which fleet needs to attend to.
//Various instances of this class area consumed in BuildCity.
public Job(BuildCity city)
{
this.city = city;
_graph = city.army.graph;
indices = new List<Vector3Int>();
initIndicesCount = 0;
plot = new BuildCity.Pixel(city);
size = new float();
payout = new float();
timeReq = new float();
completed = false;
timeUp = false;
pursue = false;
abort = false;
timeExpire = new float();
Init(out streets, out targetBuilding);
InstObj();
name = plot.Name + string.Format("{0}", bldgIndex);
timeCreate = Time.realtimeSinceStartup;
timeExpire = timeCreate + 150.0f;
timePassed = timeCreate;
}
public void Init(out List<GameObject> streetObjs, out GameObject target)
{
var bldg = city.PickBuilding(out plot, out bldgIndex);
bldg.GetComponent<MeshRenderer>().material.color = Color.red;