2
* Copyright (C) 2002,2003,2004 Daniel Heck
3
* Copyright (C) 2008,2009,2010 Ronald Lamprecht
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License along
16
* with this program; if not, write to the Free Software Foundation, Inc.,
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
#include "actors/HorseActor.hh"
28
/* -------------------- Horse -------------------- */
30
Horse::Horse() : Actor (traits), destidx(0), target(), strength(10) {
31
objFlags |= OBJBIT_STEADY;
34
std::string Horse::getClass() const {
38
Value Horse::getAttr(const std::string &key) const {
39
if (key == "destination") {
41
} else if (key == "destidx") {
43
} else if (key == "steady") {
44
return (bool)(objFlags & OBJBIT_STEADY);
45
} else if (key == "strength") {
48
return Actor::getAttr(key);
51
void Horse::setAttr(const string& key, const Value &val) {
52
if (key == "destination") {
54
if (destination.getType() == Value::NIL)
55
objFlags &= ~OBJBIT_AUTOMOVE;
58
objFlags |= OBJBIT_NEWDEST;
60
} else if (key == "destidx") {
63
objFlags |= OBJBIT_NEWDEST;
65
objFlags &= ~OBJBIT_AUTOMOVE;
66
} else if (key == "steady") {
68
objFlags |= OBJBIT_STEADY;
70
objFlags &= ~OBJBIT_STEADY;
71
} else if (key == "strength") {
74
objFlags &= ~OBJBIT_AUTOMOVE;
76
Actor::setAttr(key, val);
78
if (strength >= 0 && destidx >= 0 && getAttr("destination").getType() != Value::NIL)
79
objFlags |= OBJBIT_AUTOMOVE;
82
bool Horse::is_dead() const {
86
void Horse::think(double dtime) {
87
if (objFlags & OBJBIT_AUTOMOVE) {
89
if (objFlags & OBJBIT_AUTOMOVE) {
90
if (!(objFlags & OBJBIT_STEADY) &&
91
ecl::square(get_vel()) * 0.6 / strength > ecl::length(target - get_pos()) - 0.05)
92
add_force(- normalize(get_vel()) * strength);
94
add_force(normalize(target - get_pos()) * strength);
100
void Horse::stoneBounce(const StoneContact &sc) {
101
if ((objFlags & OBJBIT_AUTOMOVE) && (sc.stonepos == GridPos(target))) {
104
Actor::stoneBounce(sc);
107
void Horse::updateTarget(bool touched) {
108
if (objFlags & OBJBIT_NEWDEST) {
109
// target not defined so far
110
ASSERT(getDestinationByIndex(destidx, target), XLevelRuntime, "Horse actor missing valid destination");
111
objFlags &= ~OBJBIT_NEWDEST;
112
} else if (touched || length(target - get_pos()) < ((objFlags & OBJBIT_STEADY) ? 0.2 : 0.1)) {
113
int id = getId(); // in future user might kill actors on callback
115
// target reached or? try next one
116
if ((Object::getObject(id) != NULL) && (objFlags & OBJBIT_AUTOMOVE) &&
117
!getDestinationByIndex(++destidx, target)) {
118
if (getAttr("loop").to_bool()) {
119
destidx = 0; // failed -> start anew
120
getDestinationByIndex(destidx, target);
122
objFlags &= ~OBJBIT_AUTOMOVE;
127
ActorTraits Horse::traits = {"ac_horse", ac_horse, 1<<ac_horse, 24.0/64, 1.2};
130
BootRegister(new Horse(), "ac_horse");
133
} // namespace enigma