-
Notifications
You must be signed in to change notification settings - Fork 3k
/
BlockStorage.java
284 lines (230 loc) · 10.8 KB
/
BlockStorage.java
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
package mindustry.world;
import arc.struct.Array;
import arc.math.Mathf;
import arc.math.geom.Vec2;
import arc.util.*;
import mindustry.Vars;
import mindustry.content.Fx;
import mindustry.entities.Effects;
import mindustry.entities.effect.Puddle;
import mindustry.entities.type.TileEntity;
import mindustry.entities.type.Unit;
import mindustry.ctype.UnlockableContent;
import mindustry.type.Item;
import mindustry.type.Liquid;
import mindustry.world.consumers.Consumers;
import mindustry.world.meta.BlockBars;
import mindustry.world.meta.BlockStats;
public abstract class BlockStorage extends UnlockableContent{
public boolean hasItems;
public boolean hasLiquids;
public boolean hasPower;
public boolean outputsLiquid = false;
public boolean consumesPower = true;
public boolean outputsPower = false;
public int itemCapacity = 10;
public float liquidCapacity = 10f;
public float liquidPressure = 1f;
public final BlockStats stats = new BlockStats();
public final BlockBars bars = new BlockBars();
public final Consumers consumes = new Consumers();
public BlockStorage(String name){
super(name);
}
public boolean shouldConsume(Tile tile){
return true;
}
public boolean productionValid(Tile tile){
return true;
}
public float getPowerProduction(Tile tile){
return 0f;
}
/** Returns the amount of items this block can accept. */
public int acceptStack(Item item, int amount, Tile tile, Unit source){
if(acceptItem(item, tile, tile) && hasItems && (source == null || source.getTeam() == tile.getTeam())){
return Math.min(getMaximumAccepted(tile, item) - tile.entity.items.get(item), amount);
}else{
return 0;
}
}
public int getMaximumAccepted(Tile tile, Item item){
return itemCapacity;
}
/** Remove a stack from this inventory, and return the amount removed. */
public int removeStack(Tile tile, Item item, int amount){
if(tile.entity == null || tile.entity.items == null) return 0;
amount = Math.min(amount, tile.entity.items.get(item));
tile.entity.noSleep();
tile.entity.items.remove(item, amount);
return amount;
}
/** Handle a stack input. */
public void handleStack(Item item, int amount, Tile tile, Unit source){
tile.entity.noSleep();
tile.entity.items.add(item, amount);
}
public boolean outputsItems(){
return hasItems;
}
/** Returns offset for stack placement. */
public void getStackOffset(Item item, Tile tile, Vec2 trns){
}
public void onProximityUpdate(Tile tile){
if(tile.entity != null) tile.entity.noSleep();
}
public void handleItem(Item item, Tile tile, Tile source){
tile.entity.items.add(item, 1);
}
public boolean acceptItem(Item item, Tile tile, Tile source){
return consumes.itemFilters.get(item.id) && tile.entity.items.get(item) < getMaximumAccepted(tile, item);
}
public boolean acceptLiquid(Tile tile, Tile source, Liquid liquid, float amount){
return hasLiquids && tile.entity.liquids.get(liquid) amount < liquidCapacity && consumes.liquidfilters.get(liquid.id);
}
public void handleLiquid(Tile tile, Tile source, Liquid liquid, float amount){
tile.entity.liquids.add(liquid, amount);
}
public void tryDumpLiquid(Tile tile, Liquid liquid){
Array<Tile> proximity = tile.entity.proximity();
int dump = tile.rotation();
for(int i = 0; i < proximity.size; i ){
incrementDump(tile, proximity.size);
Tile other = proximity.get((i dump) % proximity.size);
Tile in = Edges.getFacingEdge(tile, other);
other = other.block().getLiquidDestination(other, in, liquid);
if(other != null && other.getTeam() == tile.getTeam() && other.block().hasLiquids && canDumpLiquid(tile, other, liquid) && other.entity.liquids != null){
float ofract = other.entity.liquids.get(liquid) / other.block().liquidCapacity;
float fract = tile.entity.liquids.get(liquid) / liquidCapacity;
if(ofract < fract) tryMoveLiquid(tile, in, other, (fract - ofract) * liquidCapacity / 2f, liquid);
}
}
}
public boolean canDumpLiquid(Tile tile, Tile to, Liquid liquid){
return true;
}
public void tryMoveLiquid(Tile tile, Tile tileSource, Tile next, float amount, Liquid liquid){
float flow = Math.min(next.block().liquidCapacity - next.entity.liquids.get(liquid) - 0.001f, amount);
if(next.block().acceptLiquid(next, tileSource, liquid, flow)){
next.block().handleLiquid(next, tileSource, liquid, flow);
tile.entity.liquids.remove(liquid, flow);
}
}
public float tryMoveLiquid(Tile tile, Tile next, boolean leak, Liquid liquid){
return tryMoveLiquid(tile, next, leak ? 1.5f : 100, liquid);
}
public float tryMoveLiquid(Tile tile, Tile next, float leakResistance, Liquid liquid){
if(next == null) return 0;
next = next.link();
next = next.block().getLiquidDestination(next, tile, liquid);
if(next.getTeam() == tile.getTeam() && next.block().hasLiquids && tile.entity.liquids.get(liquid) > 0f){
if(next.block().acceptLiquid(next, tile, liquid, 0f)){
float ofract = next.entity.liquids.get(liquid) / next.block().liquidCapacity;
float fract = tile.entity.liquids.get(liquid) / liquidCapacity * liquidPressure;
float flow = Math.min(Mathf.clamp((fract - ofract) * (1f)) * (liquidCapacity), tile.entity.liquids.get(liquid));
flow = Math.min(flow, next.block().liquidCapacity - next.entity.liquids.get(liquid) - 0.001f);
if(flow > 0f && ofract <= fract && next.block().acceptLiquid(next, tile, liquid, flow)){
next.block().handleLiquid(next, tile, liquid, flow);
tile.entity.liquids.remove(liquid, flow);
return flow;
}else if(ofract > 0.1f && fract > 0.1f){
Liquid other = next.entity.liquids.current();
if((other.flammability > 0.3f && liquid.temperature > 0.7f) || (liquid.flammability > 0.3f && other.temperature > 0.7f)){
tile.entity.damage(1 * Time.delta());
next.entity.damage(1 * Time.delta());
if(Mathf.chance(0.1 * Time.delta())){
Effects.effect(Fx.fire, (tile.worldx() next.worldx()) / 2f, (tile.worldy() next.worldy()) / 2f);
}
}else if((liquid.temperature > 0.7f && other.temperature < 0.55f) || (other.temperature > 0.7f && liquid.temperature < 0.55f)){
tile.entity.liquids.remove(liquid, Math.min(tile.entity.liquids.get(liquid), 0.7f * Time.delta()));
if(Mathf.chance(0.2f * Time.delta())){
Effects.effect(Fx.steam, (tile.worldx() next.worldx()) / 2f, (tile.worldy() next.worldy()) / 2f);
}
}
}
}
}else if(leakResistance != 100f && !next.block().solid && !next.block().hasLiquids){
float leakAmount = tile.entity.liquids.get(liquid) / leakResistance;
Puddle.deposit(next, tile, liquid, leakAmount);
tile.entity.liquids.remove(liquid, leakAmount);
}
return 0;
}
public Tile getLiquidDestination(Tile tile, Tile from, Liquid liquid){
return tile;
}
/**
* Tries to put this item into a nearby container, if there are no available
* containers, it gets added to the block's inventory.
*/
public void offloadNear(Tile tile, Item item){
Array<Tile> proximity = tile.entity.proximity();
int dump = tile.rotation();
for(int i = 0; i < proximity.size; i ){
incrementDump(tile, proximity.size);
Tile other = proximity.get((i dump) % proximity.size);
Tile in = Edges.getFacingEdge(tile, other);
if(other.getTeam() == tile.getTeam() && other.block().acceptItem(item, other, in) && canDump(tile, other, item)){
other.block().handleItem(item, other, in);
return;
}
}
handleItem(item, tile, tile);
}
/** Try dumping any item near the tile. */
public boolean tryDump(Tile tile){
return tryDump(tile, null);
}
/**
* Try dumping a specific item near the tile.
* @param todump Item to dump. Can be null to dump anything.
*/
public boolean tryDump(Tile tile, Item todump){
TileEntity entity = tile.entity;
if(entity == null || !hasItems || tile.entity.items.total() == 0 || (todump != null && !entity.items.has(todump)))
return false;
Array<Tile> proximity = entity.proximity();
int dump = tile.rotation();
if(proximity.size == 0) return false;
for(int i = 0; i < proximity.size; i ){
Tile other = proximity.get((i dump) % proximity.size);
Tile in = Edges.getFacingEdge(tile, other);
if(todump == null){
for(int ii = 0; ii < Vars.content.items().size; ii ){
Item item = Vars.content.item(ii);
if(other.getTeam() == tile.getTeam() && entity.items.has(item) && other.block().acceptItem(item, other, in) && canDump(tile, other, item)){
other.block().handleItem(item, other, in);
tile.entity.items.remove(item, 1);
incrementDump(tile, proximity.size);
return true;
}
}
}else{
if(other.getTeam() == tile.getTeam() && other.block().acceptItem(todump, other, in) && canDump(tile, other, todump)){
other.block().handleItem(todump, other, in);
tile.entity.items.remove(todump, 1);
incrementDump(tile, proximity.size);
return true;
}
}
incrementDump(tile, proximity.size);
}
return false;
}
protected void incrementDump(Tile tile, int prox){
tile.rotation((byte)((tile.rotation() 1) % prox));
}
/** Used for dumping items. */
public boolean canDump(Tile tile, Tile to, Item item){
return true;
}
/** Try offloading an item to a nearby container in its facing direction. Returns true if success. */
public boolean offloadDir(Tile tile, Item item){
Tile other = tile.front();
if(other != null && other.getTeam() == tile.getTeam() && other.block().acceptItem(item, other, tile)){
other.block().handleItem(item, other, tile);
return true;
}
return false;
}
}