-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathTileEntityCannon.java
More file actions
353 lines (292 loc) · 10.9 KB
/
TileEntityCannon.java
File metadata and controls
353 lines (292 loc) · 10.9 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
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
package openblocks.common.tileentity;
import javax.annotation.Nonnull;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.items.IItemHandler;
import openblocks.OpenBlocks;
import openblocks.api.IPointable;
import openblocks.common.entity.EntityItemProjectile;
import openblocks.rpc.ITriggerable;
import openmods.Log;
import openmods.api.ISurfaceAttachment;
import openmods.fixers.GenericInventoryTeFixerWalker;
import openmods.fixers.RegisterFixer;
import openmods.sync.SyncableDouble;
import openmods.tileentity.SyncedTileEntity;
import openmods.utils.InventoryUtils;
import openmods.utils.render.GeometryUtils;
@RegisterFixer(GenericInventoryTeFixerWalker.class)
public class TileEntityCannon extends SyncedTileEntity implements IPointable, ISurfaceAttachment, ITriggerable, ITickable {
/*
* Blocks and Entities have a right-angle offset
*/
private static final int YAW_OFFSET_DEGREES = -90;
private static final int KNOB_YAW_CHANGE_SPEED = 3;
private static final int KNOB_PITCH_CHANGE_SPEED = 20;
private static final int KNOB_VEL_CHANGE_SPEED = 20;
private static final int KNOB_LOB_MINIMUM_VALUE = 20;
private static final int KNOB_LOB_MAXIMUM_VALUE = 75;
private static final int KNOB_LOB_VERTICAL_MUL = 4;
private static final int KNOB_LOB_HORIZONTAL_MUL = 1;
private static final int KNOB_LOB_BONUS = 5;
public SyncableDouble targetPitch;
public SyncableDouble targetYaw;
public SyncableDouble targetSpeed;
public double currentPitch = 45;
public double currentYaw = 0;
private double currentSpeed = 1.4;
public Vec3d motion;
public boolean renderLine = true;
private int ticksSinceLastFire = Integer.MAX_VALUE;
private Vec3d projectileOrigin = null;
@Override
protected void createSyncedFields() {
targetPitch = new SyncableDouble();
targetYaw = new SyncableDouble();
targetSpeed = new SyncableDouble(1.4);
}
@Override
public void update() {
checkOrigin();
if (Double.isNaN(currentPitch)) {
Log.warn("Pitch was NaN");
currentPitch = 45;
targetPitch.set(currentPitch);
}
if (Double.isNaN(currentYaw)) {
Log.warn("Yaw was NaN");
currentYaw = 0;
}
// ugly, need to clean
currentPitch = currentPitch - ((currentPitch - targetPitch.get()) / KNOB_PITCH_CHANGE_SPEED);
currentYaw = GeometryUtils.normalizeAngle(currentYaw);
final double targetYaw = GeometryUtils.normalizeAngle(this.targetYaw.get());
if (Math.abs(currentYaw - targetYaw) < KNOB_YAW_CHANGE_SPEED) currentYaw = targetYaw;
else {
double dist = GeometryUtils.getAngleDistance(currentYaw, targetYaw);
currentYaw += KNOB_YAW_CHANGE_SPEED * Math.signum(dist);
}
currentSpeed = currentSpeed - ((currentSpeed - targetSpeed.get()) / KNOB_VEL_CHANGE_SPEED);
invalidateMotion();
if (!world.isRemote) {
if (world.getTotalWorldTime() % 20 == 0) {
if (world.isBlockIndirectlyGettingPowered(pos) > 0) {
ItemStack stack = findStack();
if (!stack.isEmpty()) fireStack(stack);
}
}
} else {
if (ticksSinceLastFire < 100) {
ticksSinceLastFire++;
}
}
}
@Nonnull
private ItemStack findStack() {
for (EnumFacing direction : EnumFacing.VALUES) {
final IItemHandler inventory = InventoryUtils.tryGetHandler(world, pos.offset(direction), direction.getOpposite());
if (inventory != null) {
for (int i = 0; i < inventory.getSlots(); i++) {
final ItemStack stack = inventory.extractItem(i, 64, false);
if (!stack.isEmpty()) return stack;
}
}
}
return ItemStack.EMPTY;
}
private void fireStack(@Nonnull ItemStack stack) {
final ITriggerable rpc = createServerRpcProxy(ITriggerable.class);
rpc.trigger();
// spawn the item approximately at the end of the barrel
double yawr = Math.toRadians(currentYaw);
double pitchr = Math.toRadians(currentPitch);
double barrelLength = 0.5;
double barrelBaseHeight = 0.3;
double x = pos.getX() + 0.5 - Math.sin(yawr) * Math.cos(pitchr) * barrelLength;
double y = pos.getY() + barrelBaseHeight + Math.sin(pitchr) * barrelLength;
double z = pos.getZ() + 0.5 + Math.cos(yawr) * Math.cos(pitchr) * barrelLength;
// projectileOrigin is not used here, it's used for the calculations below.
EntityItem item = new EntityItemProjectile(world, x, y, z, stack);
item.setDefaultPickupDelay();
// Now that we generate vectors instead of eular angles, this should be revised.
Vec3d motion = getMotion();
item.motionX = motion.x;
item.motionY = motion.y;
item.motionZ = motion.z;
world.spawnEntity(item);
playSoundAtBlock(OpenBlocks.Sounds.BLOCK_CANNON_ACTIVATE, 0.2f, 1.0f);
}
@Override
public void trigger() {
ticksSinceLastFire = 0;
double pitchRad = Math.toRadians(currentYaw - 90);
double x = -0.5 * Math.cos(pitchRad);
double z = -0.5 * Math.sin(pitchRad);
for (int i = 0; i < 20; i++) {
spawnParticle((i < 4? EnumParticleTypes.SMOKE_LARGE : EnumParticleTypes.SMOKE_NORMAL),
x + 0.3 + (world.rand.nextDouble() * 0.4),
0.7,
z + 0.3 + (world.rand.nextDouble() * 0.4),
0.0D, 0.0D, 0.0D);
}
}
public int getTicksSinceLastFire() {
return ticksSinceLastFire;
}
@Override
@SideOnly(Side.CLIENT)
public AxisAlignedBB getRenderBoundingBox() {
return super.getRenderBoundingBox().grow(32);
}
private Vec3d calcMotionFromAngles() {
double p = Math.toRadians(currentPitch);
double y = Math.toRadians(180 - currentYaw);
double sinPitch = Math.sin(p);
double cosPitch = Math.cos(p);
double sinYaw = Math.sin(y);
double cosYaw = Math.cos(y);
return new Vec3d(-cosPitch * sinYaw * currentSpeed,
sinPitch * currentSpeed,
-cosPitch * cosYaw * currentSpeed);
}
private void invalidateMotion() {
motion = null;
}
public Vec3d getMotion() {
if (motion == null) motion = calcMotionFromAngles();
return motion;
}
private void checkOrigin() {
if (projectileOrigin == null) {
projectileOrigin = new Vec3d(pos).addVector(0.5, 0, 0.5);
}
}
public void setTarget(BlockPos pos) {
checkOrigin();
// We target the middle of the block, at the very top.
final Vec3d target = new Vec3d(pos).addVector(0.5, 1, 0.5);
// Horizontal distance between the origin and target
final double distHorizontal = KNOB_LOB_HORIZONTAL_MUL * Math.sqrt(
Math.pow(target.x - projectileOrigin.x, 2)
+ Math.pow(target.z - projectileOrigin.z, 2));
// No vertical multiplier is applied for decline slopes.
final double distVertical = Math.max((target.y - projectileOrigin.y) * KNOB_LOB_VERTICAL_MUL, 0);
// Calculate the arc of the trajectory
final float lobScale = (float)Math.min(KNOB_LOB_MAXIMUM_VALUE,
Math.max(KNOB_LOB_MINIMUM_VALUE,
KNOB_LOB_BONUS + distHorizontal + distVertical));
// Calculate the velocity of the projectile
final Vec3d velocity = TileEntityCannonLogic.calculateTrajectory(projectileOrigin, target, lobScale);
// m/s applied to item.
final double speed = velocity.lengthVector();
targetSpeed.set(speed);
// reverse the vector to angles for cannon model
final Vec3d direction = velocity.normalize();
final double pitch = Math.asin(direction.y);
final double yaw = Math.atan2(direction.z, direction.x);
// Set yaw and pitch
targetYaw.set(Math.toDegrees(yaw) + YAW_OFFSET_DEGREES);
targetPitch.set(Math.toDegrees(pitch));
currentYaw = targetYaw.get();
currentPitch = targetPitch.get();
trySync();
}
public void disableLineRender() {
renderLine = false;
}
@Override
public void onPointingStart(ItemStack itemStack, EntityPlayer player) {
player.sendMessage(new TextComponentTranslation("openblocks.misc.selected_cannon"));
}
@Override
public void onPointingEnd(ItemStack itemStack, EntityPlayer player, BlockPos pos) {
player.sendMessage(new TextComponentTranslation("openblocks.misc.pointed_cannon", pos.getX(), pos.getY(), pos.getZ()));
setTarget(pos);
}
public void setSpeed(double speed) {
targetSpeed.set(speed);
sync();
}
public void setPitch(double pitch2) {
targetPitch.set(pitch2);
sync();
}
public void setYaw(double yaw2) {
targetYaw.set(yaw2);
sync();
}
static class TileEntityCannonLogic {
/*
* Hello, If you think you can improve the code below to work better,
* all power to you! But please, if you give up and revert your changes.
* Increment the counter below as an increasing warning to the next
* sorry soul that thinks they can make this work better.
* Regards -NC
*/
public static final int HOURS_WASTED_ON_CANNON_LOGIC = 14;
/**
* 20 physics ticks per second (on a good day)
*/
private static final double PHYS_STEPS_PER_SECOND = 20D;
/**
* Physics calculation time in partial seconds
*/
private static final double PHYS_PARTIAL_TIME = 1D / PHYS_STEPS_PER_SECOND;
/**
* Minecraft known gravity in meters/second/second
*/
private static final double PHYS_WORLD_GRAVITY = 0.8D;
/**
* Amount of gravity acceleration per physics tick.
*/
private static final double PHYS_PARTIAL_WORLD_GRAVITY = PHYS_WORLD_GRAVITY * PHYS_PARTIAL_TIME;
/**
* Squared timestep for acceleration
*/
private static final double PHYS_PARTIAL_TIME_SQUARE = PHYS_PARTIAL_TIME * PHYS_PARTIAL_TIME;
/**
* Physics gravity vector in partial time squared for acceleration calculation
*/
private static final Vec3d PHYS_GRAVITY_VECTOR_SQUARE_PARTIAL = new Vec3d(0, PHYS_PARTIAL_TIME_SQUARE * -PHYS_PARTIAL_WORLD_GRAVITY, 0);
/**
* The actual work for calculating trajectory. Which is much simpler now.
*
* @param start
* The origin of the projectile to be fired
* @param target
* The target location of the projectile
* @param scale
* The arcing size of the trajectory
* @return Vector to achieve trajectory
*/
public static Vec3d calculateTrajectory(Vec3d start, Vec3d target, float scale) {
final double n = scale * PHYS_STEPS_PER_SECOND;
final double accelerationMultiplier = 0.5 * n * n + n; // (n^2+n)/2
final Vec3d scaledAcceleration = new Vec3d(
PHYS_GRAVITY_VECTOR_SQUARE_PARTIAL.x * accelerationMultiplier,
PHYS_GRAVITY_VECTOR_SQUARE_PARTIAL.y * accelerationMultiplier,
PHYS_GRAVITY_VECTOR_SQUARE_PARTIAL.z * accelerationMultiplier);
// -1 /n * Phys = -Phys / n
final double velocityMultiplier = -PHYS_STEPS_PER_SECOND / n;
final Vec3d velocity = new Vec3d(
(start.x + scaledAcceleration.x - target.x) * velocityMultiplier,
(start.y + scaledAcceleration.y - target.y) * velocityMultiplier,
(start.z + scaledAcceleration.z - target.z) * velocityMultiplier);
return velocity;
}
}
@Override
public EnumFacing getSurfaceDirection() {
return EnumFacing.DOWN;
}
}