freg  0.3
Free-Roaming Elementary Game
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
DeferredAction.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 "World.h"
21 #include "blocks/Animal.h"
22 #include "blocks/Inventory.h"
23 #include "DeferredAction.h"
24 #include "BlockManager.h"
25 
27  const dirs dir = static_cast<dirs>(num);
28  if ( dir == DOWN && attachedBlock->Z() == 1 ) return;
29  if ( dir == UP && attachedBlock->Z() == HEIGHT-2 ) return;
30  attachedBlock->Move(dir);
31 }
32 
33 void DeferredAction::Move() const {
35  attachedBlock->Z(), static_cast<dirs>(num));
36 }
37 
38 void DeferredAction::Jump() const {
42 }
43 
44 void DeferredAction::Build() const {
45  World * const world = attachedBlock->GetWorld();
46  int x = X(), y = Y(), z = Z();
47  if ( ENVIRONMENT != world->GetBlock(x, y, z)->PushResult(ANYWHERE) ) {
48  if ( world->Move(
51  { // shift coordinates to opposite side:
52  switch ( attachedBlock->GetDir() ) {
53  case UP: --z; break;
54  case DOWN: ++z; break;
55  case NORTH: ++y; break;
56  case SOUTH: --y; break;
57  case EAST: --x; break;
58  case WEST: ++x; break;
59  }
60  } else {
61  return;
62  }
63  }
64  Inventory * const inv = attachedBlock->HasInventory();
65  Block * const material = inv->ShowBlock(srcSlot);
66  if ( not world->Build(material, x, y, z,
68  { // build not successful
69  return;
70  }
71  inv->Pull(srcSlot);
72  // put more material in building inventory slot:
73  if ( inv->Number(srcSlot) ) return;
74  const int id = material->GetId();
75  for (int i=srcSlot+1; i<inv->Size() &&
76  inv->Number(srcSlot)<MAX_STACK_SIZE; ++i)
77  {
78  const Block * const block_i = inv->ShowBlock(i);
79  if ( block_i && id==block_i->GetId() ) {
80  inv->MoveInside(i, srcSlot, inv->Number(i));
81  }
82  }
83 } // void DeferredAction::Build()
84 
85 void DeferredAction::Damage() const {
86  if ( attachedBlock->GetWorld()->Damage(X(), Y(), Z(),
88  attachedBlock->DamageKind()) <= 0 ) // durability
89  {
91  }
92 }
93 
94 void DeferredAction::Throw() const {
96  srcSlot, destSlot, num);
97 }
98 
99 void DeferredAction::Pour() const {
100  Inventory * const attached_inv = attachedBlock->HasInventory();
101  if ( attached_inv == nullptr ) return;
102 
103  Block * const vessel = attached_inv->ShowBlock(srcSlot);
104  if ( vessel == nullptr ) return;
105 
106  Inventory * const vessel_inv = vessel->HasInventory();
107  if ( vessel_inv == nullptr ) return;
108 
109  Block * const liquid = vessel_inv->ShowBlock(0);
110  if ( liquid == nullptr ) return;
111 
112  if ( attachedBlock->GetWorld()->Build(liquid, X(), Y(), Z()) ) {
113  vessel_inv->Pull(0);
114  }
115 }
116 
118  World * const world = attachedBlock->GetWorld();
119  if ( world->GetBlock(X(), Y(), Z())->Sub() == AIR ) {
120  world->Build(BlockManager::NewBlock(GRASS, FIRE), X(), Y(), Z());
121  } else if ( world->Damage(X(), Y(), Z(), 1, DAMAGE_HEAT) <= 0 ) {
122  world->DestroyAndReplace(X(), Y(), Z());
123  }
124 }
125 
126 void DeferredAction::SetGhostMove(const int dir) {
128  num = dir;
129 }
130 
131 void DeferredAction::SetMove(const int dir) {
133  num = dir;
134 }
135 
137 
138 void DeferredAction::SetBuild(const int x, const int y, const int z,
139  const int builder_slot)
140 {
141  SetXyz(x, y, z);
142  srcSlot = builder_slot;
144 }
145 
146 void DeferredAction::SetDamage(const int x, const int y, const int z) {
147  SetXyz(x, y, z);
149 }
150 
151 void DeferredAction::SetThrow(const int x, const int y, const int z,
152  const int src, const int dest, const int n)
153 {
154  SetXyz(x, y, z);
155  srcSlot = src;
156  destSlot = dest;
157  num = n;
159 }
160 
161 void DeferredAction::SetPour(const int x, const int y, const int z,
162  const int src)
163 {
164  SetXyz(x, y, z);
165  srcSlot = src;
167 }
168 
169 void DeferredAction::SetSetFire(const int x, int y, int z) {
170  SetXyz(x, y, z);
172 }
173 
175  switch ( type ) {
176  case DEFERRED_NOTHING: break;
177  case DEFERRED_GHOST_MOVE: GhostMove(); break;
178  case DEFERRED_MOVE: Move(); break;
179  case DEFERRED_JUMP: Jump(); break;
180  case DEFERRED_BUILD: Build(); break;
181  case DEFERRED_DAMAGE: Damage(); break;
182  case DEFERRED_THROW: Throw(); break;
183  case DEFERRED_POUR: Pour(); break;
184  case DEFERRED_SET_FIRE: SetFire(); break;
185  }
186 }
187 
189  Xyz(),
190  type(DEFERRED_NOTHING),
191  attachedBlock(attached),
192  srcSlot(),
193  destSlot(),
194  num()
195 {}
bool Move(int x, int y, int z, dirs dir)
Check and move.
Definition: World.cpp:375
int X() const
Definition: Active.cpp:158
void Pour() const
void Throw() const
static dirs TurnRight(dirs dir)
Definition: World.cpp:165
3
Definition: header.h:90
int Number(int i) const
Definition: Inventory.cpp:28
World * world
Definition: World.cpp:32
6
Definition: header.h:121
void MakeAction() const
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
void MoveInside(int num_from, int num_to, int num)
Definition: Inventory.cpp:115
World provides global physics and shred connection.
Definition: World.h:52
short Z() const
Definition: Xyz.cpp:30
void Move(dirs dir) override
Definition: Active.cpp:284
0
Definition: header.h:87
short X() const
Definition: Xyz.cpp:28
static dirs Anti(dirs dir)
Definition: World.cpp:191
deferred_actions type
2
Definition: header.h:89
virtual push_reaction PushResult(dirs) const
Definition: Block.cpp:201
dirs GetDir() const
Definition: Block.cpp:231
4
Definition: header.h:91
virtual Inventory * HasInventory()
Definition: Block.cpp:224
Block * ShowBlock(int slot) const
Definition: Inventory.cpp:182
void SetSetFire(int x, int y, int z)
5
Definition: Block.h:44
void SetXyz(short x, short y, short z)
Definition: Xyz.cpp:32
const int HEIGHT
Definition: header.h:40
int DamageKind() const override
Definition: Animal.cpp:68
int Size() const
Definition: Inventory.cpp:27
void SetMove(int dir)
void Jump(int x, int y, int z, dirs dir)
Definition: World.cpp:483
void SetDamage(int x, int y, int z)
int Y() const
Definition: Active.cpp:162
Block * GetBlock(int x, int y, int z) const
Definition: World.cpp:204
virtual int DamageLevel() const
Definition: Block.cpp:209
void SetFire() const
Provides declaration for class Inventory for freg.
World * GetWorld() const
Definition: Active.cpp:140
Definition: Animal.h:27
void SetThrow(int x, int y, int z, int src_slot, int dest_slot, int unum)
void GhostMove() const
short Y() const
Definition: Xyz.cpp:29
void SetBuild(int x, int y, int z, int builder_slot)
Attached block should have inventory.
int Sub() const
Definition: Block.h:144
void Build() const
19
Definition: header.h:179
static Block * NewBlock(int kind, int sub)
Use this to receive a pointer to new not-normal block.
25
Definition: header.h:185
Definition: Xyz.h:35
void DestroyAndReplace(int x, int y, int z)
Does not check target block durability.
Definition: World.cpp:525
1
Definition: header.h:88
int GetId() const
Determines kind and sub, unique for every kind-sub pair.
Definition: Block.cpp:205
void SetGhostMove(int dir)
dirs
Definition: header.h:85
void Pull(int num)
Removes block from inventory. Does not delete block.
Definition: Inventory.cpp:59
void Move() const
Provides block ability to contain other blocks inside.
Definition: Inventory.h:33
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
void Jump() const
Animal *const attachedBlock
DeferredAction(Animal *attached)
const int MAX_STACK_SIZE
Definition: Inventory.h:29
void Damage() const
Block without special physics and attributes.
Definition: Block.h:89
5
Definition: header.h:92
void SetPour(int x, int y, int z, int slot)