LCOV - code coverage report
Current view: top level - src - object_properties.cpp (source / functions) Hit Total Coverage
Test: report Lines: 33 85 38.8 %
Date: 2015-07-11 18:23:49 Functions: 4 6 66.7 %

          Line data    Source code
       1             : /*
       2             : Minetest
       3             : Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
       4             : 
       5             : This program is free software; you can redistribute it and/or modify
       6             : it under the terms of the GNU Lesser General Public License as published by
       7             : the Free Software Foundation; either version 2.1 of the License, or
       8             : (at your option) any later version.
       9             : 
      10             : This program is distributed in the hope that it will be useful,
      11             : but WITHOUT ANY WARRANTY; without even the implied warranty of
      12             : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13             : GNU Lesser General Public License for more details.
      14             : 
      15             : You should have received a copy of the GNU Lesser General Public License along
      16             : with this program; if not, write to the Free Software Foundation, Inc.,
      17             : 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      18             : */
      19             : 
      20             : #include "object_properties.h"
      21             : #include "irrlichttypes_bloated.h"
      22             : #include "exceptions.h"
      23             : #include "util/serialize.h"
      24             : #include <sstream>
      25             : 
      26             : #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
      27             : #define PP2(x) "("<<(x).X<<","<<(x).Y<<")"
      28             : 
      29         164 : ObjectProperties::ObjectProperties():
      30             :         hp_max(1),
      31             :         physical(false),
      32             :         collideWithObjects(true),
      33             :         weight(5),
      34             :         collisionbox(-0.5,-0.5,-0.5, 0.5,0.5,0.5),
      35             :         visual("sprite"),
      36             :         mesh(""),
      37             :         visual_size(1,1),
      38             :         spritediv(1,1),
      39             :         initial_sprite_basepos(0,0),
      40             :         is_visible(true),
      41             :         makes_footstep_sound(false),
      42             :         automatic_rotate(0),
      43             :         stepheight(0),
      44             :         automatic_face_movement_dir(false),
      45         164 :         automatic_face_movement_dir_offset(0.0)
      46             : {
      47         164 :         textures.push_back("unknown_object.png");
      48         164 :         colors.push_back(video::SColor(255,255,255,255));
      49         164 : }
      50             : 
      51           0 : std::string ObjectProperties::dump()
      52             : {
      53           0 :         std::ostringstream os(std::ios::binary);
      54           0 :         os<<"hp_max="<<hp_max;
      55           0 :         os<<", physical="<<physical;
      56           0 :         os<<", collideWithObjects="<<collideWithObjects;
      57           0 :         os<<", weight="<<weight;
      58           0 :         os<<", collisionbox="<<PP(collisionbox.MinEdge)<<","<<PP(collisionbox.MaxEdge);
      59           0 :         os<<", visual="<<visual;
      60           0 :         os<<", mesh="<<mesh;
      61           0 :         os<<", visual_size="<<PP2(visual_size);
      62           0 :         os<<", textures=[";
      63           0 :         for(u32 i=0; i<textures.size(); i++){
      64           0 :                 os<<"\""<<textures[i]<<"\" ";
      65             :         }
      66           0 :         os<<"]";
      67           0 :         os<<", colors=[";
      68           0 :         for(u32 i=0; i<colors.size(); i++){
      69           0 :                 os<<"\""<<colors[i].getAlpha()<<","<<colors[i].getRed()<<","<<colors[i].getGreen()<<","<<colors[i].getBlue()<<"\" ";
      70             :         }
      71           0 :         os<<"]";
      72           0 :         os<<", spritediv="<<PP2(spritediv);
      73           0 :         os<<", initial_sprite_basepos="<<PP2(initial_sprite_basepos);
      74           0 :         os<<", is_visible="<<is_visible;
      75           0 :         os<<", makes_footstep_sound="<<makes_footstep_sound;
      76           0 :         os<<", automatic_rotate="<<automatic_rotate;
      77           0 :         return os.str();
      78             : }
      79             : 
      80           0 : void ObjectProperties::serialize(std::ostream &os) const
      81             : {
      82           0 :         writeU8(os, 1); // version
      83           0 :         writeS16(os, hp_max);
      84           0 :         writeU8(os, physical);
      85           0 :         writeF1000(os, weight);
      86           0 :         writeV3F1000(os, collisionbox.MinEdge);
      87           0 :         writeV3F1000(os, collisionbox.MaxEdge);
      88           0 :         os<<serializeString(visual);
      89           0 :         writeV2F1000(os, visual_size);
      90           0 :         writeU16(os, textures.size());
      91           0 :         for(u32 i=0; i<textures.size(); i++){
      92           0 :                 os<<serializeString(textures[i]);
      93             :         }
      94           0 :         writeV2S16(os, spritediv);
      95           0 :         writeV2S16(os, initial_sprite_basepos);
      96           0 :         writeU8(os, is_visible);
      97           0 :         writeU8(os, makes_footstep_sound);
      98           0 :         writeF1000(os, automatic_rotate);
      99             :         // Added in protocol version 14
     100           0 :         os<<serializeString(mesh);
     101           0 :         writeU16(os, colors.size());
     102           0 :         for(u32 i=0; i<colors.size(); i++){
     103           0 :                 writeARGB8(os, colors[i]);
     104             :         }
     105           0 :         writeU8(os, collideWithObjects);
     106           0 :         writeF1000(os,stepheight);
     107           0 :         writeU8(os, automatic_face_movement_dir);
     108           0 :         writeF1000(os, automatic_face_movement_dir_offset);
     109             :         // Add stuff only at the bottom.
     110             :         // Never remove anything, because we don't want new versions of this
     111           0 : }
     112             : 
     113         118 : void ObjectProperties::deSerialize(std::istream &is)
     114             : {
     115         118 :         int version = readU8(is);
     116         118 :         if(version == 1)
     117             :         {
     118             :                 try{
     119         118 :                         hp_max = readS16(is);
     120         118 :                         physical = readU8(is);
     121         118 :                         weight = readF1000(is);
     122         118 :                         collisionbox.MinEdge = readV3F1000(is);
     123         118 :                         collisionbox.MaxEdge = readV3F1000(is);
     124         118 :                         visual = deSerializeString(is);
     125         118 :                         visual_size = readV2F1000(is);
     126         118 :                         textures.clear();
     127         118 :                         u32 texture_count = readU16(is);
     128         262 :                         for(u32 i=0; i<texture_count; i++){
     129         144 :                                 textures.push_back(deSerializeString(is));
     130             :                         }
     131         118 :                         spritediv = readV2S16(is);
     132         118 :                         initial_sprite_basepos = readV2S16(is);
     133         118 :                         is_visible = readU8(is);
     134         118 :                         makes_footstep_sound = readU8(is);
     135         118 :                         automatic_rotate = readF1000(is);
     136         118 :                         mesh = deSerializeString(is);
     137         118 :                         u32 color_count = readU16(is);
     138         236 :                         for(u32 i=0; i<color_count; i++){
     139         118 :                                 colors.push_back(readARGB8(is));
     140             :                         }
     141         118 :                         collideWithObjects = readU8(is);
     142         118 :                         stepheight = readF1000(is);
     143         118 :                         automatic_face_movement_dir = readU8(is);
     144         118 :                         automatic_face_movement_dir_offset = readF1000(is);
     145           0 :                 }catch(SerializationError &e){}
     146             :         }
     147             :         else
     148             :         {
     149           0 :                 throw SerializationError("unsupported ObjectProperties version");
     150             :         }
     151         121 : }
     152             : 

Generated by: LCOV version 1.11