1
1

simulator.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. enum Error {
  84. NoError, // No error
  85. EliminationTrigger, // Elimination Trigger released
  86. DataHunger, // Data Hunger (Bank 1 empty and executed)
  87. DivisionByZero, // Division by zero
  88. InvalidBankNumber, // Invalid bank number (e.g. in TRANS or BJUMP)
  89. HigherInstructionSetRequired, // Higher Instruction Set required
  90. MobilityRequired, // Mobility required
  91. DieExecuted, // DIE executed
  92. InvalidParameter , // Invalid parameter (e.g. CREATE x, -1, x)
  93. Unemployment, // No more tasks left in a robot (Unemployment)
  94. InstructionDurationTooHigh, // Instruction duration too high (i.e. > MaxInstrDur)
  95. };
  96. using Parameter = int32_t;
  97. struct Instruction {
  98. Command command = NOP;
  99. Params params = N;
  100. Parameter a = 0;
  101. Parameter b = 0;
  102. Parameter c = 0;
  103. Instruction(Command _command, Params _params = N, Parameter _a = 0, Parameter _b = 0, Parameter _c = 0)
  104. : command(_command)
  105. , params(_params)
  106. , a(_a)
  107. , b(_b)
  108. , c(_c)
  109. {
  110. }
  111. };
  112. using Bank = shared_ptr<vector<Instruction>>;
  113. using BankIndex = size_t;
  114. using InstIndex = size_t;
  115. using TaskIndex = size_t;
  116. struct Task {
  117. Direction direction = Right;
  118. BankIndex bankIndex = 0;
  119. InstIndex instIndex = 0;
  120. };
  121. struct Program {
  122. Error error = NoError;
  123. QColor color;
  124. Position position;
  125. vector<Task> tasks;
  126. TaskIndex taskIndex = 0;
  127. vector<Bank> banks;
  128. 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}};
  129. Program(QColor _color, Position _position, int32_t instructionSet, int32_t slotCount, int32_t mobile) {
  130. color = _color;
  131. position = _position;
  132. banks.resize(slotCount);
  133. tasks.resize(1);
  134. }
  135. };
  136. struct Field {
  137. Program* program = nullptr;
  138. };
  139. struct Simulator {
  140. size_t cycle = 0;
  141. vector<Program> programs;
  142. size_t size = 0;
  143. Simulator() {
  144. size = 20;
  145. programs.reserve(size * size);
  146. // std::srand(std::time(0));
  147. std::srand(0);
  148. }
  149. Position calcPosition(Position position, Direction direction, int32_t distance) {
  150. switch(direction) {
  151. case Right: return Position{(position.x + distance) % size, position.y};
  152. case Down: return Position{position.x, (position.y + distance) % size};
  153. case Left: return Position{(position.x - distance) % size, position.y};
  154. case Up: return Position{position.x, (position.y - distance) % size};
  155. }
  156. }
  157. void loadProgram(QColor color, size_t x, size_t y) {
  158. programs.push_back(Program(color, Position{x, y}, 2, 50, 1));
  159. {
  160. Bank bank = make_shared<vector<Instruction>>();
  161. bank->push_back(Instruction(BJUMP, LL, 1, 0));
  162. programs.back().banks[0] = bank;
  163. }
  164. {
  165. Bank bank = make_shared<vector<Instruction>>();
  166. bank->push_back(Instruction(SCAN, V, 1));
  167. bank->push_back(Instruction(CREATE, LLL, 2, 50, 1));
  168. bank->push_back(Instruction(TRANS, LL, 0, 0));
  169. bank->push_back(Instruction(TRANS, LL, 1, 1));
  170. bank->push_back(Instruction(RANDOM, VLL, 5, -5, 5));
  171. bank->push_back(Instruction(TURN, V, 5));
  172. bank->push_back(Instruction(ADD, VL, 6, 1));
  173. // bank->push_back(Instruction(IFG, VL, 6, 5));
  174. // bank->push_back(Instruction(DIE));
  175. bank->push_back(Instruction(AJUMP, L, 0));
  176. programs.back().banks[1] = bank;
  177. }
  178. }
  179. Program* findProgram(Position position) {
  180. for(auto& program : programs) {
  181. if(program.position.x == position.x && program.position.y == position.y && program.error == NoError) {
  182. return &program;
  183. }
  184. }
  185. return nullptr;
  186. }
  187. void simulate() {
  188. for (auto& program : programs) {
  189. auto& taskIndex = program.taskIndex;
  190. if(program.error != NoError) {
  191. continue;
  192. }
  193. if(program.tasks.empty()) {
  194. continue;
  195. }
  196. taskIndex = (taskIndex + 1) % program.tasks.size();
  197. if (taskIndex < program.tasks.size()) {
  198. auto& task = program.tasks[taskIndex];
  199. if(task.bankIndex >= program.banks.size()) {
  200. continue;
  201. }
  202. const auto bank_ptr = program.banks[task.bankIndex].get();
  203. if(bank_ptr == nullptr || task.instIndex >= bank_ptr->size()) {
  204. continue;
  205. }
  206. const auto& bank = *bank_ptr;
  207. const auto& inst = bank[task.instIndex];
  208. //prevent overrideing of instuctions...
  209. int32_t a_safe = inst.a;
  210. int32_t b_safe = inst.b;
  211. int32_t c_safe = inst.c;
  212. int32_t* a = &a_safe;
  213. int32_t* b = &b_safe;
  214. int32_t* c = &c_safe;
  215. auto decode = [&](int*& v) {
  216. if(*v >= 0 && *v <= 20) {
  217. v = &program.vars[*v];
  218. }
  219. else {
  220. switch(*v) {
  221. case 21: break;
  222. }
  223. }
  224. };
  225. switch(inst.params) {
  226. case LLL: { break; }
  227. case LLV: { decode(c); break; }
  228. case LVL: { decode(b); break; }
  229. case LVV: { decode(b); decode(c); break; }
  230. case VLL: { decode(a); break; }
  231. case VLV: { decode(a); decode(c); break; }
  232. case VVL: { decode(a); decode(b); break; }
  233. case VVV: { decode(a); decode(b); decode(c); break; }
  234. }
  235. switch(inst.command) {
  236. case CREATE: {
  237. auto remotePosition = calcPosition(program.position, task.direction, 1);
  238. auto remoteProgram = findProgram(remotePosition);
  239. if(!remoteProgram) {
  240. programs.push_back(Program(program.color, remotePosition, *a, *b, *c));
  241. }
  242. task.instIndex += 1;
  243. break;
  244. }
  245. case MOVE: {
  246. auto remotePosition = calcPosition(program.position, task.direction, 1);
  247. auto remoteProgram = findProgram(remotePosition);
  248. if(!remoteProgram) {
  249. program.position = remotePosition;
  250. }
  251. task.instIndex += 1;
  252. break;
  253. }
  254. case DIE: {
  255. program.error = DieExecuted;
  256. task.instIndex += 1;
  257. break;
  258. }
  259. case TRANS: {
  260. auto remotePosition = calcPosition(program.position, task.direction, 1);
  261. auto remoteProgram = findProgram(remotePosition);
  262. if(remoteProgram && *b < remoteProgram->banks.size() && *a < program.banks.size()) {
  263. remoteProgram->banks[*b] = program.banks[*a];
  264. }
  265. task.instIndex += 1;
  266. break;
  267. }
  268. case RTRANS: {
  269. auto remotePosition = calcPosition(program.position, task.direction, 1);
  270. auto remoteProgram = findProgram(remotePosition);
  271. if(remoteProgram && *a < remoteProgram->banks.size() && *b < program.banks.size()) {
  272. program.banks[*b] = remoteProgram->banks[*a];
  273. }
  274. task.instIndex += 1;
  275. break;
  276. }
  277. case TURN: {
  278. task.direction = static_cast<Direction>(qMax(0, (task.direction + ((*a >= 0) ? 1 : -1)) % 4));
  279. task.instIndex += 1;
  280. break;
  281. }
  282. case JUMP: {
  283. task.instIndex += *a;
  284. break;
  285. }
  286. case AJUMP: {
  287. task.instIndex = *a;
  288. break;
  289. }
  290. case BJUMP: {
  291. task.bankIndex = *a;
  292. task.instIndex = *b;
  293. break;
  294. }
  295. case SCAN: {
  296. // Scans a field, result in #a
  297. // #a=0 ...empty.
  298. // #a=1 ...enemy
  299. // #a=2 ...friend
  300. auto remotePosition = calcPosition(program.position, task.direction, 1);
  301. auto remoteProgram = findProgram(remotePosition);
  302. *a = !remoteProgram ? 0 : remoteProgram->color == program.color ? 2: 1;
  303. task.instIndex += 1;
  304. break;
  305. }
  306. case FARSCAN: {
  307. // Scans up to c fields straight in front of the bot.
  308. // The nearest bot's type is stored in #a:
  309. // #a=0 ...empty.
  310. // #a=1 ...enemy
  311. // #a=2 ...friend
  312. // Its distance is stored in #b.
  313. *a = 0;
  314. *b = 0;
  315. for(int i = 0; i < *c; ++i) {
  316. auto remotePosition = calcPosition(program.position, task.direction, i + 1);
  317. auto remoteProgram = findProgram(remotePosition);
  318. if(remoteProgram) {
  319. *a = remoteProgram->color == program.color ? 2: 1;
  320. *b = i;
  321. break;
  322. }
  323. }
  324. task.instIndex += 1;
  325. break;
  326. }
  327. case SET:
  328. *a = *b;
  329. task.instIndex += 1;
  330. break;
  331. case ADD:
  332. *a += *b;
  333. task.instIndex += 1;
  334. break;
  335. case SUB:
  336. *a -= *b;
  337. task.instIndex += 1;
  338. break;
  339. case MUL:
  340. *a *= *b;
  341. task.instIndex += 1;
  342. break;
  343. case DIV:
  344. *a /= *b;
  345. task.instIndex += 1;
  346. break;
  347. case MOD:
  348. *a %= *b;
  349. task.instIndex += 1;
  350. break;
  351. case MIN:
  352. *a = qMin(*a, *b);
  353. task.instIndex += 1;
  354. break;
  355. case MAX:
  356. *a = qMax(*a, *b);
  357. task.instIndex += 1;
  358. break;
  359. case RANDOM:
  360. *a = *b + (rand() % (*c - *b + 1));
  361. task.instIndex += 1;
  362. break;
  363. case IF:
  364. task.instIndex += ((*a == *b) ? 1 : 2);
  365. break;
  366. case IFN:
  367. task.instIndex += ((*a != *b) ? 1 : 2);
  368. break;
  369. case IFG:
  370. task.instIndex += ((*a > *b) ? 1 : 2);
  371. break;
  372. case IFL:
  373. task.instIndex += ((*a < *b) ? 1 : 2);
  374. break;
  375. case IFGE:
  376. task.instIndex += ((*a >= *b) ? 1 : 2);
  377. break;
  378. case IFLE:
  379. task.instIndex += ((*a <= *b) ? 1 : 2);
  380. break;
  381. default:
  382. task.instIndex += 1;
  383. break;
  384. }
  385. }
  386. }
  387. ++cycle;
  388. }
  389. };
  390. #endif // SIMULATOR_H