Advertisement
ZoriaRPG

ZScript String Table Processor (Revised, Octt 2018, v1.0)

Oct 15th, 2018
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. //////////////////////////////////////////////////
  2. /// Revised String Table Processor for ZScript ///
  3. /// Allows defining custom string tables       ///
  4. /// v1.0                                       ///
  5. /// 15th October, 2018                         ///
  6. /// By: ZoriaRPG                               ///
  7. //////////////////////////////////////////////////
  8.  
  9.  
  10. namespace script strings
  11. {
  12.     const int BUFFER_SIZE = 1024;
  13.     const int MAX_STRINGS = 4096;
  14.     int buffer[BUFFER_SIZE];
  15.     int ids[MAX_STRINGS];
  16.    
  17.     /*List strings in the table using the following format:
  18.    
  19.     $ <NUMBER> : <string>
  20.    
  21.     Tokens:
  22.     $ -- Indicates the beginning of a string ID.
  23.     <NUMBER> a series of numeric characters that represent the numeric ID of the string.
  24.     : -- COlon separator between <NUMBER> and <string>
  25.     <string> The string text.
  26.     */
  27.     int table[]=
  28.     "$000001:This is a string.
  29.     $000002:This is a second.
  30.     $000003:This is a third.
  31.     $000004:This is a fourth
  32.     $000005:This is a fifth.";
  33.     void run(){}
  34.    
  35.     //stores the initial position of all strings in the table into ids[]
  36.     //where the index of ids[index] == the string ID.
  37.     void init()
  38.     {
  39.         int token[7]; int token_pos; int table_pos;
  40.        
  41.         for ( table_pos = 0; table[table_pos] != NULL; ++table_pos )
  42.         {
  43.             //find the tokens
  44.             if ( table[table_pos] == '$' )
  45.             {  
  46.                 //copy the string ID into the token
  47.                 token_pos = 0;
  48.                 while( table[table_pos] != ':' )
  49.                 {
  50.                     token[token_pos] = table[q];
  51.                     ++token_pos;
  52.                     ++q;
  53.                 }
  54.                 //store the index where the token ends + 2
  55.                 ids[atoi(token)] = table_pos +2;
  56.             }
  57.         }
  58.     }
  59.     void clear()
  60.     {
  61.         for ( int q = 0; q < BUFFER_SIZE; ++q )
  62.         {
  63.             buffer[q] = 0;
  64.         }
  65.     }
  66.     void fetch(int string_id)
  67.     {
  68.         int table_pos = ids[string_id];
  69.         for ( int buffer_pos = 0; table[table_pos+1] != '$'; ++buffer_pos )
  70.         {
  71.             buffer[buffer_pos] = table[table_pos];
  72.             ++table_pos;
  73.         }
  74.     }
  75.        
  76.        
  77.     void store(int msg_id)
  78.     {
  79.         if ( msg_id <= 0 )
  80.         {
  81.             TraceError("Invalid message ID passed to strings.store()", msg_id);
  82.             return;
  83.         }
  84.         if ( msg_id >= Game->NumMessages )
  85.         {
  86.             TraceError("Invalid message ID passed to strings.store()", msg_id);
  87.             return;
  88.         }
  89.         SetMessage(msg_id, buffer);
  90.     }
  91.     void display(int string_id)
  92.     {
  93.         fetch(string_id);
  94.         store(Game->NumMessages);
  95.         Screen->Message(Game->NumMessages);
  96.     }
  97.     void display(int string_id, int template_message)
  98.     {
  99.         fetch(string_id);
  100.         store(template_message);
  101.         Screen->Message(template_message);
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement