-
Notifications
You must be signed in to change notification settings - Fork 4
/
recipe.h
80 lines (67 loc) · 2.17 KB
/
recipe.h
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
/*
* 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/>.
*/
#pragma once
#include <string>
#include <vector>
#include <chrono>
#include "item.h"
#include "inventory.hpp"
struct Recipe
{
struct ItemAmount
{
const ItemPrototype* item;
double amount; // this is mostly int, but some recipes have probabilities.
// in this case, the average value is stored here.
ItemAmount(const ItemPrototype* item_, double amount_) : item(item_),amount(amount_) {}
};
std::string name;
bool enabled;
double energy;
std::chrono::milliseconds crafting_duration() const {
return std::chrono::milliseconds(int(energy*1000.));
}
std::vector<ItemAmount> ingredients;
std::vector<ItemAmount> products;
Inventory get_itemamounts(const std::vector<ItemAmount>& amounts) const
{
Inventory result;
for (const auto& [item, amount] : amounts)
{
if (amount < 0. || double(size_t(amount)) != amount)
throw std::runtime_error("item amounts must be integers");
result[item] += size_t(amount);
}
return result;
}
Inventory get_ingredients() const { return get_itemamounts(ingredients); }
Inventory get_products() const { return get_itemamounts(products); }
/** returns the balance for the specified item. positive values means
* that the item is produced, negative means consumed. */
int balance_for(const ItemPrototype* item) const
{
int balance = 0;
for (auto [product,amount] : products)
if (product == item)
balance += amount;
for (auto [ingredient,amount] : ingredients)
if (ingredient == item)
balance -= amount;
return balance;
}
};