-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResource.java
More file actions
67 lines (52 loc) · 1.66 KB
/
Resource.java
File metadata and controls
67 lines (52 loc) · 1.66 KB
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
public class Resource {
GoodsMarket market; // Market this item is demanded on
double demand; // total demand for this demandable over last period
double demandTime[] = new double[25]; // demand in time array
int demandTimePeriod; // period over which resource is demanded (25 = quarter default)
int demandedLastTick; // total demanded last tick
long difficulty; // cost per hour to produce
long amount; // total amount of this demandable available
double price = 10; // price of this demandable, based on demand / amount
// Constructors
public Resource() {}
public Resource(GoodsMarket mkt, int timePeriod, long diff) {
market = mkt;
demandTimePeriod = timePeriod;
difficulty = diff;
}
public void tick() {
// MacSim.log(5, "Amount of Oil: " + amount);
advanceDemand();
calculatePrice();
}
void advanceDemand() {
for(int i = demandTimePeriod - 1; i > 0; i--) {
demandTime[i] = demandTime[i - 1];
}
demandTime[1] = demandedLastTick;
}
void calculatePrice() {
price = difficulty * demand / amount;
}
public void demand(int amount) {
demand += amount;
}
public long price(long amount) {
return amount * (long)price;
}
public boolean produce(long amt) {
amount += amt;
calculatePrice();
return true;
}
public int trade(int amt) {
if(amt <= amount) {
amount -= amt;
double tempPrice = price;
calculatePrice();
return (int)tempPrice * amt;
} else {
return 0;
}
}
}