RuneHive-Game
Loading...
Searching...
No Matches
GameEngine.java
Go to the documentation of this file.
1package com.runehive.game.engine;
2
3import com.google.common.util.concurrent.AbstractScheduledService;
4import com.runehive.Config;
5import com.runehive.content.writer.InterfaceWriter;
6import com.runehive.content.writer.impl.InformationWriter;
7import com.runehive.game.engine.sync.ClientSynchronizer;
8import com.runehive.game.engine.sync.ParallelClientSynchronizer;
9import com.runehive.game.engine.sync.SequentialClientSynchronizer;
10import com.runehive.game.engine.sync.task.NpcPreUpdateTask;
11import com.runehive.game.engine.sync.task.PlayerPreUpdateTask;
12import com.runehive.game.world.World;
13import com.runehive.game.world.entity.MobList;
14import com.runehive.game.world.entity.mob.npc.Npc;
15import com.runehive.game.world.entity.mob.player.Player;
16import com.runehive.util.Stopwatch;
17import org.apache.logging.log4j.LogManager;
18import org.apache.logging.log4j.Logger;
19
20import java.util.concurrent.TimeUnit;
21
22public final class GameEngine extends AbstractScheduledService {
23
24 public static final int TICK_MILLIS = 600;
25
26 public static int millisToTicks(final int millis) {
27 return millis / TICK_MILLIS;
28 }
29
30 public static int clientTicksToMillis(final int clientTicks) {
31 return clientTicks * 20;
32 }
33
34 public static int clientTicksToServerTicks(final int clientTicks) {
35 return millisToTicks(clientTicksToMillis(clientTicks));
36 }
37
38 private static final Logger logger = LogManager.getLogger(GameEngine.class);
39
41
42 @Override
43 protected String serviceName() {
44 return "GameThread";
45 }
46
50
51 @Override
52 protected void runOneIteration() {
53 try {
54 final Stopwatch stopwatch = Stopwatch.start();
55 final Stopwatch stopwatch2 = Stopwatch.start();
56
57 World world = World.get();
59 MobList<Npc> npcs = World.getNpcs();
60
61 world.dequeLogins();
62
63 long elapsed = stopwatch.elapsedTime(TimeUnit.MILLISECONDS);
64 if (elapsed > 10 && Config.SERVER_DEBUG) {
65 System.out.printf("world.dequeLogins(): %d ms%n", elapsed);
66 }
67
68 stopwatch.reset();
69 world.dequeLogouts();
70 elapsed = stopwatch.elapsedTime(TimeUnit.MILLISECONDS);
71 if (elapsed > 10 && Config.SERVER_DEBUG) {
72 System.out.printf("world.dequeLogouts(): %d ms%n", elapsed);
73 }
74
75 stopwatch.reset();
76 npcs.forEach(npc -> new NpcPreUpdateTask(npc).run());
77 elapsed = stopwatch.elapsedTime(TimeUnit.MILLISECONDS);
78 if (elapsed > 10 && Config.SERVER_DEBUG) {
79 System.out.printf("NpcPreUpateTask: %d ms%n", elapsed);
80 }
81
82 stopwatch.reset();
83 players.forEach(player -> new PlayerPreUpdateTask(player).run());
84 elapsed = stopwatch.elapsedTime(TimeUnit.MILLISECONDS);
85 if (elapsed > 10 && Config.SERVER_DEBUG) {
86 System.out.printf("PlayerPreUpdateTask: %d ms%n", elapsed);
87 }
88
89 stopwatch.reset();
90 world.process();
91 elapsed = stopwatch.elapsedTime(TimeUnit.MILLISECONDS);
92 if (elapsed > 10 && Config.SERVER_DEBUG) {
93 System.out.printf("world.process(): %d ms%n", elapsed);
94 }
95
96 stopwatch.reset();
98 elapsed = stopwatch.elapsedTime(TimeUnit.MILLISECONDS);
99 if (elapsed > 10 && Config.SERVER_DEBUG) {
100 System.out.printf("npc.sequence(): %d ms%n", elapsed);
101 }
102
103 stopwatch.reset();
104 players.forEach(player -> {
105 try {
106 player.sequence();
107 } catch (Exception ex) {
108 logger.error(String.format("error player.sequence(): %s", player), ex);
109 }
110 });
111 elapsed = stopwatch.elapsedTime(TimeUnit.MILLISECONDS);
112 if (elapsed > 10 && Config.SERVER_DEBUG) {
113 System.out.printf("player.sequence(): %d ms%n", elapsed);
114 }
115
116 stopwatch.reset();
117 try {
118 synchronizer.synchronize(players, npcs);
119 } catch (Exception ex) {
120 logger.fatal("Error in the main game sequencer.", ex);
121 }
122 elapsed = stopwatch.elapsedTime(TimeUnit.MILLISECONDS);
123 if (elapsed > 10 && Config.SERVER_DEBUG) {
124 System.out.printf("synchronizer.synchronize(): %d ms%n", elapsed);
125 }
126 players.forEach(player -> {
128 });
129
130 stopwatch.reset();
131 if (stopwatch2.elapsedTime(TimeUnit.MILLISECONDS) > 60 && Config.SERVER_DEBUG) {
132 System.out.printf("CYCLE END: %d ms%n", stopwatch2.elapsedTime(TimeUnit.MILLISECONDS));
133 }
134 } catch(Exception e) {
135 e.printStackTrace();
136 }
137 }
138
139 @Override
140 protected Scheduler scheduler() {
141 return Scheduler.newFixedRateSchedule(0, TICK_MILLIS, TimeUnit.MILLISECONDS);
142 }
143
144}
The class that contains setting-related constants for the server.
Definition Config.java:24
static final boolean PARALLEL_GAME_ENGINE
This will use the parallel game game.
Definition Config.java:108
static boolean SERVER_DEBUG
The development state flag.
Definition Config.java:145
Handles writing on an itemcontainer.
static void write(InterfaceWriter writer)
Class handles writing on the quest tab itemcontainer.
static int clientTicksToMillis(final int clientTicks)
final ClientSynchronizer synchronizer
static int millisToTicks(final int millis)
static int clientTicksToServerTicks(final int clientTicks)
Represents the game world.
Definition World.java:46
static MobList< Npc > getNpcs()
Definition World.java:548
static MobList< Player > getPlayers()
Definition World.java:544
An Iterable implementation acting as a repository that holds instances of Entitys.
Definition MobList.java:26
void forEach(Consumer<? super E > action)
Definition MobList.java:162
long elapsedTime(TimeUnit unit)
static Stopwatch start()