summaryrefslogtreecommitdiffstats
path: root/sigencore/plugins
diff options
context:
space:
mode:
authorBen Boeckel <MathStuf@gmail.com>2009-03-05 10:32:27 -0500
committerBen Boeckel <MathStuf@gmail.com>2009-03-05 10:32:27 -0500
commit1be72ce76f243027dafcd7eefe60c6ff14cd9464 (patch)
treefffd47287cdd1ce69197f2eb557d8ec538b2aa68 /sigencore/plugins
parentb6277d85e51d1e1e20bbd35579ec41031ef1f491 (diff)
downloadsigen-1be72ce76f243027dafcd7eefe60c6ff14cd9464.tar.gz
sigen-1be72ce76f243027dafcd7eefe60c6ff14cd9464.tar.xz
sigen-1be72ce76f243027dafcd7eefe60c6ff14cd9464.zip
Move TurnArena to plugin for default arenas
Diffstat (limited to 'sigencore/plugins')
-rw-r--r--sigencore/plugins/arenas/standard/TurnArena.cpp115
-rw-r--r--sigencore/plugins/arenas/standard/TurnArena.h59
2 files changed, 174 insertions, 0 deletions
diff --git a/sigencore/plugins/arenas/standard/TurnArena.cpp b/sigencore/plugins/arenas/standard/TurnArena.cpp
new file mode 100644
index 00000000..2d006011
--- /dev/null
+++ b/sigencore/plugins/arenas/standard/TurnArena.cpp
@@ -0,0 +1,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();
+}
diff --git a/sigencore/plugins/arenas/standard/TurnArena.h b/sigencore/plugins/arenas/standard/TurnArena.h
new file mode 100644
index 00000000..91222d27
--- /dev/null
+++ b/sigencore/plugins/arenas/standard/TurnArena.h
@@ -0,0 +1,59 @@
+/*
+ * 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/>.
+ */
+
+#ifndef SIGBATTLE_TURNARENA
+#define SIGBATTLE_TURNARENA
+
+// Sigbattle includes
+#include "Arena.h"
+#include "TeamMember.h"
+
+// Qt includes
+#include <QtCore/QFutureWatcher>
+
+// Forward declarations
+class QTimer;
+
+namespace Sigbattle
+{
+class SIGBATTLE_EXPORT TurnArena : public Arena
+{
+ Q_OBJECT
+
+ public:
+ TurnArena(Sigscript::SigmodWrapper* sigmod, QSet<Player*> players, const bool isWild, Sigscript::Config* parent);
+ public slots:
+ signals:
+ void roundAboutToStart();
+ void roundStart();
+ void roundAboutToEnd();
+ void roundEnd();
+ protected:
+ void setupBattle();
+ protected slots:
+ void processRound();
+ private:
+ QFutureWatcher<TeamMember::RequestedAction>* m_watcher;
+ QTimer* m_timer;
+};
+
+bool sortActions(const TeamMember::RequestedAction& reqAction1, const TeamMember::RequestedAction& reqAction2);
+
+}
+Q_DECLARE_METATYPE(Sigbattle::TurnArena*)
+
+#endif