File: findnode.cc

package info (click to toggle)
widelands 1:19+repack-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 370,608 kB
  • ctags: 20,609
  • sloc: cpp: 108,404; ansic: 18,695; python: 5,155; sh: 487; xml: 460; makefile: 233
file content (144 lines) | stat: -rw-r--r-- 4,443 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * Copyright (C) 2008-2010, 2013 by the Widelands Development Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#include "logic/findnode.h"

#include "base/wexception.h"
#include "logic/field.h"
#include "logic/map.h"
#include "logic/map_objects/immovable.h"

namespace Widelands {

FindNodeAnd::Subfunctor::Subfunctor(const FindNode& init_findfield, bool const init_negate)
   : negate(init_negate), findfield(init_findfield) {
}

void FindNodeAnd::add(const FindNode& findfield, bool const negate) {
	subfunctors.push_back(Subfunctor(findfield, negate));
}

bool FindNodeAnd::accept(const Map& map, const FCoords& coord) const {
	for (const Subfunctor& subfunctor : subfunctors) {
		if (subfunctor.findfield.accept(map, coord) == subfunctor.negate) {
			return false;
		}
	}
	return true;
}

bool FindNodeCaps::accept(const Map&, const FCoords& coord) const {
	NodeCaps nodecaps = coord.field->nodecaps();

	if ((nodecaps & BUILDCAPS_SIZEMASK) < (mincaps & BUILDCAPS_SIZEMASK))
		return false;

	if ((mincaps & ~BUILDCAPS_SIZEMASK) & ~(nodecaps & ~BUILDCAPS_SIZEMASK))
		return false;

	return true;
}

bool FindNodeSize::accept(const Map&, const FCoords& coord) const {
	if (BaseImmovable const* const immovable = coord.field->get_immovable())
		if (immovable->get_size() > BaseImmovable::NONE)
			return false;
	NodeCaps const nodecaps = coord.field->nodecaps();

	switch (size) {
	case sizeBuild:
		return nodecaps & (BUILDCAPS_SIZEMASK | BUILDCAPS_FLAG | BUILDCAPS_MINE);
	case sizeMine:
		return nodecaps & BUILDCAPS_MINE;
	case sizePort:
		return nodecaps & BUILDCAPS_PORT;
	case sizeSmall:
		return (nodecaps & BUILDCAPS_SIZEMASK) >= BUILDCAPS_SMALL;
	case sizeMedium:
		return (nodecaps & BUILDCAPS_SIZEMASK) >= BUILDCAPS_MEDIUM;
	case sizeBig:
		return (nodecaps & BUILDCAPS_SIZEMASK) >= BUILDCAPS_BIG;
	case sizeAny:
		return true;
	}
	NEVER_HERE();
}

bool FindNodeImmovableSize::accept(const Map&, const FCoords& coord) const {
	int32_t size = BaseImmovable::NONE;

	if (BaseImmovable* const imm = coord.field->get_immovable())
		size = imm->get_size();

	switch (size) {
	case BaseImmovable::NONE:
		return sizes & sizeNone;
	case BaseImmovable::SMALL:
		return sizes & sizeSmall;
	case BaseImmovable::MEDIUM:
		return sizes & sizeMedium;
	case BaseImmovable::BIG:
		return sizes & sizeBig;
	default:
		throw wexception("FindNodeImmovableSize: bad size = %i", size);
	}
}

bool FindNodeImmovableAttribute::accept(const Map&, const FCoords& coord) const {
	if (BaseImmovable* const imm = coord.field->get_immovable())
		return imm->has_attribute(attribute);
	return false;
}

bool FindNodeResource::accept(const Map&, const FCoords& coord) const {
	return resource == coord.field->get_resources() && coord.field->get_resources_amount();
}

bool FindNodeResourceBreedable::accept(const Map& map, const FCoords& coord) const {
	// Accept a tile that is full only if a neighbor also matches resource and
	// is not full.
	if (resource != coord.field->get_resources()) {
		return false;
	}
	if (coord.field->get_resources_amount() < coord.field->get_initial_res_amount()) {
		return true;
	}
	for (Direction dir = FIRST_DIRECTION; dir <= LAST_DIRECTION; ++dir) {
		const FCoords neighb = map.get_neighbour(coord, dir);
		if (resource == neighb.field->get_resources() &&
		    neighb.field->get_resources_amount() < neighb.field->get_initial_res_amount()) {
			return true;
		}
	}
	return false;
}

bool FindNodeShore::accept(const Map& map, const FCoords& coord) const {
	if (!(coord.field->nodecaps() & MOVECAPS_WALK))
		return false;

	for (Direction dir = FIRST_DIRECTION; dir <= LAST_DIRECTION; ++dir) {
		FCoords neighb = map.get_neighbour(coord, dir);
		if (neighb.field->nodecaps() & MOVECAPS_SWIM)
			return true;
	}

	return false;
}
}