Skip to content

Commit

Permalink
Fixed AI under timeWarp by making multiple calls to inGame.performUpd…
Browse files Browse the repository at this point in the history
…ate rather than one with an increased deltaTime
  • Loading branch information
bjasspa committed Jan 30, 2016
1 parent 8446dd6 commit abe3693
Show file tree
Hide file tree
Showing 11 changed files with 457 additions and 378 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package de.phbouillon.android.framework;

public interface TimeFactorChangeListener {
void timeFactorChanged(float oldTimeFactor, float newTimeFactor);
void timeFactorChanged(int oldTimeFactor, int newTimeFactor);
}
94 changes: 47 additions & 47 deletions src/de/phbouillon/android/framework/impl/AndroidGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/* Alite - Discover the Universe on your Favorite Android Device
* Copyright (C) 2015 Philipp Bouillon
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License, or
Expand Down Expand Up @@ -57,9 +57,9 @@ enum GLGameState {
Finished,
Idle
}

public static boolean resetting = false;

private GLSurfaceView glView;
private Graphics graphics;
private Audio audio;
Expand All @@ -74,7 +74,7 @@ enum GLGameState {
private long startTime = System.nanoTime();
private long lastTime = startTime;
private int frames = 0;
private float timeFactor = 1.0f;
private int timeFactor = 1;
public static float fps;
public static float scaleFactor;
protected final TextureManager textureManager;
Expand All @@ -87,25 +87,25 @@ public AndroidGame(int targetWidth, int targetHeight) {
this.targetWidth = targetWidth;
textureManager = new TextureManager(this);
}

public void setTimeFactorChangeListener(TimeFactorChangeListener tfl) {
timeFactorChangeListener = tfl;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

deviceWidth = metrics.widthPixels;
deviceHeight = metrics.heightPixels;
deviceHeight = metrics.heightPixels;
glView = new GLSurfaceView(this);
glView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
glView.setRenderer(AndroidGame.this);
glView.setRenderer(AndroidGame.this);
if (fileIO == null) {
fileIO = new AndroidFileIO(this);
}
Expand All @@ -120,37 +120,37 @@ public void onCreate(Bundle savedInstanceState) {
input = new AndroidInput(this, glView, (float) frameBufferWidth / (float) aspect.width(), (float) frameBufferHeight / (float) aspect.height(), aspect.left, aspect.top);
AliteLog.d("Calculating Sizes", "Sizes in OC: Width: " + deviceWidth + ", Height: " + deviceHeight + ", Aspect: " + aspect.left + ", " + aspect.top + ", " + aspect.right + ", " + aspect.bottom);
}

@Override
public void onDestroy() {
super.onDestroy();
}

public int [] getSize() {
if (sizeArray == null) {
sizeArray = new int [] {deviceWidth, deviceHeight};
}
return sizeArray;
}

@Override
public void onResume() {
super.onResume();
if (glView != null) {
if (glView != null) {
glView.onResume();
}
if (screen != null) {
screen.resume();
}
}
state = GLGameState.Running;
}

@Override
public void onPause() {
super.onPause();
super.onPause();
glView.onPause();
SoundManager.stopAll();
if (screen != null) {
if (screen != null) {
if (fatalException == null) {
if (resetting) {
resetting = false;
Expand All @@ -161,21 +161,21 @@ public void onPause() {
screen.pause();
}

fatalException = null;
fatalException = null;
if (isFinishing()) {
state = GLGameState.Finished;
} else {
state = GLGameState.Paused;
}
}
if (isFinishing() && screen != null) {
screen.dispose();
screen = null;
}

}

protected abstract void saveState();

@Override
public Input getInput() {
return input;
Expand All @@ -197,16 +197,16 @@ public Audio getAudio() {
}

@Override
public synchronized void setScreen(Screen screen) {
public synchronized void setScreen(Screen screen) {
if (screen == null) {
throw new IllegalArgumentException("Screen must not be null");
}

this.screen.pause();
textureManager.clear();

this.screen = null;
screen.loadAssets();
screen.loadAssets();
this.screen = screen;
screen.activate();
screen.resume();
Expand All @@ -218,13 +218,13 @@ public synchronized void setScreen(Screen screen) {
public Screen getCurrentScreen() {
return screen;
}

public View getCurrentView() {
return glView;
}

public Rect calculateTargetRect(Rect rect) {
int width = rect.right - rect.left; // "right" and "left" aren't named correctly here; right - left yields the width!
int width = rect.right - rect.left; // "right" and "left" aren't named correctly here; right - left yields the width!
int height = rect.bottom - rect.top; // No adding of 1 required -- same is true for height...
float xFactor = (float) targetWidth / (float) width;
float yFactor = (float) targetHeight / (float) height;
Expand All @@ -233,7 +233,7 @@ public Rect calculateTargetRect(Rect rect) {
AliteLog.d("[ALITE-CTR]", "Width: " + width + ", Height: " + height);
AliteLog.d("[ALITE-CTR]", "TargetWidth: " + targetWidth + ", targetHeight: " + targetHeight);
AliteLog.d("[ALITE-CTR]", "Factors: " + xFactor + ", " + yFactor);

int x1, y1, x2, y2;
if (xFactor > yFactor) {
x1 = rect.left;
Expand Down Expand Up @@ -261,17 +261,17 @@ public void onSurfaceCreated(GL10 unused, EGLConfig config) {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
deviceWidth = metrics.widthPixels;
deviceHeight = metrics.heightPixels;
deviceHeight = metrics.heightPixels;
Rect aspect = calculateTargetRect(new Rect(0, 0, deviceWidth, deviceHeight));
float ratio = (float) aspect.width() / (float) aspect.height();
GlUtils.setViewport(aspect);
GlUtils.gluPerspective(this, 45.0f, ratio, 1.0f, 900000.0f);

AliteLog.d("Recalculating Sizes", "Sizes in OSC: Width: " + deviceWidth + ", Height: " + deviceHeight + ", Aspect: " + aspect.left + ", " + aspect.top + ", " + aspect.right + ", " + aspect.bottom);

GLES11.glEnable(GLES11.GL_TEXTURE_2D);
graphics = new AndroidGraphics(fileIO, scaleFactor, aspect, textureManager);

screen = getStartScreen();
// glView.onResume();
afterSurfaceCreated();
Expand All @@ -282,11 +282,11 @@ public void onSurfaceCreated(GL10 unused, EGLConfig config) {
screen.resume();
startTime = System.nanoTime();
}

@Override
public void onSurfaceChanged(GL10 unused, int width, int height) {
}

private void drawFatalException() {
try {
if (fatalException != null) {
Expand All @@ -299,14 +299,14 @@ private void drawFatalException() {
throw new RuntimeException("Alite has ended with an uncaught exception while trying to process an uncaught exception.", t);
}
}

@Override
public void onDrawFrame(GL10 unused) {
if (fatalException != null) {
drawFatalException();
return;
}
try {
try {
GLGameState state = this.state;
long nanoTime = System.nanoTime();
while ((nanoTime - startTime) < 33333333l) {
Expand Down Expand Up @@ -354,18 +354,18 @@ public void onDrawFrame(GL10 unused) {
public float getScaleFactor() {
return scaleFactor;
}
public void afterSurfaceCreated() {
}
public float getTimeFactor() {

public void afterSurfaceCreated() {
}

public int getTimeFactor() {
return timeFactor;
}
public void setTimeFactor(float tf) {
if (timeFactorChangeListener != null && Math.abs(timeFactor - tf) > 0.0001f) {
timeFactorChangeListener.timeFactorChanged(timeFactor, tf);

public void setTimeFactor(int tf) {
if ((timeFactorChangeListener != null) && (timeFactor != tf)) {
timeFactorChangeListener.timeFactorChanged(timeFactor, tf);
}
timeFactor = tf;
timeFactor = tf;
}
}
2 changes: 1 addition & 1 deletion src/de/phbouillon/android/games/alite/model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public Condition getCondition() {
public void setCondition(Condition newCondition) {
condition = newCondition;
if((condition != Condition.GREEN) && (condition != Condition.YELLOW))
alite.setTimeFactor(1.0f);
alite.setTimeFactor(1);
}

public LegalStatus getLegalStatus() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public class PlayerCobra {
public static final int MAXIMUM_FUEL = 70;
public static final int MAXIMUM_MISSILES = 4;
public static final int DEFAULT_MISSILES = 3;
public static final int SPEED_UP_FACTOR = 10; // speed-up factor to use when torus can't be engaged
public static final float MAX_SPEED = 367.4f; // m/s -- about 1320 km/h; just faster than sonic speed on Earth
public static final float TORUS_SPEED = 33400.0f; // torus drive speed
public static final float TORUS_TEST_SPEED = 10000.0f; // value to use to test if torus is engaged
public static final float SPEED_UP_FACTOR = 10.0f; // speed-up factor to use when torus can't be engaged
public static final float MAX_SHIELD = 24;
public static final float MAX_FUEL = 70;
public static final float MAX_CABIN_TEMPERATURE = 30;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,9 +476,10 @@ public void performUpdate(float deltaTime) {
if (isDisposed || inGame == null) {
return;
}
float dtf = deltaTime * ((Alite) game).getTimeFactor();
inGame.performUpdate(dtf, allObjects);

int tf = ((Alite) game).getTimeFactor();
while((--tf >= 0) && inGame.isPlayerAlive()) {
inGame.performUpdate(deltaTime, allObjects);
}
if (informationScreen != null) {
informationScreen.update(deltaTime);
}
Expand All @@ -496,18 +497,19 @@ public void performUpdate(float deltaTime) {
if (newScreen != null) {
performScreenChange(newScreen);
} else {
deltaTime *= ((Alite) game).getTimeFactor();
if (star != null) {
star.applyDeltaRotation(0.0f, (float) Math.toDegrees(0.02f * dtf), 0.0f);
star.applyDeltaRotation(0.0f, (float) Math.toDegrees(0.02f * deltaTime), 0.0f);
}
if (planet != null) {
planet.applyDeltaRotation(0.0f, (float) Math.toDegrees(0.015f * dtf), 0.0f);
planet.applyDeltaRotation(0.0f, (float) Math.toDegrees(0.015f * deltaTime), 0.0f);
}

if (spaceStation != null) {
if (resetSpaceStation) {
performResetSpaceStation();
}
spaceStation.applyDeltaRotation(0.0f, 0.0f, (float) Math.toDegrees(SPACE_STATION_ROTATION_SPEED * dtf));
spaceStation.applyDeltaRotation(0.0f, 0.0f, (float) Math.toDegrees(SPACE_STATION_ROTATION_SPEED * deltaTime));
}
}
} catch (NullPointerException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void checkShipStationProximity() {
return;
}
float distanceSq = ship.getPosition().distanceSq(station.getPosition());
if (distanceSq <= STATION_PROXIMITY_DISTANCE_SQ * alite.getTimeFactor() * alite.getTimeFactor()) {
if (distanceSq <= STATION_PROXIMITY_DISTANCE_SQ) {
if (ship.getProximity() != station) {
ship.setProximity(station);
AliteLog.e("Proximity", "Setting ship/station proximity");
Expand All @@ -125,7 +125,7 @@ void checkProximity(List <AliteObject> allObjects) {
continue;
}
SpaceObject objectA = (SpaceObject) allObjects.get(i);
float objectAProximityDistance = objectA.getBoundingSphereRadiusSq() * PROXIMITY_WARNING_RADIUS_FACTOR * alite.getTimeFactor();
float objectAProximityDistance = objectA.getBoundingSphereRadiusSq() * PROXIMITY_WARNING_RADIUS_FACTOR;
float distanceCamSq = objectA.getPosition().distanceSq(inGame.getShip().getPosition());
if (distanceCamSq <= objectAProximityDistance) {
objectA.setProximity(inGame.getShip());
Expand All @@ -136,7 +136,7 @@ void checkProximity(List <AliteObject> allObjects) {
}
SpaceObject objectB = (SpaceObject) allObjects.get(j);
float distanceSq = objectA.getPosition().distanceSq(objectB.getPosition());
float objectBProximityDistance = objectB.getBoundingSphereRadiusSq() * PROXIMITY_WARNING_RADIUS_FACTOR * alite.getTimeFactor();
float objectBProximityDistance = objectB.getBoundingSphereRadiusSq() * PROXIMITY_WARNING_RADIUS_FACTOR;
if (distanceSq <= objectAProximityDistance + objectBProximityDistance) {
objectA.setProximity(objectB);
objectB.setProximity(objectA);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ final List <TimedEvent> registerTimedEvents() {

@Override
public void doPerform() {
long nd = ((alite.getCobra().isEquipmentInstalled(EquipmentStore.navalEnergyUnit)) ? NAVAL_REFRESH_RATE:NORMAL_REFRESH_RATE) / ((long) alite.getTimeFactor());
long nd = ((alite.getCobra().isEquipmentInstalled(EquipmentStore.navalEnergyUnit)) ? NAVAL_REFRESH_RATE:NORMAL_REFRESH_RATE) / alite.getTimeFactor();
if (delay != nd) {
updateDelay(nd);
}
Expand Down
Loading

0 comments on commit abe3693

Please sign in to comment.