summaryrefslogtreecommitdiffstats
path: root/plugins/arena/standard/turn/TurnArena.cpp
blob: 95af8086f1d8356ef9ea6f969de4b1cf761da531 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Copyright 2008-2009 Ben Boeckel <MathStuf@gmail.com>
 * 
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

// Header include
#include "TurnArena.h"

// Sigencore includes
#include <sigencore/Player.h>
#include <sigencore/RunScript.h>
#include <sigencore/Team.h>

// Sigscript includes
#include <sigscript/GameWrapper.h>
#include <sigscript/MoveWrapper.h>

// KDE includes
#include <KIcon>
#include <kross/core/actioncollection.h>

// Qt includes
#include <QtCore/QtAlgorithms>
#include <QtCore/QtConcurrentMap>
#include <QtCore/QFuture>
#include <QtGui/QIcon>

using namespace Sigcore;
using namespace Sigscript;
using namespace Sigencore;

bool sortActions(const TeamMember::RequestedAction& reqAction1, const TeamMember::RequestedAction& reqAction2)
{
    TeamMember::Action action1 = reqAction1.second.isCanceled() ? TeamMember::Action(TeamMember::Timeout, TeamMember::ActionData()) : reqAction1.second;
    TeamMember::Action action2 = reqAction2.second.isCanceled() ? TeamMember::Action(TeamMember::Timeout, TeamMember::ActionData()) : reqAction2.second;
    const int priority1 = actionPriority(reqAction1.first, action1);
    const int priority2 = actionPriority(reqAction1.first, action2);
    if (priority1 < priority2)
        return true;
    else if (priority1 == priority2)
    {
        const int speed1 = reqAction1.first->statValue(Sigmod::ST_Speed);
        const int speed2 = reqAction2.first->statValue(Sigmod::ST_Speed);
        if (speed1 < speed2)
            return true;
        else if (speed1 == speed2)
            return Sigcore::Fraction::poll(1, 2);
    }
    return false;
}

TurnArena::TurnArena(GameWrapper* sigmod, Config* parent) :
        Arena(sigmod, parent),
        m_priorityScripts(new Kross::ActionCollection("priority-scripts", m_actions))
{
    setupBattle();
}

QString TurnArena::name()
{
    return "Sigen turn arena";
}

QString TurnArena::description()
{
    return "An arena where creatures act in order of their speed (unless move priority takes precedence)";
}

QIcon TurnArena::icon()
{
    // TODO
    return KIcon();
}

QStringList TurnArena::extensions()
{
    return QStringList();
}

bool TurnArena::isTeamAllowed(Sigencore::Team* team)
{
    Q_UNUSED(team)
    // TODO: Use rules format here?
    return true;
}

void TurnArena::handleAction(TeamMember* teamMember, TeamMember::Action action)
{
    Arena::handleAction(teamMember, action);
}

void TurnArena::setupBattle()
{
    connect(this, SIGNAL(battleStarted()), SLOT(processRound()));
    Arena::setupBattle();
}

void TurnArena::distributeWinnings()
{
    Arena::distributeWinnings();
}

void TurnArena::checkForLosers()
{
    Arena::checkForLosers();
}

void TurnArena::registerScript(const Script& script)
{
    Arena::registerScript(script);
}

void TurnArena::cleanUp()
{
    Arena::cleanUp();
}

void TurnArena::processRound()
{
    emit(roundAboutToStart());
    emit(roundStarted());
    QFuture<TeamMember::RequestedAction> reqActions = QtConcurrent::mapped(active(Fighters), &requestDecision);
    reqActions.waitForFinished();
    QList<TeamMember::RequestedAction> actions = reqActions.results();
    qStableSort(actions.begin(), actions.end(), sortActions);
    foreach (const TeamMember::RequestedAction& reqAction, actions)
    {
        TeamMember::Action action = reqAction.second;
        if (action.first == TeamMember::Attack)
        {
            MoveWrapper* move = m_game->move(action.second.first.toInt());
            const Script script = move->priorityScript();
            ObjectMap objects;
            objects["arena"] = this;
            objects["move"] = move;
            objects["owner"] = reqAction.first;
            objects["player"] = reqAction.first->team()->player();
            objects["sigmod"] = m_game;
            // TODO: Add other players
            runScript(QString("priority-%1").arg(QUuid::createUuid().toString()), script, objects, m_priorityScripts)->trigger();
        }
    }
    qStableSort(actions.begin(), actions.end(), sortActions);
    foreach (const TeamMember::RequestedAction& action, actions)
    {
        if (action.first->currentHp())
            handleAction(action.first, action.second);
    }
    emit(roundAboutToEnd());
    emit(roundEnded());
    // TODO: Check if there is anyone left to fight
    processRound();
}