| 538 | } |
| 539 | |
| 540 | static int32_t do_cycle(color_ostream &out, bool force_designate) { |
| 541 | DEBUG(cycle,out).print("running {} cycle\n", plugin_name); |
| 542 | |
| 543 | // mark that we have recently run |
| 544 | cycle_timestamp = world->frame_counter; |
| 545 | |
| 546 | validate_burrow_configs(out); |
| 547 | |
| 548 | // scan trees and clearcut marked burrows |
| 549 | int32_t expected_yield; |
| 550 | TreesBySize designatable_trees_by_size; |
| 551 | vector<df::unit *> citizens; |
| 552 | Units::getCitizens(citizens, true); |
| 553 | int32_t newly_marked = scan_trees(out, &expected_yield, |
| 554 | &designatable_trees_by_size, true, citizens); |
| 555 | |
| 556 | // check how many logs we have already |
| 557 | int32_t usable_logs; |
| 558 | scan_logs(out, &usable_logs, citizens); |
| 559 | |
| 560 | if (config.get_bool(CONFIG_WAITING_FOR_MIN) |
| 561 | && usable_logs <= config.get_int(CONFIG_MIN_LOGS)) { |
| 562 | DEBUG(cycle,out).print("minimum threshold crossed\n"); |
| 563 | config.set_bool(CONFIG_WAITING_FOR_MIN, false); |
| 564 | } |
| 565 | else if (!config.get_bool(CONFIG_WAITING_FOR_MIN) |
| 566 | && usable_logs > config.get_int(CONFIG_MAX_LOGS)) { |
| 567 | DEBUG(cycle,out).print("maximum threshold crossed\n"); |
| 568 | config.set_bool(CONFIG_WAITING_FOR_MIN, true); |
| 569 | } |
| 570 | |
| 571 | // if we already have designated enough, we're done |
| 572 | int32_t limit = force_designate || !config.get_bool(CONFIG_WAITING_FOR_MIN) ? |
| 573 | config.get_int(CONFIG_MAX_LOGS) : |
| 574 | config.get_int(CONFIG_MIN_LOGS); |
| 575 | if (usable_logs + expected_yield > limit) { |
| 576 | return newly_marked; |
| 577 | } |
| 578 | |
| 579 | // designate until the expected yield gets us to our target or we run out |
| 580 | // of accessible trees |
| 581 | int32_t needed = config.get_int(CONFIG_MAX_LOGS) - |
| 582 | (usable_logs + expected_yield); |
| 583 | DEBUG(cycle,out).print("needed logs for this cycle: {}\n", needed); |
| 584 | for (auto & entry : designatable_trees_by_size) { |
| 585 | if (!Designations::markPlant(entry.second)) |
| 586 | continue; |
| 587 | ++newly_marked; |
| 588 | needed -= entry.first; |
| 589 | if (needed <= 0) { |
| 590 | return newly_marked; |
| 591 | } |
| 592 | } |
| 593 | out.print("autochop: insufficient accessible trees to reach log target! Still need {} logs!\n", |
| 594 | needed); |
| 595 | return newly_marked; |
| 596 | } |
| 597 |
no test coverage detected