Main.cpp 930 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #include "server/network/Server.h"
  2. #include "server/Clock.h"
  3. #include "server/GameServer.h"
  4. #include "commands/ConsoleEditor.h"
  5. static const u64 NANOS_PER_TICK = 50000000;
  6. int main() {
  7. Server server(11196);
  8. if(server.hasError()) {
  9. return 0;
  10. }
  11. Clock tps;
  12. GameServer gameServer(tps);
  13. ServerCommands serverCommands;
  14. Clock clock;
  15. u64 lag = 0;
  16. while(serverCommands.isRunning()) {
  17. lag += clock.update();
  18. while(lag >= NANOS_PER_TICK) {
  19. lag -= NANOS_PER_TICK;
  20. tps.update();
  21. ConsoleEditor::clearPrintLine();
  22. server.consumeEvents(gameServer);
  23. gameServer.handleCommands(serverCommands);
  24. gameServer.tick();
  25. ConsoleEditor::printLine();
  26. }
  27. u64 waitNanos = NANOS_PER_TICK - lag;
  28. if(waitNanos > 300000) {
  29. clock.wait(waitNanos);
  30. }
  31. }
  32. return 0;
  33. }