Inexor
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Classes | Macros | Enumerations | Functions | Variables
worldio.cpp File Reference
#include <boost/algorithm/clamp.hpp>
#include <fcntl.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <memory>
#include "SDL_timer.h"
#include "inexor/client/network.hpp"
#include "inexor/engine/blend.hpp"
#include "inexor/engine/lightmap.hpp"
#include "inexor/engine/material.hpp"
#include "inexor/engine/octa.hpp"
#include "inexor/engine/octaedit.hpp"
#include "inexor/engine/octarender.hpp"
#include "inexor/engine/octree.hpp"
#include "inexor/engine/pvs.hpp"
#include "inexor/engine/renderbackground.hpp"
#include "inexor/engine/shader.hpp"
#include "inexor/engine/world.hpp"
#include "inexor/engine/worldio.hpp"
#include "inexor/fpsgame/entities.hpp"
#include "inexor/fpsgame/fps.hpp"
#include "inexor/fpsgame/render.hpp"
#include "inexor/io/Logging.hpp"
#include "inexor/io/filesystem/mediadirs.hpp"
#include "inexor/io/legacy/stream.hpp"
#include "inexor/model/model.hpp"
#include "inexor/model/rendermodel.hpp"
#include "inexor/network/SharedVar.hpp"
#include "inexor/shared/command.hpp"
#include "inexor/shared/cube_endian.hpp"
#include "inexor/shared/cube_formatting.hpp"
#include "inexor/shared/cube_hash.hpp"
#include "inexor/shared/cube_loops.hpp"
#include "inexor/shared/cube_tools.hpp"
#include "inexor/shared/ents.hpp"
#include "inexor/shared/geom.hpp"
#include "inexor/shared/tools.hpp"
#include "inexor/sound/sound.hpp"
#include "inexor/texture/slot.hpp"
#include "inexor/texture/texture.hpp"
#include "inexor/ui/legacy/menus.hpp"
#include "inexor/util/legacy_time.hpp"

Classes

struct  surfacecompat
 surface description More...
 
struct  normalscompat
 normal vector description More...
 
struct  mergecompat
 merged edge description More...
 

Macros

#define GAME_IDENTITY   "fps"
 

Enumerations

enum  {
  OCTSAV_CHILDREN = 0, OCTSAV_EMPTY, OCTSAV_SOLID, OCTSAV_NORMAL,
  OCTSAV_LODCUBE
}
 OCTREE children type enumeration. More...
 

Functions

void cutogz (char *s)
 remove map postfix (.ogz) from file path/name to get map name More...
 
void getmapfilename (const char *fname, const char *realname, char *mapname)
 get the map name from a path/file More...
 
static void fixent (entity &e, int version)
 fix entity attributes according to the program version (the entity format has changed over time) More...
 
bool loadents (const char *fname, vector< entity > &ents, uint *crc)
 load/parse entities from a file More...
 
 VARP (savebak, 0, 2, 2)
 all map files will be backuped as .BAK files when changes will be saved More...
 
void setmapfilenames (const char *fname, const char *cname=nullptr)
 generate file path from file and folder name More...
 
void mapcfgname ()
 generate map configuration file (.cfg) More...
 
 COMMAND (mapcfgname,"")
 
void backup (char *name, char *backupname)
 remove old backup file but keep the file name for the new backup More...
 
void savec (cube *c, const ivec &o, int size, stream *f, bool nolms)
 save OCTREE (and its children) to stream (file) this file calls itself (recursion) because of the OCTREE's structure More...
 
cubeloadchildren (stream *f, const ivec &co, int size, bool &failed)
 forward function "loadchildren" More...
 
void convertoldsurfaces (cube &c, const ivec &co, int size, surfacecompat *srcsurfs, int hassurfs, normalscompat *normals, int hasnorms, mergecompat *merges, int hasmerges)
 convert a surface from a newer map version to a surface of older version (?) More...
 
static int convertoldmaterial (int mat)
 convert a material index from Cube2 to a material index from Cube1 More...
 
void loadc (stream *f, cube &c, const ivec &co, int size, bool &failed)
 load a cube from a stream (file) More...
 
 VAR (dbgvars, 0, 0, 1)
 print map variables to screen More...
 
void savevslot (stream *f, VSlot &vs, int prev)
 
void savevslots (stream *f, int numvslots)
 save vertex slots (vslots) to a (file) stream More...
 
void loadvslot (stream *f, VSlot &vs, int changed)
 load one vertex slot from a (file) stream More...
 
void loadvslots (stream *f, int numvslots)
 load all vertex slots from a (file) stream More...
 
bool save_world (const char *mname, bool nolms)
 save the current game world to a map file (.OGZ) More...
 
void savecurrentmap ()
 save map data to a file automaticly detect map name More...
 
 COMMAND (savecurrentmap,"")
 
void savemap (char *mname)
 save map data to a map file More...
 
 COMMAND (savemap,"s")
 
uint getmapcrc ()
 
void clearmapcrc ()
 clear the CRC32 checksum More...
 
bool load_world (const char *mname, const char *cname)
 
void writeobj (char *name)
 Export/Convert the current octree map, texture coordinates, material information and more to an Object File and a Material Library File. More...
 
 COMMAND (writeobj,"s")
 

Variables

string ogzname
 
string bakname
 
string cfgname
 
string picname
 
static int savemapprogress = 0
 
static uint mapcrc = 0
 CRC32 is a checksum (and error detection) algorithm to generate map checksums so servers can detect modified maps. More...
 

Class Documentation

struct surfacecompat

surface description

Class Members
uchar h
uchar layer
uchar lmid
uchar texcoords[8]
uchar w
ushort x
ushort y
struct normalscompat

normal vector description

Class Members
bvec normals[4]
struct mergecompat

merged edge description

Class Members
ushort u1
ushort u2
ushort v1
ushort v2

Macro Definition Documentation

#define GAME_IDENTITY   "fps"

Referenced by load_world(), loadents(), and save_world().

Enumeration Type Documentation

anonymous enum

OCTREE children type enumeration.

Enumerator
OCTSAV_CHILDREN 
OCTSAV_EMPTY 
OCTSAV_SOLID 
OCTSAV_NORMAL 
OCTSAV_LODCUBE 

Function Documentation

void backup ( char *  name,
char *  backupname 
)

remove old backup file but keep the file name for the new backup

Parameters
namefile name of the new backup
backupnamefile name of the old backup
See Also
save_world

remove old backup file

rename

References copystring(), and findfile().

Referenced by save_world().

void clearmapcrc ( )

clear the CRC32 checksum

References mapcrc.

Referenced by resetmap().

COMMAND ( mapcfgname  ,
""   
)
COMMAND ( savecurrentmap  ,
""   
)
COMMAND ( savemap  ,
"s"   
)
COMMAND ( writeobj  ,
"s"   
)
static int convertoldmaterial ( int  mat)
inlinestatic

convert a material index from Cube2 to a material index from Cube1

Returns
the index in the old material format

weird bit operations

References MATF_CLIP_SHIFT, MATF_FLAG_SHIFT, and MATF_VOLUME_SHIFT.

Referenced by loadc().

void convertoldsurfaces ( cube c,
const ivec co,
int  size,
surfacecompat srcsurfs,
int  hassurfs,
normalscompat normals,
int  hasnorms,
mergecompat merges,
int  hasmerges 
)

convert a surface from a newer map version to a surface of older version (?)

Parameters
ca reference to a cube whose surfaces will be converted
coa reference to an output integer vector [mathematical vector]
size
srcsurfs
hassurfs
normals
hasnorms
merges
hasmerges

each cube consists of 6 faces

a cube consists of 2 vertices per face * 6 faces

References ivec::add(), C, ivec::cross(), hmap::dc, dimension, ivec::dot(), encodenormal(), f(), facecoords, genfaceverts(), surfacecompat::h, ivec::iszero(), surfacecompat::layer, LAYER_BLEND, LAYER_BOTTOM, LAYER_DUP, LAYER_TOP, LM_PACKH, LM_PACKW, surfaceinfo::lmid, surfacecompat::lmid, LMID_RESERVED, loopi(), loopk, ivec::mask(), MAXFACEVERTS, ivec::mul(), vertinfo::norm, normalscompat::normals, sphere::numverts, surfaceinfo::numverts, R, setsurfaces(), vertinfo::setxyz(), ivec::shl(), surfacecompat::texcoords, vertinfo::u, mergecompat::u1, mergecompat::u2, vertinfo::v, mergecompat::v1, mergecompat::v2, vc, sphere::verts, surfaceinfo::verts, surfacecompat::w, surfacecompat::x, and surfacecompat::y.

Referenced by loadc().

void cutogz ( char *  s)

remove map postfix (.ogz) from file path/name to get map name

Referenced by getmapfilename().

static void fixent ( entity e,
int  version 
)
static

fix entity attributes according to the program version (the entity format has changed over time)

Parameters
ea reference to an entity
versionthe Inexor (Sauerbraten / Cube game) version
See Also
load_world
loadents

old sauerbomber maps

References entity::attr1, ET_BOMBS, ET_ENVMAP, ET_MAPMODEL, ET_OBSTACLE, ET_PARTICLES, ET_PLAYERSTART, ET_SOUND, ET_SPOTLIGHT, and entity::type.

Referenced by load_world(), and loadents().

uint getmapcrc ( )

References mapcrc.

Referenced by game::sendmessages().

void getmapfilename ( const char *  fname,
const char *  realname,
char *  mapname 
)

get the map name from a path/file

Parameters
fnameactual map file name
realnamemap display name
mapnamea pointer to where the final map name will be copied (call by reference)

References copystring(), cutogz(), DIR_MAP, inexor::filesystem::getmediapath(), and MAXSTRLEN.

Referenced by ai::getwaypointfile(), loadents(), mapcfgname(), and setmapfilenames().

bool load_world ( const char *  mname,
const char *  cname 
)

References vector< T, MINSIZE >::add(), allchanged(), compatheader::ambient, attachentities(), entity::attr1, entity::attr3, entity::attr4, octaheader::blendmap, compatheader::blendmap, LightMap::bpp, compatheader::bumperror, cfgname, clearmainmenu(), copystring(), LightMap::data, entities::deleteentity(), sphere::ebuf, entitiesinoctanodes(), entities::entname(), entities::ents, ET_GAMESPECIFIC, ET_LIGHT, ET_MAPMODEL, ET_SPOTLIGHT, execfile(), entities::extraentinfosize(), f(), LightMap::finalize(), fixent(), fixlightmapnormals(), fixrotatedlightmaps(), flushpreloadedmodels(), freeocta(), GAME_IDENTITY, stream::getchar(), stream::getcrc(), entities::getents(), getident(), stream::getlil(), ID_FVAR, ID_SVAR, ID_VAR, identflags, IDF_OVERRIDDEN, IDF_OVERRIDE, initlights(), insideworld(), compatheader::lavacolour, compatheader::lerpangle, compatheader::lerpsubdiv, compatheader::lerpsubdivsize, compatheader::lighterror, compatheader::lightlod, octaheader::lightmaps, lightmaps, compatheader::lightprecision, lilswap(), LM_ALPHA, LM_BUMPMAP1, LM_PACKH, LM_PACKW, LM_TYPE, loadblendmap(), loadchildren(), loadpvs(), loadvslots(), Log, loopi(), octaheader::magic, mapcrc, maptitle, compatheader::maptitle, MAPVERSION, mapversion, MAXENTS, MAXSTRLEN, inexor::rpc::min(), entities::newentity(), octaheader::numents, octaheader::numpvs, octaheader::numvars, octaheader::numvslots, entity::o, ogzname, opengzfile(), picname, vector< T, MINSIZE >::pop(), game::preload(), inexor::sound::preloadmapsounds(), preloadusedmapmodels(), stream::read(), entities::readent(), game::readgamedata(), renderbackground(), renderprogress(), resetmap(), stream::seek(), setfvar(), setmapfilenames(), setsvar(), setvar(), vector< T, MINSIZE >::shrink(), compatheader::skylight, startmap(), texmru, textureload(), LightMap::type, entity::type, LightMap::unlitx, LightMap::unlity, validatec(), octaheader::version, compatheader::watercolour, compatheader::waterfallcolour, inexor::util::log_manager::world, worldroot, worldscale, and octaheader::worldsize.

Referenced by game::changemapserv(), and game::receivefile().

void loadc ( stream f,
cube c,
const ivec co,
int  size,
bool &  failed 
)
cube * loadchildren ( stream f,
const ivec co,
int  size,
bool &  failed 
)

forward function "loadchildren"

load all 8 children from a octree cube (from a file stream)

Parameters
f
co
size
failed
f(file) stream
f(file) stream
size?
faileda reference to a bool variable which will be informed about failure or success (the function itself is typeless)
See Also
loadc

References loadc(), loopi(), and newcubes().

Referenced by load_world(), and loadc().

bool loadents ( const char *  fname,
vector< entity > &  ents,
uint crc 
)
void loadvslot ( stream f,
VSlot vs,
int  changed 
)
void loadvslots ( stream f,
int  numvslots 
)

load all vertex slots from a (file) stream

Parameters
f(file) stream
numvslotsthe number of vslots to read

References changed(), stream::getlil(), loadvslot(), loopi(), loopv, and vslots.

Referenced by load_world().

void mapcfgname ( )

generate map configuration file (.cfg)

See Also
getmapfilename

References cfgname, defformatstring, game::getclientmap(), getmapfilename(), mapname(), path(), and result().

bool save_world ( const char *  mname,
bool  nolms 
)

save the current game world to a map file (.OGZ)

Parameters
mnamemap name
nolmsenable or disable lightmap loading
Warning
map stream will be compressed using GZIP automaticly!

validate map name

eventually save backup file

open output stream

get light map data

render savemap progress background

write map header

"magic number"

map version

size of header structure

size of the game world

set amount of entities to 0

ernumerate and count entities

PVS (potentially visible set) tree

ligthmaps

blendmap

enumerate and count idents which represent map variables (settings like "fog"...)

write header to map file

write map variables/settings from ident list

TODO: extend map format here...(?)

save texture IDs in map file

save "extra entities" ?

vertex shader slots?

save octree structure and display another progress bar menawhile

done

References allchanged(), entity::attr1, backup(), bakname, octaheader::blendmap, LightMap::bpp, compactvslots(), LightMap::data, sphere::ebuf, inexor::util::log_manager::edit, entities::ents, enumerate, ET_EMPTY, entities::extraentinfosize(), f(), hmap::flags, GAME_IDENTITY, game::getclientmap(), entities::getents(), getnumviewcells(), octaheader::headersize, ID_FVAR, ID_SVAR, ID_VAR, idents, IDF_OVERRIDDEN, IDF_OVERRIDE, IDF_READONLY, vector< T, MINSIZE >::length(), octaheader::lightmaps, lightmaps, lilswap(), LM_PACKH, LM_PACKW, Log, loopv, octaheader::magic, MAPVERSION, multiplayer(), octaheader::numents, octaheader::numpvs, octaheader::numvars, octaheader::numvslots, entity::o, ogzname, opengzfile(), stream::putchar(), stream::putlil(), renderprogress(), saveblendmap(), savec(), savemapprogress, savepvs(), savevslots(), setmapfilenames(), shouldsaveblendmap(), texmru, LightMap::type, LightMap::unlitx, LightMap::unlity, octaheader::version, vslots, inexor::util::log_manager::world, worldroot, octaheader::worldsize, worldsize, stream::write(), entities::writeent(), and game::writegamedata().

Referenced by savecurrentmap(), savemap(), and game::sendmap().

void savec ( cube c,
const ivec o,
int  size,
stream f,
bool  nolms 
)

save OCTREE (and its children) to stream (file) this file calls itself (recursion) because of the OCTREE's structure

Parameters
cthe cube (or child of a parent's cube) which contains the OCTREE data
oa reference to an integer vector [mathematic vector]
sizethe size of the stream
fthe stream to which data will be written
nolmssave without lightmaps
See Also

render progress bar in the background

save children (recursion!)

lightmaps will be saved

texture coordinates

material type

surface information

References allocnodes, C, dimension, ext(), cube::ext, faceconvexity(), vertinfo::getxyz(), isempty, isentirelysolid, LAYER_DUP, loopi(), loopj, loopk, MAT_AIR, cube::material, MAXFACEVERTS, vertinfo::norm, sphere::numverts, surfaceinfo::numverts, OCTSAV_CHILDREN, OCTSAV_EMPTY, OCTSAV_NORMAL, OCTSAV_SOLID, stream::putchar(), stream::putlil(), R, renderprogress(), savemapprogress, cubeext::surfaces, cube::texture, surfaceinfo::totalverts(), vertinfo::u, surfaceinfo::used(), vertinfo::v, vc, sphere::verts, surfaceinfo::verts, cubeext::verts(), visibletris(), and stream::write().

Referenced by save_world().

void savecurrentmap ( )

save map data to a file automaticly detect map name

References game::getclientmap(), and save_world().

void savemap ( char *  mname)

save map data to a map file

Parameters
mnamemap name

References save_world().

void savevslot ( stream f,
VSlot vs,
int  prev 
)
void savevslots ( stream f,
int  numvslots 
)

save vertex slots (vslots) to a (file) stream

Parameters
f(file) stream
numvslotsthe number of vslots which will be written to the file stream

References VSlot::changed, cur, VSlot::index, loopi(), VSlot::next, stream::putlil(), savevslot(), and vslots.

Referenced by save_world().

void setmapfilenames ( const char *  fname,
const char *  cname = nullptr 
)

generate file path from file and folder name

Parameters
cnamefoldername
fnamefile name (if not specified: "untitled")
See Also
load_world
save_world

References bakname, cfgname, formatstring(), getmapfilename(), mapname(), ogzname, path(), picname, and totalmillis.

Referenced by load_world(), and save_world().

VAR ( dbgvars  ,
,
,
 
)

print map variables to screen

VARP ( savebak  ,
,
,
 
)

all map files will be backuped as .BAK files when changes will be saved

void writeobj ( char *  name)

Export/Convert the current octree map, texture coordinates, material information and more to an Object File and a Material Library File.

Parameters
namethe .OBJ file name

print a small comment to file

link reference to material library

extract geometric data from OCTREE

add center vector to vertices and write them to file

write texture coordinates to file

write materials to file

write material library file

add a little comment line

print abstract material descriptions to file

References hashbase< hashtable< K, T >, hashtableentry< K, T >, K, T >::access(), vector< T, MINSIZE >::add(), vec::add(), defformatstring, vtxarray::eslist, f(), vector< T, MINSIZE >::find(), elementset::length, vector< T, MINSIZE >::length(), lookupvslot(), loopj, loopk, loopl, loopv, inexor::rpc::max(), inexor::rpc::min(), Texture::name, notexture, openfile(), path(), vertex::pos, stream::printf(), readva(), VSlot::slot, vector< T, MINSIZE >::sort(), Slot::sts, vertex::tc, vtxarray::texs, elementset::texture, valist, sphere::verts, and vtxarray::voffset.

Variable Documentation

string bakname

Referenced by save_world(), and setmapfilenames().

string cfgname
uint mapcrc = 0
static

CRC32 is a checksum (and error detection) algorithm to generate map checksums so servers can detect modified maps.

Warning
use getmapcrc() and clearmapcrc() to access/clear the checksum do NOT directly access it!

Referenced by clearmapcrc(), getmapcrc(), and load_world().

string ogzname
string picname

Referenced by load_world(), and setmapfilenames().

int savemapprogress = 0
static

Referenced by save_world(), and savec().