simulator.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. #ifndef SIMULATOR_H
  2. #define SIMULATOR_H
  3. #include <array>
  4. #include <vector>
  5. #include <memory>
  6. #include <climits>
  7. #include <stdint.h>
  8. using namespace std;
  9. struct Position {
  10. size_t x = 0;
  11. size_t y = 0;
  12. Position()
  13. : x(0)
  14. , y(0)
  15. {}
  16. Position(size_t _x, size_t _y)
  17. : x(_x)
  18. , y(_y)
  19. {}
  20. Position(const Position& _pos)
  21. : x(_pos.x)
  22. , y(_pos.y)
  23. {}
  24. };
  25. enum Direction {
  26. Right,
  27. Down,
  28. Left,
  29. Up,
  30. };
  31. enum Command : uint16_t {
  32. NOP,
  33. CREATE,
  34. MOVE,
  35. DIE,
  36. TRANS,
  37. RTRANS,
  38. TURN,
  39. JUMP,
  40. AJUMP,
  41. BJUMP,
  42. SCAN,
  43. FARSCAN,
  44. SET,
  45. ADD,
  46. SUB,
  47. MUL,
  48. DIV,
  49. MOD,
  50. MIN,
  51. MAX,
  52. RANDOM,
  53. IF,
  54. IFN,
  55. IFG,
  56. IFL,
  57. IFGE,
  58. IFLE,
  59. // INIT,
  60. // BREAK,
  61. // RESUME,
  62. // SEIZE,
  63. // SLEEP,
  64. // QUIT,
  65. };
  66. enum Params : uint16_t {
  67. LLL,
  68. LLV,
  69. LVL,
  70. LVV,
  71. VLL,
  72. VLV,
  73. VVL,
  74. VVV,
  75. N = LLL,
  76. L = LLL,
  77. V = VLL,
  78. LL = LLL,
  79. LV = LVL,
  80. VL = VLL,
  81. VV = VVV,
  82. };
  83. using Parameter = int32_t;
  84. struct Instruction {
  85. Command command = NOP;
  86. Params params = N;
  87. Parameter a = 0;
  88. Parameter b = 0;
  89. Parameter c = 0;
  90. Instruction(Command _command, Params _params = N, Parameter _a = 0, Parameter _b = 0, Parameter _c = 0)
  91. : command(_command)
  92. , params(_params)
  93. , a(_a)
  94. , b(_b)
  95. , c(_c)
  96. {
  97. }
  98. };
  99. using Bank = shared_ptr<vector<Instruction>>;
  100. using BankIndex = size_t;
  101. using InstIndex = size_t;
  102. using TaskIndex = size_t;
  103. struct Task {
  104. Direction direction = Right;
  105. BankIndex bankIndex = 0;
  106. InstIndex instIndex = 0;
  107. };
  108. struct Program {
  109. QColor color;
  110. Position position;
  111. vector<Task> tasks;
  112. TaskIndex taskIndex = 0;
  113. vector<Bank> banks;
  114. array<int32_t, 20> vars{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
  115. Program(QColor _color, Position _position, int32_t instructionSet, int32_t slotCount, int32_t mobile) {
  116. color = _color;
  117. position = _position;
  118. banks.resize(slotCount);
  119. tasks.resize(1);
  120. }
  121. };
  122. struct Field {
  123. Program* program = nullptr;
  124. };
  125. struct Simulator {
  126. size_t cylcle = 0;
  127. vector<Program> programs;
  128. size_t size = 0;
  129. Simulator() {
  130. size = 20;
  131. programs.reserve(size * size);
  132. // std::srand(std::time(0));
  133. std::srand(0);
  134. }
  135. Position calcPosition(Position position, Direction direction, int32_t distance) {
  136. switch(direction) {
  137. case Right: return Position{(position.x + distance) % size, position.y};
  138. case Down: return Position{position.x, (position.y + distance) % size};
  139. case Left: return Position{(position.x - distance) % size, position.y};
  140. case Up: return Position{position.x, (position.y - distance) % size};
  141. }
  142. }
  143. void loadProgram(QColor color, size_t x, size_t y) {
  144. programs.push_back(Program(color, Position{x, y}, 2, 50, 1));
  145. {
  146. Bank bank = make_shared<vector<Instruction>>();
  147. bank->push_back(Instruction(BJUMP, LL, 1, 0));
  148. programs.back().banks[0] = bank;
  149. }
  150. {
  151. Bank bank = make_shared<vector<Instruction>>();
  152. bank->push_back(Instruction(SCAN, V, 1));
  153. bank->push_back(Instruction(CREATE, LLL, 2, 50, 1));
  154. bank->push_back(Instruction(TRANS, LL, 0, 0));
  155. bank->push_back(Instruction(TRANS, LL, 1, 1));
  156. bank->push_back(Instruction(RANDOM, VLL, 5, -5, 5));
  157. bank->push_back(Instruction(TURN, V, 5));
  158. bank->push_back(Instruction(AJUMP, L, 0));
  159. programs.back().banks[1] = bank;
  160. }
  161. }
  162. Program* findProgram(Position position) {
  163. for(auto& program : programs) {
  164. if(program.position.x == position.x && program.position.y == position.y) {
  165. return &program;
  166. }
  167. }
  168. return nullptr;
  169. }
  170. void simulate() {
  171. for (auto& program : programs) {
  172. auto& taskIndex = program.taskIndex;
  173. if(program.tasks.empty()) {
  174. continue;
  175. }
  176. taskIndex = (taskIndex + 1) % program.tasks.size();
  177. if (taskIndex < program.tasks.size()) {
  178. auto& task = program.tasks[taskIndex];
  179. if(task.bankIndex >= program.banks.size()) {
  180. continue;
  181. }
  182. const auto bank_ptr = program.banks[task.bankIndex].get();
  183. if(bank_ptr == nullptr || task.instIndex >= bank_ptr->size()) {
  184. continue;
  185. }
  186. const auto& bank = *bank_ptr;
  187. const auto& inst = bank[task.instIndex];
  188. //prevent overrideing of instuctions...
  189. int32_t a_safe = inst.a;
  190. int32_t b_safe = inst.b;
  191. int32_t c_safe = inst.c;
  192. int32_t* a = &a_safe;
  193. int32_t* b = &b_safe;
  194. int32_t* c = &c_safe;
  195. auto decode = [&](int*& v) {
  196. if(*v >= 0 && *v <= 20) {
  197. v = &program.vars[*v];
  198. }
  199. else {
  200. switch(*v) {
  201. case 21: break;
  202. }
  203. }
  204. };
  205. switch(inst.params) {
  206. case LLL: { break; }
  207. case LLV: { decode(c); break; }
  208. case LVL: { decode(b); break; }
  209. case LVV: { decode(b); decode(c); break; }
  210. case VLL: { decode(a); break; }
  211. case VLV: { decode(a); decode(c); break; }
  212. case VVL: { decode(a); decode(b); break; }
  213. case VVV: { decode(a); decode(b); decode(c); break; }
  214. }
  215. switch(inst.command) {
  216. case CREATE: {
  217. auto remotePosition = calcPosition(program.position, task.direction, 1);
  218. auto remoteProgram = findProgram(remotePosition);
  219. if(!remoteProgram) {
  220. programs.push_back(Program(program.color, remotePosition, *a, *b, *c));
  221. }
  222. task.instIndex += 1;
  223. break;
  224. }
  225. case MOVE: {
  226. auto remotePosition = calcPosition(program.position, task.direction, 1);
  227. auto remoteProgram = findProgram(remotePosition);
  228. if(!remoteProgram) {
  229. program.position = remotePosition;
  230. }
  231. task.instIndex += 1;
  232. break;
  233. }
  234. case TRANS: {
  235. auto remotePosition = calcPosition(program.position, task.direction, 1);
  236. auto remoteProgram = findProgram(remotePosition);
  237. if(remoteProgram && *b < remoteProgram->banks.size() && *a < program.banks.size()) {
  238. remoteProgram->banks[*b] = program.banks[*a];
  239. }
  240. task.instIndex += 1;
  241. break;
  242. }
  243. case RTRANS: {
  244. auto remotePosition = calcPosition(program.position, task.direction, 1);
  245. auto remoteProgram = findProgram(remotePosition);
  246. if(remoteProgram && *a < remoteProgram->banks.size() && *b < program.banks.size()) {
  247. program.banks[*b] = remoteProgram->banks[*a];
  248. }
  249. task.instIndex += 1;
  250. break;
  251. }
  252. case TURN: {
  253. task.direction = static_cast<Direction>(qMax(0, (task.direction + ((*a >= 0) ? 1 : -1)) % 4));
  254. task.instIndex += 1;
  255. break;
  256. }
  257. case JUMP: {
  258. task.instIndex += *a;
  259. break;
  260. }
  261. case AJUMP: {
  262. task.instIndex = *a;
  263. break;
  264. }
  265. case BJUMP: {
  266. task.bankIndex = *a;
  267. task.instIndex = *b;
  268. break;
  269. }
  270. case SCAN: {
  271. // Scans a field, result in #a
  272. // #a=0 ...empty.
  273. // #a=1 ...enemy
  274. // #a=2 ...friend
  275. auto remotePosition = calcPosition(program.position, task.direction, 1);
  276. auto remoteProgram = findProgram(remotePosition);
  277. *a = !remoteProgram ? 0 : remoteProgram->color == program.color ? 2: 1;
  278. task.instIndex += 1;
  279. break;
  280. }
  281. case FARSCAN: {
  282. // Scans up to c fields straight in front of the bot.
  283. // The nearest bot's type is stored in #a:
  284. // #a=0 ...empty.
  285. // #a=1 ...enemy
  286. // #a=2 ...friend
  287. // Its distance is stored in #b.
  288. *a = 0;
  289. *b = 0;
  290. for(int i = 0; i < *c; ++i) {
  291. auto remotePosition = calcPosition(program.position, task.direction, i + 1);
  292. auto remoteProgram = findProgram(remotePosition);
  293. if(remoteProgram) {
  294. *a = remoteProgram->color == program.color ? 2: 1;
  295. *b = i;
  296. break;
  297. }
  298. }
  299. task.instIndex += 1;
  300. break;
  301. }
  302. case SET:
  303. *a = *b;
  304. task.instIndex += 1;
  305. break;
  306. case ADD:
  307. *a += *b;
  308. task.instIndex += 1;
  309. break;
  310. case SUB:
  311. *a -= *b;
  312. task.instIndex += 1;
  313. break;
  314. case MUL:
  315. *a *= *b;
  316. task.instIndex += 1;
  317. break;
  318. case DIV:
  319. *a /= *b;
  320. task.instIndex += 1;
  321. break;
  322. case MOD:
  323. *a %= *b;
  324. task.instIndex += 1;
  325. break;
  326. case MIN:
  327. *a = qMin(*a, *b);
  328. task.instIndex += 1;
  329. break;
  330. case MAX:
  331. *a = qMax(*a, *b);
  332. task.instIndex += 1;
  333. break;
  334. case RANDOM:
  335. *a = *b + (rand() % (*c - *b + 1));
  336. task.instIndex += 1;
  337. break;
  338. case IF:
  339. task.instIndex += ((*a == *b) ? 1 : 2);
  340. break;
  341. case IFN:
  342. task.instIndex += ((*a != *b) ? 1 : 2);
  343. break;
  344. case IFG:
  345. task.instIndex += ((*a > *b) ? 1 : 2);
  346. break;
  347. case IFL:
  348. task.instIndex += ((*a < *b) ? 1 : 2);
  349. break;
  350. case IFGE:
  351. task.instIndex += ((*a >= *b) ? 1 : 2);
  352. break;
  353. case IFLE:
  354. task.instIndex += ((*a <= *b) ? 1 : 2);
  355. break;
  356. default:
  357. task.instIndex += 1;
  358. break;
  359. }
  360. }
  361. }
  362. ++cylcle;
  363. }
  364. };
  365. #endif // SIMULATOR_H