~ubuntu-branches/ubuntu/saucy/enigma/saucy

« back to all changes in this revision

Viewing changes to src/actors/HorseActor.cc

  • Committer: Bazaar Package Importer
  • Author(s): Erich Schubert
  • Date: 2010-05-26 02:27:26 UTC
  • mfrom: (5.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20100526022726-7tnbf65s6btbibu2
Tags: 1.10~~pre-alpha+r2100-1
* New SVN checkout, shortly after upstream "pre-alpha" release
* Target unstable, to get more testing for enigma
* Remove spelling patches included upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2002,2003,2004 Daniel Heck
 
3
 * Copyright (C) 2008,2009,2010 Ronald Lamprecht
 
4
 *
 
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.
 
9
 *  
 
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.
 
14
 *
 
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.
 
18
 *
 
19
 */
 
20
 
 
21
#include "actors/HorseActor.hh"
 
22
#include "errors.hh"
 
23
//#include "main.hh"
 
24
#include "world.hh"
 
25
 
 
26
namespace enigma {
 
27
 
 
28
/* -------------------- Horse  -------------------- */
 
29
 
 
30
    Horse::Horse() : Actor (traits), destidx(0), target(), strength(10) {
 
31
        objFlags |= OBJBIT_STEADY;
 
32
    }
 
33
 
 
34
    std::string Horse::getClass() const {
 
35
        return "ac_horse";
 
36
    }
 
37
 
 
38
    Value Horse::getAttr(const std::string &key) const {
 
39
        if (key == "destination") {
 
40
            return destination;
 
41
        } else if (key == "destidx") { 
 
42
            return destidx;
 
43
        } else if (key == "steady") { 
 
44
            return (bool)(objFlags & OBJBIT_STEADY);
 
45
        } else if (key == "strength") { 
 
46
            return strength;
 
47
        } else
 
48
            return Actor::getAttr(key);
 
49
    }
 
50
    
 
51
    void Horse::setAttr(const string& key, const Value &val) {
 
52
        if (key == "destination") {
 
53
            destination = val;
 
54
            if (destination.getType() == Value::NIL)
 
55
                objFlags &= ~OBJBIT_AUTOMOVE;
 
56
            else {
 
57
                destidx = 0;
 
58
                objFlags |= OBJBIT_NEWDEST;
 
59
            }
 
60
        } else if (key == "destidx") { 
 
61
            destidx = val;
 
62
            if (destidx >= 0)
 
63
                objFlags |= OBJBIT_NEWDEST;
 
64
            else
 
65
                objFlags &= ~OBJBIT_AUTOMOVE;
 
66
        } else if (key == "steady") {
 
67
            if (val.to_bool())
 
68
                 objFlags |= OBJBIT_STEADY;
 
69
            else
 
70
                 objFlags &= ~OBJBIT_STEADY;
 
71
        } else if (key == "strength") { 
 
72
            strength = val;
 
73
            if (strength < 0)
 
74
                objFlags &= ~OBJBIT_AUTOMOVE;
 
75
        } else
 
76
            Actor::setAttr(key, val);
 
77
        
 
78
        if (strength >= 0 &&  destidx >= 0 && getAttr("destination").getType() != Value::NIL)
 
79
            objFlags |= OBJBIT_AUTOMOVE;
 
80
    }
 
81
 
 
82
    bool Horse::is_dead() const {
 
83
        return false;
 
84
    }
 
85
    
 
86
    void Horse::think(double dtime) {
 
87
        if (objFlags & OBJBIT_AUTOMOVE) {
 
88
            updateTarget();
 
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);
 
93
                else                
 
94
                    add_force(normalize(target - get_pos()) * strength);
 
95
            }
 
96
        }
 
97
        Actor::think(dtime);
 
98
    }
 
99
 
 
100
    void Horse::stoneBounce(const StoneContact &sc) {
 
101
        if ((objFlags & OBJBIT_AUTOMOVE) && (sc.stonepos == GridPos(target))) {
 
102
            updateTarget(true);           
 
103
        }
 
104
        Actor::stoneBounce(sc);
 
105
    }
 
106
    
 
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
 
114
            performAction(true);
 
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);
 
121
                } else
 
122
                    objFlags &= ~OBJBIT_AUTOMOVE;
 
123
            }
 
124
        }
 
125
    }
 
126
    
 
127
    ActorTraits Horse::traits = {"ac_horse", ac_horse, 1<<ac_horse, 24.0/64, 1.2};
 
128
    
 
129
    BOOT_REGISTER_START
 
130
        BootRegister(new Horse(), "ac_horse");
 
131
    BOOT_REGISTER_END
 
132
 
 
133
} // namespace enigma
 
134