X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Futility.cpp;h=7c87b9ae4f06e326279c20ac0d31a030964ba724;hb=c8997e48c070b8fcc460f01ba90896ad3afbf8b5;hp=0721100cb98e845871e033aaf5920fcb9a624a14;hpb=1c90f9fc2eba43be71f9d49dad75c5d0514d4e50;p=oweals%2Fminetest.git diff --git a/src/utility.cpp b/src/utility.cpp index 0721100cb..7c87b9ae4 100644 --- a/src/utility.cpp +++ b/src/utility.cpp @@ -25,6 +25,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "gettime.h" #include "sha1.h" #include "base64.h" +#include "log.h" +#include TimeTaker::TimeTaker(const char *name, u32 *result) { @@ -47,7 +49,7 @@ u32 TimeTaker::stop(bool quiet) else { if(quiet == false) - std::cout< MYRAND_MAX) + { + errorstream<<"WARNING: myrand_range: max-min > MYRAND_MAX"< max) + { + assert(0); + return max; + } + return (myrand()%(max-min+1))+min; +} + /* blockpos: position of block in block coordinates camera_pos: position of camera in nodes camera_dir: an unit vector pointing to camera direction range: viewing range */ -bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir, f32 range, - f32 *distance_ptr) +bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir, + f32 camera_fov, f32 range, f32 *distance_ptr) { v3s16 blockpos_nodes = blockpos_b * MAP_BLOCKSIZE; @@ -191,7 +208,7 @@ bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir, f32 range, return true; // If block is far away, it's not in sight - if(d > range * BS) + if(d > range) return false; // Maximum radius of a block @@ -199,26 +216,119 @@ bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir, f32 range, // If block is (nearly) touching the camera, don't // bother validating further (that is, render it anyway) - if(d > block_max_radius * 1.5) - { - // Cosine of the angle between the camera direction - // and the block direction (camera_dir is an unit vector) - f32 cosangle = dforward / d; - - // Compensate for the size of the block - // (as the block has to be shown even if it's a bit off FOV) - // This is an estimate. - cosangle += block_max_radius / dforward; - - // If block is not in the field of view, skip it - //if(cosangle < cos(FOV_ANGLE/2)) - if(cosangle < cos(FOV_ANGLE/2. * 4./3.)) - return false; - } + if(d < block_max_radius) + return true; + + // Cosine of the angle between the camera direction + // and the block direction (camera_dir is an unit vector) + f32 cosangle = dforward / d; + + // Compensate for the size of the block + // (as the block has to be shown even if it's a bit off FOV) + // This is an estimate, plus an arbitary factor + cosangle += block_max_radius / d * 0.5; + + // If block is not in the field of view, skip it + if(cosangle < cos(camera_fov / 2)) + return false; return true; } +// Creates a string encoded in JSON format (almost equivalent to a C string literal) +std::string serializeJsonString(const std::string &plain) +{ + std::ostringstream os(std::ios::binary); + os<<"\""; + for(size_t i = 0; i < plain.size(); i++) + { + char c = plain[i]; + switch(c) + { + case '"': os<<"\\\""; break; + case '\\': os<<"\\\\"; break; + case '/': os<<"\\/"; break; + case '\b': os<<"\\b"; break; + case '\f': os<<"\\f"; break; + case '\n': os<<"\\n"; break; + case '\r': os<<"\\r"; break; + case '\t': os<<"\\t"; break; + default: + { + if(c >= 32 && c <= 126) + { + os<> c; + if(c != '"') + throw SerializationError("JSON string must start with doublequote"); + + // Parse characters + for(;;) + { + c = is.get(); + if(is.eof()) + throw SerializationError("JSON string ended prematurely"); + if(c == '"') + { + return os.str(); + } + else if(c == '\\') + { + c2 = is.get(); + if(is.eof()) + throw SerializationError("JSON string ended prematurely"); + switch(c2) + { + default: os<> std::hex >> hexnumber; + os<<((char)hexnumber); + break; + } + } + } + else + { + os<