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 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. enum {
  26. ColorBlack,
  27. ColorRed,
  28. ColorGreen,
  29. ColorYellow,
  30. ColorBlue,
  31. ColorMagenta,
  32. ColorCyan,
  33. ColorWhite,
  34. ColorBright,
  35. };
  36. static const wchar_t CP437[256] = (
  37. L" ☺☻♥♦♣♠•◘○◙♂♀♪♫☼"
  38. L"►◄↕‼¶§▬↨↑↓→←∟↔▲▼"
  39. L" !\"#$%&'()*+,-./"
  40. L"0123456789:;<=>?"
  41. L"@ABCDEFGHIJKLMNO"
  42. L"PQRSTUVWXYZ[\\]^_"
  43. L"`abcdefghijklmno"
  44. L"pqrstuvwxyz{|}~⌂"
  45. L"ÇüéâäàåçêëèïîìÄÅ"
  46. L"ÉæÆôöòûùÿÖÜ¢£¥₧ƒ"
  47. L"áíóúñѪº¿⌐¬½¼¡«»"
  48. L"░▒▓│┤╡╢╖╕╣║╗╝╜╛┐"
  49. L"└┴┬├─┼╞╟╚╔╩╦╠═╬╧"
  50. L"╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀"
  51. L"αßΓπΣσµτΦΘΩδ∞φε∩"
  52. L"≡±≥≤⌠⌡÷≈°∙·√ⁿ²■ "
  53. );
  54. enum {
  55. CellRows = 24,
  56. CellCols = 80,
  57. };
  58. static const size_t CellsSize = sizeof(uint8_t[CellRows][CellCols]);
  59. static const uint8_t CellInitX = CellCols / 2;
  60. static const uint8_t CellInitY = CellRows / 2;
  61. struct Meta {
  62. time_t createTime;
  63. time_t modifyTime;
  64. time_t accessTime;
  65. uint32_t modifyCount;
  66. uint32_t accessCount;
  67. };
  68. struct Tile {
  69. alignas(4096) uint8_t cells[CellRows][CellCols];
  70. uint8_t colors[CellRows][CellCols];
  71. struct Meta meta;
  72. };
  73. static_assert(4096 == sizeof(struct Tile), "struct Tile is page-sized");
  74. enum {
  75. TileRows = 64,
  76. TileCols = 64,
  77. };
  78. static const size_t TilesSize = sizeof(struct Tile[TileRows][TileCols]);
  79. static const uint32_t TileInitX = TileCols / 2;
  80. static const uint32_t TileInitY = TileRows / 2;
  81. enum {
  82. MapRows = 7,
  83. MapCols = 7,
  84. };
  85. struct Map {
  86. time_t now;
  87. struct Meta min;
  88. struct Meta max;
  89. struct Meta meta[MapRows][MapCols];
  90. };
  91. struct ServerMessage {
  92. enum {
  93. ServerTile,
  94. ServerMove,
  95. ServerPut,
  96. ServerCursor,
  97. ServerMap,
  98. } type;
  99. union {
  100. struct {
  101. uint8_t cellX;
  102. uint8_t cellY;
  103. } move;
  104. struct {
  105. uint8_t cellX;
  106. uint8_t cellY;
  107. uint8_t color;
  108. uint8_t cell;
  109. } put;
  110. struct {
  111. uint8_t oldCellX;
  112. uint8_t oldCellY;
  113. uint8_t newCellX;
  114. uint8_t newCellY;
  115. } cursor;
  116. };
  117. };
  118. static const uint8_t CursorNone = UINT8_MAX;
  119. struct ClientMessage {
  120. enum {
  121. ClientMove,
  122. ClientFlip,
  123. ClientPut,
  124. ClientMap,
  125. } type;
  126. union {
  127. struct {
  128. int8_t dx;
  129. int8_t dy;
  130. } move;
  131. struct {
  132. uint8_t color;
  133. uint8_t cell;
  134. } put;
  135. };
  136. };