@@ -228,10 +228,23 @@ private static bool FindPlaceToBuild(Village vill, BuildingTask task)
228
228
229
229
// check building has same type
230
230
var existingBuilding = vill . Build . Buildings . FirstOrDefault ( x => x . Type == task . Building ) ;
231
-
231
+ if ( existingBuilding == null )
232
+ {
233
+ var currentBuilding = vill . Build . CurrentlyBuilding . FirstOrDefault ( x => x . Building == task . Building ) ;
234
+ if ( currentBuilding != null )
235
+ {
236
+ existingBuilding = new Building ( )
237
+ {
238
+ Id = ( byte ) currentBuilding . Location ,
239
+ Level = currentBuilding . Level ,
240
+ Type = currentBuilding . Building ,
241
+ UnderConstruction = true ,
242
+ } ;
243
+ }
244
+ }
232
245
// Only special buildings (warehouse, cranny, granary etc.) can have multiple
233
246
// buildings of it's type and use ConstructNew option
234
- if ( ! BuildingsData . CanHaveMultipleBuildings ( task . Building ) ) task . ConstructNew = false ;
247
+ task . ConstructNew = BuildingsData . CanHaveMultipleBuildings ( task . Building ) ;
235
248
236
249
// change to current building place instead of build new one
237
250
if ( existingBuilding != null && ! task . ConstructNew )
@@ -252,7 +265,7 @@ private static bool FindPlaceToBuild(Village vill, BuildingTask task)
252
265
253
266
// now find place to build in empty slot
254
267
var FreeSites = vill . Build . Buildings
255
- . Where ( x => x . Type == BuildingEnum . Site && 19 <= x . Id && x . Id <= 39 )
268
+ . Where ( x => x . Type == BuildingEnum . Site && 18 < x . Id && x . Id < 39 )
256
269
. OrderBy ( a => Guid . NewGuid ( ) ) // Shuffle the free sites
257
270
. ToList ( ) ;
258
271
@@ -361,45 +374,55 @@ public static bool AddBuildingTask(Account acc, Village vill, BuildingTask task,
361
374
}
362
375
// other building
363
376
else if ( task . BuildingId == null ||
364
- vill . Build . Buildings . Any ( x => x . Id == task . BuildingId &&
365
- x . Type != task . Building &&
366
- x . Type != BuildingEnum . Site ) )
377
+ vill . Build . Buildings . Any ( x => x . Id == task . BuildingId &&
378
+ x . Type != task . Building &&
379
+ x . Type != BuildingEnum . Site ) )
367
380
{
368
381
//Check if bot has any space to build new buildings, otherwise return
369
382
if ( ! FindPlaceToBuild ( vill , task ) ) return false ;
370
383
}
371
384
385
+ // double check building's location
386
+
372
387
// checking multiple building
373
388
// you need at least one at level 20 before building other
374
- if ( BuildingsData . CanHaveMultipleBuildings ( task . Building ) )
389
+ if ( BuildingsData . CanHaveMultipleBuildings ( task . Building ) && ! BuildingHelper . IsResourceField ( task . Building ) )
375
390
{
376
391
if ( task . ConstructNew )
377
392
{
393
+ var maxLevel = BuildingsData . MaxBuildingLevel ( acc , task . Building ) ;
378
394
// Highest level building
379
395
var highestLvl = vill . Build
380
396
. Buildings
381
- . Where ( x => x . Type == task . Building )
382
- . OrderByDescending ( x => x . Level )
383
- . FirstOrDefault ( ) ;
384
-
385
- if ( highestLvl != null &&
386
- highestLvl . Level != BuildingsData . MaxBuildingLevel ( acc , task . Building ) )
397
+ . FirstOrDefault ( x => x . Type == task . Building && x . Level == maxLevel ) ;
398
+ if ( highestLvl == null )
387
399
{
388
- task . BuildingId = highestLvl . Id ;
400
+ var currentBuilding = vill . Build
401
+ . CurrentlyBuilding
402
+ . FirstOrDefault ( x => x . Building == task . Building && x . Level == maxLevel ) ;
403
+ if ( currentBuilding == null )
404
+ {
405
+ var plannedBuilding = vill . Build
406
+ . Tasks
407
+ . FirstOrDefault ( x => x . Building == task . Building && x . Level == maxLevel ) ;
408
+
409
+ if ( plannedBuilding == null )
410
+ {
411
+ var onlyBuilding = vill . Build
412
+ . Buildings
413
+ . FirstOrDefault ( x => x . Type == task . Building ) ;
414
+ if ( onlyBuilding != null ) task . BuildingId = onlyBuilding . Id ;
415
+ }
416
+ }
389
417
}
390
418
}
391
419
}
392
420
else if ( ! BuildingHelper . IsResourceField ( task . Building ) )
393
421
{
394
- var buildings = vill . Build . Buildings . Where ( x => x . Type == task . Building ) ;
395
- if ( buildings . Count ( ) > 0 )
396
- {
397
- var id = buildings . First ( ) . Id ;
398
- if ( id != task . BuildingId )
399
- {
400
- task . BuildingId = id ;
401
- }
402
- }
422
+ var onlyBuilding = vill . Build
423
+ . Buildings
424
+ . FirstOrDefault ( x => x . Type == task . Building ) ;
425
+ if ( onlyBuilding != null ) task . BuildingId = onlyBuilding . Id ;
403
426
}
404
427
405
428
if ( bottom ) vill . Build . Tasks . Add ( task ) ;
0 commit comments