Advertisement
Guest User

Untitled

a guest
Dec 14th, 2013
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.24 KB | None | 0 0
  1.    
  2.  
  3.     #include <cmath>
  4.     #include <limits>
  5.     #include <string>
  6.     #include <cstdint>
  7.     #include <cstdarg>
  8.      
  9.      
  10.             template<class T>
  11.             struct SComplex
  12.             {
  13.                 typedef T value_type;
  14.      
  15.                 value_type f;
  16.      
  17.                 // TODO
  18.                
  19.                 // Must make those comparisions better
  20.      
  21.                 bool operator==(const SComplex& sz) const
  22.                 {
  23.                     const T epsilon = std::numeric_limits<T>::epsilon();
  24.                     return std::abs(this->f - sz.f) <= 0.01;
  25.                 }
  26.      
  27.                 bool operator<(const SComplex& sz) const
  28.                 {
  29.                     const T epsilon = std::numeric_limits<T>::epsilon();
  30.                     return ( ((this->f - sz.f) < epsilon) && (fabs(this->f - sz.f) > epsilon) );
  31.                 }
  32.             };
  33.             template<size_t N>
  34.             struct SVector
  35.             {
  36.                 SComplex<float> c[N];
  37.                
  38.                 template<class Functor>
  39.                 bool comp(const SVector& b) const
  40.                 {
  41.                     Functor comparer;
  42.                     for(size_t i = 0; i < N; ++i)
  43.                     {
  44.                         if(!(comparer(c[i], b.c[i])))
  45.                             return false;
  46.                     }
  47.                     return true;                
  48.                 }
  49.                
  50.                 bool operator==(const SVector& b) const
  51.                 {
  52.                     for(size_t i = 0; i < N; ++i)
  53.                     {
  54.                         if(!(c[i] == b.c[i])) return false;
  55.                     }
  56.                     return true;      
  57.                 }
  58.      
  59.                 SComplex<float>& operator[](size_t i)
  60.                 {
  61.                     return c[i];
  62.                 }
  63.                
  64.                 const SComplex<float>& operator[](size_t i) const
  65.                 {
  66.                     return c[i];
  67.                 }
  68.             };
  69.      
  70.             static_assert(sizeof(SComplex<float>) == sizeof(float), "Wrong size of SComplex");
  71.            
  72.             /* typedef the wrappers */
  73.             typedef uint32_t        hash;
  74.             typedef uint32_t        uhash;      /* upper case hash */
  75.             typedef SString<32>     string32;
  76.             typedef SComplex<float> complex;
  77.             typedef SVector<2>      vec2;
  78.             typedef SVector<3>      vec3;
  79.             typedef SVector<4>      vec4;
  80.             typedef int             integer;
  81.             typedef unsigned int    flags;
  82.             typedef unsigned int    obj_id;
  83.      
  84.  
  85.     #define OPI(item, op)   (a.item op b.item)
  86.     #define EQ(item)        OPI(item, ==)
  87.     #define LE(item)        OPI(item, <)
  88.      
  89.         struct SDataIPL_INST_OBJECT
  90.         {
  91.             obj_id  id;
  92.             // won't use modelname because it makes no difference
  93.             integer interior;
  94.             vec3    pos;
  95.             vec4    rot;
  96.            
  97.             /* used by the parser, not to be used in comparisions */
  98.             integer lod;
  99.            
  100.             bool operator==(const SDataIPL_INST_OBJECT& b) const
  101.             {
  102.                 const SDataIPL_INST_OBJECT& a = *this;
  103.                 return EQ(id) && EQ(interior) && EQ(pos) && EQ(rot);
  104.             }
  105.         };
  106.        
  107.         struct SDataIPL_INST
  108.         {
  109.             typedef SDataIPL_INST_OBJECT SObject;
  110.            
  111.             SObject obj;
  112.             SObject lod;
  113.             bool has_lod;
  114.            
  115.             bool operator==(const SDataIPL_INST& b) const
  116.             {
  117.                 const SDataIPL_INST& a = *this;
  118.                
  119.                 if(EQ(obj) && EQ(has_lod))
  120.                 {
  121.                     if(has_lod) return EQ(lod);
  122.                     return true;
  123.                 }
  124.                 return false;
  125.             }
  126.         };
  127.        
  128.         /*
  129.          *  ParseConfigLine
  130.          *      Parses @str (modifying it's content) as if it is a Grand Theft Auto config line.
  131.          *      Grand Theft Auto config lines use '#' and ';' as comments and they ignore ',' and spaces.
  132.          *      So, if @str is "  a, b, c,   d, e ; cool " the output is "a, b, c,   d, e "
  133.          */
  134.         inline char* ParseConfigLine(char* str)
  135.         {
  136.             char* result = str;
  137.             bool bTrim = true;
  138.            
  139.             //printf("\n>>p> %s\n", str);
  140.             if(result)
  141.             {
  142.                 /* Iterate on string making apropiate changes */
  143.                 for(char* p = result; *p; ++p)
  144.                 {
  145.                     if(*p <= ' ' || *p == ',')
  146.                     {
  147.                         *p = ' ';
  148.                         if(bTrim) result = p+1;
  149.                     }
  150.                     else if(*p == '#' || *p == ';')
  151.                     {
  152.                         /* Comments, end line here */
  153.                         *p = 0;
  154.                         break;
  155.                     }
  156.                     else
  157.                         bTrim = false; /* First non space character, no more trim */
  158.                 }
  159.             }
  160.             return result;
  161.         }
  162.        
  163.            
  164.         }
  165.     }
  166.      
  167.         /*
  168.          *  ScanConfigLine
  169.          *      Scans the GTA configuration line @str with format @fmt
  170.          *      Retruns true if the scan sucessfully acquieres @count arguments, returns false otherwise
  171.          */
  172.         inline bool ScanConfigLine(int count, const char* str, const char* fmt, ...)
  173.         {
  174.             va_list va; va_start(va, fmt);
  175.             int result = vsscanf(str, fmt, va);
  176.             va_end(va);
  177.             return (result == count);
  178.         }
  179.        
  180.        
  181.        
  182.        
  183.        
  184.     #include <vector>
  185.     typedef std::vector<SDataIPL_INST> VectorIPL;
  186.      
  187.     static VectorIPL newIPL;
  188.     static VectorIPL gameIPL;
  189.     static const char* instFormat = "%d %s %d %g %g %g %g %g %g %g %d \n";
  190.     static const int   instCount  = 11;
  191.      
  192.     void ReadIPL(const char* filename, VectorIPL& ipl)
  193.     {
  194.         char *line, dummy[64], buf[256];
  195.      
  196.         if(FILE* f = fopen(filename, "r"))
  197.         {
  198.             std::vector<SDataIPL_INST::SObject> lines;
  199.        
  200.             fgets(buf, sizeof(buf), f);
  201.        
  202.             while(line = ParseConfigLine(fgets(buf, sizeof(buf), f)))
  203.             {
  204.                 lines.resize(lines.size() + 1);
  205.                 auto& inst = lines.back();
  206.                    
  207.                 if(!ScanConfigLine(instCount, line, instFormat,
  208.                        &inst.id, dummy, &inst.interior,
  209.                        &inst.pos[0].f, &inst.pos[1].f, &inst.pos[2].f,
  210.                        &inst.rot[0].f, &inst.rot[1].f, &inst.rot[2].f, &inst.rot[3].f,
  211.                        &inst.lod))
  212.                 {
  213.                     if(line[0] != 'e') { puts("end fail"); throw 0; }
  214.                     lines.resize(lines.size() - 1);
  215.                     break;
  216.                 }
  217.             }
  218.            
  219.             for(auto& line : lines)
  220.             {
  221.                 if(line.lod >= 0)
  222.                 {
  223.                     lines[line.lod].lod = -2;
  224.                 }
  225.             }
  226.            
  227.             for(auto& line : lines)
  228.             {
  229.                 if(line.lod != -2)
  230.                 {
  231.                     ipl.push_back( SDataIPL_INST() );
  232.                     auto& item = ipl.back();
  233.                     item.obj = line;
  234.                     if(item.has_lod = (line.lod >= 0))
  235.                     {
  236.                         item.lod = lines[line.lod];
  237.                     }
  238.                 }
  239.             }
  240.      
  241.             fclose(f);
  242.             return;
  243.         }
  244.        
  245.         puts("fopen fail");
  246.         throw 0;
  247.     }
  248.        
  249.        
  250.        
  251.        
  252.        
  253.        
  254.        
  255.        
  256.        
  257.        
  258.        
  259.        
  260.        
  261.     #include <algorithm>
  262.        
  263.     int main()
  264.     {
  265.         ReadIPL("new.ipl", newIPL);
  266.         ReadIPL("game.ipl", gameIPL);
  267.         puts("Readen");
  268.        
  269.         if(gameIPL.size() != newIPL.size())
  270.             puts("IPL size wrong");
  271.         else
  272.             printf("sizecmp: %d == %d\n", gameIPL.size(), newIPL.size());
  273.            
  274.         size_t nNotFound = 0;
  275.         for(auto& inst : gameIPL)
  276.         {
  277.             if(std::find(newIPL.begin(), newIPL.end(), inst) == newIPL.end())
  278.             {
  279.                 printf("\n>\n");
  280.                
  281.                 auto print = [](const SDataIPL_INST_OBJECT& inst)
  282.                 {
  283.                     printf("%d dummy %d   %f %f %f   %f %f %f %f   %d\n",
  284.                         inst.id, inst.interior,
  285.                         inst.pos[0].f, inst.pos[1].f, inst.pos[2].f,
  286.                         inst.rot[0].f, inst.rot[1].f, inst.rot[2].f, inst.rot[3].f,
  287.                         inst.lod);
  288.                 };
  289.                        
  290.                 print(inst.obj);
  291.                 if(inst.has_lod) print(inst.lod);
  292.                 printf("<\n");
  293.                    
  294.                 ++nNotFound;
  295.             }
  296.         }
  297.        
  298.         if(nNotFound > 0)
  299.         {
  300.             printf("could not find %d items\n", nNotFound);
  301.         }
  302.         else puts("everything okay");
  303.      
  304.         puts("done");
  305.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement