burandotorg

artdecode.c

Nov 2nd, 2015
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.06 KB | None | 0 0
  1. /*
  2.    artdecode.c (artefact signal decoder) - Cmdr. Evrithiki
  3.  
  4.    This script uses ROOT (http://root.cern.ch) to decode and draw polygonal
  5.    renderings encoded by 'Unknown Artefact' (sic; British English) in the
  6.    game Elite: Dangerous (or anything using the same encoding system)
  7.    on a 26x26 2D plot.
  8.  
  9.    The encoding scheme uses letters A-Z to encode triplets of X,Y coordinates
  10.    to create a triangle. A series of these triangles will, when plotted,
  11.    create a complete image.
  12.  
  13.    The string AA will encode 0,0 and ZZ will encode 25,25. (An offset
  14.    can be added to plotted coordinates if desired; this will also
  15.    increase the size of the canvas.)
  16.  
  17.    This script will only accept letters in the English alphabet. Case
  18.    does not matter ('A' is the same as 'a').
  19.  
  20.    For more information and prewritten examples, see
  21.    https://forums.frontier.co.uk/showthread.php?t=175726&page=498.
  22.  
  23.    This script will error-check input and will not accept strings of
  24.    coordinates with missing letters, nor will it accept any containing
  25.    digits (1-9).
  26.  
  27.    It is designed to be run as a script within ROOT. From a ROOT prompt,
  28.    enter '.x artdecode.c' and follow the prompts.
  29.  
  30.    This work is licensed under the Creative Commons Attribution-
  31.    NonCommercial-ShareAlike 4.0 International License. To view a copy of
  32.    this license, visit http://creativecommons.org/licenses/by-nc-sa/4.0/.
  33.  
  34.    Please credit "Cmdr. Evrithiki" if you use it. It would be neat if
  35.    someone was able to make a cross-platform graphical version of this
  36.    script and improved on the graphics, particularly the shading!
  37. */
  38.  
  39. /* declare global variables - note, Float_t is a special ROOT vartype. */
  40.  
  41. Float_t coordsX[3] = {-1};
  42. Float_t coordsY[3] = {-1};
  43. bool verbose(true);
  44. string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; /* English only */
  45.  
  46. /* You can specify an offset here to add white space around the render */
  47.  
  48. int offset = 0;
  49. int size = 26 + (offset*2);
  50.  
  51. /* Determine whether character is an English alphabetic letter or a number.
  52.    Returns -1 if it's a number, the position in the alphabet if a letter. */
  53.  
  54. int letterpos (char input) {
  55.   int pos = -1;
  56.   for (int i=0; i<alphabet.length(); i++) {
  57.     if (input == alphabet[i]) {
  58.       pos = i;
  59.     }
  60.   }
  61.   return pos;
  62. }
  63.  
  64. /* error-check string (content can only be spaces or letters) */
  65.  
  66. bool stringOK (string input) {
  67.   /* check for non-letter/space content */
  68.   for (int i=0; i<input.length(); i++) {
  69.     if ((letterpos(input[i]) == -1) && (input[i] != ' ')) {
  70.       cout << "Failed check at " << i << " (" << input[i]
  71.         << ", " << letterpos(input[i]) << ")" << endl;
  72.       return false;
  73.     }
  74.   }
  75.  
  76.   /* check that letters are in groupings of 6 */
  77.  
  78.   int count(0);
  79.   for (int i=0; i<input.length(); i++) {
  80.     if (input[i] != ' ') {
  81.       count++;
  82.     } else {
  83.       if (count != 6) {
  84.         cout << "Found a word not of length 6." << endl;
  85.         return false;
  86.       } else {
  87.         count = 0;
  88.       }
  89.     }
  90.   }
  91.   return true;
  92. }
  93.  
  94. /* count how many 'words' are in input */
  95.  
  96. int wordcount (string input) {
  97.   int words(1);
  98.   for (int i=0; i<input.length(); i++) {
  99.     if (input[i] == ' ') {
  100.       words++;
  101.     }
  102.   }
  103.   return words;
  104. }
  105.  
  106. /* Do the actual decoding */
  107.  
  108. int decode (string input) {
  109.   if (verbose) {cout << input << " --> ";}
  110.   int t=0;
  111.   for (int i=0; i<input.length(); i+=2) {
  112.     coordsX[t] = letterpos(input[i]) + offset;
  113.     coordsY[t] = letterpos(input[i+1]) + offset;
  114.     t++;
  115.   }
  116.   for (int i=0; i<(input.length()/2); i++) {
  117.     if (verbose) {
  118.       cout << setw(2) << coordsX[i] << "," << setw(2) << coordsY[i] << " ";
  119.       if (i == ((input.length()/2)-1)) {cout << endl;}
  120.     }
  121.   }
  122.   return 1;
  123. }
  124.  
  125. string intstr (int input) {
  126.   stringstream convert;
  127.   string output;
  128.  
  129.   convert << input;
  130.   output = convert.str();
  131.   convert.str("");
  132.  
  133.   return output;
  134. } //convert int to string
  135.  
  136. /* main program starts here */
  137.  
  138. int artdecode () {
  139.  
  140.   string input("");
  141.  
  142.   cout << "String to decode (case insensitive, one line): ";
  143.   getline(cin, input);
  144.  
  145.   /* convert string to uppercase */
  146.   for (int i=0; i<input.length(); i++) {
  147.     input[i] = toupper(input[i]);
  148.   }
  149.  
  150.   if (!stringOK(input)) {
  151.     cout << "Input can only contain letters and spaces." << endl;
  152.     return 0;
  153.   }
  154.  
  155.   /* We will not be using Fill(), so a TH2C will suffice */
  156.  
  157.   TH2C *Message = new TH2C("Message","Decoded Message",size,0,size,size,0,size);
  158.   Message->Draw();
  159.   gStyle->SetOptStat(0); /* remove stats box */
  160.  
  161.   int wcount(0);
  162.  
  163.   string group("");
  164.  
  165.   for (int i=0; i<input.length(); i+=7) {
  166.     for (int ii=0; ii<6; ii++) {
  167.       group[ii] = input[i+ii];
  168.     }
  169.    
  170.     wcount++;
  171.    
  172.     decode(group.c_str());
  173.    
  174.     /* Draw decoded triangle. Be ye warned, ROOT shading can be weird */
  175.    
  176.     TPolyLine *triangle = new TPolyLine(3,coordsX,coordsY);
  177.    
  178.     triangle->SetFillColor(kGray);
  179.     triangle->SetLineColor(kBlack);
  180.     triangle->SetLineWidth(3);
  181.     triangle->Draw("f");
  182.     triangle->Draw();
  183.   }
  184.  
  185.   cout << "Done." << endl;
  186.  
  187.   return 1;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment