~widelands-dev/widelands/bug-1797213-idle-wares-at-flags-dirty-hack

« back to all changes in this revision

Viewing changes to src/economy/flag.cc

  • Committer: GunChleoc
  • Date: 2018-10-22 08:08:45 UTC
  • Revision ID: fios@foramnagaidhlig.net-20181022080845-ee81jf2f5x4f3z1f
Put idle carrier in a loop + even more log output. We have an idle transfer and nextstep is empty.

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
 
38
38
namespace {
39
39
const Widelands::Coords test_coords(199, 199);
 
40
const Widelands::Coords target_flag_coords(197, 199);
40
41
const uint32_t test_coords_hash = test_coords.hash();
41
42
 
42
43
} // namespace
353
354
 * \return true/allow on low congestion-risk.
354
355
 */
355
356
bool Flag::has_capacity_for_ware(WareInstance& ware) const {
 
357
        /*
356
358
        bool test = false;
357
359
        if (get_position().hash() == test_coords_hash) {
358
 
                log("NOCOM has_capacity_for_ware (%d, %d) %s - this\n", get_position().x, get_position().y, ware.descr().name().c_str());
359
 
                test = true;
360
 
        }
361
 
        if (abs(get_position().x - test_coords.x) <= 1 && abs(get_position().y - test_coords.y) <= 1) {
362
 
                log("NOCOM has_capacity_for_ware (%d, %d) %s - very near flag\n", get_position().x, get_position().y, ware.descr().name().c_str());
363
 
                test = true;
364
 
        }
365
 
        /*else if (get_position().x == test_coords.x - 2 || get_position().x == test_coords.x + 2 || get_position().y == test_coords.y - 2 || get_position().y == test_coords.y + 2) {
366
 
                log("NOCOM has_capacity_for_ware (%d, %d) %s - near flag\n", get_position().x, get_position().y, ware.descr().name().c_str());
 
360
                log("NOCOM *************** Flag::has_capacity_for_ware (%d, %d) %s - this\n", get_position().x, get_position().y, ware.descr().name().c_str());
 
361
                test = true;
 
362
        }
 
363
        if (abs(get_position().x - test_coords.x) <= 1 && abs(get_position().y - test_coords.y) <= 0) {
 
364
                log("NOCOM *************** Flag::has_capacity_for_ware (%d, %d) %s - very near flag\n", get_position().x, get_position().y, ware.descr().name().c_str());
 
365
                test = true;
 
366
        }
 
367
        if (abs(get_position().x - test_coords.x) <= 2 && abs(get_position().y - test_coords.y) <= 0) {
 
368
                log("NOCOM *************** Flag::has_capacity_for_ware (%d, %d) %s - near flag\n", get_position().x, get_position().y, ware.descr().name().c_str());
367
369
                test = true;
368
370
        }
369
371
        */
 
372
 
 
373
        const bool test = get_position().hash() == target_flag_coords.hash();
 
374
        if (test) {
 
375
                log("NOCOM *************** Flag::has_capacity_for_ware (%d, %d) %s\n", get_position().x, get_position().y, ware.descr().name().c_str());
 
376
        }
 
377
 
370
378
        // NOCOM check this
371
379
        // avoid iteration for the easy cases
372
380
        if (ware_filled_ < ware_capacity_ - 2) {
373
 
                if (test) log("NOCOM ware_filled_ < ware_capacity_ - 2 true\n");
 
381
                if (test) log("NOCOM HAS CAPACITY ware_filled_ < ware_capacity_ - 2 true\n");
374
382
                return true;  // more than two free slots, allow
375
383
        }
376
384
        if (ware_filled_ >= ware_capacity_) {
377
 
                if (test) log("NOCOM ware_filled_ >= ware_capacity_ false\n");
 
385
                if (test) log("NOCOM NO CAPACITY ware_filled_ >= ware_capacity_ false\n");
378
386
                return false;  // all slots filled, no room
379
387
        }
380
388
 
381
389
        DescriptionIndex const descr_index = ware.descr_index();
382
390
        for (int i = 0; i < ware_filled_; ++i) {
383
391
                if (wares_[i].ware->descr_index() == descr_index) {
384
 
                        if (test) log("NOCOM already there - false\n");
 
392
                        if (test) log("NOCOM NO CAPACITY already there - false\n");
385
393
                        return false;  // ware of this type already present, leave room for other types
386
394
                }
387
395
        }
388
 
        if (test) log("NOCOM end - true\n");
 
396
        if (test) log("NOCOM end - HAS CAPACITY\n");
389
397
        return true;  // ware of this type not present, allow
390
398
}
391
399
 
394
402
 * has_capacity_for_ware() also checks ware's type to prevent congestion.
395
403
 */
396
404
bool Flag::has_capacity() const {
397
 
        if (get_position().hash() == test_coords_hash) {
398
 
                log("NOCOM **** Flag::has_capacity (%d, %d)\n", get_position().x, get_position().y);
 
405
        if (get_position().hash() == target_flag_coords.hash()) {
 
406
                log("NOCOM *************** Flag::has_capacity (%d, %d)\n", get_position().x, get_position().y);
399
407
        }
400
408
        return (ware_filled_ < ware_capacity_);
401
409
}
484
492
Flag::PendingWare* Flag::get_ware_for_flag(Flag& destflag, bool const pending_only) {
485
493
        bool test = false;
486
494
        if (get_position().hash() == test_coords_hash) {
487
 
                log("NOCOM get_ware_for_flag (%d, %d) -> (%d, %d) - this\n", get_position().x, get_position().y, destflag.get_position().x, destflag.get_position().y);
 
495
                log("NOCOM ######################### get_ware_for_flag (%d, %d) -> (%d, %d) - this\n", get_position().x, get_position().y, destflag.get_position().x, destflag.get_position().y);
488
496
                test = true;
489
497
        }
490
498
        if (destflag.get_position().hash() == test_coords_hash) {
491
 
                log("NOCOM get_ware_for_flag (%d, %d) -> (%d, %d) - other\n", destflag.get_position().x, destflag.get_position().y, get_position().x, get_position().y);
 
499
                log("NOCOM ######################### get_ware_for_flag (%d, %d) -> (%d, %d) - other\n", destflag.get_position().x, destflag.get_position().y, get_position().x, get_position().y);
492
500
                test = true;
493
501
        }
494
502
        // NOCOM test this
495
 
        if (test) log("NOCOM ware_filled_: %d\n", ware_filled_);
 
503
        if (test) log("NOCOM +++ ware_filled_: %d\n", ware_filled_);
496
504
        for (int32_t i = 0; i < ware_filled_; ++i) {
497
505
                PendingWare* pw = &wares_[i];
498
506
                if (test) {
499
507
                        log("NOCOM testing ware: %s\n", pw->ware->descr().name().c_str());
500
508
                        log("NOCOM !pending_only ? %s\n", !pending_only ? "true" : "false");
501
509
                        log("NOCOM pw->pending ? %s\n", pw->pending ? "true" : "false");
 
510
                        //pw->nextstep.get(get_owner()->egbase())->
502
511
                        log("NOCOM pw->nextstep == &destflag ? %s\n", pw->nextstep == &destflag ? "true" : "false");
 
512
                        if (pw->nextstep != nullptr) {
 
513
                                Coords dest_position(pw->nextstep.get(get_owner()->egbase())->get_positions(get_owner()->egbase())[0]);
 
514
                                log("NOCOM pw->nextstep position: (%d, %d)\n", dest_position.x, dest_position.y);
 
515
                        } else {
 
516
                                // NOCOM nextstep is empty where we should have a destination
 
517
                                log("NOCOM pw->nextstep is empty\n");
 
518
                        }
503
519
                        log("NOCOM destflag.allow_ware_from_flag(*pw->ware, *this) ? %s\n", destflag.allow_ware_from_flag(*pw->ware, *this) ? "true" : "false");
504
520
                }
505
521
                if ((!pending_only || pw->pending) && pw->nextstep == &destflag &&
509
525
                }
510
526
        }
511
527
 
 
528
        // We didn't find anything with priority, so let's schedule a ware for the next round if there is one
 
529
        if (current_wares() > 0 && destflag.current_wares() < 6) {
 
530
                PendingWare* pw = &wares_[0];
 
531
                if (pw->nextstep == nullptr) {
 
532
                        upcast(Game, game, &get_owner()->egbase());
 
533
                        init_ware(*game, *pw->ware, *pw);
 
534
                        pw->ware->update(*game);
 
535
                }
 
536
        }
 
537
 
512
538
        if (test) log("NOCOM no ware found\n");
513
539
        return nullptr;
514
540
}
820
846
void Flag::call_carrier(Game& game, WareInstance& ware, PlayerImmovable* const nextstep) {
821
847
        last_update_ = game.get_gametime();
822
848
        if (get_position().hash() == test_coords_hash) {
823
 
                log("NOCOM call_carrier - this\n");
 
849
                log("NOCOM **** call_carrier - this\n");
 
850
                /*
 
851
                if (nextstep != nullptr) {
 
852
                        Coords dest_position(nextstep->get(get_owner()->egbase())->get_positions(get_owner()->egbase())[0]);
 
853
                        log("NOCOM nextstep position: (%d, %d)", dest_position.x, dest_position.y);
 
854
                } else {
 
855
                        // NOCOM nextstep is empty where we should have a destination
 
856
                        log("NOCOM nextstep is empty\n");
 
857
                }
 
858
                */
824
859
        }
825
860
        PendingWare* pi = nullptr;
826
861
        int32_t i = 0;
909
944
        if (flag.get_position().hash() == test_coords_hash) {
910
945
                log("NOCOM allow_ware_from_flag - other\n");
911
946
        }
 
947
 
912
948
        // NOCOM test this
913
949
        // avoid iteration for the easy cases
914
950
        if (ware_filled_ < ware_capacity_ - 2) {
1151
1187
        const int gametime = game.get_gametime();
1152
1188
        if (gametime > last_update_ + kTriggerPotentiallyFrozenFlagInterval) {
1153
1189
                if (ware_filled_ > 0) {
1154
 
                        molog("%d: Potentially frozen flag for Player %d at (%d, %d) - %d wares\n", static_cast<int>(gametime / 1000), static_cast<unsigned int>(owner().player_number()), get_position().x, get_position().y, ware_filled_);
 
1190
                        if (test) {
 
1191
                                log("%d: Potentially frozen flag for Player %d at (%d, %d) - %d wares\n", static_cast<int>(gametime / 1000), static_cast<unsigned int>(owner().player_number()), get_position().x, get_position().y, ware_filled_);
 
1192
                        }
1155
1193
                        ++freeze_counter_;
1156
1194
                }
1157
1195
                last_update_ = gametime;
1158
1196
        } else {
1159
1197
                freeze_counter_ = 0;
1160
1198
        }
1161
 
        if (freeze_counter_ > 2) {
1162
 
                if ( ware_filled_ > 0) {
1163
 
                        log("%d: Unfreezing flag for Player %d at (%d, %d) - %d wares\n", static_cast<int>(gametime / 1000), static_cast<unsigned int>(owner().player_number()), get_position().x, get_position().y, ware_filled_);
 
1199
        if (freeze_counter_ > 1) {
 
1200
                if (ware_filled_ > 0) {
 
1201
                        if (test) {
 
1202
                                log("%d: Unfreezing flag for Player %d at (%d, %d) - %d wares\n", static_cast<int>(gametime / 1000), static_cast<unsigned int>(owner().player_number()), get_position().x, get_position().y, ware_filled_);
 
1203
                        }
1164
1204
                        for (int32_t i = 0; i < ware_filled_; ++i) {
1165
1205
                                init_ware(game, *wares_[i].ware, wares_[i]);
1166
1206
                                wares_[i].ware->update(game);