]> git.lizzy.rs Git - minetest.git/blobdiff - src/utility.h
fine-tuning of map generator and server and stuff.
[minetest.git] / src / utility.h
index 484b5828b12c74c3986bcce33343dca6a0325713..c4f45ba0f1822232379fe617493cd0980352ccd2 100644 (file)
@@ -550,6 +550,41 @@ inline bool isInArea(v2s16 p, s16 d)
        );
 }
 
+inline s16 rangelim(s16 i, s16 min, s16 max)
+{
+       if(i < min)
+               return min;
+       if(i > max)
+               return max;
+       return i;
+}
+
+inline s16 rangelim(s16 i, s16 max)
+{
+       if(i < 0)
+               return 0;
+       if(i > max)
+               return max;
+       return i;
+}
+
+inline v3s16 arealim(v3s16 p, s16 d)
+{
+       if(p.X < 0)
+               p.X = 0;
+       if(p.Y < 0)
+               p.Y = 0;
+       if(p.Z < 0)
+               p.Z = 0;
+       if(p.X > d-1)
+               p.X = d-1;
+       if(p.Y > d-1)
+               p.Y = d-1;
+       if(p.Z > d-1)
+               p.Z = d-1;
+       return p;
+}
+
 inline std::wstring narrow_to_wide(const std::string& mbs)
 {
        size_t wcl = mbs.size();
@@ -595,25 +630,28 @@ inline float wrapDegrees(float f)
        return f;
 }
 
-inline std::string lowercase(std::string s)
+inline std::string lowercase(const std::string &s)
 {
+       std::string s2;
        for(size_t i=0; i<s.size(); i++)
        {
-               if(s[i] >= 'A' && s[i] <= 'Z')
-                       s[i] -= 'A' - 'a';
+               char c = s[i];
+               if(c >= 'A' && c <= 'Z')
+                       c -= 'A' - 'a';
+               s2 += c;
        }
-       return s;
+       return s2;
 }
 
-inline bool is_yes(std::string s)
+inline bool is_yes(const std::string &s)
 {
-       s = lowercase(trim(s));
-       if(s == "y" || s == "yes" || s == "true")
+       std::string s2 = lowercase(trim(s));
+       if(s2 == "y" || s2 == "yes" || s2 == "true")
                return true;
        return false;
 }
 
-inline s32 stoi(std::string s, s32 min, s32 max)
+inline s32 stoi(const std::string &s, s32 min, s32 max)
 {
        s32 i = atoi(s.c_str());
        if(i < min)
@@ -628,6 +666,20 @@ inline s32 stoi(std::string s)
        return atoi(s.c_str());
 }
 
+inline std::string itos(s32 i)
+{
+       std::ostringstream o;
+       o<<i;
+       return o.str();
+}
+
+inline std::string ftos(float f)
+{
+       std::ostringstream o;
+       o<<f;
+       return o.str();
+}
+
 /*
        A base class for simple background thread implementation
 */
@@ -1006,10 +1058,11 @@ class Settings
        // Asks if empty
        bool getBoolAsk(std::string name, std::string question, bool def)
        {
-               std::string s = get(name);
-               if(s != "")
-                       return is_yes(s);
+               // If it is in settings
+               if(m_settings.find(name))
+                       return getBool(name);
                
+               std::string s;
                char templine[10];
                std::cout<<question<<" [y/N]: ";
                std::cin.getline(templine, 10);
@@ -1036,10 +1089,11 @@ class Settings
 
        u16 getU16Ask(std::string name, std::string question, u16 def)
        {
-               std::string s = get(name);
-               if(s != "")
-                       return stoi(s, 0, 65535);
+               // If it is in settings
+               if(m_settings.find(name))
+                       return getU16(name);
                
+               std::string s;
                char templine[10];
                std::cout<<question<<" ["<<def<<"]: ";
                std::cin.getline(templine, 10);
@@ -1061,13 +1115,85 @@ class Settings
                return stoi(get(name));
        }
 
+       void clear()
+       {
+               m_settings.clear();
+               m_defaults.clear();
+       }
+
+       Settings & operator+=(Settings &other)
+       {
+               if(&other == this)
+                       return *this;
+
+               for(core::map<std::string, std::string>::Iterator
+                               i = other.m_settings.getIterator();
+                               i.atEnd() == false; i++)
+               {
+                       m_settings.insert(i.getNode()->getKey(),
+                                       i.getNode()->getValue());
+               }
+               
+               for(core::map<std::string, std::string>::Iterator
+                               i = other.m_defaults.getIterator();
+                               i.atEnd() == false; i++)
+               {
+                       m_defaults.insert(i.getNode()->getKey(),
+                                       i.getNode()->getValue());
+               }
+
+       }
+
+       Settings & operator=(Settings &other)
+       {
+               if(&other == this)
+                       return *this;
+
+               clear();
+               (*this) += other;
+               
+               return *this;
+       }
+
 private:
        core::map<std::string, std::string> m_settings;
        core::map<std::string, std::string> m_defaults;
 };
 
 /*
-       A thread-safe queue
+       FIFO queue
+*/
+template<typename T>
+class Queue
+{
+public:
+       void push_back(T t)
+       {
+               m_list.push_back(t);
+       }
+       
+       T pop_front()
+       {
+               if(m_list.size() == 0)
+                       throw ItemNotFoundException("MutexedQueue: queue is empty");
+
+               typename core::list<T>::Iterator begin = m_list.begin();
+               T t = *begin;
+               m_list.erase(begin);
+               return t;
+       }
+
+       u32 size()
+       {
+               return m_list.size();
+       }
+
+protected:
+       core::list<T> m_list;
+};
+
+/*
+       Thread-safe FIFO queue
 */
 
 template<typename T>
@@ -1251,5 +1377,209 @@ class RequestQueue
        MutexedQueue< GetRequest<Key, T, Caller, CallerData> > m_queue;
 };
 
+/*
+       Pseudo-random (VC++ rand() sucks)
+*/
+int myrand(void);
+void mysrand(unsigned seed);
+#define MYRAND_MAX 32767
+
+/*
+       Some kind of a thing that stores attributes related to
+       coordinate points
+*/
+
+struct Attribute
+{
+       Attribute()
+       {
+       }
+
+       Attribute(const std::string &value):
+               m_value(value)
+       {
+       }
+
+       Attribute(float value)
+       {
+               m_value = ftos(value);
+       }
+
+       void set(const std::string &value)
+       {
+               m_value = value;
+       }
+       
+       std::string get()
+       {
+               return m_value;
+       }
+       
+       bool getBool()
+       {
+               return is_yes(get());
+       }
+       
+       float getFloat()
+       {
+               float f;
+               std::istringstream vis(get());
+               vis>>f;
+               return f;
+       }
+
+       u16 getU16()
+       {
+               return stoi(get(), 0, 65535);
+       }
+
+       s16 getS16()
+       {
+               return stoi(get(), -32768, 32767);
+       }
+
+       s32 getS32()
+       {
+               return stoi(get());
+       }
+
+       std::string m_value;
+};
+
+class PointAttributeList
+{
+       struct PointWithAttr
+       {
+               v3s16 p;
+               Attribute attr;
+       };
+
+public:
+       ~PointAttributeList()
+       {
+               /*for(core::list<PointWithAttr>::Iterator
+                               i = m_points.begin();
+                               i != m_points.end(); i++)
+               {
+                       PointWithAttr &pwa = *i;
+                       //delete pwa.attr;
+               }*/
+       }
+
+       Attribute getNearAttr(v3s16 p)
+       {
+               core::list<PointWithAttr>::Iterator
+                               nearest_i = m_points.end();
+               s16 nearest_d = 32767;
+               for(core::list<PointWithAttr>::Iterator
+                               i = m_points.begin();
+                               i != m_points.end(); i++)
+               {
+                       PointWithAttr &pwa = *i;
+                       s16 d = pwa.p.getDistanceFrom(p);
+                       if(d < nearest_d)
+                       {
+                               nearest_i = i;
+                               nearest_d = d;
+                       }
+               }
+
+               if(nearest_i == m_points.end())
+                       Attribute();
+
+               return nearest_i->attr;
+       }
+       
+       Attribute getNearAttr(v2s16 p)
+       {
+               return getNearAttr(v3s16(p.X, 0, p.Y));
+       }
+
+       bool empty()
+       {
+               return (m_points.size() == 0);
+       }
+       
+       /*
+               Take all points in range, or at least the nearest point,
+               and interpolate the values as floats
+       */
+       float getInterpolatedFloat(v3s16 p);
+       
+       float getInterpolatedFloat(v2s16 p)
+       {
+               return getInterpolatedFloat(v3s16(p.X, 0, p.Y));
+       }
+       
+       //float getInterpolatedFloat(v3s16 p, s32 range);
+       /*float getInterpolatedFloat(v2s16 p, s32 range)
+       {
+               return getInterpolatedFloat(v3s16(p.X, 0, p.Y), range);
+       }*/
+       
+       void addPoint(v3s16 p, const Attribute &attr)
+       {
+               PointWithAttr pattr;
+               pattr.p = p;
+               pattr.attr = attr;
+               m_points.push_back(pattr);
+       }
+
+       void addPoint(v2s16 p, const Attribute &attr)
+       {
+               addPoint(v3s16(p.X, 0, p.Y), attr);
+       }
+
+private:
+       core::list<PointWithAttr> m_points;
+};
+
+/*
+       Basically just a wrapper to core::map<PointAttributeList*>
+*/
+
+class PointAttributeDatabase
+{
+public:
+       ~PointAttributeDatabase()
+       {
+               for(core::map<std::string, PointAttributeList*>::Iterator
+                               i = m_lists.getIterator();
+                               i.atEnd() == false; i++)
+               {
+                       delete i.getNode()->getValue();
+               }
+       }
+
+       PointAttributeList *getList(const std::string &name)
+       {
+               PointAttributeList *list = NULL;
+
+               core::map<std::string, PointAttributeList*>::Node *n;
+               n = m_lists.find(name);
+               
+               if(n == NULL)
+               {
+                       list = new PointAttributeList();
+                       m_lists.insert(name, list);
+               }
+               else
+               {
+                       list = n->getValue();
+               }
+
+               return list;
+       }
+private:
+       core::map<std::string, PointAttributeList*> m_lists;
+};
+
+/*
+       Miscellaneous functions
+*/
+
+bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir, f32 range);
+
+
 #endif