]> git.lizzy.rs Git - minetest.git/blobdiff - src/utility.h
dig_immediate=2 -> 0.5 seconds digging time
[minetest.git] / src / utility.h
index 14b49772bdbdbf4f6e9b83e4ce44831638f866e9..f4c7c30171073a20192fd57b5ea6f8823dbbf4b0 100644 (file)
@@ -1694,6 +1694,12 @@ inline std::string deSerializeLongString(std::istream &is)
        return s;
 }
 
+// Creates a string encoded in JSON format (almost equivalent to a C string literal)
+std::string serializeJsonString(const std::string &plain);
+
+// Reads a string encoded in JSON format
+std::string deSerializeJsonString(std::istream &is);
+
 //
 
 inline u32 time_to_daynight_ratio(u32 time_of_day)
@@ -1751,6 +1757,50 @@ class IntervalLimiter
        float m_accumulator;
 };
 
+/*
+       Splits a list into "pages". For example, the list [1,2,3,4,5] split
+       into two pages would be [1,2,3],[4,5]. This function computes the
+       minimum and maximum indices of a single page.
+
+       length: Length of the list that should be split
+       page: Page number, 1 <= page <= pagecount
+       pagecount: The number of pages, >= 1
+       minindex: Receives the minimum index (inclusive).
+       maxindex: Receives the maximum index (exclusive).
+
+       Ensures 0 <= minindex <= maxindex <= length.
+*/
+inline void paging(u32 length, u32 page, u32 pagecount, u32 &minindex, u32 &maxindex)
+{
+       if(length < 1 || pagecount < 1 || page < 1 || page > pagecount)
+       {
+               // Special cases or invalid parameters
+               minindex = maxindex = 0;
+       }
+       else if(pagecount <= length)
+       {
+               // Less pages than entries in the list:
+               // Each page contains at least one entry
+               minindex = (length * (page-1) + (pagecount-1)) / pagecount;
+               maxindex = (length * page + (pagecount-1)) / pagecount;
+       }
+       else
+       {
+               // More pages than entries in the list:
+               // Make sure the empty pages are at the end
+               if(page < length)
+               {
+                       minindex = page-1;
+                       maxindex = page;
+               }
+               else
+               {
+                       minindex = 0;
+                       maxindex = 0;
+               }
+       }
+}
+
 std::string translatePassword(std::string playername, std::wstring password);
 
 enum PointedThingType