Merge branch 'fixedratetickloop' of https://github.com/dbrownidau/2006rebotted into dbrownidau-fixedratetickloop

This commit is contained in:
dginovker
2020-02-11 18:24:45 -05:00
@@ -3,6 +3,11 @@ package com.rebotted;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.mina.common.IoAcceptor; import org.apache.mina.common.IoAcceptor;
import org.apache.mina.transport.socket.nio.SocketAcceptor; import org.apache.mina.transport.socket.nio.SocketAcceptor;
@@ -73,6 +78,8 @@ public class GameEngine {
public static FightCaves fightCaves = new FightCaves(); public static FightCaves fightCaves = new FightCaves();
private static PestControl pestControl = new PestControl(); private static PestControl pestControl = new PestControl();
public static Trawler trawler = new Trawler(); public static Trawler trawler = new Trawler();
private final static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
private final static Lock lock = new ReentrantLock();
/** /**
* Port and Cycle rate. * Port and Cycle rate.
@@ -145,12 +152,24 @@ public class GameEngine {
*/ */
System.out.println("Server listening on port " + serverlistenerPort); System.out.println("Server listening on port " + serverlistenerPort);
/**
* Main Server Tick
*
* This scheduler will tick once every 600ms. If the previous tick takes
* 300ms to execute, this scheduler will wait 300ms only before the next
* tick.
*
* scheduleAtFixedRate() does not invoke concurrent Runnables.
*/
scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
/** /**
* Main Server Tick * Main Server Tick
*/ */
try { try {
while (!GameEngine.shutdownServer) { if (GameEngine.shutdownServer) {
Thread.sleep(600); scheduler.shutdown();
}
itemHandler.process(); itemHandler.process();
playerHandler.process(); playerHandler.process();
npcHandler.process(); npcHandler.process();
@@ -169,12 +188,10 @@ public class GameEngine {
continue; continue;
} }
PlayerSave.saveGame((Client) p); PlayerSave.saveGame((Client) p);
System.out.println("Saved game for " + p.playerName System.out.println("Saved game for " + p.playerName + ".");
+ ".");
lastMassSave = System.currentTimeMillis(); lastMassSave = System.currentTimeMillis();
} }
} }
}
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
System.out.println("A fatal exception has been thrown!"); System.out.println("A fatal exception has been thrown!");
@@ -191,7 +208,20 @@ public class GameEngine {
PlayerSave.saveGame((Client) p); PlayerSave.saveGame((Client) p);
System.out.println("Saved game for " + p.playerName + "."); System.out.println("Saved game for " + p.playerName + ".");
} }
scheduler.shutdown(); // Kills the tickloop thread if Exception is thrown.
} }
}
}, 0, GameConstants.CYCLE_TIME, TimeUnit.MILLISECONDS);
try {
while (!scheduler.awaitTermination(60, TimeUnit.SECONDS)) {
// TODO
// Cleanup?
}
} catch (InterruptedException e) {
e.printStackTrace();
}
acceptor = null; acceptor = null;
connectionHandler = null; connectionHandler = null;
sac = null; sac = null;