No Description https://ascii.town
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

torus.h 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* Copyright (C) 2017 Curtis McEnroe <june@causal.agency>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Affero General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <assert.h>
  17. #include <stdalign.h>
  18. #include <stdbool.h>
  19. #include <stddef.h>
  20. #include <stdint.h>
  21. #include <stdlib.h>
  22. #include <time.h>
  23. #include <wchar.h>
  24. #define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0]))
  25. static const char DefaultDataPath[] = "torus.dat";
  26. static const char DefaultSockPath[] = "torus.sock";
  27. static const char DefaultFontPath[] = "default8x16.psfu";
  28. enum {
  29. ColorBlack,
  30. ColorRed,
  31. ColorGreen,
  32. ColorYellow,
  33. ColorBlue,
  34. ColorMagenta,
  35. ColorCyan,
  36. ColorWhite,
  37. ColorBright,
  38. };
  39. static const wchar_t CP437[256] = (
  40. L" ☺☻♥♦♣♠•◘○◙♂♀♪♫☼"
  41. L"►◄↕‼¶§▬↨↑↓→←∟↔▲▼"
  42. L" !\"#$%&'()*+,-./"
  43. L"0123456789:;<=>?"
  44. L"@ABCDEFGHIJKLMNO"
  45. L"PQRSTUVWXYZ[\\]^_"
  46. L"`abcdefghijklmno"
  47. L"pqrstuvwxyz{|}~⌂"
  48. L"ÇüéâäàåçêëèïîìÄÅ"
  49. L"ÉæÆôöòûùÿÖÜ¢£¥₧ƒ"
  50. L"áíóúñѪº¿⌐¬½¼¡«»"
  51. L"░▒▓│┤╡╢╖╕╣║╗╝╜╛┐"
  52. L"└┴┬├─┼╞╟╚╔╩╦╠═╬╧"
  53. L"╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀"
  54. L"αßΓπΣσµτΦΘΩδ∞φε∩"
  55. L"≡±≥≤⌠⌡÷≈°∙·√ⁿ²■ "
  56. );
  57. enum {
  58. CellRows = 25,
  59. CellCols = 80,
  60. };
  61. static const size_t CellsSize = sizeof(uint8_t[CellRows][CellCols]);
  62. static const uint8_t CellInitX = CellCols / 2;
  63. static const uint8_t CellInitY = CellRows / 2;
  64. struct Meta {
  65. time_t createTime;
  66. time_t modifyTime;
  67. time_t accessTime;
  68. uint32_t modifyCount;
  69. uint32_t accessCount;
  70. };
  71. struct Tile {
  72. alignas(4096)
  73. time_t createTime;
  74. time_t modifyTime;
  75. alignas(16) uint8_t cells[CellRows][CellCols];
  76. alignas(16) uint8_t colors[CellRows][CellCols];
  77. uint32_t modifyCount;
  78. uint32_t accessCount;
  79. time_t accessTime;
  80. };
  81. static_assert(4096 == sizeof(struct Tile), "struct Tile is page-sized");
  82. static inline struct Meta tileMeta(const struct Tile *tile) {
  83. return (struct Meta) {
  84. .createTime = tile->createTime,
  85. .modifyTime = tile->modifyTime,
  86. .accessTime = tile->accessTime,
  87. .modifyCount = tile->modifyCount,
  88. .accessCount = tile->accessCount,
  89. };
  90. }
  91. enum {
  92. TileRows = 512,
  93. TileCols = 512,
  94. };
  95. static const size_t TilesSize = sizeof(struct Tile[TileRows][TileCols]);
  96. static const uint32_t TileInitX = 0;
  97. static const uint32_t TileInitY = 0;
  98. static const struct {
  99. uint32_t tileX;
  100. uint32_t tileY;
  101. } Ports[] = {
  102. { TileInitX, TileInitY },
  103. { TileCols * 3 / 4, TileRows * 3 / 4 }, // NW
  104. { TileCols * 1 / 4, TileRows * 3 / 4 }, // NE
  105. { TileCols * 1 / 4, TileRows * 1 / 4 }, // SE
  106. { TileCols * 3 / 4, TileRows * 1 / 4 }, // SW
  107. };
  108. enum {
  109. MapRows = 11,
  110. MapCols = 11,
  111. };
  112. struct Map {
  113. time_t now;
  114. struct Meta min;
  115. struct Meta max;
  116. struct Meta meta[MapRows][MapCols];
  117. };
  118. struct ServerMessage {
  119. enum {
  120. ServerTile,
  121. ServerMove,
  122. ServerPut,
  123. ServerCursor,
  124. ServerMap,
  125. } type;
  126. union {
  127. struct {
  128. uint8_t cellX;
  129. uint8_t cellY;
  130. } move;
  131. struct {
  132. uint8_t cellX;
  133. uint8_t cellY;
  134. uint8_t color;
  135. uint8_t cell;
  136. } put;
  137. struct {
  138. uint8_t oldCellX;
  139. uint8_t oldCellY;
  140. uint8_t newCellX;
  141. uint8_t newCellY;
  142. } cursor;
  143. };
  144. };
  145. static const uint8_t CursorNone = UINT8_MAX;
  146. struct ClientMessage {
  147. enum {
  148. ClientMove,
  149. ClientFlip,
  150. ClientPut,
  151. ClientMap,
  152. ClientTele,
  153. } type;
  154. union {
  155. struct {
  156. int8_t dx;
  157. int8_t dy;
  158. } move;
  159. struct {
  160. uint8_t color;
  161. uint8_t cell;
  162. } put;
  163. uint8_t port;
  164. };
  165. };