/* Minetest Copyright (C) 2010-2013 celeron55, Perttu Ahola This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "mapblock_mesh.h" #include "light.h" #include "mapblock.h" #include "map.h" #include "main.h" // for g_profiler #include "profiler.h" #include "nodedef.h" #include "gamedef.h" #include "mesh.h" #include "content_mapblock.h" #include "noise.h" #include "shader.h" #include "settings.h" #include "util/directiontables.h" float srgb_linear_multiply(float f, float m, float max) { f = f * f; // SRGB -> Linear f *= m; f = sqrt(f); // Linear -> SRGB if(f > max) f = max; return f; } /* MeshMakeData */ MeshMakeData::MeshMakeData(IGameDef *gamedef): m_vmanip(), m_blockpos(-1337,-1337,-1337), m_crack_pos_relative(-1337, -1337, -1337), m_smooth_lighting(false), m_gamedef(gamedef) {} void MeshMakeData::fill(MapBlock *block) { m_blockpos = block->getPos(); v3s16 blockpos_nodes = m_blockpos*MAP_BLOCKSIZE; /* Copy data */ // Allocate this block + neighbors m_vmanip.clear(); m_vmanip.addArea(VoxelArea(blockpos_nodes-v3s16(1,1,1)*MAP_BLOCKSIZE, blockpos_nodes+v3s16(1,1,1)*MAP_BLOCKSIZE*2-v3s16(1,1,1))); { //TimeTaker timer("copy central block data"); // 0ms // Copy our data block->copyTo(m_vmanip); } { //TimeTaker timer("copy neighbor block data"); // 0ms /* Copy neighbors. This is lightning fast. Copying only the borders would be *very* slow. */ // Get map Map *map = block->getParent(); for(u16 i=0; i<26; i++) { const v3s16 &dir = g_26dirs[i]; v3s16 bp = m_blockpos + dir; MapBlock *b = map->getBlockNoCreateNoEx(bp); if(b) b->copyTo(m_vmanip); } } } void MeshMakeData::fillSingleNode(MapNode *node) { m_blockpos = v3s16(0,0,0); v3s16 blockpos_nodes = v3s16(0,0,0); VoxelArea area(blockpos_nodes-v3s16(1,1,1)*MAP_BLOCKSIZE, blockpos_nodes+v3s16(1,1,1)*MAP_BLOCKSIZE*2-v3s16(1,1,1)); s32 volume = area.getVolume(); s32 our_node_index = area.index(1,1,1); // Allocate this block + neighbors m_vmanip.clear(); m_vmanip.addArea(area); // Fill in data MapNode *data = new MapNode[volume]; for(s32 i = 0; i < volume; i++) { if(i == our_node_index) { data[i] = *node; } else { data[i] = MapNode(CONTENT_AIR, LIGHT_MAX, 0); } } m_vmanip.copyFrom(data, area, area.MinEdge, area.MinEdge, area.getExtent()); delete[] data; } void MeshMakeData::setCrack(int crack_level, v3s16 crack_pos) { if(crack_level >= 0) m_crack_pos_relative = crack_pos - m_blockpos*MAP_BLOCKSIZE; } void MeshMakeData::setSmoothLighting(bool smooth_lighting) { m_smooth_lighting = smooth_lighting; } /* Light and vertex color functions */ /* Calculate non-smooth lighting at interior of node. Single light bank. */ static u8 getInteriorLight(enum LightBank bank, MapNode n, s32 increment, MeshMakeData *data) { INodeDefManager *ndef = data->m_gamedef->ndef(); u8 light = n.getLight(bank, ndef); while(increment > 0) { light = undiminish_light(light); --increment; } while(increment < 0) { light = diminish_light(light); ++increment; } return decode_light(light); } /* Calculate non-smooth lighting at interior of node. Both light banks. */ u16 getInteriorLight(MapNode n, s32 increment, MeshMakeData *data) { u16 day = getInteriorLight(LIGHTBANK_DAY, n, increment, data); u16 night = getInteriorLight(LIGHTBANK_NIGHT, n, increment, data); return day | (night << 8); } /* Calculate non-smooth lighting at face of node. Single light bank. */ static u8 getFaceLight(enum LightBank bank, MapNode n, MapNode n2, v3s16 face_dir, MeshMakeData *data) { INodeDefManager *ndef = data->m_gamedef->ndef(); u8 light; u8 l1 = n.getLight(bank, ndef); u8 l2 = n2.getLight(bank, ndef); if(l1 > l2) light = l1; else light = l2; // Boost light level for light sources u8 light_source = MYMAX(ndef->get(n).light_source, ndef->get(n2).light_source); //if(light_source >= light) //return decode_light(undiminish_light(light_source)); if(light_source > light) //return decode_light(light_source); light = light_source; // Make some nice difference to different sides // This makes light come from a corner /*if(face_dir.X == 1 || face_dir.Z == 1 || face_dir.Y == -1) light = diminish_light(diminish_light(light)); else if(face_dir.X == -1 || face_dir.Z == -1) light = diminish_light(light);*/ // All neighboring faces have different shade (like in minecraft) if(face_dir.X == 1 || face_dir.X == -1 || face_dir.Y == -1) light = diminish_light(diminish_light(light)); else if(face_dir.Z == 1 || face_dir.Z == -1) light = diminish_light(light); return decode_light(light); } /* Calculate non-smooth lighting at face of node. Both light banks. */ u16 getFaceLight(MapNode n, MapNode n2, v3s16 face_dir, MeshMakeData *data) { u16 day = getFaceLight(LIGHTBANK_DAY, n, n2, face_dir, data); u16 night = getFaceLight(LIGHTBANK_NIGHT, n, n2, face_dir, data); return day | (night << 8); } /* Calculate smooth lighting at the XYZ- corner of p. Single light bank. */ static u8 getSmoothLight(enum LightBank bank, v3s16 p, MeshMakeData *data) { static v3s16 dirs8[8] = { v3s16(0,0,0), v3s16(0,0,1), v3s16(0,1,0), v3s16(0,1,1), v3s16(1,0,0), v3s16(1,1,0), v3s16(1,0,1), v3s16(1,1,1), }; INodeDefManager *ndef = data->m_gamedef->ndef(); u16 ambient_occlusion = 0; u16 light = 0; u16 light_count = 0; u8 light_source_max = 0; for(u32 i=0; i<8; i++) { MapNode n = data->m_vmanip.getNodeNoEx(p - dirs8[i]); const ContentFeatures &f = ndef->get(n); if(f.light_source > light_source_max) light_source_max = f.light_source; // Check f.solidness because fast-style leaves look // better this way if(f.param_type == CPT_LIGHT && f.solidness != 2) { light += decode_light(n.getLight(bank, ndef)); light_count++; } else if(n.getContent() != CONTENT_IGNORE) { ambient_occlusion++; } } if(light_count == 0) return 255; light /= light_count; // Boost brightness around light sources if(decode_light(light_source_max) >= light) //return decode_light(undiminish_light(light_source_max)); return decode_light(light_source_max); if(ambient_occlusion > 4) { //ambient_occlusion -= 4; //light = (float)light / ((float)ambient_occlusion * 0.5 + 1.0); float light_amount = (8 - ambient_occlusion) / 4.0; float light_f = (float)light / 255.0; light_f = pow(light_f, 2.2f); // gamma -> linear space light_f = light_f * light_amount; light_f = pow(light_f, 1.0f/2.2f); // linear -> gamma space if(light_f > 1.0) light_f = 1.0; light = 255.0 * light_f + 0.5; } return light; } /* Calculate smooth lighting at the XYZ- corner of p. Both light banks. */ static u16 getSmoothLight(v3s16 p, MeshMakeData *data) { u16 day = getSmoothLight(LIGHTBANK_DAY, p, data); u16 night = getSmoothLight(LIGHTBANK_NIGHT, p, data); return day | (night << 8); } /* Calculate smooth lighting at the given corner of p. Both light banks. */ u16 getSmoothLight(v3s16 p, v3s16 corner, MeshMakeData *data) { if(corner.X == 1) p.X += 1; else assert(corner.X == -1); if(corner.Y == 1) p.Y += 1; else assert(corner.Y == -1); if(corner.Z == 1) p.Z += 1; else assert(corner.Z == -1); return getSmoothLight(p, data); } /* Converts from day + night color values (0..255) and a given daynight_ratio to the final SColor shown on screen. */ static void finalColorBlend(video::SColor& result, u8 day, u8 night, u32 daynight_ratio) { s32 rg = (day * daynight_ratio + night * (1000-daynight_ratio)) / 1000; s32 b = rg; // Moonlight is blue b += (day - night) / 13; rg -= (day - night) / 23; // Emphase blue a bit in darker places // Each entry of this array represents a range of 8 blue levels static u8 emphase_blue_when_dark[32] = { 1, 4, 6, 6, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; if(b < 0) b = 0; if(b > 255) b = 255; b += emphase_blue_when_dark[b / 8]; // Artificial light is yellow-ish static u8 emphase_yellow_when_artificial[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 10, 15, 15, 15 }; rg += emphase_yellow_when_artificial[night/16]; if(rg < 0) rg = 0; if(rg > 255) rg = 255; result.setRed(rg); result.setGreen(rg); result.setBlue(b); } /* Mesh generation helpers */ /* vertex_dirs: v3s16[4] */ static void getNodeVertexDirs(v3s16 dir, v3s16 *vertex_dirs) { /* If looked from outside the node towards the face, the corners are: 0: bottom-right 1: bottom-left 2: top-left 3: top-right */ if(dir == v3s16(0,0,1)) { // If looking towards z+, this is the face that is behind // the center point, facing towards z+. vertex_dirs[0] = v3s16(-1,-1, 1); vertex_dirs[1] = v3s16( 1,-1, 1); vertex_dirs[2] = v3s16( 1, 1, 1); vertex_dirs[3] = v3s16(-1, 1, 1); } else if(dir == v3s16(0,0,-1)) { // faces towards Z- vertex_dirs[0] = v3s16( 1,-1,-1); vertex_dirs[1] = v3s16(-1,-1,-1); vertex_dirs[2] = v3s16(-1, 1,-1); vertex_dirs[3] = v3s16( 1, 1,-1); } else if(dir == v3s16(1,0,0)) { // faces towards X+ vertex_dirs[0] = v3s16( 1,-1, 1); vertex_dirs[1] = v3s16( 1,-1,-1); vertex_dirs[2] = v3s16( 1, 1,-1); vertex_dirs[3] = v3s16( 1, 1, 1); } else if(dir == v3s16(-1,0,0)) { // faces towards X- vertex_dirs[0] = v3s16(-1,-1,-1); vertex_dirs[1] = v3s16(-1,-1, 1); vertex_dirs[2] = v3s16(-1, 1, 1); vertex_dirs[3] = v3s16(-1, 1,-1); } else if(dir == v3s16(0,1,0)) { // faces towards Y+ (assume Z- as "down" in texture) vertex_dirs[0] = v3s16( 1, 1,-1); vertex_dirs[1] = v3s16(-1, 1,-1); vertex_dirs[2] = v3s16(-1, 1, 1); vertex_dirs[3] = v3s16( 1, 1, 1); } else if(dir == v3s16(0,-1,0)) { // faces towards Y- (assume Z+ as "down" in texture) vertex_dirs[0] = v3s16( 1,-1, 1); vertex_dirs[1] = v3s16(-1,-1, 1); vertex_dirs[2] = v3s16(-1,-1,-1); vertex_dirs[3] = v3s16( 1,-1,-1); } } struct FastFace { TileSpec tile; video::S3DVertex vertices[4]; // Precalculated vertices }; static void makeFastFace(TileSpec tile, u16 li0, u16 li1, u16 li2, u16 li3, v3f p, v3s16 dir, v3f scale, u8 light_source, std::vector &dest) { FastFace face; // Position is at the center of the cube. v3f pos = p * BS; float x0 = 0.0; float y0 = 0.0; float w = 1.0; float h = 1.0; v3f vertex_pos[4]; v3s16 vertex_dirs[4]; getNodeVertexDirs(dir, vertex_dirs); v3s16 t; u16 t1; switch (tile.rotation) { case 0: break; case 1: //R90 t = vertex_dirs[0]; vertex_dirs[0] = vertex_dirs[3]; vertex_dirs[3] = vertex_dirs[2]; vertex_dirs[2] = vertex_dirs[1]; vertex_dirs[1] = t; t1=li0; li0=li3; li3=li2; li2=li1; li1=t1; break; case 2: //R180 t = vertex_dirs[0]; vertex_dirs[0] = vertex_dirs[2]; vertex_dirs[2] = t; #!/usr/bin/perl =info install: cpan JSON JSON::XS touch list_full list chmod a+rw list_full list freebsd: www/fcgiwrap www/nginx rc.conf.local: nginx_enable="YES" fcgiwrap_enable="YES" fcgiwrap_user="www" nginx: location / { index index.html; } location /announce { fastcgi_pass unix:/var/run/fcgiwrap/fcgiwrap.sock; fastcgi_param SCRIPT_FILENAME $document_root/master.cgi; include fastcgi_params; } apache .htaccess: AddHandler cgi-script .cgi DirectoryIndex index.html Options +ExecCGI +FollowSymLinks Order allow,deny <FilesMatch (\.(html?|cgi|fcgi|css|js|gif|png|jpe?g|ico)|(^)|\w+)$> Allow from all </FilesMatch> Deny from all =cut use strict; no strict qw(refs); use warnings "NONFATAL" => "all"; no warnings qw(uninitialized); use utf8; use Socket; use Time::HiRes qw(time sleep); use IO::Socket::INET; use JSON; use Net::Ping; our $root_path; ($ENV{'SCRIPT_FILENAME'} || $0) =~ m|^(.+)[/\\].+?$|; #v0w $root_path = $1 . '/' if $1; $root_path =~ s|\\|/|g; our %config = ( #debug => 1, list_full => $root_path . 'list_full', list_pub => $root_path . 'list', time_purge => 86400 * 30, time_alive => 650, source_check => 1, ping_timeout => 3, ping => 1, mineping => 1, pingable => 1, trusted => [qw( 176.9.122.10 )], #masterserver self ip - if server on same ip with masterserver doesnt announced #blacklist => [], # [qw(2.3.4.5 4.5.6.7 8.9.0.1), '1.2.3.4', qr/^10\.20\.30\./, ], # list, or quoted, ips, or regex ); do($root_path . 'config.pl'); our $ping = Net::Ping->new("udp", $config{ping_timeout}); $ping->hires(); sub get_params_one(@) { local %_ = %{ref $_[0] eq 'HASH' ? shift : {}}; for (@_) { tr/+/ /, s/%([a-f\d]{2})/pack 'H*', $1/gei for my ($k, $v) = /^([^=]+=?)=(.+)$/ ? ($1, $2) : (/^([^=]*)=?$/, /^-/); $_{$k} = $v; } wantarray ? %_ : \%_; } sub get_params(;$$) { #v7 my ($string, $delim) = @_; $delim ||= '&'; read(STDIN, local $_ = '', $ENV{'CONTENT_LENGTH'}) if !$string and $ENV{'CONTENT_LENGTH'}; local %_ = $string ? get_params_one split $delim, $string : (get_params_one(@ARGV), map { get_params_one split $delim, $_ } split(/;\s*/, $ENV{'HTTP_COOKIE'}), $ENV{'QUERY_STRING'}, $_); wantarray ? %_ : \%_; } sub get_params_utf8(;$$) { local $_ = &get_params; utf8::decode $_ for %$_; wantarray ? %$_ : $_; } sub file_rewrite(;$@) { local $_ = shift; return unless open my $fh, '>', $_; print $fh @_; } sub file_read ($) { open my $f, '<', $_[0] or return; local $/ = undef; my $ret = <$f>; close $f; return \$ret; } sub read_json { my $ret = {}; eval { $ret = JSON->new->utf8->relaxed(1)->decode(${ref $_[0] ? $_[0] : file_read($_[0]) or \''} || '{}'); }; #'mc warn "json error [$@] on [", ${ref $_[0] ? $_[0] : \$_[0]}, "]" if $@; $ret; } sub printu (@) { for (@_) { print($_), next unless utf8::is_utf8($_); my $s = $_; utf8::encode($s); print($s); } } sub name_to_ip_noc($) { my ($name) = @_; unless ($name =~ /^\d+\.\d+\.\d+\.\d+$/) { local $_ = (gethostbyname($name))[4]; return ($name, 1) unless length($_) == 4; $name = inet_ntoa($_); } return $name; } sub float { return ($_[0] < 8 and $_[0] - int($_[0])) ? sprintf('%.' . ($_[0] < 1 ? 3 : ($_[0] < 3 ? 2 : 1)) . 'f', $_[0]) : int($_[0]); } sub mineping ($$) { my ($addr, $port) = @_; warn "mineping($addr, $port)" if $config{debug}; my $data; my $time = time; eval { my $socket = IO::Socket::INET->new( 'PeerAddr' => $addr, 'PeerPort' => $port, 'Proto' => 'udp', 'Timeout' => $config{ping_timeout}, ); $socket->send("\x4f\x45\x74\x03\x00\x00\x00\x01"); local $SIG{ALRM} = sub { die "alarm time out"; }; alarm $config{ping_timeout}; $socket->recv($data, POSIX::BUFSIZ) or die "recv: $!"; alarm 0; 1; # return value from eval on normalcy } or return 0; return 0 unless length $data; $time = float(time - $time); warn "recvd: ", length $data, " [$time]" if $config{debug}; return $time; } sub request (;$) { my ($r) = @_; $r ||= \%ENV; my $param = get_params_utf8; my $after = sub { if ($param->{json}) { my $j = {}; eval { $j = JSON->new->decode($param->{json}) || {} }; $param->{$_} = $j->{$_} for keys %$j