Main.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include "common/NetworkPackets.h"
  2. #include "data/Array.h"
  3. #include "data/HashMap.h"
  4. #include "math/Vector.h"
  5. #include "network/Server.h"
  6. #include "raw-terminal/Console.h"
  7. #include "server/snuviscript/Snuvi.h"
  8. #include "utils/Clock.h"
  9. #include "utils/Random.h"
  10. #include "utils/SplitString.h"
  11. static constexpr int WORLD_SIZE = 16;
  12. static bool running = true;
  13. static Array<bool, WORLD_SIZE * WORLD_SIZE * WORLD_SIZE> world(false);
  14. static HashMap<Server::Client, int> players;
  15. static void handleCommands() {
  16. const ConsoleLine* line = readConsoleLine("> ");
  17. if(line != nullptr) {
  18. StringBuffer<50> buffer;
  19. for(int i = 0; i < line->length; i++) {
  20. buffer.appendUnicode(line->data[i]);
  21. }
  22. buffer.printLine();
  23. if(buffer == "stop") {
  24. running = false;
  25. }
  26. }
  27. }
  28. static void handleCommand(List<uint32>& s) {
  29. StringBuffer<256> buffer;
  30. for(uint32 u : s) {
  31. buffer.appendUnicode(u);
  32. }
  33. SplitString<256> split(buffer);
  34. if(split.getLength() >= 1) {
  35. if(strcmp(split[0], "/script") == 0) {
  36. if(split.getLength() >= 2) {
  37. Snuvi::start(split[1]);
  38. } else {
  39. puts("/script <script>");
  40. }
  41. }
  42. }
  43. }
  44. static void set(int x, int y, int z, bool b) {
  45. if(x < 0 || x >= WORLD_SIZE || y < 0 || y >= WORLD_SIZE || z < 0 ||
  46. z >= WORLD_SIZE) {
  47. return;
  48. }
  49. world[x * WORLD_SIZE * WORLD_SIZE + y * WORLD_SIZE + z] = b;
  50. OutPacket out(100);
  51. out.writeU8(static_cast<uint8>(ToClientPacket::SET_BLOCK));
  52. out.writeS32(x);
  53. out.writeS32(y);
  54. out.writeS32(z);
  55. out.writeU8(b);
  56. Server::send(out, PacketSendMode::RELIABLE);
  57. }
  58. static void onConnect(Server::Client client) {
  59. OutPacket out(100);
  60. out.writeU8(static_cast<uint8>(ToClientPacket::WORLD));
  61. for(bool b : world) {
  62. out.writeU8(b);
  63. }
  64. Server::send(client, out, PacketSendMode::RELIABLE);
  65. players.add(client, 0);
  66. }
  67. static void onDisconnect(Server::Client client) {
  68. players.remove(client);
  69. }
  70. static void onPacket(Server::Client client, InPacket& in) {
  71. uint8 type = 0;
  72. if(in.read(type)) {
  73. puts("no data");
  74. return;
  75. }
  76. switch(static_cast<ToServerPacket>(type)) {
  77. case ToServerPacket::SET_BLOCK: {
  78. IntVector3 pos;
  79. in.read(pos);
  80. uint8 type;
  81. in.read(type);
  82. set(pos[0], pos[1], pos[2], type);
  83. break;
  84. }
  85. case ToServerPacket::PLAYER: {
  86. Vector3 pos;
  87. in.read(pos[0]);
  88. in.read(pos[1]);
  89. in.read(pos[2]);
  90. OutPacket out(100);
  91. out.writeU8(static_cast<uint8>(ToClientPacket::PLAYER));
  92. out.writeFloat(pos[0]);
  93. out.writeFloat(pos[1]);
  94. out.writeFloat(pos[2]);
  95. out.writeS32(client);
  96. for(Server::Client c : players.keys()) {
  97. if(c != client) {
  98. Server::send(c, out, PacketSendMode::RELIABLE);
  99. }
  100. }
  101. break;
  102. }
  103. case ToServerPacket::CHAT: {
  104. List<uint32> s;
  105. uint32 u;
  106. while(!in.read(u)) {
  107. s.add(u);
  108. }
  109. if(s.getLength() > 0 && s[0] == '/') {
  110. handleCommand(s);
  111. return;
  112. }
  113. OutPacket out(100);
  114. out.writeU8(static_cast<uint8>(ToClientPacket::CHAT));
  115. for(uint32 u : s) {
  116. out.writeU32(u);
  117. }
  118. Server::send(out, PacketSendMode::RELIABLE);
  119. break;
  120. }
  121. default: printf("invalid package type %d\n", static_cast<int>(type));
  122. }
  123. }
  124. static void init() {
  125. Snuvi::init();
  126. Server::setPacketHandler(onPacket);
  127. Server::setConnectHandler(onConnect);
  128. Server::setDisconnectHandler(onDisconnect);
  129. Random r(0);
  130. for(bool& b : world) {
  131. b = r.next() & 1;
  132. }
  133. }
  134. static void tick() {
  135. handleCommands();
  136. Server::tick();
  137. }
  138. static void loop() {
  139. init();
  140. Clock clock;
  141. constexpr Clock::Nanos NANOS_PER_TICK = 50000000;
  142. Clock::Nanos lag = 0;
  143. while(running) {
  144. lag += clock.update();
  145. while(lag >= NANOS_PER_TICK) {
  146. lag -= NANOS_PER_TICK;
  147. tick();
  148. }
  149. Clock::Nanos waitNanos = NANOS_PER_TICK - lag;
  150. if(waitNanos > 300000) {
  151. clock.wait(waitNanos);
  152. }
  153. }
  154. }
  155. int main() {
  156. if(initConsole()) {
  157. return 0;
  158. }
  159. Error e = Server::start(11196, 50);
  160. if(e.has()) {
  161. puts(e.message);
  162. return 0;
  163. }
  164. loop();
  165. Server::stop();
  166. return 0;
  167. }