freg  0.3
Free-Roaming Elementary Game
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Player.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 PLAYER_H
21 #define PLAYER_H
22 
23 #include <QObject>
24 #include <QSettings>
25 #include "header.h"
26 #include "Xyz.h"
27 
28 class World;
29 class Block;
30 class Animal;
31 class Inventory;
32 class Shred;
33 
34 class Player final : public QObject, private Xyz {
35  /** \class Player Player.h
36  * \brief This class contains information specific to player
37  * and interface for manipulating him.
38  *
39  * It receives input from screen, processes it, and dispatches
40  * actions to world.
41  *
42  * It loads player from file and saves him to it.
43  *
44  * Note: player block with its inventory is stored it shred
45  * file with all other blocks. player_save contains only
46  * coordinates where player is, his home coordinates and world
47  * player belongs to.
48  *
49  * Also it does checks for player walking over the shred border. */
50  Q_OBJECT
51 public:
52  /// Constructor creates or loads player.
53  /** It puts player block to the world if there is no player block,
54  * and makes necessary connections. */
55  Player();
56  ~Player();
57 
58  long GlobalX() const;
59  long GlobalY() const;
60 
61  int X() const;
62  int Y() const;
63  using Xyz::Z;
64 
65  /// This returns current player direction (see enum dirs in header.h)
66  dirs GetDir() const;
67 
68  /// This returns player hitpoints, also known as durability.
69  int HP() const;
70  /// This returns player breath reserve. On error returns -100.
71  int BreathPercent() const;
72  /// Can be > 100 if player is gorged. On error returns -100.
73  int SatiationPercent() const;
74 
75  const Block * GetBlock() const;
76 
77  /// This returns true if block at (x, y, z) is visible to player.
78  bool Visible(int x, int y, int z) const;
79 
80  /// This returns how player is using something now.
81  /** See enum usage_types in header.h. */
82  int UsingType() const;
83  int GetUsingInInventory() const;
84  void SetUsingTypeNo();
85  /// This returns how player is using himself.
86  /** For example, OPEN means he is looking in his backpack.
87  * See enum usage_types in header.h. */
88  int UsingSelfType() const;
89 
90  /// Returns nullptr if there is no inventory, otherwise returns inventory.
91  Inventory * PlayerInventory() const;
92 
93  long GetLongitude() const;
94  long GetLatitude() const;
95 
96  bool GetCreativeMode() const;
97  void SetCreativeMode(bool turn);
98 
99  void SetDir(dirs);
100  void Move(dirs);
101  void Jump();
102 
103  /// Closes backpack, chests, etc.
104  void StopUseAll();
105  /// Tries to switch usingSelfType from NO to OPEN.
106  void Backpack();
107  void Inscribe() const;
108  void Examine();
109  void Examine(int x, int y, int z);
110  /// Returns true if xyz are in world bounds.
111  bool Damage() const;
112  void Use();
113  /// Tries to throw (drop out) block number num from inventory.
114  void Throw(int src, int dest = 0, int num = 1);
115 
116  /// Tries to use block number num in inventory.
117  usage_types Use(int num);
118  /// Tries to get block number num from outer inventory.
119  // Returns true on success.
120  bool Obtain(int src, int dest = 0, int num = 1);
121  void Inscribe(int num);
122  void Craft (int num);
123  void Build (int num);
124  void Wield (int num);
125  /// Can also wield appropriate things.
126  void MoveInsideInventory(int num_from, int num_to, int num = 1);
127  void ProcessCommand(QString command);
128 
129  /// Turns low-case ASCII chars array (length <= 12) into unique quint64.
130  /// Can be used as switch statement for switch on strings.
131  constexpr static quint64 UniqueIntFromString(const char * const chars ) {
132  return chars[0] == '\0' ?
133  0 : (UniqueIntFromString(chars + 1) << 5) | (chars[0]-'a');
134  }
135 
136 signals:
137  void Moved(long x, long y, int z) const;
138  /// This is emitted when a notification is needed to be displayed.
139  /** It should be connected to screen::Notify(QString). */
140  void Notify(QString) const;
141 
142  /// This is emitted when player walks over shred border.
143  /** It should be connected to World::ReloadShreds(int) signal. */
144  void OverstepBorder(int);
145 
146  /// This is emitted when some player property is updated.
147  /** It shoul be connected to screen::UpdatePlayer() signal. */
148  void Updated();
149  void GetString(QString &);
150  void Destroyed();
151  void ShowFile(QString path);
152  void GetFocus(int * x, int * y, int * z) const;
153 
154 private slots:
155  /// Checks if player walked over the shred border.
156  /** This is connected to player's block signal Moved(int).
157  * It emits OverstepBorder(int) signal when player walks
158  * over the shred border. */
159  void CheckOverstep(int dir);
160 
161  /// This is called when player block is destroyed.
162  void BlockDestroy();
163 
164  void SetPlayer(int set_x, int set_y, int set_z);
165  /// Dir is not used, for slot signature compatibility only.
166  void UpdateXYZ();
167 
168 private:
169  Player & operator=(const Player &) = delete;
170  Player(const Player &) = delete;
171 
172  Animal * NewPlayer() const;
173 
174  /// Checks player/inventory/block existence, size limits.
175  Block * ValidBlock(int num) const;
176  Shred * GetShred() const;
177  World * GetWorld() const;
178  bool ForbiddenAdminCommands() const;
179 
180  QSettings settings;
189  bool cleaned = false;
190 };
191 
192 #endif // PLAYER_H
bool Obtain(int src, int dest=0, int num=1)
Tries to get block number num from outer inventory.
Definition: Player.cpp:257
void SetUsingTypeNo()
Definition: Player.cpp:53
Block * ValidBlock(int num) const
Checks player/inventory/block existence, size limits.
Definition: Player.cpp:192
void ShowFile(QString path)
bool Damage() const
Returns true if xyz are in world bounds.
Definition: Player.cpp:414
int GetUsingInInventory() const
Definition: Player.cpp:54
void BlockDestroy()
This is called when player block is destroyed.
Definition: Player.cpp:439
int homeY
Definition: Player.h:182
World provides global physics and shred connection.
Definition: World.h:52
long GetLatitude() const
Definition: Player.cpp:56
short Z() const
Definition: Xyz.cpp:30
void MoveInsideInventory(int num_from, int num_to, int num=1)
Can also wield appropriate things.
Definition: Player.cpp:281
long GlobalX() const
Definition: Player.cpp:44
void SetDir(dirs)
Definition: Player.cpp:408
void Examine()
Definition: Player.cpp:95
void Updated()
This is emitted when some player property is updated.
int BreathPercent() const
This returns player breath reserve. On error returns -100.
Definition: Player.cpp:72
long GetLongitude() const
Definition: Player.cpp:55
Animal * NewPlayer() const
Definition: Player.cpp:449
void SetCreativeMode(bool turn)
Definition: Player.cpp:60
dirs GetDir() const
This returns current player direction (see enum dirs in header.h)
Definition: Player.cpp:49
void SetPlayer(int set_x, int set_y, int set_z)
Definition: Player.cpp:454
Definition: Shred.h:32
bool ForbiddenAdminCommands() const
Definition: Player.cpp:320
Inventory * PlayerInventory() const
Returns nullptr if there is no inventory, otherwise returns inventory.
Definition: Player.cpp:80
int Y() const
Definition: Player.cpp:40
int SatiationPercent() const
Can be > 100 if player is gorged. On error returns -100.
Definition: Player.cpp:74
int UsingType() const
This returns how player is using something now.
Definition: Player.cpp:50
void Destroyed()
void Craft(int num)
Definition: Player.cpp:312
void GetString(QString &)
Player & operator=(const Player &)=delete
int usingType
Definition: Player.h:185
World * GetWorld() const
Definition: Player.cpp:47
bool Visible(int x, int y, int z) const
This returns true if block at (x, y, z) is visible to player.
Definition: Player.cpp:402
bool GetCreativeMode() const
Definition: Player.cpp:57
void Build(int num)
Definition: Player.cpp:298
void Moved(long x, long y, int z) const
void GetFocus(int *x, int *y, int *z) const
void StopUseAll()
Closes backpack, chests, etc.
Definition: Player.cpp:51
int usingSelfType
Definition: Player.h:186
void Wield(int num)
Definition: Player.cpp:270
void ProcessCommand(QString command)
Definition: Player.cpp:329
void Notify(QString) const
This is emitted when a notification is needed to be displayed.
Definition: Animal.h:27
void Inscribe() const
Definition: Player.cpp:180
int homeX
Definition: Player.h:182
static constexpr quint64 UniqueIntFromString(const char *const chars)
Definition: Player.h:131
int usingInInventory
Definition: Player.h:187
long GlobalY() const
Definition: Player.cpp:45
const Block * GetBlock() const
Definition: Player.cpp:58
usage_types
Definition: header.h:196
Definition: Xyz.h:35
Shred * GetShred() const
Definition: Player.cpp:46
dirs
Definition: header.h:85
int homeZ
Definition: Player.h:182
Provides block ability to contain other blocks inside.
Definition: Inventory.h:33
~Player()
Definition: Player.cpp:531
int UsingSelfType() const
This returns how player is using himself.
Definition: Player.cpp:52
void Backpack()
Tries to switch usingSelfType from NO to OPEN.
Definition: Player.cpp:155
Animal * creator
Definition: Player.h:184
long homeLongi
Definition: Player.h:181
bool creativeMode
Definition: Player.h:188
void OverstepBorder(int)
This is emitted when player walks over shred border.
Animal * player
Definition: Player.h:183
void Jump()
Definition: Player.cpp:133
QSettings settings
Definition: Player.h:180
long homeLati
Definition: Player.h:181
void UpdateXYZ()
Dir is not used, for slot signature compatibility only.
Definition: Player.cpp:90
This class contains information specific to player and interface for manipulating him...
Definition: Player.h:34
Block without special physics and attributes.
Definition: Block.h:89
Player()
Constructor creates or loads player.
Definition: Player.cpp:497
void Use()
Definition: Player.cpp:163
int X() const
Definition: Player.cpp:36
void CheckOverstep(int dir)
Checks if player walked over the shred border.
Definition: Player.cpp:427
void Throw(int src, int dest=0, int num=1)
Tries to throw (drop out) block number num from inventory.
Definition: Player.cpp:249
int HP() const
This returns player hitpoints, also known as durability.
void Move(dirs)
Definition: Player.cpp:145
bool cleaned
Definition: Player.h:189