freg  0.3
Free-Roaming Elementary Game
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
World.h
Go to the documentation of this file.
1  /* freg, Free-Roaming Elementary Game with open and interactive world
2  * Copyright (C) 2012-2014 Alexander 'mmaulwurff' Kromm
3  * mmaulwurff@gmail.com
4  *
5  * This file is part of FREG.
6  *
7  * FREG is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * FREG is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with FREG. If not, see <http://www.gnu.org/licenses/>. */
19 
20 #ifndef WORLD_H
21 #define WORLD_H
22 
23 #include <QThread>
24 #include <QMutex>
25 #include <QSettings>
26 #include "header.h"
27 #include "worldmap.h"
28 
29 class Block;
30 class Shred;
31 class ShredStorage;
32 class QByteArray;
33 class QReadWriteLock;
34 
35 const int TIME_STEPS_IN_SEC = 10;
36 
37 const int MOON_LIGHT_FACTOR = 1;
38 const int SUN_LIGHT_FACTOR = 8;
39 const int MAX_LIGHT_RADIUS = 15;
40 
41 const int MAX_BREATH = 60;
42 /// 10 bits to store durability in file, signed.
43 const int MAX_DURABILITY = 1024;
44 const int MAX_NOTE_LENGTH = 144;
45 
50 };
51 
52 class World final : public QThread {
53  /** \class World world.h
54  * \brief World provides global physics and shred connection.
55  *
56  * Designed to be single. */
57  Q_OBJECT
58 public:
59  World(QString world_name, bool * error);
60  ~World();
61 
62 public: // Block work section
63  Block * GetBlock(int x, int y, int z) const;
64  Shred * GetShred(int i, int j) const;
65  Shred * GetShredByPos(int x, int y) const;
66  Shred * GetNearShred(Shred *, dirs dir) const;
67 
68 public: // Lighting section
69  int Enlightened(int x, int y, int z) const;
70  int Enlightened(int x, int y, int z, dirs dir) const;
71  int SunLight (int x, int y, int z) const;
72  int FireLight (int x, int y, int z) const;
73  int LightMap (int x, int y, int z) const;
74 
75  int ClampX(int x) const;
76  int ClampY(int y) const;
77  int ClampZ(int z) const;
78 
79  void SunShineVertical (int x, int y, int z = HEIGHT-2,
80  int level = MAX_LIGHT_RADIUS);
81  void SunShineHorizontal(int x, int y, int z);
82  /// If init is false, light will not spread from non-invisible blocks.
83  void Shine(int x, int y, int z, int level);
84  void RemoveSunLight(int x, int y, int z);
85 
86  bool GetEvernight() const;
87 private:
88  bool SetFireLightMap(int level, int x, int y, int z);
89  void AddFireLight (int x, int y, int z, int level);
90  void RemoveFireLight(int x, int y, int z);
91 
92  /// Called when block is moved.
93  void ReEnlighten(int x, int y, int z);
94  void ReEnlightenAll();
95  void ReEnlightenTime();
96  /// Called from ReloadShreds(int), enlightens only needed shreds.
97  void ReEnlightenMove(dirs);
98  void UpShine(int x, int y, int z_bottom);
99  void UpShineInit(int x, int y, int z_bottom);
100  void CrossUpShine(int x, int y, int z_bottom);
101 
102 public: // Information section
103  QString WorldName() const;
104  /// False on error, true if focus is received to _targ successfully.
105  bool Focus(int x, int y, int z,
106  int * x_targ, int * y_targ, int * z_targ, dirs dir) const;
107  int NumShreds() const;
108  bool ShredInCentralZone(long longi, long lati) const;
109  static dirs TurnRight(dirs dir);
110  static dirs TurnLeft (dirs dir);
111  static dirs Anti(dirs dir);
112  long Longitude() const;
113  long Latitude() const;
114 
115  const WorldMap * GetMap() const;
116 
117  QByteArray * GetShredData(long longi, long lati) const;
118  void SetShredData(QByteArray *, long longi, long lati);
119 private:
120  int ShredPos(int x, int y) const;
121 
122 public: // Visibility section
123  bool DirectlyVisible(int x_from, int y_from, int z_from,
124  int x_to, int y_to, int z_to) const;
125  /// At least one side of block is visible.
126  bool Visible(int x_from, int y_from, int z_from,
127  int x_to, int y_to, int z_to) const;
128 
129 public: // Movement section
130  /// Check and move
131  bool Move(int x, int y, int z, dirs dir);
132  void Jump(int x, int y, int z, dirs dir);
133 private:
134  /// This CAN move blocks, but not xyz block.
135  can_move_results CanMove(int x, int y, int z,
136  int x_to, int y_to, int z_to, dirs dir);
137  void NoCheckMove(int x, int y, int z,
138  int x_to, int y_to, int z_to, dirs dir);
139 
140 public: // Time section
141  times_of_day PartOfDay() const;
142  /// This returns seconds from start of current day.
143  int TimeOfDay() const;
144  /// Returns time in seconds since world creation.
145  ulong Time() const;
146  QString TimeOfDayStr() const;
147  /// Returns number of physics steps since second start.
148  int MiniTime() const;
149 
150 public: // Interactions section
151  /// Returns damaged block result durability.
152  int Damage(int x, int y, int z, int level, int dmg_kind);
153  /// Does not check target block durability.
154  void DestroyAndReplace(int x, int y, int z);
155  /// Returns true on successfull build, otherwise false.
156  bool Build(Block * thing, int x, int y, int z,
157  int dir = UP,
158  Block * who = nullptr,
159  bool anyway = false);
160  /// Returns true on success. Gets a string and inscribes block.
161  bool Inscribe(int x, int y, int z);
162 
163 private: // Inventory functions section
164  /// Returns true if block_from can be put into block_to.
165  bool Exchange(Block * block_from, Block * block_to,
166  int src, int dest, int num);
167 public:
168  /// Returns true on success.
169  bool Drop(Block * from, int x_to, int y_to, int z_to,
170  int src, int dest, int num);
171  bool Get(Block * to, int x_from, int y_from, int z_from,
172  int src, int dest, int num);
173 
174 public: // Block information section
175  static bool InVertBounds(int z);
176  bool InBounds(int x, int y) const;
177  bool InBounds(int x, int y, int z) const;
178  int GetBound() const;
179 
180  int SetNote(QString note);
181  int ChangeNote(QString note, int note_id);
182  QString GetNote(int note_id) const;
183 
184 public: // World section
185  void ReloadAllShreds(long lati, long longi,
186  int new_x, int new_y, int new_z);
187 private:
188  void SetNumActiveShreds(int num);
189  /// Also saves all shreds.
190  void DeleteAllShreds();
191  void LoadAllShreds();
192  void ReloadShreds();
193  void run() override;
194  Shred ** FindShred(int x, int y) const;
195 
196 public:
197  QMutex * GetLock();
198  void Lock();
199  bool TryLock();
200  void Unlock();
201 
202 public slots:
203  void SetReloadShreds(int direction);
204  void PhysEvents();
205 
206 signals:
207  void Notify(QString) const;
208  void GetString(QString &) const;
209  void Updated(int, int, int);
210  void UpdatedAll();
211  void UpdatedAround(int x, int y, int z, int range);
212  void StartMove(int);
213  /// Emitted when world active zone moved to int direction.
214  void Moved(int);
215  /// This is emitted when a pack of updates is complete.
216  void UpdatesEnded();
217  void NeedPlayer(int, int, int);
218  void StartReloadAll();
219  void FinishReloadAll();
220  void ExitReceived();
221 
222 private:
223  World(const World &) = delete;
224  World & operator=(const World &) = delete;
225 
226  const QString worldName;
227  const WorldMap map;
228  QSettings settings;
229  ulong time;
230  int timeStep;
232  /** N
233  * | E
234  * W--+--> latitude ( x for shreds )
235  * |
236  * S v longitude ( y for shreds )
237  * center of active zone: */
239  int numShreds; ///< size of loaded zone
240  int numActiveShreds; ///< size of active zone
241  QMutex mutex;
242  const bool evernight;
244  int newX, newY, newZ;
245  /// UP for no reset, DOWN for full reset, NSEW for side shift.
246  volatile dirs toResetDir;
248 
251  QList<QString> notes;
252 };
253 
254 extern World * world;
255 
256 #endif // WORLD_H
bool Move(int x, int y, int z, dirs dir)
Check and move.
Definition: World.cpp:375
int TimeOfDay() const
This returns seconds from start of current day.
Definition: World.cpp:59
const int MAX_DURABILITY
10 bits to store durability in file, signed.
Definition: World.h:43
long newLati
Definition: World.h:243
const WorldMap map
Definition: World.h:227
void ReEnlightenTime()
static dirs TurnRight(dirs dir)
Definition: World.cpp:165
void SunShineHorizontal(int x, int y, int z)
static bool InVertBounds(int z)
Definition: World.cpp:131
Shred * GetNearShred(Shred *, dirs dir) const
Definition: World.cpp:48
int numShreds
size of loaded zone
Definition: World.h:239
long Latitude() const
Definition: World.cpp:63
can_move_results
Definition: World.h:46
void SetShredData(QByteArray *, long longi, long lati)
Definition: World.cpp:72
void GetString(QString &) const
void ExitReceived()
Shred ** FindShred(int x, int y) const
Definition: World.cpp:209
int timeStep
Definition: World.h:230
bool Drop(Block *from, int x_to, int y_to, int z_to, int src, int dest, int num)
Returns true on success.
Definition: World.cpp:88
int ClampZ(int z) const
World provides global physics and shred connection.
Definition: World.h:52
void ReloadAllShreds(long lati, long longi, int new_x, int new_y, int new_z)
Definition: World.cpp:149
void UpdatedAround(int x, int y, int z, int range)
int ClampY(int y) const
void StartMove(int)
long Longitude() const
Definition: World.cpp:62
Shred ** shreds
Definition: World.h:231
void Updated(int, int, int)
0
Definition: header.h:87
static dirs Anti(dirs dir)
Definition: World.cpp:191
times_of_day
Definition: header.h:104
bool Inscribe(int x, int y, int z)
Returns true on success. Gets a string and inscribes block.
Definition: World.cpp:579
Shred * GetShredByPos(int x, int y) const
Definition: World.cpp:44
void UpShineInit(int x, int y, int z_bottom)
int SetNote(QString note)
Definition: World.cpp:133
~World()
Definition: World.cpp:720
Definition: Shred.h:32
long latitude
Definition: World.h:238
const int MOON_LIGHT_FACTOR
Definition: World.h:37
const int MAX_BREATH
Definition: World.h:41
void AddFireLight(int x, int y, int z, int level)
QMutex * GetLock()
Definition: World.cpp:160
QString GetNote(int note_id) const
Definition: World.cpp:147
bool Visible(int x_from, int y_from, int z_from, int x_to, int y_to, int z_to) const
At least one side of block is visible.
Definition: World.cpp:354
const int HEIGHT
Definition: header.h:40
void ReloadShreds()
Definition: World.cpp:213
volatile dirs toResetDir
UP for no reset, DOWN for full reset, NSEW for side shift.
Definition: World.h:246
void StartReloadAll()
bool Exchange(Block *block_from, Block *block_to, int src, int dest, int num)
Returns true if block_from can be put into block_to.
Definition: World.cpp:593
void ReEnlightenAll()
int LightMap(int x, int y, int z) const
Use Enlightened instead, which is smart wrapper of this.
const int MAX_LIGHT_RADIUS
Definition: World.h:39
const QString worldName
Definition: World.h:226
void Lock()
Definition: World.cpp:161
World * world
Definition: World.cpp:32
bool initial_lighting
Definition: World.h:250
void Jump(int x, int y, int z, dirs dir)
Definition: World.cpp:483
ulong Time() const
Returns time in seconds since world creation.
Definition: World.cpp:61
void Shine(int x, int y, int z, int level)
If init is false, light will not spread from non-invisible blocks.
can_move_results CanMove(int x, int y, int z, int x_to, int y_to, int z_to, dirs dir)
This CAN move blocks, but not xyz block.
Definition: World.cpp:391
void Notify(QString) const
void NeedPlayer(int, int, int)
const int SUN_LIGHT_FACTOR
Definition: World.h:38
void ReEnlightenMove(dirs)
Called from ReloadShreds(int), enlightens only needed shreds.
Block * GetBlock(int x, int y, int z) const
Definition: World.cpp:204
int SunLight(int x, int y, int z) const
bool Get(Block *to, int x_from, int y_from, int z_from, int src, int dest, int num)
Definition: World.cpp:99
ulong time
Definition: World.h:229
World(QString world_name, bool *error)
Definition: World.cpp:657
void NoCheckMove(int x, int y, int z, int x_to, int y_to, int z_to, dirs dir)
Definition: World.cpp:449
void UpdatedAll()
int newX
Definition: World.h:244
bool GetEvernight() const
Definition: World.cpp:64
void UpdatesEnded()
This is emitted when a pack of updates is complete.
const WorldMap * GetMap() const
Definition: World.cpp:66
World & operator=(const World &)=delete
void Unlock()
Definition: World.cpp:163
int ClampX(int x) const
void LoadAllShreds()
Definition: World.cpp:621
void FinishReloadAll()
QString TimeOfDayStr() const
Definition: World.cpp:82
void ReEnlighten(int x, int y, int z)
Called when block is moved.
int GetBound() const
Definition: World.cpp:126
times_of_day PartOfDay() const
Definition: World.cpp:78
bool ShredInCentralZone(long longi, long lati) const
Definition: World.cpp:34
ShredStorage * shredStorage
Definition: World.h:249
int MiniTime() const
Returns number of physics steps since second start.
Definition: World.cpp:60
void RemoveSunLight(int x, int y, int z)
void DestroyAndReplace(int x, int y, int z)
Does not check target block durability.
Definition: World.cpp:525
dirs
Definition: header.h:85
bool TryLock()
Definition: World.cpp:162
void PhysEvents()
Definition: World.cpp:298
void SetReloadShreds(int direction)
Definition: World.cpp:287
int numActiveShreds
size of active zone
Definition: World.h:240
Shred * GetShred(int i, int j) const
Definition: World.cpp:40
void DeleteAllShreds()
Also saves all shreds.
Definition: World.cpp:632
void run() override
Definition: World.cpp:291
bool DirectlyVisible(int x_from, int y_from, int z_from, int x_to, int y_to, int z_to) const
Definition: World.cpp:326
bool InBounds(int x, int y) const
Definition: World.cpp:117
int sunMoonFactor
Definition: World.h:247
bool Build(Block *thing, int x, int y, int z, int dir=UP, Block *who=nullptr, bool anyway=false)
Returns true on successfull build, otherwise false.
Definition: World.cpp:550
int Damage(int x, int y, int z, int level, int dmg_kind)
Returns damaged block result durability.
Definition: World.cpp:508
QSettings settings
Definition: World.h:228
const int TIME_STEPS_IN_SEC
Definition: World.h:35
void Moved(int)
Emitted when world active zone moved to int direction.
const int MAX_NOTE_LENGTH
Definition: World.h:44
bool SetFireLightMap(int level, int x, int y, int z)
void RemoveFireLight(int x, int y, int z)
QList< QString > notes
Definition: World.h:251
int NumShreds() const
Definition: World.cpp:58
int newY
Definition: World.h:244
int newZ
Definition: World.h:244
bool Focus(int x, int y, int z, int *x_targ, int *y_targ, int *z_targ, dirs dir) const
False on error, true if focus is received to _targ successfully.
Definition: World.cpp:491
int ShredPos(int x, int y) const
Definition: World.cpp:38
void CrossUpShine(int x, int y, int z_bottom)
int ChangeNote(QString note, int note_id)
Definition: World.cpp:138
void SunShineVertical(int x, int y, int z=HEIGHT-2, int level=MAX_LIGHT_RADIUS)
Block without special physics and attributes.
Definition: Block.h:89
long longitude
Definition: World.h:238
void UpShine(int x, int y, int z_bottom)
int FireLight(int x, int y, int z) const
long newLongi
Definition: World.h:243
static dirs TurnLeft(dirs dir)
Definition: World.cpp:178
QString WorldName() const
Definition: World.cpp:65
QByteArray * GetShredData(long longi, long lati) const
Definition: World.cpp:68
void SetNumActiveShreds(int num)
Definition: World.cpp:638
const bool evernight
Definition: World.h:242
QMutex mutex
Definition: World.h:241
int Enlightened(int x, int y, int z) const