]> git.lizzy.rs Git - dragonfireclient.git/blob - src/utility.h
Remove stuff made obsolete by making players more ActiveObject-like and raise protoco...
[dragonfireclient.git] / src / utility.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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 #ifndef UTILITY_HEADER
21 #define UTILITY_HEADER
22
23 #include <iostream>
24 #include <fstream>
25 #include <string>
26 #include <sstream>
27 #include <vector>
28 #include <jthread.h>
29 #include <jmutex.h>
30 #include <jmutexautolock.h>
31 #include <cstring>
32
33 #include "common_irrlicht.h"
34 #include "debug.h"
35 #include "exceptions.h"
36 #include "porting.h"
37 #include "strfnd.h" // For trim()
38
39 extern const v3s16 g_6dirs[6];
40
41 extern const v3s16 g_26dirs[26];
42
43 // 26th is (0,0,0)
44 extern const v3s16 g_27dirs[27];
45
46 inline void writeU64(u8 *data, u64 i)
47 {
48         data[0] = ((i>>56)&0xff);
49         data[1] = ((i>>48)&0xff);
50         data[2] = ((i>>40)&0xff);
51         data[3] = ((i>>32)&0xff);
52         data[4] = ((i>>24)&0xff);
53         data[5] = ((i>>16)&0xff);
54         data[6] = ((i>> 8)&0xff);
55         data[7] = ((i>> 0)&0xff);
56 }
57
58 inline void writeU32(u8 *data, u32 i)
59 {
60         data[0] = ((i>>24)&0xff);
61         data[1] = ((i>>16)&0xff);
62         data[2] = ((i>> 8)&0xff);
63         data[3] = ((i>> 0)&0xff);
64 }
65
66 inline void writeU16(u8 *data, u16 i)
67 {
68         data[0] = ((i>> 8)&0xff);
69         data[1] = ((i>> 0)&0xff);
70 }
71
72 inline void writeU8(u8 *data, u8 i)
73 {
74         data[0] = ((i>> 0)&0xff);
75 }
76
77 inline u64 readU64(u8 *data)
78 {
79         return ((u64)data[0]<<56) | ((u64)data[1]<<48)
80                 | ((u64)data[2]<<40) | ((u64)data[3]<<32)
81                 | ((u64)data[4]<<24) | ((u64)data[5]<<16)
82                 | ((u64)data[6]<<8) | ((u64)data[7]<<0);
83 }
84
85 inline u32 readU32(u8 *data)
86 {
87         return (data[0]<<24) | (data[1]<<16) | (data[2]<<8) | (data[3]<<0);
88 }
89
90 inline u16 readU16(u8 *data)
91 {
92         return (data[0]<<8) | (data[1]<<0);
93 }
94
95 inline u8 readU8(u8 *data)
96 {
97         return (data[0]<<0);
98 }
99
100 inline void writeS32(u8 *data, s32 i){
101         writeU32(data, (u32)i);
102 }
103 inline s32 readS32(u8 *data){
104         return (s32)readU32(data);
105 }
106
107 inline void writeS16(u8 *data, s16 i){
108         writeU16(data, (u16)i);
109 }
110 inline s16 readS16(u8 *data){
111         return (s16)readU16(data);
112 }
113
114 inline void writeS8(u8 *data, s8 i){
115         writeU8(data, (u8)i);
116 }
117 inline s8 readS8(u8 *data){
118         return (s8)readU8(data);
119 }
120
121 inline void writeF1000(u8 *data, f32 i){
122         writeS32(data, i*1000);
123 }
124 inline f32 readF1000(u8 *data){
125         return (f32)readS32(data)/1000.;
126 }
127
128 inline void writeV3S32(u8 *data, v3s32 p)
129 {
130         writeS32(&data[0], p.X);
131         writeS32(&data[4], p.Y);
132         writeS32(&data[8], p.Z);
133 }
134 inline v3s32 readV3S32(u8 *data)
135 {
136         v3s32 p;
137         p.X = readS32(&data[0]);
138         p.Y = readS32(&data[4]);
139         p.Z = readS32(&data[8]);
140         return p;
141 }
142
143 inline void writeV3F1000(u8 *data, v3f p)
144 {
145         writeF1000(&data[0], p.X);
146         writeF1000(&data[4], p.Y);
147         writeF1000(&data[8], p.Z);
148 }
149 inline v3f readV3F1000(u8 *data)
150 {
151         v3f p;
152         p.X = (float)readF1000(&data[0]);
153         p.Y = (float)readF1000(&data[4]);
154         p.Z = (float)readF1000(&data[8]);
155         return p;
156 }
157
158 inline void writeV2F1000(u8 *data, v2f p)
159 {
160         writeF1000(&data[0], p.X);
161         writeF1000(&data[4], p.Y);
162 }
163 inline v2f readV2F1000(u8 *data)
164 {
165         v2f p;
166         p.X = (float)readF1000(&data[0]);
167         p.Y = (float)readF1000(&data[4]);
168         return p;
169 }
170
171 inline void writeV2S16(u8 *data, v2s16 p)
172 {
173         writeS16(&data[0], p.X);
174         writeS16(&data[2], p.Y);
175 }
176
177 inline v2s16 readV2S16(u8 *data)
178 {
179         v2s16 p;
180         p.X = readS16(&data[0]);
181         p.Y = readS16(&data[2]);
182         return p;
183 }
184
185 inline void writeV2S32(u8 *data, v2s32 p)
186 {
187         writeS32(&data[0], p.X);
188         writeS32(&data[2], p.Y);
189 }
190
191 inline v2s32 readV2S32(u8 *data)
192 {
193         v2s32 p;
194         p.X = readS32(&data[0]);
195         p.Y = readS32(&data[2]);
196         return p;
197 }
198
199 inline void writeV3S16(u8 *data, v3s16 p)
200 {
201         writeS16(&data[0], p.X);
202         writeS16(&data[2], p.Y);
203         writeS16(&data[4], p.Z);
204 }
205
206 inline v3s16 readV3S16(u8 *data)
207 {
208         v3s16 p;
209         p.X = readS16(&data[0]);
210         p.Y = readS16(&data[2]);
211         p.Z = readS16(&data[4]);
212         return p;
213 }
214
215 /*
216         The above stuff directly interfaced to iostream
217 */
218
219 inline void writeU8(std::ostream &os, u8 p)
220 {
221         char buf[1];
222         writeU8((u8*)buf, p);
223         os.write(buf, 1);
224 }
225 inline u8 readU8(std::istream &is)
226 {
227         char buf[1];
228         is.read(buf, 1);
229         return readU8((u8*)buf);
230 }
231
232 inline void writeU16(std::ostream &os, u16 p)
233 {
234         char buf[2];
235         writeU16((u8*)buf, p);
236         os.write(buf, 2);
237 }
238 inline u16 readU16(std::istream &is)
239 {
240         char buf[2];
241         is.read(buf, 2);
242         return readU16((u8*)buf);
243 }
244
245 inline void writeU32(std::ostream &os, u32 p)
246 {
247         char buf[4];
248         writeU32((u8*)buf, p);
249         os.write(buf, 4);
250 }
251 inline u32 readU32(std::istream &is)
252 {
253         char buf[4];
254         is.read(buf, 4);
255         return readU32((u8*)buf);
256 }
257
258 inline void writeS32(std::ostream &os, s32 p)
259 {
260         char buf[4];
261         writeS32((u8*)buf, p);
262         os.write(buf, 4);
263 }
264 inline s32 readS32(std::istream &is)
265 {
266         char buf[4];
267         is.read(buf, 4);
268         return readS32((u8*)buf);
269 }
270
271 inline void writeS16(std::ostream &os, s16 p)
272 {
273         char buf[2];
274         writeS16((u8*)buf, p);
275         os.write(buf, 2);
276 }
277 inline s16 readS16(std::istream &is)
278 {
279         char buf[2];
280         is.read(buf, 2);
281         return readS16((u8*)buf);
282 }
283
284 inline void writeS8(std::ostream &os, s8 p)
285 {
286         char buf[1];
287         writeS8((u8*)buf, p);
288         os.write(buf, 1);
289 }
290 inline s8 readS8(std::istream &is)
291 {
292         char buf[1];
293         is.read(buf, 1);
294         return readS8((u8*)buf);
295 }
296
297 inline void writeF1000(std::ostream &os, f32 p)
298 {
299         char buf[4];
300         writeF1000((u8*)buf, p);
301         os.write(buf, 4);
302 }
303 inline f32 readF1000(std::istream &is)
304 {
305         char buf[4];
306         is.read(buf, 4);
307         return readF1000((u8*)buf);
308 }
309
310 inline void writeV3F1000(std::ostream &os, v3f p)
311 {
312         char buf[12];
313         writeV3F1000((u8*)buf, p);
314         os.write(buf, 12);
315 }
316 inline v3f readV3F1000(std::istream &is)
317 {
318         char buf[12];
319         is.read(buf, 12);
320         return readV3F1000((u8*)buf);
321 }
322
323 inline void writeV2F1000(std::ostream &os, v2f p)
324 {
325         char buf[8];
326         writeV2F1000((u8*)buf, p);
327         os.write(buf, 8);
328 }
329 inline v2f readV2F1000(std::istream &is)
330 {
331         char buf[8];
332         is.read(buf, 8);
333         return readV2F1000((u8*)buf);
334 }
335
336 inline void writeV2S16(std::ostream &os, v2s16 p)
337 {
338         char buf[4];
339         writeV2S16((u8*)buf, p);
340         os.write(buf, 4);
341 }
342 inline v2s16 readV2S16(std::istream &is)
343 {
344         char buf[4];
345         is.read(buf, 4);
346         return readV2S16((u8*)buf);
347 }
348
349 inline void writeV3S16(std::ostream &os, v3s16 p)
350 {
351         char buf[6];
352         writeV3S16((u8*)buf, p);
353         os.write(buf, 6);
354 }
355 inline v3s16 readV3S16(std::istream &is)
356 {
357         char buf[6];
358         is.read(buf, 6);
359         return readV3S16((u8*)buf);
360 }
361
362 /*
363         None of these are used at the moment
364 */
365
366 template <typename T>
367 class SharedPtr
368 {
369 public:
370         SharedPtr(T *t=NULL)
371         {
372                 refcount = new int;
373                 *refcount = 1;
374                 ptr = t;
375         }
376         SharedPtr(SharedPtr<T> &t)
377         {
378                 //*this = t;
379                 drop();
380                 refcount = t.refcount;
381                 (*refcount)++;
382                 ptr = t.ptr;
383         }
384         ~SharedPtr()
385         {
386                 drop();
387         }
388         SharedPtr<T> & operator=(T *t)
389         {
390                 drop();
391                 refcount = new int;
392                 *refcount = 1;
393                 ptr = t;
394                 return *this;
395         }
396         SharedPtr<T> & operator=(SharedPtr<T> &t)
397         {
398                 drop();
399                 refcount = t.refcount;
400                 (*refcount)++;
401                 ptr = t.ptr;
402                 return *this;
403         }
404         T* operator->()
405         {
406                 return ptr;
407         }
408         T & operator*()
409         {
410                 return *ptr;
411         }
412         bool operator!=(T *t)
413         {
414                 return ptr != t;
415         }
416         bool operator==(T *t)
417         {
418                 return ptr == t;
419         }
420         T & operator[](unsigned int i)
421         {
422                 return ptr[i];
423         }
424 private:
425         void drop()
426         {
427                 assert((*refcount) > 0);
428                 (*refcount)--;
429                 if(*refcount == 0)
430                 {
431                         delete refcount;
432                         if(ptr != NULL)
433                                 delete ptr;
434                 }
435         }
436         T *ptr;
437         int *refcount;
438 };
439
440 template <typename T>
441 class Buffer
442 {
443 public:
444         Buffer()
445         {
446                 m_size = 0;
447                 data = NULL;
448         }
449         Buffer(unsigned int size)
450         {
451                 m_size = size;
452                 if(size != 0)
453                         data = new T[size];
454                 else
455                         data = NULL;
456         }
457         Buffer(const Buffer &buffer)
458         {
459                 m_size = buffer.m_size;
460                 if(m_size != 0)
461                 {
462                         data = new T[buffer.m_size];
463                         memcpy(data, buffer.data, buffer.m_size);
464                 }
465                 else
466                         data = NULL;
467         }
468         Buffer(const T *t, unsigned int size)
469         {
470                 m_size = size;
471                 if(size != 0)
472                 {
473                         data = new T[size];
474                         memcpy(data, t, size);
475                 }
476                 else
477                         data = NULL;
478         }
479         ~Buffer()
480         {
481                 drop();
482         }
483         Buffer& operator=(const Buffer &buffer)
484         {
485                 if(this == &buffer)
486                         return *this;
487                 drop();
488                 m_size = buffer.m_size;
489                 if(m_size != 0)
490                 {
491                         data = new T[buffer.m_size];
492                         memcpy(data, buffer.data, buffer.m_size);
493                 }
494                 else
495                         data = NULL;
496                 return *this;
497         }
498         T & operator[](unsigned int i) const
499         {
500                 return data[i];
501         }
502         T * operator*() const
503         {
504                 return data;
505         }
506         unsigned int getSize() const
507         {
508                 return m_size;
509         }
510 private:
511         void drop()
512         {
513                 if(data)
514                         delete[] data;
515         }
516         T *data;
517         unsigned int m_size;
518 };
519
520 template <typename T>
521 class SharedBuffer
522 {
523 public:
524         SharedBuffer()
525         {
526                 m_size = 0;
527                 data = NULL;
528                 refcount = new unsigned int;
529                 (*refcount) = 1;
530         }
531         SharedBuffer(unsigned int size)
532         {
533                 m_size = size;
534                 if(m_size != 0)
535                         data = new T[m_size];
536                 else
537                         data = NULL;
538                 refcount = new unsigned int;
539                 (*refcount) = 1;
540         }
541         SharedBuffer(const SharedBuffer &buffer)
542         {
543                 //std::cout<<"SharedBuffer(const SharedBuffer &buffer)"<<std::endl;
544                 m_size = buffer.m_size;
545                 data = buffer.data;
546                 refcount = buffer.refcount;
547                 (*refcount)++;
548         }
549         SharedBuffer & operator=(const SharedBuffer & buffer)
550         {
551                 //std::cout<<"SharedBuffer & operator=(const SharedBuffer & buffer)"<<std::endl;
552                 if(this == &buffer)
553                         return *this;
554                 drop();
555                 m_size = buffer.m_size;
556                 data = buffer.data;
557                 refcount = buffer.refcount;
558                 (*refcount)++;
559                 return *this;
560         }
561         /*
562                 Copies whole buffer
563         */
564         SharedBuffer(T *t, unsigned int size)
565         {
566                 m_size = size;
567                 if(m_size != 0)
568                 {
569                         data = new T[m_size];
570                         memcpy(data, t, m_size);
571                 }
572                 else
573                         data = NULL;
574                 refcount = new unsigned int;
575                 (*refcount) = 1;
576         }
577         /*
578                 Copies whole buffer
579         */
580         SharedBuffer(const Buffer<T> &buffer)
581         {
582                 m_size = buffer.getSize();
583                 if(m_size != 0)
584                 {
585                         data = new T[m_size];
586                         memcpy(data, *buffer, buffer.getSize());
587                 }
588                 else
589                         data = NULL;
590                 refcount = new unsigned int;
591                 (*refcount) = 1;
592         }
593         ~SharedBuffer()
594         {
595                 drop();
596         }
597         T & operator[](unsigned int i) const
598         {
599                 //assert(i < m_size)
600                 return data[i];
601         }
602         T * operator*() const
603         {
604                 return data;
605         }
606         unsigned int getSize() const
607         {
608                 return m_size;
609         }
610         operator Buffer<T>() const
611         {
612                 return Buffer<T>(data, m_size);
613         }
614 private:
615         void drop()
616         {
617                 assert((*refcount) > 0);
618                 (*refcount)--;
619                 if(*refcount == 0)
620                 {
621                         if(data)
622                                 delete[] data;
623                         delete refcount;
624                 }
625         }
626         T *data;
627         unsigned int m_size;
628         unsigned int *refcount;
629 };
630
631 inline SharedBuffer<u8> SharedBufferFromString(const char *string)
632 {
633         SharedBuffer<u8> b((u8*)string, strlen(string)+1);
634         return b;
635 }
636
637 template<typename T>
638 class MutexedVariable
639 {
640 public:
641         MutexedVariable(T value):
642                 m_value(value)
643         {
644                 m_mutex.Init();
645         }
646
647         T get()
648         {
649                 JMutexAutoLock lock(m_mutex);
650                 return m_value;
651         }
652
653         void set(T value)
654         {
655                 JMutexAutoLock lock(m_mutex);
656                 m_value = value;
657         }
658         
659         // You'll want to grab this in a SharedPtr
660         JMutexAutoLock * getLock()
661         {
662                 return new JMutexAutoLock(m_mutex);
663         }
664         
665         // You pretty surely want to grab the lock when accessing this
666         T m_value;
667
668 private:
669         JMutex m_mutex;
670 };
671
672 /*
673         TimeTaker
674 */
675
676 class TimeTaker
677 {
678 public:
679         TimeTaker(const char *name, u32 *result=NULL);
680
681         ~TimeTaker()
682         {
683                 stop();
684         }
685
686         u32 stop(bool quiet=false);
687
688         u32 getTime();
689
690 private:
691         const char *m_name;
692         u32 m_time1;
693         bool m_running;
694         u32 *m_result;
695 };
696
697 #ifndef SERVER
698 // Sets the color of all vertices in the mesh
699 void setMeshVerticesColor(scene::IMesh* mesh, video::SColor& color);
700 #endif
701
702 // Calculates the borders of a "d-radius" cube
703 inline void getFacePositions(core::list<v3s16> &list, u16 d)
704 {
705         if(d == 0)
706         {
707                 list.push_back(v3s16(0,0,0));
708                 return;
709         }
710         if(d == 1)
711         {
712                 /*
713                         This is an optimized sequence of coordinates.
714                 */
715                 list.push_back(v3s16( 0, 1, 0)); // top
716                 list.push_back(v3s16( 0, 0, 1)); // back
717                 list.push_back(v3s16(-1, 0, 0)); // left
718                 list.push_back(v3s16( 1, 0, 0)); // right
719                 list.push_back(v3s16( 0, 0,-1)); // front
720                 list.push_back(v3s16( 0,-1, 0)); // bottom
721                 // 6
722                 list.push_back(v3s16(-1, 0, 1)); // back left
723                 list.push_back(v3s16( 1, 0, 1)); // back right
724                 list.push_back(v3s16(-1, 0,-1)); // front left
725                 list.push_back(v3s16( 1, 0,-1)); // front right
726                 list.push_back(v3s16(-1,-1, 0)); // bottom left
727                 list.push_back(v3s16( 1,-1, 0)); // bottom right
728                 list.push_back(v3s16( 0,-1, 1)); // bottom back
729                 list.push_back(v3s16( 0,-1,-1)); // bottom front
730                 list.push_back(v3s16(-1, 1, 0)); // top left
731                 list.push_back(v3s16( 1, 1, 0)); // top right
732                 list.push_back(v3s16( 0, 1, 1)); // top back
733                 list.push_back(v3s16( 0, 1,-1)); // top front
734                 // 18
735                 list.push_back(v3s16(-1, 1, 1)); // top back-left
736                 list.push_back(v3s16( 1, 1, 1)); // top back-right
737                 list.push_back(v3s16(-1, 1,-1)); // top front-left
738                 list.push_back(v3s16( 1, 1,-1)); // top front-right
739                 list.push_back(v3s16(-1,-1, 1)); // bottom back-left
740                 list.push_back(v3s16( 1,-1, 1)); // bottom back-right
741                 list.push_back(v3s16(-1,-1,-1)); // bottom front-left
742                 list.push_back(v3s16( 1,-1,-1)); // bottom front-right
743                 // 26
744                 return;
745         }
746
747         // Take blocks in all sides, starting from y=0 and going +-y
748         for(s16 y=0; y<=d-1; y++)
749         {
750                 // Left and right side, including borders
751                 for(s16 z=-d; z<=d; z++)
752                 {
753                         list.push_back(v3s16(d,y,z));
754                         list.push_back(v3s16(-d,y,z));
755                         if(y != 0)
756                         {
757                                 list.push_back(v3s16(d,-y,z));
758                                 list.push_back(v3s16(-d,-y,z));
759                         }
760                 }
761                 // Back and front side, excluding borders
762                 for(s16 x=-d+1; x<=d-1; x++)
763                 {
764                         list.push_back(v3s16(x,y,d));
765                         list.push_back(v3s16(x,y,-d));
766                         if(y != 0)
767                         {
768                                 list.push_back(v3s16(x,-y,d));
769                                 list.push_back(v3s16(x,-y,-d));
770                         }
771                 }
772         }
773
774         // Take the bottom and top face with borders
775         // -d<x<d, y=+-d, -d<z<d
776         for(s16 x=-d; x<=d; x++)
777         for(s16 z=-d; z<=d; z++)
778         {
779                 list.push_back(v3s16(x,-d,z));
780                 list.push_back(v3s16(x,d,z));
781         }
782 }
783
784 class IndentationRaiser
785 {
786 public:
787         IndentationRaiser(u16 *indentation)
788         {
789                 m_indentation = indentation;
790                 (*m_indentation)++;
791         }
792         ~IndentationRaiser()
793         {
794                 (*m_indentation)--;
795         }
796 private:
797         u16 *m_indentation;
798 };
799
800 inline s16 getContainerPos(s16 p, s16 d)
801 {
802         return (p>=0 ? p : p-d+1) / d;
803 }
804
805 inline v2s16 getContainerPos(v2s16 p, s16 d)
806 {
807         return v2s16(
808                 getContainerPos(p.X, d),
809                 getContainerPos(p.Y, d)
810         );
811 }
812
813 inline v3s16 getContainerPos(v3s16 p, s16 d)
814 {
815         return v3s16(
816                 getContainerPos(p.X, d),
817                 getContainerPos(p.Y, d),
818                 getContainerPos(p.Z, d)
819         );
820 }
821
822 inline v2s16 getContainerPos(v2s16 p, v2s16 d)
823 {
824         return v2s16(
825                 getContainerPos(p.X, d.X),
826                 getContainerPos(p.Y, d.Y)
827         );
828 }
829
830 inline v3s16 getContainerPos(v3s16 p, v3s16 d)
831 {
832         return v3s16(
833                 getContainerPos(p.X, d.X),
834                 getContainerPos(p.Y, d.Y),
835                 getContainerPos(p.Z, d.Z)
836         );
837 }
838
839 inline bool isInArea(v3s16 p, s16 d)
840 {
841         return (
842                 p.X >= 0 && p.X < d &&
843                 p.Y >= 0 && p.Y < d &&
844                 p.Z >= 0 && p.Z < d
845         );
846 }
847
848 inline bool isInArea(v2s16 p, s16 d)
849 {
850         return (
851                 p.X >= 0 && p.X < d &&
852                 p.Y >= 0 && p.Y < d
853         );
854 }
855
856 inline bool isInArea(v3s16 p, v3s16 d)
857 {
858         return (
859                 p.X >= 0 && p.X < d.X &&
860                 p.Y >= 0 && p.Y < d.Y &&
861                 p.Z >= 0 && p.Z < d.Z
862         );
863 }
864
865 inline s16 rangelim(s16 i, s16 max)
866 {
867         if(i < 0)
868                 return 0;
869         if(i > max)
870                 return max;
871         return i;
872 }
873
874 #define rangelim(d, min, max) ((d) < (min) ? (min) : ((d)>(max)?(max):(d)))
875
876 inline v3s16 arealim(v3s16 p, s16 d)
877 {
878         if(p.X < 0)
879                 p.X = 0;
880         if(p.Y < 0)
881                 p.Y = 0;
882         if(p.Z < 0)
883                 p.Z = 0;
884         if(p.X > d-1)
885                 p.X = d-1;
886         if(p.Y > d-1)
887                 p.Y = d-1;
888         if(p.Z > d-1)
889                 p.Z = d-1;
890         return p;
891 }
892
893 inline std::wstring narrow_to_wide(const std::string& mbs)
894 {
895         size_t wcl = mbs.size();
896         Buffer<wchar_t> wcs(wcl+1);
897         size_t l = mbstowcs(*wcs, mbs.c_str(), wcl);
898         if(l == (size_t)(-1))
899                 return L"<invalid multibyte string>";
900         wcs[l] = 0;
901         return *wcs;
902 }
903
904 inline std::string wide_to_narrow(const std::wstring& wcs)
905 {
906         size_t mbl = wcs.size()*4;
907         SharedBuffer<char> mbs(mbl+1);
908         size_t l = wcstombs(*mbs, wcs.c_str(), mbl);
909         if(l == (size_t)(-1))
910                 mbs[0] = 0;
911         else
912                 mbs[l] = 0;
913         return *mbs;
914 }
915
916 // Split a string using the given delimiter. Returns a vector containing
917 // the component parts.
918 inline std::vector<std::wstring> str_split(const std::wstring &str, wchar_t delimiter)
919 {
920         std::vector<std::wstring> parts;
921         std::wstringstream sstr(str);
922         std::wstring part;
923         while(std::getline(sstr, part, delimiter))
924                 parts.push_back(part);
925         return parts;
926 }
927
928
929 /*
930         See test.cpp for example cases.
931         wraps degrees to the range of -360...360
932         NOTE: Wrapping to 0...360 is not used because pitch needs negative values.
933 */
934 inline float wrapDegrees(float f)
935 {
936         // Take examples of f=10, f=720.5, f=-0.5, f=-360.5
937         // This results in
938         // 10, 720, -1, -361
939         int i = floor(f);
940         // 0, 2, 0, -1
941         int l = i / 360;
942         // NOTE: This would be used for wrapping to 0...360
943         // 0, 2, -1, -2
944         /*if(i < 0)
945                 l -= 1;*/
946         // 0, 720, 0, -360
947         int k = l * 360;
948         // 10, 0.5, -0.5, -0.5
949         f -= float(k);
950         return f;
951 }
952
953 /* Wrap to 0...360 */
954 inline float wrapDegrees_0_360(float f)
955 {
956         // Take examples of f=10, f=720.5, f=-0.5, f=-360.5
957         // This results in
958         // 10, 720, -1, -361
959         int i = floor(f);
960         // 0, 2, 0, -1
961         int l = i / 360;
962         // Wrap to 0...360
963         // 0, 2, -1, -2
964         if(i < 0)
965                 l -= 1;
966         // 0, 720, 0, -360
967         int k = l * 360;
968         // 10, 0.5, -0.5, -0.5
969         f -= float(k);
970         return f;
971 }
972
973 /* Wrap to -180...180 */
974 inline float wrapDegrees_180(float f)
975 {
976         f += 180;
977         f = wrapDegrees_0_360(f);
978         f -= 180;
979         return f;
980 }
981
982 inline std::string lowercase(const std::string &s)
983 {
984         std::string s2;
985         for(size_t i=0; i<s.size(); i++)
986         {
987                 char c = s[i];
988                 if(c >= 'A' && c <= 'Z')
989                         c -= 'A' - 'a';
990                 s2 += c;
991         }
992         return s2;
993 }
994
995 inline bool is_yes(const std::string &s)
996 {
997         std::string s2 = lowercase(trim(s));
998         if(s2 == "y" || s2 == "yes" || s2 == "true" || s2 == "1")
999                 return true;
1000         return false;
1001 }
1002
1003 inline s32 mystoi(const std::string &s, s32 min, s32 max)
1004 {
1005         s32 i = atoi(s.c_str());
1006         if(i < min)
1007                 i = min;
1008         if(i > max)
1009                 i = max;
1010         return i;
1011 }
1012
1013
1014 // MSVC2010 includes it's own versions of these
1015 //#if !defined(_MSC_VER) || _MSC_VER < 1600
1016
1017 inline s32 mystoi(std::string s)
1018 {
1019         return atoi(s.c_str());
1020 }
1021
1022 inline s32 mystoi(std::wstring s)
1023 {
1024         return atoi(wide_to_narrow(s).c_str());
1025 }
1026
1027 inline float mystof(std::string s)
1028 {
1029         float f;
1030         std::istringstream ss(s);
1031         ss>>f;
1032         return f;
1033 }
1034
1035 //#endif
1036
1037 #define stoi mystoi
1038 #define stof mystof
1039
1040 inline std::string itos(s32 i)
1041 {
1042         std::ostringstream o;
1043         o<<i;
1044         return o.str();
1045 }
1046
1047 inline std::string ftos(float f)
1048 {
1049         std::ostringstream o;
1050         o<<f;
1051         return o.str();
1052 }
1053
1054 inline void str_replace(std::string & str, std::string const & pattern,
1055                 std::string const & replacement)
1056 {
1057         std::string::size_type start = str.find(pattern, 0);
1058         while(start != str.npos)
1059         {
1060                 str.replace(start, pattern.size(), replacement);
1061                 start = str.find(pattern, start+replacement.size());
1062         }
1063 }
1064
1065 inline void str_replace_char(std::string & str, char from, char to)
1066 {
1067         for(unsigned int i=0; i<str.size(); i++)
1068         {
1069                 if(str[i] == from)
1070                         str[i] = to;
1071         }
1072 }
1073
1074 /*
1075         A base class for simple background thread implementation
1076 */
1077
1078 class SimpleThread : public JThread
1079 {
1080         bool run;
1081         JMutex run_mutex;
1082
1083 public:
1084
1085         SimpleThread():
1086                 JThread(),
1087                 run(true)
1088         {
1089                 run_mutex.Init();
1090         }
1091
1092         virtual ~SimpleThread()
1093         {}
1094
1095         virtual void * Thread() = 0;
1096
1097         bool getRun()
1098         {
1099                 JMutexAutoLock lock(run_mutex);
1100                 return run;
1101         }
1102         void setRun(bool a_run)
1103         {
1104                 JMutexAutoLock lock(run_mutex);
1105                 run = a_run;
1106         }
1107
1108         void stop()
1109         {
1110                 setRun(false);
1111                 while(IsRunning())
1112                         sleep_ms(100);
1113         }
1114 };
1115
1116 /*
1117         FIFO queue (well, actually a FILO also)
1118 */
1119 template<typename T>
1120 class Queue
1121 {
1122 public:
1123         void push_back(T t)
1124         {
1125                 m_list.push_back(t);
1126         }
1127         
1128         T pop_front()
1129         {
1130                 if(m_list.size() == 0)
1131                         throw ItemNotFoundException("Queue: queue is empty");
1132
1133                 typename core::list<T>::Iterator begin = m_list.begin();
1134                 T t = *begin;
1135                 m_list.erase(begin);
1136                 return t;
1137         }
1138         T pop_back()
1139         {
1140                 if(m_list.size() == 0)
1141                         throw ItemNotFoundException("Queue: queue is empty");
1142
1143                 typename core::list<T>::Iterator last = m_list.getLast();
1144                 T t = *last;
1145                 m_list.erase(last);
1146                 return t;
1147         }
1148
1149         u32 size()
1150         {
1151                 return m_list.size();
1152         }
1153
1154 protected:
1155         core::list<T> m_list;
1156 };
1157
1158 /*
1159         Thread-safe FIFO queue (well, actually a FILO also)
1160 */
1161
1162 template<typename T>
1163 class MutexedQueue
1164 {
1165 public:
1166         MutexedQueue()
1167         {
1168                 m_mutex.Init();
1169         }
1170         u32 size()
1171         {
1172                 JMutexAutoLock lock(m_mutex);
1173                 return m_list.size();
1174         }
1175         void push_back(T t)
1176         {
1177                 JMutexAutoLock lock(m_mutex);
1178                 m_list.push_back(t);
1179         }
1180         T pop_front(u32 wait_time_max_ms=0)
1181         {
1182                 u32 wait_time_ms = 0;
1183
1184                 for(;;)
1185                 {
1186                         {
1187                                 JMutexAutoLock lock(m_mutex);
1188
1189                                 if(m_list.size() > 0)
1190                                 {
1191                                         typename core::list<T>::Iterator begin = m_list.begin();
1192                                         T t = *begin;
1193                                         m_list.erase(begin);
1194                                         return t;
1195                                 }
1196
1197                                 if(wait_time_ms >= wait_time_max_ms)
1198                                         throw ItemNotFoundException("MutexedQueue: queue is empty");
1199                         }
1200
1201                         // Wait a while before trying again
1202                         sleep_ms(10);
1203                         wait_time_ms += 10;
1204                 }
1205         }
1206         T pop_back(u32 wait_time_max_ms=0)
1207         {
1208                 u32 wait_time_ms = 0;
1209
1210                 for(;;)
1211                 {
1212                         {
1213                                 JMutexAutoLock lock(m_mutex);
1214
1215                                 if(m_list.size() > 0)
1216                                 {
1217                                         typename core::list<T>::Iterator last = m_list.getLast();
1218                                         T t = *last;
1219                                         m_list.erase(last);
1220                                         return t;
1221                                 }
1222
1223                                 if(wait_time_ms >= wait_time_max_ms)
1224                                         throw ItemNotFoundException("MutexedQueue: queue is empty");
1225                         }
1226
1227                         // Wait a while before trying again
1228                         sleep_ms(10);
1229                         wait_time_ms += 10;
1230                 }
1231         }
1232
1233         JMutex & getMutex()
1234         {
1235                 return m_mutex;
1236         }
1237
1238         core::list<T> & getList()
1239         {
1240                 return m_list;
1241         }
1242
1243 protected:
1244         JMutex m_mutex;
1245         core::list<T> m_list;
1246 };
1247
1248 /*
1249         A single worker thread - multiple client threads queue framework.
1250 */
1251
1252 template<typename Caller, typename Data>
1253 class CallerInfo
1254 {
1255 public:
1256         Caller caller;
1257         Data data;
1258 };
1259
1260 template<typename Key, typename T, typename Caller, typename CallerData>
1261 class GetResult
1262 {
1263 public:
1264         Key key;
1265         T item;
1266         core::list<CallerInfo<Caller, CallerData> > callers;
1267 };
1268
1269 template<typename Key, typename T, typename Caller, typename CallerData>
1270 class ResultQueue: public MutexedQueue< GetResult<Key, T, Caller, CallerData> >
1271 {
1272 };
1273
1274 template<typename Key, typename T, typename Caller, typename CallerData>
1275 class GetRequest
1276 {
1277 public:
1278         GetRequest()
1279         {
1280                 dest = NULL;
1281         }
1282         GetRequest(ResultQueue<Key,T, Caller, CallerData> *a_dest)
1283         {
1284                 dest = a_dest;
1285         }
1286         GetRequest(ResultQueue<Key,T, Caller, CallerData> *a_dest,
1287                         Key a_key)
1288         {
1289                 dest = a_dest;
1290                 key = a_key;
1291         }
1292         ~GetRequest()
1293         {
1294         }
1295         
1296         Key key;
1297         ResultQueue<Key, T, Caller, CallerData> *dest;
1298         core::list<CallerInfo<Caller, CallerData> > callers;
1299 };
1300
1301 template<typename Key, typename T, typename Caller, typename CallerData>
1302 class RequestQueue
1303 {
1304 public:
1305         u32 size()
1306         {
1307                 return m_queue.size();
1308         }
1309
1310         void add(Key key, Caller caller, CallerData callerdata,
1311                         ResultQueue<Key, T, Caller, CallerData> *dest)
1312         {
1313                 JMutexAutoLock lock(m_queue.getMutex());
1314                 
1315                 /*
1316                         If the caller is already on the list, only update CallerData
1317                 */
1318                 for(typename core::list< GetRequest<Key, T, Caller, CallerData> >::Iterator
1319                                 i = m_queue.getList().begin();
1320                                 i != m_queue.getList().end(); i++)
1321                 {
1322                         GetRequest<Key, T, Caller, CallerData> &request = *i;
1323
1324                         if(request.key == key)
1325                         {
1326                                 for(typename core::list< CallerInfo<Caller, CallerData> >::Iterator
1327                                                 i = request.callers.begin();
1328                                                 i != request.callers.end(); i++)
1329                                 {
1330                                         CallerInfo<Caller, CallerData> &ca = *i;
1331                                         if(ca.caller == caller)
1332                                         {
1333                                                 ca.data = callerdata;
1334                                                 return;
1335                                         }
1336                                 }
1337                                 CallerInfo<Caller, CallerData> ca;
1338                                 ca.caller = caller;
1339                                 ca.data = callerdata;
1340                                 request.callers.push_back(ca);
1341                                 return;
1342                         }
1343                 }
1344
1345                 /*
1346                         Else add a new request to the queue
1347                 */
1348
1349                 GetRequest<Key, T, Caller, CallerData> request;
1350                 request.key = key;
1351                 CallerInfo<Caller, CallerData> ca;
1352                 ca.caller = caller;
1353                 ca.data = callerdata;
1354                 request.callers.push_back(ca);
1355                 request.dest = dest;
1356                 
1357                 m_queue.getList().push_back(request);
1358         }
1359
1360         GetRequest<Key, T, Caller, CallerData> pop(bool wait_if_empty=false)
1361         {
1362                 return m_queue.pop_front(wait_if_empty);
1363         }
1364
1365 private:
1366         MutexedQueue< GetRequest<Key, T, Caller, CallerData> > m_queue;
1367 };
1368
1369 /*
1370         Pseudo-random (VC++ rand() sucks)
1371 */
1372 int myrand(void);
1373 void mysrand(unsigned seed);
1374 #define MYRAND_MAX 32767
1375
1376 int myrand_range(int min, int max);
1377
1378 /*
1379         Miscellaneous functions
1380 */
1381
1382 bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
1383                 f32 camera_fov, f32 range, f32 *distance_ptr=NULL);
1384
1385 /*
1386         Queue with unique values with fast checking of value existence
1387 */
1388
1389 template<typename Value>
1390 class UniqueQueue
1391 {
1392 public:
1393         
1394         /*
1395                 Does nothing if value is already queued.
1396                 Return value:
1397                         true: value added
1398                         false: value already exists
1399         */
1400         bool push_back(Value value)
1401         {
1402                 // Check if already exists
1403                 if(m_map.find(value) != NULL)
1404                         return false;
1405
1406                 // Add
1407                 m_map.insert(value, 0);
1408                 m_list.push_back(value);
1409                 
1410                 return true;
1411         }
1412
1413         Value pop_front()
1414         {
1415                 typename core::list<Value>::Iterator i = m_list.begin();
1416                 Value value = *i;
1417                 m_map.remove(value);
1418                 m_list.erase(i);
1419                 return value;
1420         }
1421
1422         u32 size()
1423         {
1424                 assert(m_list.size() == m_map.size());
1425                 return m_list.size();
1426         }
1427
1428 private:
1429         core::map<Value, u8> m_map;
1430         core::list<Value> m_list;
1431 };
1432
1433 #if 1
1434 template<typename Key, typename Value>
1435 class MutexedMap
1436 {
1437 public:
1438         MutexedMap()
1439         {
1440                 m_mutex.Init();
1441                 assert(m_mutex.IsInitialized());
1442         }
1443         
1444         void set(const Key &name, const Value &value)
1445         {
1446                 JMutexAutoLock lock(m_mutex);
1447
1448                 m_values[name] = value;
1449         }
1450         
1451         bool get(const Key &name, Value *result)
1452         {
1453                 JMutexAutoLock lock(m_mutex);
1454
1455                 typename core::map<Key, Value>::Node *n;
1456                 n = m_values.find(name);
1457
1458                 if(n == NULL)
1459                         return false;
1460                 
1461                 if(result != NULL)
1462                         *result = n->getValue();
1463                         
1464                 return true;
1465         }
1466
1467 private:
1468         core::map<Key, Value> m_values;
1469         JMutex m_mutex;
1470 };
1471 #endif
1472
1473 /*
1474         Generates ids for comparable values.
1475         Id=0 is reserved for "no value".
1476
1477         Is fast at:
1478         - Returning value by id (very fast)
1479         - Returning id by value
1480         - Generating a new id for a value
1481
1482         Is not able to:
1483         - Remove an id/value pair (is possible to implement but slow)
1484 */
1485 template<typename T>
1486 class MutexedIdGenerator
1487 {
1488 public:
1489         MutexedIdGenerator()
1490         {
1491                 m_mutex.Init();
1492                 assert(m_mutex.IsInitialized());
1493         }
1494         
1495         // Returns true if found
1496         bool getValue(u32 id, T &value)
1497         {
1498                 if(id == 0)
1499                         return false;
1500                 JMutexAutoLock lock(m_mutex);
1501                 if(m_id_to_value.size() < id)
1502                         return false;
1503                 value = m_id_to_value[id-1];
1504                 return true;
1505         }
1506         
1507         // If id exists for value, returns the id.
1508         // Otherwise generates an id for the value.
1509         u32 getId(const T &value)
1510         {
1511                 JMutexAutoLock lock(m_mutex);
1512                 typename core::map<T, u32>::Node *n;
1513                 n = m_value_to_id.find(value);
1514                 if(n != NULL)
1515                         return n->getValue();
1516                 m_id_to_value.push_back(value);
1517                 u32 new_id = m_id_to_value.size();
1518                 m_value_to_id.insert(value, new_id);
1519                 return new_id;
1520         }
1521
1522 private:
1523         JMutex m_mutex;
1524         // Values are stored here at id-1 position (id 1 = [0])
1525         core::array<T> m_id_to_value;
1526         core::map<T, u32> m_value_to_id;
1527 };
1528
1529 /*
1530         Checks if a string contains only supplied characters
1531 */
1532 inline bool string_allowed(const std::string &s, const std::string &allowed_chars)
1533 {
1534         for(u32 i=0; i<s.size(); i++)
1535         {
1536                 bool confirmed = false;
1537                 for(u32 j=0; j<allowed_chars.size(); j++)
1538                 {
1539                         if(s[i] == allowed_chars[j])
1540                         {
1541                                 confirmed = true;
1542                                 break;
1543                         }
1544                 }
1545                 if(confirmed == false)
1546                         return false;
1547         }
1548         return true;
1549 }
1550
1551 /*
1552         Forcefully wraps string into rows using \n
1553         (no word wrap, used for showing paths in gui)
1554 */
1555 inline std::string wrap_rows(const std::string &from, u32 rowlen)
1556 {
1557         std::string to;
1558         for(u32 i=0; i<from.size(); i++)
1559         {
1560                 if(i != 0 && i%rowlen == 0)
1561                         to += '\n';
1562                 to += from[i];
1563         }
1564         return to;
1565 }
1566
1567 /*
1568         Some helper stuff
1569 */
1570 #define MYMIN(a,b) ((a)<(b)?(a):(b))
1571 #define MYMAX(a,b) ((a)>(b)?(a):(b))
1572
1573 /*
1574         Returns integer position of node in given floating point position
1575 */
1576 inline v3s16 floatToInt(v3f p, f32 d)
1577 {
1578         v3s16 p2(
1579                 (p.X + (p.X>0 ? d/2 : -d/2))/d,
1580                 (p.Y + (p.Y>0 ? d/2 : -d/2))/d,
1581                 (p.Z + (p.Z>0 ? d/2 : -d/2))/d);
1582         return p2;
1583 }
1584
1585 /*
1586         Returns floating point position of node in given integer position
1587 */
1588 inline v3f intToFloat(v3s16 p, f32 d)
1589 {
1590         v3f p2(
1591                 (f32)p.X * d,
1592                 (f32)p.Y * d,
1593                 (f32)p.Z * d
1594         );
1595         return p2;
1596 }
1597
1598 /*
1599         More serialization stuff
1600 */
1601
1602 // Creates a string with the length as the first two bytes
1603 inline std::string serializeString(const std::string &plain)
1604 {
1605         //assert(plain.size() <= 65535);
1606         if(plain.size() > 65535)
1607                 throw SerializationError("String too long for serializeString");
1608         char buf[2];
1609         writeU16((u8*)&buf[0], plain.size());
1610         std::string s;
1611         s.append(buf, 2);
1612         s.append(plain);
1613         return s;
1614 }
1615
1616 // Creates a string with the length as the first two bytes from wide string
1617 inline std::string serializeWideString(const std::wstring &plain)
1618 {
1619         //assert(plain.size() <= 65535);
1620         if(plain.size() > 65535)
1621                 throw SerializationError("String too long for serializeString");
1622         char buf[2];
1623         writeU16((u8*)buf, plain.size());
1624         std::string s;
1625         s.append(buf, 2);
1626         for(u32 i=0; i<plain.size(); i++)
1627         {
1628                 writeU16((u8*)buf, plain[i]);
1629                 s.append(buf, 2);
1630         }
1631         return s;
1632 }
1633
1634 // Reads a string with the length as the first two bytes
1635 inline std::string deSerializeString(std::istream &is)
1636 {
1637         char buf[2];
1638         is.read(buf, 2);
1639         if(is.gcount() != 2)
1640                 throw SerializationError("deSerializeString: size not read");
1641         u16 s_size = readU16((u8*)buf);
1642         if(s_size == 0)
1643                 return "";
1644         Buffer<char> buf2(s_size);
1645         is.read(&buf2[0], s_size);
1646         std::string s;
1647         s.reserve(s_size);
1648         s.append(&buf2[0], s_size);
1649         return s;
1650 }
1651
1652 // Reads a wide string with the length as the first two bytes
1653 inline std::wstring deSerializeWideString(std::istream &is)
1654 {
1655         char buf[2];
1656         is.read(buf, 2);
1657         if(is.gcount() != 2)
1658                 throw SerializationError("deSerializeString: size not read");
1659         u16 s_size = readU16((u8*)buf);
1660         if(s_size == 0)
1661                 return L"";
1662         std::wstring s;
1663         s.reserve(s_size);
1664         for(u32 i=0; i<s_size; i++)
1665         {
1666                 is.read(&buf[0], 2);
1667                 wchar_t c16 = readU16((u8*)buf);
1668                 s.append(&c16, 1);
1669         }
1670         return s;
1671 }
1672
1673 // Creates a string with the length as the first four bytes
1674 inline std::string serializeLongString(const std::string &plain)
1675 {
1676         char buf[4];
1677         writeU32((u8*)&buf[0], plain.size());
1678         std::string s;
1679         s.append(buf, 4);
1680         s.append(plain);
1681         return s;
1682 }
1683
1684 // Reads a string with the length as the first four bytes
1685 inline std::string deSerializeLongString(std::istream &is)
1686 {
1687         char buf[4];
1688         is.read(buf, 4);
1689         if(is.gcount() != 4)
1690                 throw SerializationError("deSerializeLongString: size not read");
1691         u32 s_size = readU32((u8*)buf);
1692         if(s_size == 0)
1693                 return "";
1694         Buffer<char> buf2(s_size);
1695         is.read(&buf2[0], s_size);
1696         std::string s;
1697         s.reserve(s_size);
1698         s.append(&buf2[0], s_size);
1699         return s;
1700 }
1701
1702 //
1703
1704 inline u32 time_to_daynight_ratio(u32 time_of_day)
1705 {
1706         const s32 daylength = 16;
1707         const s32 nightlength = 6;
1708         const s32 daytimelength = 8;
1709         s32 d = daylength;
1710         s32 t = (((time_of_day)%24000)/(24000/d));
1711         if(t < nightlength/2 || t >= d - nightlength/2)
1712                 //return 300;
1713                 return 350;
1714         else if(t >= d/2 - daytimelength/2 && t < d/2 + daytimelength/2)
1715                 return 1000;
1716         else
1717                 return 750;
1718 }
1719
1720 // Random helper. Usually d=BS
1721 inline core::aabbox3d<f32> getNodeBox(v3s16 p, float d)
1722 {
1723         return core::aabbox3d<f32>(
1724                 (float)p.X * d - 0.5*d,
1725                 (float)p.Y * d - 0.5*d,
1726                 (float)p.Z * d - 0.5*d,
1727                 (float)p.X * d + 0.5*d,
1728                 (float)p.Y * d + 0.5*d,
1729                 (float)p.Z * d + 0.5*d
1730         );
1731 }
1732         
1733 class IntervalLimiter
1734 {
1735 public:
1736         IntervalLimiter():
1737                 m_accumulator(0)
1738         {
1739         }
1740         /*
1741                 dtime: time from last call to this method
1742                 wanted_interval: interval wanted
1743                 return value:
1744                         true: action should be skipped
1745                         false: action should be done
1746         */
1747         bool step(float dtime, float wanted_interval)
1748         {
1749                 m_accumulator += dtime;
1750                 if(m_accumulator < wanted_interval)
1751                         return false;
1752                 m_accumulator -= wanted_interval;
1753                 return true;
1754         }
1755 protected:
1756         float m_accumulator;
1757 };
1758
1759 std::string translatePassword(std::string playername, std::wstring password);
1760
1761 enum PointedThingType
1762 {
1763         POINTEDTHING_NOTHING,
1764         POINTEDTHING_NODE,
1765         POINTEDTHING_OBJECT
1766 };
1767
1768 struct PointedThing
1769 {
1770         PointedThingType type;
1771         v3s16 node_undersurface;
1772         v3s16 node_abovesurface;
1773         s16 object_id;
1774
1775         PointedThing();
1776         std::string dump() const;
1777         void serialize(std::ostream &os) const;
1778         void deSerialize(std::istream &is);
1779         bool operator==(const PointedThing &pt2) const;
1780         bool operator!=(const PointedThing &pt2) const;
1781 };
1782
1783 #endif
1784