summaryrefslogtreecommitdiffstats
path: root/libksirtet/common/inter.cpp
blob: 2cd3e981b8825f2264639f2088b2b77af3f4d672 (plain)
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
145
146
147
148
149
150
#include "inter.h"
#include "inter.moc"

#include <klocale.h>
#include <tdeaction.h>
#include <tdeapplication.h>

#include "factory.h"
#include "field.h"
#include "commonprefs.h"
#include "main.h"


const ActionData Interface::ACTION_DATA[Nb_Actions] = {
    { I18N_NOOP("Move Left"),     "move left",     TQT_SLOT(moveLeft()),  0 },
    { I18N_NOOP("Move Right"),    "move right",    TQT_SLOT(moveRight()), 0 },
    { I18N_NOOP("Drop Down"),     "drop down",     TQT_SLOT(dropDownStart()),
                                                   TQT_SLOT(dropDownStop()) },
    { I18N_NOOP("One Line Down"), "one line down", TQT_SLOT(oneLineDown()), 0 },
    { I18N_NOOP("Rotate Left"),   "rotate left",   TQT_SLOT(rotateLeft()),  0 },
    { I18N_NOOP("Rotate Right"),  "rotate right",  TQT_SLOT(rotateRight()), 0 },
    { I18N_NOOP("Move to Left Column"), "move left total",
                                                   TQT_SLOT(moveLeftTotal()), 0 },
    { I18N_NOOP("Move to Right Column"), "move right total",
                                                   TQT_SLOT(moveRightTotal()), 0 }
};

const int Interface::KEYCODE_ONE[Nb_Actions] = {
    Key_Left, Key_Right, Key_Down, Key_Shift, Key_Up, Key_Return,
    CTRL+Key_Left, CTRL+Key_Right
};
const int Interface::KEYCODE_TWO[Nb_Actions] = {
    Key_F, Key_G, Key_D, Key_Space, Key_E, Key_C, SHIFT+Key_F, SHIFT+Key_G
};

Interface::Interface(const MPGameInfo &gi, TQWidget *parent)
    : MPSimpleInterface(gi, Nb_Actions, ACTION_DATA, parent)
{
    setDefaultKeycodes(1, 0, KEYCODE_ONE);
    setDefaultKeycodes(2, 0, KEYCODE_TWO);
    setDefaultKeycodes(2, 1, KEYCODE_ONE);
}

MPBoard *Interface::newBoard(uint i)
{
    Field *f = static_cast<Field *>(cfactory->createField(this));
    f->settingsChanged();
    connect(this, TQT_SIGNAL(settingsChanged()), f, TQT_SLOT(settingsChanged()));
    if ( i==0 ) _firstField = f;
    return f;
}

void Interface::normalGame()
{
    singleHuman();
}

void Interface::arcadeGame()
{
    singleHuman();
    _firstField->setArcade();
}

void Interface::_init()
{
    if ( !server() ) return;
    _data.resize(nbPlayers());
    _scores.setPlayerCount( nbPlayers() );
    for (uint i=0; i<nbPlayers(); i++)
        _scores.setName(i, playerName(i));
}

bool Interface::_readPlayData()
{
    bool end = false;
    for (uint i=0; i<nbPlayers(); i++) {
        readingStream(i) >> _data[i];
        if (_data[i].end) end = true;
    }
    return end;
}

void Interface::_sendPlayData()
{
    ServerPlayData sd;
    for(uint i=0; i<nbPlayers(); i++) {
        sd.prevHeight = _data[prev(i)].height;
        sd.nextHeight = _data[next(i)].height;
        sd.gift       = _data[prev(i)].gift;
        writingStream(i) << sd;
    }
}

void Interface::_showHighscores(TQWidget *parent)
{
    if ( !server() || nbPlayers()!=1 ) _scores.show(parent);
    else BaseInterface::_showHighscores(parent);
}

void Interface::_showGameOverData()
{
    if ( !server() || nbPlayers()!=1 ) _scores.show(this);
    else if ( !_firstField->isArcade() ) {
        _score.setType(KExtHighscore::Won);
        BaseField::gameOver(_score, this);
    }
}

uint Interface::prev(uint i) const
{
	if ( i==0 ) return nbPlayers()-1;
	else return i-1;
}

uint Interface::next(uint i) const
{
	if ( i==(nbPlayers()-1) ) return 0;
	else return i+1;
}

// server only
void Interface::_treatInit()
{
    ServerInitData sid;
    sid.seed = kapp->random();
    sid.initLevel = CommonPrefs::initialGameLevel();
    for (uint i=0; i<nbPlayers(); i++) {
        sid.prevName = playerName(prev(i));
        sid.nextName = playerName(next(i));
        sid.name     = playerName(i);
        writingStream(i) << sid;
    }
}

void Interface::_sendGameOverData(TQDataStream &s)
{
    bool multiplayers = ( nbPlayers()>1 );
    for (uint i=0; i<nbPlayers(); i++) {
        readingStream(i) >> _score;
        if (multiplayers) _scores.addScore(i, _score);
    }
    if (multiplayers) s << _scores;
    // else no need to send anything
}

// client only
void Interface::_readGameOverData(TQDataStream &s)
{
    s >> _scores;
}