freg  0.3
Free-Roaming Elementary Game
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Filter.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 "Filter.h"
21 #include "World.h"
22 
23 const int FILTER_INV_SIZE = 2;
24 
25 Filter::Filter(const int kind, const int sub) :
26  Container(kind, sub, FILTER_INV_SIZE)
27 {}
28 
29 Filter::Filter(QDataStream & str, const int kind, const int sub) :
30  Container(str, kind, sub, FILTER_INV_SIZE)
31 {}
32 
33 QString Filter::FullName() const {
34  return tr("Filter (%1)").arg(SubName(Sub()));
35 }
36 
37 QString Filter::InvFullName(const int slot_number) const {
38  return ( slot_number > 0 ) ?
39  Container::InvFullName(slot_number) : ( Number(0) == 0 ) ?
40  tr("-example-") : Inventory::InvFullName(0);
41 }
42 
43 bool Filter::Get(Block * const block, int) {
44  if ( IsEmpty() ) {
45  return Container::Get(block, 0);
46  } else {
47  World * const world = GetWorld();
48  Container::Get(block, 1);
49  if ( block->Kind() == ShowBlock(0)->Kind() &&
50  block->Sub() == ShowBlock(0)->Sub() )
51  {
52  int x, y, z;
53  world->Focus(X(), Y(), Z(), &x, &y, &z, GetDir());
54  return world->Drop(this, x, y, z, 1, 0, 1);
55  } else {
56  return world->Drop(this, X(), Y(), Z()-1, 1, 0, 1);
57  }
58  }
59 }
int X() const
Definition: Active.cpp:158
bool Get(Block *block, int start) override
Returns true on success.
Definition: Filter.cpp:43
bool IsEmpty() const
Definition: Inventory.cpp:187
Container is multi-purpose container for blocks.
Definition: Containers.h:28
static QString SubName(int sub)
Returns translated substance name.
Definition: Block.cpp:69
int Number(int i) const
Definition: Inventory.cpp:28
World * world
Definition: World.cpp:32
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
World provides global physics and shred connection.
Definition: World.h:52
short Z() const
Definition: Xyz.cpp:30
Filter(int kind, int sub)
Definition: Filter.cpp:25
QString FullName() const override
Definition: Filter.cpp:33
virtual bool Get(Block *block, int start=0)
Returns true on success.
Definition: Inventory.cpp:78
dirs GetDir() const
Definition: Block.cpp:231
Block * ShowBlock(int slot) const
Definition: Inventory.cpp:182
int Y() const
Definition: Active.cpp:162
World * GetWorld() const
Definition: Active.cpp:140
int Kind() const
Definition: Block.h:145
int Sub() const
Definition: Block.h:144
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
const int FILTER_INV_SIZE
Definition: Filter.cpp:23
Block without special physics and attributes.
Definition: Block.h:89
virtual QString InvFullName(int num) const
Definition: Inventory.cpp:145
QString InvFullName(int slot_number) const override
Definition: Filter.cpp:37