-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.cpp
808 lines (684 loc) · 26.9 KB
/
main.cpp
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
/*
* Copyright (c) 2017, 2018 Florian Jung
*
* This file is part of factorio-bot.
*
* factorio-bot is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License,
* version 3, as published by the Free Software Foundation.
*
* factorio-bot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with factorio-bot. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <climits>
#include <cassert>
#include "factorio_io.h"
#include "scheduler.hpp"
#include "gui/gui.h"
#include "goal.hpp"
#include "mine_planning.h"
#include "logging.hpp"
using namespace std;
using namespace sched;
static float cluster_quality(int diam, size_t coal_size, size_t iron_size, size_t copper_size, size_t stone_size)
{
const float COAL_SIZE = 100;
const float IRON_SIZE = 25*10;
const float COPPER_SIZE = 25*10;
const float STONE_SIZE = 100;
return (diam + 50)
/ min(1.f,coal_size/COAL_SIZE) // penalize too-small chunks
/ min(1.f,iron_size/IRON_SIZE)
/ min(1.f,copper_size/COPPER_SIZE)
/ min(1.f,stone_size/STONE_SIZE);
}
struct start_mines_t
{
std::shared_ptr<ResourcePatch> coal;
std::shared_ptr<ResourcePatch> iron;
std::shared_ptr<ResourcePatch> copper;
std::shared_ptr<ResourcePatch> stone;
Pos water;
Area area;
};
static start_mines_t find_start_mines(FactorioGame* game, GUI::MapGui* gui, Pos_f pos = Pos_f(0.,0.))
{
Logger log("find_start_mines");
start_mines_t result;
// FIXME: we treat "known && !can_walk" as water and "known && can_walk" as land. this is suboptimal.
// FIXME so many magic numbers!
// enumerate all relevant resource patches, limit the number to 5
vector< pair<float, shared_ptr<ResourcePatch>> > patches[Resource::N_RESOURCES];
for (auto patch : game->resource_patches) if (patch->size() >= 30)
{
switch (patch->type)
{
case Resource::COAL:
case Resource::IRON:
case Resource::COPPER:
case Resource::STONE:
patches[patch->type].emplace_back( (pos-patch->bounding_box.center()).len(), patch );
break;
case Resource::NONE:
// this should never happen
assert(patch->type != Resource::NONE);
break;
default: // empty
break;
}
}
// only consider the 5 closest patches of each resource
int radius = 0;
for (auto i : {Resource::COAL, Resource::IRON, Resource::COPPER, Resource::STONE})
{
sort(patches[i].begin(), patches[i].end());
patches[i].resize(min(patches[i].size(), size_t(5)));
if (!patches[i].empty())
radius = max(radius, int(patches[i].back().first));
else
throw runtime_error("find_start_mines could not find "+Resource::typestr[i]);
}
// find potential water sources in the starting area
int water_radius = radius + 100;
auto view = game->walk_map.view(pos-Pos(water_radius+1,water_radius+1), pos+Pos(water_radius+1,water_radius+1), Pos(0,0));
auto resview = game->resource_map.view(pos-Pos(water_radius+1,water_radius+1), pos+Pos(water_radius+1,water_radius+1), Pos(0,0));
struct watersource_t { Pos pos; Pos get_pos() const { return pos;} };
WorldList<watersource_t> watersources;
for (int x = int(pos.x)-water_radius; x<=int(pos.x)+water_radius; x++)
for (int y = int(pos.y)-water_radius; y<=int(pos.y)+water_radius; y++)
if (view.at(x,y).land())
{
int n_water = 0;
size_t water_size = 0;
for (Pos p : directions4)
if (view.at(Pos(x,y)+p).water())
{
n_water++;
water_size = resview.at(Pos(x,y)+p).resource_patch.lock()->size();
}
if (n_water == 1 && water_size > 100)
watersources.insert( watersource_t{Pos(x,y)} );
}
float best = 999999999999; // best quality found so far
// brute-force all 4-tuples and find the best
for (auto& coal : patches[Resource::COAL])
for (auto& iron : patches[Resource::IRON])
for (auto& copper : patches[Resource::COPPER])
for (auto& stone : patches[Resource::STONE])
{
log << endl;
log << "coal:\t" << coal.second->bounding_box.center().str() << endl;
log << "iron:\t" << iron.second->bounding_box.center().str() << endl;
log << "copper:\t" << copper.second->bounding_box.center().str() << endl;
log << "stone:\t" << stone.second->bounding_box.center().str() << endl;
// calculate the bounding box enclosing the 4-tuple of essential resources
int x1 = min( coal .second->bounding_box.center().x,
min( iron .second->bounding_box.center().x,
min( stone .second->bounding_box.center().x,
copper.second->bounding_box.center().x )));
int x2 = max( coal .second->bounding_box.center().x,
max( iron .second->bounding_box.center().x,
max( stone .second->bounding_box.center().x,
copper.second->bounding_box.center().x )));
int y1 = min( coal .second->bounding_box.center().y,
min( iron .second->bounding_box.center().y,
min( stone .second->bounding_box.center().y,
copper.second->bounding_box.center().y )));
int y2 = max( coal .second->bounding_box.center().y,
max( iron .second->bounding_box.center().y,
max( stone .second->bounding_box.center().y,
copper.second->bounding_box.center().y )));
int diam = max( x2-x1, y2-y1 );
if (cluster_quality(diam, coal.second->size(), iron.second->size(), copper.second->size(), stone.second->size()) > best)
continue; // we're worse than the best even without extending the diameter to include water. discard and continue.
log << "diam = " << diam << endl;
// find the closest water source and calculate the new bounding box
Pos_f box_center = Pos_f((x1+x2)/2., (y1+y2)/2.);
Pos best_water_pos;
int best_water_diam = INT_MAX;
for (auto water_pos : watersources.around(box_center))
{
if ((water_pos.pos - box_center).len() >= 1.5*(best_water_diam-diam/2) )
break;
log << ".";
int new_diam = max(
max(x2,water_pos.pos.x)-min(x1,water_pos.pos.x),
max(y2,water_pos.pos.y)-min(y1,water_pos.pos.y) );
if (new_diam < best_water_diam)
{
best_water_diam = new_diam;
best_water_pos = water_pos.pos;
log << "\b*";
}
}
log << endl;
log << "found water at " << best_water_pos.str() << endl;
log << "best_water_diam = " << best_water_diam << endl;
x1 = min(x1, best_water_pos.x);
y1 = min(y1, best_water_pos.y);
x2 = max(x2, best_water_pos.x);
y2 = max(y2, best_water_pos.y);
float quality = cluster_quality(best_water_diam, coal.second->size(), iron.second->size(), copper.second->size(), stone.second->size());
if (quality < best)
{
best=quality;
// TODO
result.coal = coal.second;
result.copper = copper.second;
result.stone = stone.second;
result.iron = iron.second;
result.water = best_water_pos;
result.area = Area(x1,y1,x2,y2);
}
}
gui->rect( result.area.left_top, result.area.right_bottom, GUI::Color(0,255,0) );
gui->rect( result.coal->bounding_box.left_top, result.coal->bounding_box.right_bottom, GUI::Color(255,0,255) );
gui->rect( result.copper->bounding_box.left_top, result.copper->bounding_box.right_bottom, GUI::Color(255,0,255) );
gui->rect( result.iron->bounding_box.left_top, result.iron->bounding_box.right_bottom, GUI::Color(255,0,255) );
gui->rect( result.stone->bounding_box.left_top, result.stone->bounding_box.right_bottom, GUI::Color(255,0,255) );
return result;
}
struct debug_draw_actions_state_t
{
int count = 0;
Pos last;
};
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wshadow"
static void debug_draw_actions(const action::ActionBase* action, GUI::MapGui* gui, debug_draw_actions_state_t& state)
{
Logger log("debug_draw");
using namespace action;
if (auto g = dynamic_cast<const WalkTo*>(action))
{
log << "WalkTo " << g->destination.str() << endl;
GUI::Color color = GUI::Color(255,0,255);
color.blend( GUI::Color(127,127,127), (state.count%5)/4.0f);
gui->line(state.last, g->destination.center(), color);
gui->rect(g->destination.left_top, g->destination.right_bottom, GUI::Color(255,127,0));
state.last = g->destination.center();
state.count++;
}
else if (auto g = dynamic_cast<const TakeFromInventory*>(action))
{
log << "TakeFromInventory" << endl;
gui->rect(state.last,1, GUI::Color(0,127,255));
}
else if (auto g = dynamic_cast<const CompoundAction*>(action))
{
log << "recursing into CompoundAction" << endl;
for (const auto& sub : g->subactions)
debug_draw_actions(sub.get(), gui, state);
}
}
#pragma GCC diagnostic pop
static void debug_draw_actions(const action::ActionBase* action, GUI::MapGui* gui, Pos start)
{
debug_draw_actions_state_t state;
state.last = start;
debug_draw_actions(action, gui, state);
}
int main(int argc, const char** argv)
{
if (argc != 3 && argc != 6)
{
cout << "Usage: " << argv[0] << " outfile-prefix datapath [rcon-host rcon-port rcon-password]" << endl;
cout << " outfile-prefix: path to the file generated by the mod, minus the '...1.txt' part" << endl;
cout << " The rcon options can be left out. In this case, the bot is observe-only." << endl;
cout << " This can be useful for debugging, since Factorio doesn't even need to run. It is" << endl;
cout << " sufficient to read a previously generated outfile. " << endl;
return 1;
}
FactorioGame factorio(argv[1]);
cout << "reading static data" << endl;
while(true)
{
string packet = factorio.read_packet();
if (packet == "0 STATIC_DATA_END")
break;
else
factorio.parse_packet(packet);
}
cout << "done reading static data" << endl;
goal::GoalList test_goals;
{
test_goals.push_back( make_unique<goal::PlaceEntity>(Entity(Pos(1,1), &factorio.get_entity_prototype("assembling-machine-1"))) );
test_goals.push_back( make_unique<goal::PlaceEntity>(Entity(Pos(-3,4), &factorio.get_entity_prototype("wooden-chest"))) );
test_goals.push_back( make_unique<goal::RemoveEntity>(Entity(Pos(12,12), &factorio.get_entity_prototype("tree-01"))) );
auto test_actions = test_goals.calculate_actions(&factorio, 1, nullopt);
test_goals.dump(&factorio);
cout << "ActionList" << endl;
item_balance_t balance;
Pos pos{0,0};
for (const auto& a : test_actions)
{
auto [newpos, duration] = a->walk_result(pos);
auto bal = a->inventory_balance();
cout << "\t" << a->str() << " | walks from " << pos.str() << " to " << newpos.str() << " in " << std::chrono::duration_cast<chrono::milliseconds>(duration).count() << "ms" << endl;
for (auto [k,v] : bal)
cout << "\t\t" << k->name << " -> " << v << endl;
pos = newpos;
accumulate_map(balance, a->inventory_balance());
}
cout << "total item balance for this task:" << endl;
for (auto [k,v] : balance)
cout << "\t" << k->name << " -> " << v << endl;
}
//exit(0);
bool online = false;
if (argc == 6)
{
string host(argv[3]);
int port = atoi(argv[4]);
string pass(argv[5]);
factorio.rcon_connect(host, port, pass);
online = true;
}
int frame=0;
GUI::MapGui gui(&factorio, argv[2]);
// quick read first part of the file before doing any GUI work. Useful for debugging, since reading in 6000 lines will take more than 6 seconds.
for (int i=0; i<6000; i++)
{
//cout << i << endl;
factorio.parse_packet(factorio.read_packet());
}
size_t player_idx = SIZE_MAX;
for (size_t i = 0; i < factorio.players.size(); i++)
if (factorio.players[i].connected)
{
player_idx = i;
break;
}
if (player_idx == SIZE_MAX)
{
cout << "Could not determine a player index :(" << endl;
if (!online)
{
cout << "making up one (1)" << endl;
player_idx = 1;
}
else
exit(1);
}
cout << endl << "Player index = " << player_idx << endl << endl;
struct StrategyPlayer
{
Scheduler scheduler;
shared_ptr<Task> current_task = nullptr;
enum class TaskExecutionState { APPROACHING_START_LOCATION, AWAITING_LAUNCH, LAUNCHED, FINISHED };
TaskExecutionState task_execution_state = TaskExecutionState::FINISHED;
std::shared_ptr<action::CraftRecipe> current_crafting_action;
std::optional<Scheduler::owned_recipe_t> current_craft;
StrategyPlayer(FactorioGame* game, size_t idx) : scheduler(game,idx) {}
void add_task(const std::shared_ptr<Task>& task)
{
// TODO FIXME
}
void update_task(FactorioGame* game, Player& player, const std::shared_ptr<Task>& task)
{
Logger log("strategy");
log << "Player " << player.id << "'s current task has changed from "
<< (current_task ? current_task->name : "(null)")
<< " to " << (task ? task->name : "(null)") << endl;
current_task = task;
player.set_actions(nullptr);
task_execution_state = TaskExecutionState::FINISHED;
if (task)
{
task_execution_state = TaskExecutionState::APPROACHING_START_LOCATION;
auto approach_action = make_shared<action::WalkTo>(game, player.id, task->start_location, task->start_radius);
player.set_actions(approach_action);
}
}
void tick_scheduler(FactorioGame* game, Player& player)
{
Logger log("strategy");
std::shared_ptr<Task> task = scheduler.get_current_task();
if (current_task != task)
update_task(game, player, task);
assert(current_task == task);
bool actions_finished = player.tick_actions();
switch (task_execution_state)
{
case TaskExecutionState::APPROACHING_START_LOCATION:
if (actions_finished)
{
log << "player #" << player.id << " has approached task '" << current_task->name << "''s start location" << endl;
task_execution_state = TaskExecutionState::AWAITING_LAUNCH;
}
else
break;
[[fallthrough]];
case TaskExecutionState::AWAITING_LAUNCH:
if (player.inventory.can_satisfy(current_task->required_items, current_task->owner_id))
{
log << "player #" << player.id << " has launched task '" << current_task->name << "'" << endl;
player.set_actions(current_task->actions);
actions_finished = false;
task_execution_state = TaskExecutionState::LAUNCHED;
}
break;
case TaskExecutionState::LAUNCHED:
if (actions_finished)
{
log << "player #" << player.id << "'s task '" << current_task->name << "' has finished. ";
if (current_task->goals.has_value())
{
log << "Its goals are " << (current_task->goals->all_fulfilled(game) ? "" : "NOT ") << "fulfilled:" << endl;
current_task->goals->dump(game);
}
else
log << "It had no goals, only actions (which have been executed)" << endl;
log << "removing that task from the scheduler..." << endl;
scheduler.remove_task(current_task);
if (current_task->finished_callback)
current_task->finished_callback();
current_task = nullptr;
task_execution_state = TaskExecutionState::FINISHED;
scheduler.recalculate();
}
else
break;
[[fallthrough]];
case TaskExecutionState::FINISHED:
//scheduler.recalculate();
break;
}
tick_craftinglist();
}
void tick_craftinglist()
{
Logger log("craftinglist");
assert( (current_crafting_action!=nullptr) == current_craft.has_value() );
if (current_crafting_action)
assert(current_crafting_action->recipe == current_craft->second);
// check if the current craft has finished
if (current_craft.has_value())
{
if (current_crafting_action->is_finished())
{
auto t = current_craft->first.lock();
log << "player #" << scheduler.player_idx << " has finished crafting " << current_craft->second->name << "(" << (t ? t->name : "?") << ")" << endl;
scheduler.confirm_current_craft(current_craft.value());
current_crafting_action = nullptr;
current_craft = nullopt;
log << "there is " << (scheduler.peek_current_craft() ? "a" : "no") << " next craft." << endl;
}
}
// check if there is a new craft
// TODO FIXME: ugly.
if (current_craft.has_value() != scheduler.peek_current_craft().has_value() || (current_craft.has_value() && scheduler.peek_current_craft().has_value() && !Scheduler::owned_recipe_t_equal(current_craft.value(), scheduler.peek_current_craft().value())))
{
log << "player #" << scheduler.player_idx << "'s current_craft has changed from ";
if (current_craft.has_value())
{
log << current_craft->second->name;
if (auto t = current_craft->first.lock())
log << "(" << t->name << ")";
else
log << "(?)";
}
else
log << "(null)";
log << " to ";
if (scheduler.peek_current_craft().has_value())
{
log << scheduler.peek_current_craft()->second->name;
if (auto t = scheduler.peek_current_craft()->first.lock())
log << "(" << t->name << ")";
else
log << "(?)";
}
else
log << "(null)";
log << endl;
// abort the previous craft
if (current_craft.has_value())
{
log << "aborting previous craft" << endl;
current_crafting_action->abort();
scheduler.retreat_current_craft(current_craft.value());
current_crafting_action = nullptr;
current_craft = nullopt;
}
current_craft = scheduler.peek_current_craft();
// launch the new
if (current_craft.has_value())
{
auto owning_task = std::shared_ptr(current_craft->first);
// create and launch a CraftRecipe action
current_crafting_action = make_shared<action::CraftRecipe>(scheduler.game, scheduler.player_idx, owning_task->owner_id, current_craft->second);
owning_task->associated_crafting_actions.push_back(current_crafting_action);
action::registry.start_action(current_crafting_action);
// confirm this to the scheduler
scheduler.accept_current_craft();
}
}
}
};
vector<StrategyPlayer> splayers;
for (size_t i=0; i<=player_idx; i++)
splayers.emplace_back(&factorio, i);
struct EarlyStrategy
{
using tasklist_t = std::vector< std::shared_ptr<Task> >;
FactorioGame* game;
GUI::MapGui* gui;
size_t player_idx;
start_mines_t start_mines;
struct facility_t
{
string name;
vector<PlannedEntity> entities;
int level = 0; // this is the desired level
int actual_level = 0;
facility_t(string n, const vector<PlannedEntity>& e) : name(n), entities(e), level(0) {}
};
std::array<facility_t,4> facilities;
EarlyStrategy(FactorioGame* game_, GUI::MapGui* gui_, size_t player_idx_) :
game(game_),
gui(gui_),
player_idx(player_idx_),
start_mines(find_start_mines(game, gui)),
facilities {
facility_t("coal", plan_early_coal_rig(*start_mines.coal, game)),
facility_t("iron", plan_early_smelter_rig(*start_mines.iron, game)),
facility_t("copper", plan_early_smelter_rig(*start_mines.copper, game)),
facility_t("stone", plan_early_chest_rig(*start_mines.stone, game)) }
{
}
// create some initial tasks
tasklist_t initial_tasks()
{
// FIXME this is unneeded now that the steel-axe item is gone
return {};
}
enum class mine_t { COAL = 0, IRON = 1, COPPER = 2, STONE = 3 };
tasklist_t add_mine(mine_t mine)
{
facility_t& facility = facilities[int(mine)];
facility.level++;
auto task = make_shared<Task>(facility.name + " facility upgrade ("+to_string(facility.level)+")");
int new_level = facility.level;
task->finished_callback = [&facility, new_level](){ facility.actual_level = new_level; };
task->priority_ = 0;
task->goals.emplace();
for (const auto& ent : facility.entities) if (ent.level < facility.level)
task->goals->push_back(make_unique<goal::PlaceEntity>(ent));
// special handling for the first iron drill or coal
if ((mine == mine_t::IRON || mine == mine_t::COAL) && facility.level == 1)
{
if (mine == mine_t::IRON)
{
// the first iron drill gets 8 + 4 wood
const EntityPrototype* miner = &game->get_entity_prototype("burner-mining-drill");
const EntityPrototype* furnace = &game->get_entity_prototype("stone-furnace");
const int n_wood = 4;
for (const auto& ent : facility.entities) if (ent.level < facility.level)
{
if (ent.proto == miner)
task->goals->push_back(make_unique<goal::InventoryPredicate>(ent, Inventory{{&game->get_item_prototype("wood"), n_wood*2}}, INV_FUEL));
else if (ent.proto == furnace)
task->goals->push_back(make_unique<goal::InventoryPredicate>(ent, Inventory{{&game->get_item_prototype("wood"), n_wood}}, INV_FUEL));
}
}
else if (mine == mine_t::COAL)
{
// the first coal drill gets 1 wood
task->goals->push_back(make_unique<goal::InventoryPredicate>(facility.entities.front(), Inventory{{&game->get_item_prototype("wood"), 1}}, INV_FUEL));
}
}
if (mine == mine_t::COAL && facility.level > 1)
{
// all subsequent coal drills get 1 coal
task->goals->push_back(make_shared<goal::InventoryPredicate>(
static_cast<goal::PlaceEntity*>(task->goals->back().get())->entity,
Inventory{{&game->get_item_prototype("coal"), 1}}, INV_FUEL
));
}
task->update_actions_from_goals(game, player_idx); // HACK
// TODO: remove this if/else; instead, the scheduler should decide what to craft
// and what to use from the inventory.
if (mine == mine_t::IRON && facility.level == 1) // special handling for the first iron drill
task->auto_craft_from({&game->get_item_prototype("burner-mining-drill"), &game->get_item_prototype("stone-furnace"), &game->get_item_prototype("wood")}, game);
else
task->auto_craft_from({&game->get_item_prototype("iron-plate"), &game->get_item_prototype("stone"), &game->get_item_prototype("wood"), &game->get_item_prototype("coal")}, game);
task->actions_changed();
return {task};
}
tasklist_t do_coal_refill()
{
Logger log("do_coal_refill");
Logger log2("detail");
const ItemPrototype* coal = &game->get_item_prototype("coal");
int n_coal = 0;
for (const auto& ent : game->actual_entities.within_range(Area(-200,-200,200,200)))
if (const ContainerData* data = ent.data_or_null<ContainerData>())
{
log2 << "considering " << ent.str() << " with fuel_is_output = " << data->fuel_is_output << endl;
for (const auto& [key,val] : data->inventories)
if (key.item == coal && (inventory_flags[key.inv].take || (key.inv == INV_FUEL && data->fuel_is_output)))
n_coal += val;
}
n_coal += game->players[player_idx].inventory[coal].unclaimed();
int n_consumers = /*coal: facilities[0].actual_level*0 + */ facilities[1].actual_level*3 + facilities[2].actual_level*3 + facilities[3].actual_level*2;
int coal_per_furnace = (n_consumers>0) ? n_coal / n_consumers : 0;
log << "found " << n_coal << " coal ready for use (" << game->players[player_idx].inventory[coal].unclaimed() << " in our inventory). we have " << n_consumers << " furnace-equivalent consumers, which get " << coal_per_furnace << " coal each." << endl;
const EntityPrototype* miner = &game->get_entity_prototype("burner-mining-drill");
const EntityPrototype* furnace = &game->get_entity_prototype("stone-furnace");
auto task = make_shared<Task>("coal refill");
task->priority_ = -10;
task->goals.emplace();
for (size_t i=1; i<4; i++) // facility[0] is coal which does not need to be refilled
{
const facility_t& facility = facilities[i];
for (const PlannedEntity& ent : facility.entities) if (ent.level < facility.actual_level)
{
if (ent.proto == miner)
task->goals->push_back(make_unique<goal::InventoryPredicate>(ent, Inventory{{coal, coal_per_furnace*2}}, INV_FUEL));
else if (ent.proto == furnace)
task->goals->push_back(make_unique<goal::InventoryPredicate>(ent, Inventory{{coal, coal_per_furnace}}, INV_FUEL));
}
}
task->update_actions_from_goals(game, player_idx); // HACK
if (!task->actions->subactions.empty())
task->actions_changed();
return {task};
}
bool check_coal_refill_needed()
{
Logger log("check_coal");
bool need_refill = false;
const EntityPrototype* miner = &game->get_entity_prototype("burner-mining-drill");
const EntityPrototype* furnace = &game->get_entity_prototype("stone-furnace");
const ItemPrototype* coal = &game->get_item_prototype("coal");
for (size_t i=1; i<4; i++) // facility[0] is coal which does not need to be refilled
{
const facility_t& facility = facilities[i];
for (const PlannedEntity& ent : facility.entities) if (ent.level < facility.actual_level)
{
if (ent.proto == miner || ent.proto == furnace)
{
if (Entity* actual_ent = game->actual_entities.search_or_null(ent))
{
int n_coal = actual_ent->data<ContainerData>().inventories.get_or(coal, INV_FUEL, 0);
{ Logger log2("verbose"); log2 << "entity " << ent.str() << " has " << n_coal << " left" << endl; }
if (n_coal <= 2)
{
log << "entity " << ent.str() << " needs a coal refill." << endl;
need_refill = true;
}
}
else
{
log << "wtf, entity " << ent.str() << " could not be found on the map" << endl;
}
}
}
}
if (need_refill)
log << "coal refill is needed" << endl;
else
log << "no coal refill is needed" << endl;
return need_refill;
}
};
// TODO: update_actions_from_goals, actions_changed, and update crafting list
EarlyStrategy early_strategy(&factorio, &gui, player_idx);
splayers[player_idx].scheduler.add_tasks(early_strategy.initial_tasks());
while (true)
{
bool consistent_state = factorio.parse_packet( factorio.read_packet() );
frame++;
//cout << "frame " << frame << endl; if (frame>1000) break; // useful for profiling with gprof / -pg option, since we must cleanly exit then (not by ^C)
GUI::wait(0.001);
if (!consistent_state)
continue;
for (auto& player : factorio.players)
{
auto& splayer = splayers[player.id];
splayer.tick_scheduler(&factorio, player);
}
if (int key = gui.key())
{
using m = EarlyStrategy::mine_t;
Logger log("menu");
switch(key)
{
case '1': splayers[player_idx].scheduler.add_tasks(early_strategy.add_mine(m::COAL)); break;
case '2': splayers[player_idx].scheduler.add_tasks(early_strategy.add_mine(m::IRON)); break;
case '3': splayers[player_idx].scheduler.add_tasks(early_strategy.add_mine(m::COPPER)); break;
case '4': splayers[player_idx].scheduler.add_tasks(early_strategy.add_mine(m::STONE)); break;
case '5': splayers[player_idx].scheduler.add_tasks(early_strategy.do_coal_refill()); break;
case 'c': early_strategy.check_coal_refill_needed(); break;
case 'a':
log << "scheduler.update_item_allocation()" << endl;
splayers[player_idx].scheduler.update_item_allocation();
break;
case 'd':
factorio.players[player_idx].inventory.dump();
break;
case 'r':
log << "scheduler.recalculate()" << endl;
splayers[player_idx].scheduler.recalculate();
break;
case 's':
{
splayers[player_idx].scheduler.dump();
if (auto task = splayers[player_idx].scheduler.get_current_task())
{
gui.clear();
debug_draw_actions(task->actions.get(), &gui, factorio.players[player_idx].position);
}
break;
}
}
}
}
}