1
1

simulator.h 13 KB

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