diff options
author | Ben Boeckel <MathStuf@gmail.com> | 2009-03-05 18:51:01 -0500 |
---|---|---|
committer | Ben Boeckel <MathStuf@gmail.com> | 2009-03-05 18:51:01 -0500 |
commit | 450814b0562b417fbd5db0db606decd0c3fcc977 (patch) | |
tree | 3136e3d3516be2bae95f47d7522d54288ffcfa2e /sigencore | |
parent | 88560349afde25f4f36a49ba4cfb90c4a7248b86 (diff) | |
download | sigen-450814b0562b417fbd5db0db606decd0c3fcc977.tar.gz sigen-450814b0562b417fbd5db0db606decd0c3fcc977.tar.xz sigen-450814b0562b417fbd5db0db606decd0c3fcc977.zip |
Move ATB arena code from sigbattle to arenaplugin
Diffstat (limited to 'sigencore')
-rw-r--r-- | sigencore/plugins/arenas/atb/ATBArena.cpp | 71 | ||||
-rw-r--r-- | sigencore/plugins/arenas/atb/ATBArena.h | 54 | ||||
-rw-r--r-- | sigencore/plugins/arenas/atb/ATBTimer.cpp | 92 | ||||
-rw-r--r-- | sigencore/plugins/arenas/atb/ATBTimer.h | 56 | ||||
-rw-r--r-- | sigencore/plugins/arenas/atb/ActionQueue.h | 71 |
5 files changed, 344 insertions, 0 deletions
diff --git a/sigencore/plugins/arenas/atb/ATBArena.cpp b/sigencore/plugins/arenas/atb/ATBArena.cpp new file mode 100644 index 00000000..eaea0134 --- /dev/null +++ b/sigencore/plugins/arenas/atb/ATBArena.cpp @@ -0,0 +1,71 @@ +/* + * 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 includes +#include "ATBArena.h" + +// Sigbattle includes +#include "ATBTimer.h" + +// Qt includes +#include <QtCore/QTimer> + +Sigbattle::ATBArena::ATBArena(Sigscript::SigmodWrapper* sigmod, QSet<Player*> players, const bool isWild, Sigscript::Config* parent) : + Arena(sigmod, players, isWild, parent) +{ + setupBattle(); +} + +void Sigbattle::ATBArena::handleAction(TeamMember* teamMember, TeamMember::Action action) +{ + if (action.first == TeamMember::Skip) + { + m_decisions.enqueue(requestDecision(teamMember)); + return; + } + Arena::handleAction(teamMember, action); +} + +void Sigbattle::ATBArena::setupBattle() +{ + m_atbTimer = new ATBTimer(this, m_decisions); + m_processTicker = new QTimer(this); + connect(m_processTicker, SIGNAL(timeout()), this, SLOT(processActions())); + m_processTicker->start(100); + Arena::setupBattle(); +} + +void Sigbattle::ATBArena::processActions() +{ + if (!m_decisions.isEmpty()) + { + while (m_decisions.head().second.isFinished()) + { + TeamMember::RequestedAction action = m_decisions.dequeue(); + handleAction(action.first, action.second.result()); + } + } +} + +void Sigbattle::ATBArena::cleanUp() +{ + delete m_atbTimer; + m_atbTimer = NULL; + delete m_processTicker; + m_processTicker = NULL; + Arena::cleanUp(); +} diff --git a/sigencore/plugins/arenas/atb/ATBArena.h b/sigencore/plugins/arenas/atb/ATBArena.h new file mode 100644 index 00000000..f5398d22 --- /dev/null +++ b/sigencore/plugins/arenas/atb/ATBArena.h @@ -0,0 +1,54 @@ +/* + * 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_ATBARENA +#define SIGBATTLE_ATBARENA + +// Sigbattle includes +#include "ActionQueue.h" +#include "Arena.h" + +// Forward declaraions +class QTimer; + +namespace Sigbattle +{ +class ATBTimer; + +class SIGBATTLE_EXPORT ATBArena : public Arena +{ + Q_OBJECT + + public: + ATBArena(Sigscript::SigmodWrapper* sigmod, QSet<Player*> players, const bool isWild, Sigscript::Config* parent); + + void handleAction(TeamMember* teamMember, TeamMember::Action action); + public slots: + signals: + protected: + void setupBattle(); + protected slots: + void processActions(); + void cleanUp(); + private: + ActionQueue m_decisions; + ATBTimer* m_atbTimer; + QTimer* m_processTicker; +}; +} + +#endif diff --git a/sigencore/plugins/arenas/atb/ATBTimer.cpp b/sigencore/plugins/arenas/atb/ATBTimer.cpp new file mode 100644 index 00000000..06abc827 --- /dev/null +++ b/sigencore/plugins/arenas/atb/ATBTimer.cpp @@ -0,0 +1,92 @@ +/* + * 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 "ATBTimer.h" + +// Sigbattle includes +#include "ATBArena.h" + +// Sigscript includes +#include "../sigscript/RulesWrapper.h" +#include "../sigscript/SigmodWrapper.h" + +// Qt includes +#include <QtCore/QtAlgorithms> +#include <QtCore/QtConcurrentFilter> +#include <QtCore/QtConcurrentMap> +#include <QtCore/QFuture> +#include <QtCore/QMultiMap> +#include <QtCore/QTimerEvent> + +bool Sigbattle::compareTimers(TeamMember* const & member1, TeamMember* const & member2) +{ + return member1->timer() < member2->timer(); +} + +void Sigbattle::increaseMeter(TeamMember* teamMember) +{ + // TODO: Tweak this? + teamMember->advanceTimer(teamMember->statValue(Sigmod::ST_Speed) / 10); +} + +bool Sigbattle::isOverflowed(const TeamMember* teamMember) +{ + // TODO: Tweak threshold? + return 1000. < teamMember->timer(); +} + +Sigbattle::ATBTimer::ATBTimer(ATBArena* arena, ActionQueue& actions) : + QThread(arena), + m_arena(arena), + m_actions(actions) +{ +} + +Sigbattle::ATBTimer::~ATBTimer() +{ +} + +void Sigbattle::ATBTimer::run() +{ + startTimer(50); + exec(); +} + +void Sigbattle::ATBTimer::timerEvent(QTimerEvent* event) +{ + Q_UNUSED(event) + const QList<TeamMember*> active = m_arena->active(); + QList<TeamMember::RequestedAction> actions; + QtConcurrent::blockingMap(active, increaseMeter); + QList<TeamMember*> overflow = QtConcurrent::blockingFiltered(active, isOverflowed); + qSort(overflow.begin(), overflow.end(), compareTimers); + foreach (TeamMember* teamMember, overflow) + { + TeamMember::RequestedAction action = requestDecision(teamMember); + m_actions.enqueue(action); +// if (m_arena->sigmod()->rules()->pausedATB()) +// actions.append(action); + } +// if (m_arena->sigmod()->rules()->pausedATB()) +// { +// killTimer(event->timerId()); +// for (QMutableListIterator<TeamMember::RequestedAction> i(actions); i.hasNext(); i.next()) +// i.value().second.waitForFinished(); +// startTimer(50); +// } +} diff --git a/sigencore/plugins/arenas/atb/ATBTimer.h b/sigencore/plugins/arenas/atb/ATBTimer.h new file mode 100644 index 00000000..5053741e --- /dev/null +++ b/sigencore/plugins/arenas/atb/ATBTimer.h @@ -0,0 +1,56 @@ +/* + * 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_ATBTIMER +#define SIGBATTLE_ATBTIMER + +// Sigbattle includes +#include "Global.h" + +// Qt includes +#include <QtCore/QThread> + +namespace Sigbattle +{ +class ActionQueue; +class ATBArena; +class Player; +class TeamMember; + +class SIGBATTLE_EXPORT ATBTimer : public QThread +{ + Q_OBJECT + + public: + ATBTimer(ATBArena* arena, ActionQueue& actions); + ~ATBTimer(); + protected: + void run(); + + void timerEvent(QTimerEvent* event); + private: + ATBArena* m_arena; + ActionQueue& m_actions; +}; + +SIGBATTLE_EXPORT bool compareTimers(TeamMember* const & member1, TeamMember* const & member2); +SIGBATTLE_EXPORT void increaseMeter(TeamMember* teamMember); +SIGBATTLE_EXPORT bool isOverflowed(const TeamMember* teamMember); + +} + +#endif diff --git a/sigencore/plugins/arenas/atb/ActionQueue.h b/sigencore/plugins/arenas/atb/ActionQueue.h new file mode 100644 index 00000000..fc3a8232 --- /dev/null +++ b/sigencore/plugins/arenas/atb/ActionQueue.h @@ -0,0 +1,71 @@ +/* + * 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_ACTIONQUEUE +#define SIGBATTLE_ACTIONQUEUE + +// Sigbattle includes +#include "Global.h" +#include "TeamMember.h" + +// Qt includes +#include <QtCore/QMutex> +#include <QtCore/QMutexLocker> +#include <QtCore/QQueue> + +namespace Sigbattle +{ +class SIGBATTLE_EXPORT ActionQueue +{ + public: + TeamMember::RequestedAction dequeue(); + void enqueue(const TeamMember::RequestedAction& action); + TeamMember::RequestedAction& head(); + + bool isEmpty(); + private: + QQueue<TeamMember::RequestedAction> m_queue; + QMutex m_mutex; +}; + +inline TeamMember::RequestedAction ActionQueue::dequeue() +{ + QMutexLocker locker(&m_mutex); + return m_queue.dequeue(); +} + +inline void ActionQueue::enqueue(const TeamMember::RequestedAction& action) +{ + QMutexLocker locker(&m_mutex); + m_queue.enqueue(action); +} + +inline TeamMember::RequestedAction& ActionQueue::head() +{ + QMutexLocker locker(&m_mutex); + return m_queue.head(); +} + +inline bool ActionQueue::isEmpty() +{ + QMutexLocker locker(&m_mutex); + return m_queue.isEmpty(); +} + +} + +#endif |