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

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
/*
 * Copyright (C) 2008,2009,2010 Ronald Lamprecht
 *
 * 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 "stones/PassageStone.hh"

#include "errors.hh"
//#include "main.hh"

namespace enigma {

    PassageStone::PassageStone(int color, int flavor) : Stone() {
        state = color;
        objFlags |= (flavor << 24);
    }
    
    std::string PassageStone::getClass() const {
        return "st_passage";
    }
    
    void PassageStone::setAttr(const string& key, const Value &val) {
        if (key == "color") {
            Stone::setAttr("state", val);
        } else if (key == "flavor") {
            std::string fs = val.to_string();
            int fo = (objFlags & OBJBIT_FLAVOR) >> 24;
            int fi = -1;
            if (fs == "square") fi = 0;
            else if (fs == "slash") fi = 1;
            else if (fs == "cross") fi = 2;
            else if (fs == "frame") fi = 3;
            if (fi != -1 && fi !=fo) {
                objFlags = (objFlags & ~OBJBIT_FLAVOR) | (fi << 24);
                if (isDisplayable())
                    init_model();
            }
            return;
        } else
            Stone::setAttr(key, val);
    }
    
    Value PassageStone::getAttr(const std::string &key) const {
        if (key == "color") {
            return state;
        } else if (key == "flavor") {
            return flavor();
        }
        return Stone::getAttr(key);
    }
    
    Value PassageStone::message(const Message &m) {
        if (m.message == "signal" || m.message == "_trigger") {
            toggleState();
            return Value();
        }
        return Stone::message(m);
    }
    
    void PassageStone::init_model()  {
        set_model(ecl::strf("st_passage_%s_%s", (state == BLACK) ? "black" : "white", flavor().c_str()));
    }

    bool PassageStone::is_floating() const {
        return true;
    }
    
    bool PassageStone::is_transparent (Direction d) const {
        return true;
    }
    
    StoneResponse PassageStone::collision_response(const StoneContact &sc) {
        Value accolor = sc.actor->getAttr("color"); 
        
        if (server::GameCompatibility != GAMET_ENIGMA && sc.actor->getClass() != "ac_marble")
            return STONE_REBOUND;
            
        if (state == BLACK) {
            return (accolor && accolor == BLACK) ?  STONE_PASS : STONE_REBOUND;
        }
        else {
            return (accolor && accolor == WHITE) ?  STONE_PASS : STONE_REBOUND;
        }
    }
    
    std::string PassageStone::flavor() const {
        switch ((objFlags & OBJBIT_FLAVOR) >> 24) {
            case 0 : return "square";
            case 1 : return "slash";
            case 2 : return "cross";
            case 3 : return "frame";
        }
        ASSERT(false, XLevelRuntime, "PassageStone unknown flavor");
    }

    int PassageStone::traitsIdx() const {
        return ((objFlags & OBJBIT_FLAVOR) >> 24) + 4 * state;
    }

    StoneTraits PassageStone::traits[8] = {
        {"st_passage_black_square", st_passage_black_square, stf_transparent, material_stone, 1.0, MOVABLE_PERSISTENT},
        {"st_passage_black_slash",  st_passage_black_slash,  stf_transparent, material_stone, 1.0, MOVABLE_PERSISTENT},
        {"st_passage_black_cross",  st_passage_black_cross,  stf_transparent, material_stone, 1.0, MOVABLE_PERSISTENT},
        {"st_passage_black_frame",  st_passage_black_frame,  stf_transparent, material_stone, 1.0, MOVABLE_PERSISTENT},
        {"st_passage_white_square", st_passage_white_square, stf_transparent, material_stone, 1.0, MOVABLE_PERSISTENT},
        {"st_passage_white_slash",  st_passage_white_slash,  stf_transparent, material_stone, 1.0, MOVABLE_PERSISTENT},
        {"st_passage_white_cross",  st_passage_white_cross,  stf_transparent, material_stone, 1.0, MOVABLE_PERSISTENT},
        {"st_passage_white_frame",  st_passage_white_frame,  stf_transparent, material_stone, 1.0, MOVABLE_PERSISTENT},
    };
    
    BOOT_REGISTER_START
        BootRegister(new PassageStone(BLACK, 0), "st_passage");
        BootRegister(new PassageStone(BLACK, 0), "st_passage_black");
        BootRegister(new PassageStone(BLACK, 0), "st_passage_black_square");
        BootRegister(new PassageStone(BLACK, 1), "st_passage_black_slash");
        BootRegister(new PassageStone(BLACK, 2), "st_passage_black_cross");
        BootRegister(new PassageStone(BLACK, 3), "st_passage_black_frame");
        BootRegister(new PassageStone(WHITE, 0), "st_passage_white");
        BootRegister(new PassageStone(WHITE, 0), "st_passage_white_square");
        BootRegister(new PassageStone(WHITE, 1), "st_passage_white_slash");
        BootRegister(new PassageStone(WHITE, 2), "st_passage_white_cross");
        BootRegister(new PassageStone(WHITE, 3), "st_passage_white_frame");
    BOOT_REGISTER_END

} // namespace enigma