RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
GameEngine.java
1package com.osroyale.game.engine;
2
3import com.google.common.util.concurrent.AbstractScheduledService;
4import com.osroyale.Config;
5import com.osroyale.content.writer.InterfaceWriter;
6import com.osroyale.content.writer.impl.InformationWriter;
7import com.osroyale.game.engine.sync.ClientSynchronizer;
8import com.osroyale.game.engine.sync.ParallelClientSynchronizer;
9import com.osroyale.game.engine.sync.SequentialClientSynchronizer;
10import com.osroyale.game.engine.sync.task.NpcPreUpdateTask;
11import com.osroyale.game.engine.sync.task.PlayerPreUpdateTask;
12import com.osroyale.game.world.World;
13import com.osroyale.game.world.entity.MobList;
14import com.osroyale.game.world.entity.mob.npc.Npc;
15import com.osroyale.game.world.entity.mob.player.Player;
16import com.osroyale.util.Stopwatch;
17import org.apache.logging.log4j.LogManager;
18import org.apache.logging.log4j.Logger;
19
20import java.util.concurrent.TimeUnit;
21
51
52public final class GameEngine extends AbstractScheduledService {
53
54 public static final int TICK_MILLIS = 600;
55
56 public static int millisToTicks(final int millis) {
57 return millis / TICK_MILLIS;
58 }
59
60 public static int clientTicksToMillis(final int clientTicks) {
61 return clientTicks * 20;
62 }
63
64 public static int clientTicksToServerTicks(final int clientTicks) {
65 return millisToTicks(clientTicksToMillis(clientTicks));
66 }
67
68 private static final Logger logger = LogManager.getLogger(GameEngine.class);
69
70 private final ClientSynchronizer synchronizer;
71
72 @Override
73 protected String serviceName() {
74 return "GameThread";
75 }
76
77 public GameEngine() {
79 }
80
81 @Override
82 protected void runOneIteration() {
83 try {
84 final Stopwatch stopwatch = Stopwatch.start();
85 final Stopwatch stopwatch2 = Stopwatch.start();
86
87 World world = World.get();
88 MobList<Player> players = World.getPlayers();
89 MobList<Npc> npcs = World.getNpcs();
90
91 world.dequeLogins();
92
93 long elapsed = stopwatch.elapsedTime(TimeUnit.MILLISECONDS);
94 if (elapsed > 10 && Config.SERVER_DEBUG) {
95 System.out.printf("world.dequeLogins(): %d ms%n", elapsed);
96 }
97
98 stopwatch.reset();
99 world.dequeLogouts();
100 elapsed = stopwatch.elapsedTime(TimeUnit.MILLISECONDS);
101 if (elapsed > 10 && Config.SERVER_DEBUG) {
102 System.out.printf("world.dequeLogouts(): %d ms%n", elapsed);
103 }
104
105 stopwatch.reset();
106 npcs.forEach(npc -> new NpcPreUpdateTask(npc).run());
107 elapsed = stopwatch.elapsedTime(TimeUnit.MILLISECONDS);
108 if (elapsed > 10 && Config.SERVER_DEBUG) {
109 System.out.printf("NpcPreUpateTask: %d ms%n", elapsed);
110 }
111
112 stopwatch.reset();
113 players.forEach(player -> new PlayerPreUpdateTask(player).run());
114 elapsed = stopwatch.elapsedTime(TimeUnit.MILLISECONDS);
115 if (elapsed > 10 && Config.SERVER_DEBUG) {
116 System.out.printf("PlayerPreUpdateTask: %d ms%n", elapsed);
117 }
118
119 stopwatch.reset();
120 world.process();
121 elapsed = stopwatch.elapsedTime(TimeUnit.MILLISECONDS);
122 if (elapsed > 10 && Config.SERVER_DEBUG) {
123 System.out.printf("world.process(): %d ms%n", elapsed);
124 }
125
126 stopwatch.reset();
127 npcs.forEach(Npc::sequence);
128 elapsed = stopwatch.elapsedTime(TimeUnit.MILLISECONDS);
129 if (elapsed > 10 && Config.SERVER_DEBUG) {
130 System.out.printf("npc.sequence(): %d ms%n", elapsed);
131 }
132
133 stopwatch.reset();
134 players.forEach(player -> {
135 try {
136 player.sequence();
137 } catch (Exception ex) {
138 logger.error(String.format("error player.sequence(): %s", player), ex);
139 }
140 });
141 elapsed = stopwatch.elapsedTime(TimeUnit.MILLISECONDS);
142 if (elapsed > 10 && Config.SERVER_DEBUG) {
143 System.out.printf("player.sequence(): %d ms%n", elapsed);
144 }
145
146 stopwatch.reset();
147 try {
148 synchronizer.synchronize(players, npcs);
149 } catch (Exception ex) {
150 logger.fatal("Error in the main game sequencer.", ex);
151 }
152 elapsed = stopwatch.elapsedTime(TimeUnit.MILLISECONDS);
153 if (elapsed > 10 && Config.SERVER_DEBUG) {
154 System.out.printf("synchronizer.synchronize(): %d ms%n", elapsed);
155 }
156 players.forEach(player -> {
157 InterfaceWriter.write(new InformationWriter(player));
158 });
159
160 stopwatch.reset();
161 if (stopwatch2.elapsedTime(TimeUnit.MILLISECONDS) > 60 && Config.SERVER_DEBUG) {
162 System.out.printf("CYCLE END: %d ms%n", stopwatch2.elapsedTime(TimeUnit.MILLISECONDS));
163 }
164 } catch(Exception e) {
165 e.printStackTrace();
166 }
167 }
168
169 @Override
170 protected Scheduler scheduler() {
171 return Scheduler.newFixedRateSchedule(0, TICK_MILLIS, TimeUnit.MILLISECONDS);
172 }
173
174}
static final boolean PARALLEL_GAME_ENGINE
Definition Config.java:149
static boolean SERVER_DEBUG
Definition Config.java:186
void forEach(Consumer<? super E > action)
Definition MobList.java:162