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
|
/*
* Copyright 2008 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"
// Sigcore includes
#include "../sigcore/Fraction.h"
// Sigscript includes
#include "../sigscript/MoveWrapper.h"
#include "../sigscript/SigmodWrapper.h"
// KDE includes
#include <kross/core/action.h>
#include <kross/core/actioncollection.h>
// Qt includes
#include <QtAlgorithms>
#include <QtCore/QtConcurrentMap>
#include <QtCore/QFuture>
#include <QtCore/QTimer>
bool Sigbattle::sortActions(const TeamMember::RequestedAction& reqAction1, const TeamMember::RequestedAction& reqAction2)
{
TeamMember::Action action1 = reqAction1.second.isFinished() ? reqAction1.second : TeamMember::Action(TeamMember::Timeout, TeamMember::ActionData());
TeamMember::Action action2 = reqAction2.second.isFinished() ? reqAction2.second : TeamMember::Action(TeamMember::Timeout, TeamMember::ActionData());
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;
}
Sigbattle::TurnArena::TurnArena(Sigscript::SigmodWrapper* sigmod, QSet<Player*> players, const bool isWild, Sigscript::Config* parent) :
Arena(sigmod, players, isWild, parent),
m_watcher(new QFutureWatcher<TeamMember::RequestedAction>(this)),
m_timer(new QTimer(this))
{
setupBattle();
m_timer->setSingleShot(true);
connect(m_timer, SIGNAL(timeout()), m_watcher, SLOT(cancel()));
}
void Sigbattle::TurnArena::setupBattle()
{
// TODO: setup everything for the arena to do with the turns
Arena::setupBattle();
}
void Sigbattle::TurnArena::processRound()
{
emit(roundAboutToStart());
emit(roundStart());
QFuture<TeamMember::RequestedAction> reqActions = QtConcurrent::mapped(active(), &Sigbattle::requestDecision);
m_watcher->setFuture(reqActions);
m_timer->start(120000);
reqActions.waitForFinished();
m_timer->stop();
QList<TeamMember::RequestedAction> actions = reqActions.results();
qStableSort(actions.begin(), actions.end(), sortActions);
for (int i = 1; i < actions.size(); ++i)
{
TeamMember::Action action = actions[i].second;
if (action.first == TeamMember::Attack)
{
Sigscript::MoveWrapper* move = sigmod()->move(action.second.first.toInt());
const Sigcore::Script script = move->priorityScript();
if (!script.script().isEmpty())
{
Kross::Action* kaction = new Kross::Action(m_actions, QUuid::createUuid().toString());
kaction->setInterpreter(script.interpreter());
kaction->setCode(script.script().toUtf8());
kaction->addObject(this, "arena");
kaction->addObject(actions[i].first, "user");
for (int j = 0; j < i; ++j)
kaction->addObject(actions[j].first, QString("fighter%1").arg(j));
kaction->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(roundEnd());
if (!isOver())
processRound();
}
|