freg  0.3
Free-Roaming Elementary Game
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Dwarf.cpp
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 #include "Dwarf.h"
21 #include "World.h"
22 #include "Shred.h"
23 #include "BlockManager.h"
24 #include <QDataStream>
25 
26 int Dwarf::Weight() const {
27  World * const world = GetWorld();
28  const int bound = world->GetBound();
29  return ( (X() < bound && world->GetBlock(X()+1, Y(), Z())->Catchable()) ||
30  ( X() > 0 && world->GetBlock(X()-1, Y(), Z())->Catchable()) ||
31  ( Y() < bound && world->GetBlock(X(), Y()+1, Z())->Catchable()) ||
32  ( Y() > 0 && world->GetBlock(X(), Y()-1, Z())->Catchable()) ) ?
34 }
35 
36 Block * Dwarf::DropAfterDamage(bool * const delete_block) {
37  Block * const cadaver = Animal::DropAfterDamage(delete_block);
39  return cadaver;
40 }
41 
43 int Dwarf::Start() const { return IN_LEFT + 1; }
44 int Dwarf::LightRadius() const { return lightRadius; }
45 bool Dwarf::Access() const { return false; }
46 Inventory * Dwarf::HasInventory() { return this; }
47 void Dwarf::ReceiveSignal(const QString str) { Active::ReceiveSignal(str); }
48 
49 QString Dwarf::FullName() const {
50  return ( DIFFERENT == Sub() ) ?
51  tr("Creator") :
52  tr("Rational creature");
53 }
54 
58 }
59 
61  const Block * const in_left = ShowBlock(IN_LEFT);
62  const Block * const in_right = ShowBlock(IN_RIGHT);
63  const int left_rad = in_left ? in_left ->LightRadius() : 0;
64  const int right_rad = in_right ? in_right->LightRadius() : 0;
65  return qMax(MIN_DWARF_LIGHT_RADIUS, qMax(left_rad, right_rad));
66 }
67 
68 int Dwarf::DamageKind() const {
69  return ( DIFFERENT == Sub() ) ?
72 }
73 
74 int Dwarf::DamageLevel() const {
75  int level = 1;
76  if ( Number(IN_RIGHT) ) {
77  level += ShowBlock(IN_RIGHT)->DamageLevel();
78  }
79  if ( Number(IN_LEFT) ) {
80  level += ShowBlock(IN_LEFT)->DamageLevel();
81  }
82  return level;
83 }
84 
85 void Dwarf::Damage(const int dmg, const int dmg_kind) {
86  if ( dmg_kind >= DAMAGE_PUSH_UP ) {
88  return;
89  }
90  int damage_to_self = dmg;
91  const int places[] = { ON_HEAD, ON_BODY, ON_LEGS };
92  for (const int i : places) {
93  if ( Number(i) == 0 ) continue;
94  Block * const armour = ShowBlock(i);
95  const int dur_before_damage = armour->GetDurability();
96  const int damage_divider = (i == ON_BODY) ? 2 : 4;
97  armour->Damage(dmg/damage_divider, dmg_kind);
98  if ( armour->GetDurability() < dur_before_damage ) {
99  damage_to_self -= dmg/damage_divider;
100  if ( armour->GetDurability() <= 0 ) {
101  delete armour;
102  Pull(i);
103  }
104  }
105  }
106  Active::Damage(damage_to_self, dmg_kind);
107 }
108 
109 void Dwarf::Move(const dirs dir) {
110  Shred * const last_shred = GetShred();
111  Falling::Move(dir);
112  if ( last_shred != GetShred() ) {
113  for (int i=0; i<Size(); ++i)
114  for (int j=0; j<Number(i); ++j) {
115  ShowBlockInSlot(i, j)->UseOnShredMove(this);
116  }
117  }
118 }
119 
120 int Dwarf::NutritionalValue(const subs sub) const {
121  switch ( sub ) {
122  case GREENERY: return SECONDS_IN_HOUR/20;
123  case SUB_NUT: return SECONDS_IN_HOUR/2;
124  case H_MEAT: return SECONDS_IN_HOUR*2.5f;
125  case A_MEAT: return SECONDS_IN_HOUR*2;
126  default: return 0;
127  }
128 }
129 
130 bool Dwarf::GetExact(Block * const block, const int to) {
131  if ( block == nullptr ) return true;
132  if ( (to > IN_LEFT || ( Number(to) == 0 && (
133  IN_RIGHT==to
134  || IN_LEFT ==to
135  || ( ON_HEAD ==to && WEARABLE_HEAD==block->Wearable() )
136  || ( ON_BODY ==to && WEARABLE_BODY==block->Wearable() )
137  || ( ON_LEGS ==to && WEARABLE_LEGS==block->Wearable() ))))
138  && Inventory::GetExact(block, to) )
139  {
140  if ( (lightRadius = UpdateLightRadiusInner()) == 0 ) {
141  GetWorld()->GetShred(X(), Y())->RemShining(this);
142  } else {
143  ReceiveSignal("hello");
144  GetWorld()->GetShred(X(), Y())->AddShining(this);
145  GetWorld()->Shine(X(), Y(), Z(), lightRadius);
146  }
147  return true;
148  } else {
149  return false;
150  }
151 }
152 
153 void Dwarf::SaveAttributes(QDataStream & out) const {
156 }
157 
158 bool Dwarf::Inscribe(QString) {
159  SendSignalAround(tr("Don't touch me!"));
160  return false;
161 }
162 
163 QString Dwarf::InvFullName(const int slot_number) const {
164  if ( Number(slot_number) == 0 ) {
165  switch ( slot_number ) {
166  case ON_HEAD: return tr("-head-");
167  case ON_BODY: return tr("-body-");
168  case ON_LEGS: return tr("-legs-");
169  case IN_RIGHT: return tr("-right hand-");
170  case IN_LEFT: return tr("-left hand-");
171  }
172  }
173  return Inventory::InvFullName(slot_number);
174 }
175 
176 Dwarf::Dwarf(const int kind, const int sub) :
177  Animal(kind, sub),
178  Inventory(),
179  lightRadius(MIN_DWARF_LIGHT_RADIUS)
180 {
181  Block::Inscribe("Urist");
182 }
183 
184 Dwarf::Dwarf(QDataStream & str, const int kind, const int sub) :
185  Animal(str, kind, sub),
186  Inventory(str),
187  lightRadius(UpdateLightRadiusInner())
188 {}
int X() const
Definition: Active.cpp:158
virtual usage_types UseOnShredMove(Block *user)
Definition: Block.cpp:214
void SendSignalAround(QString) const
Definition: Active.cpp:106
virtual void UpdateLightRadius()
Definition: Active.cpp:32
int DamageKind() const override
Definition: Dwarf.cpp:68
16
Definition: header.h:176
virtual bool Catchable() const
Definition: Block.cpp:206
int lightRadius
Definition: Dwarf.h:68
int Number(int i) const
Definition: Inventory.cpp:28
World * world
Definition: World.cpp:32
int UpdateLightRadiusInner() const
Definition: Dwarf.cpp:60
World provides global physics and shred connection.
Definition: World.h:52
virtual void Damage(int dmg, int dmg_kind)
Definition: Block.cpp:117
short Z() const
Definition: Xyz.cpp:30
13
Definition: Block.h:52
virtual int DamageKind() const
Definition: Block.cpp:208
void Move(dirs dir) override
Definition: Active.cpp:284
QString FullName() const override
Definition: Dwarf.cpp:49
virtual int Weight() const
Definition: Block.cpp:242
int Start() const override
Definition: Dwarf.cpp:43
bool GetExact(Block *, int to) override
Returns true if block found its place.
Definition: Dwarf.cpp:130
Block * DropAfterDamage(bool *delete_block) override
Should return dropped block.
Definition: Animal.cpp:105
virtual bool Get(Block *block, int start=0)
Returns true on success.
Definition: Inventory.cpp:78
virtual Inventory * HasInventory()
Definition: Block.cpp:224
int ShouldAct() const override
Definition: Dwarf.cpp:42
Block * ShowBlock(int slot) const
Definition: Inventory.cpp:182
Definition: Shred.h:32
void SaveAttributes(QDataStream &out) const override
Definition: Animal.cpp:87
7 (hominid meat)
Definition: header.h:167
void RemShining(Active *)
Definition: Shred.cpp:291
Shred * GetShred() const
Definition: Active.cpp:139
int Size() const
Definition: Inventory.cpp:27
Block * DropAfterDamage(bool *delete_block) override
Should return dropped block.
Definition: Dwarf.cpp:36
virtual int LightRadius() const
Definition: Block.cpp:210
void Shine(int x, int y, int z, int level)
If init is false, light will not spread from non-invisible blocks.
void ReceiveSignal(QString) override
Receive text signal.
Definition: Active.cpp:156
11
Definition: header.h:171
int Y() const
Definition: Active.cpp:162
void SaveAttributes(QDataStream &out) const override
Definition: Dwarf.cpp:153
Dwarf(int sub, int id)
Definition: Dwarf.cpp:176
Block * GetBlock(int x, int y, int z) const
Definition: World.cpp:204
void ReceiveSignal(QString) override
Definition: Dwarf.cpp:47
virtual int DamageLevel() const
Definition: Block.cpp:209
World * GetWorld() const
Definition: Active.cpp:140
Definition: Animal.h:27
Block * ShowBlockInSlot(int slot, int index) const
Don't move block shown by this function.
Definition: Inventory.cpp:177
int Sub() const
Definition: Block.h:144
virtual wearable Wearable() const
Definition: Block.cpp:212
void Move(dirs direction) override
Definition: Dwarf.cpp:109
virtual bool GetExact(Block *block, int num)
Returns true if block found its place.
Definition: Inventory.cpp:103
subs
Substance block is made from.
Definition: header.h:157
int LightRadius() const override
Definition: Dwarf.cpp:44
13
Definition: header.h:128
static Block * NewBlock(int kind, int sub)
Use this to receive a pointer to new not-normal block.
int GetBound() const
Definition: World.cpp:126
int NutritionalValue(subs) const override
Definition: Dwarf.cpp:120
virtual int Weight() const
Definition: Inventory.cpp:169
dirs
Definition: header.h:85
void Pull(int num)
Removes block from inventory. Does not delete block.
Definition: Inventory.cpp:59
22
Definition: header.h:182
Provides block ability to contain other blocks inside.
Definition: Inventory.h:33
void Damage(int dmg, int dmg_kind) override
Definition: Active.cpp:142
Shred * GetShred(int i, int j) const
Definition: World.cpp:40
bool Access() const override
Definition: Dwarf.cpp:45
int GetDurability() const
Definition: Block.cpp:232
int Weight() const override
Definition: Dwarf.cpp:26
void Damage(int dmg, int dmg_kind) override
Definition: Dwarf.cpp:85
bool Inscribe(QString str) override
Returns true on success.
Definition: Dwarf.cpp:158
void AddShining(Active *)
Definition: Shred.cpp:285
QString InvFullName(int slot_number) const override
Definition: Dwarf.cpp:163
Block without special physics and attributes.
Definition: Block.h:89
Inventory * HasInventory() override
Definition: Dwarf.cpp:46
8 (animal meat)
Definition: header.h:168
virtual QString InvFullName(int num) const
Definition: Inventory.cpp:145
virtual void SaveAttributes(QDataStream &out) const
Definition: Inventory.cpp:65
void UpdateLightRadius() override
Definition: Dwarf.cpp:55
int DamageLevel() const override
Definition: Dwarf.cpp:74
14
Definition: header.h:174
virtual bool Inscribe(QString str)
Returns true on success.
Definition: Block.cpp:216
const int MIN_DWARF_LIGHT_RADIUS
Definition: Dwarf.h:26