Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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/>.
- */
- package xdm;
- import java.util.ArrayList;
- import net.sf.l2j.gameserver.ThreadPoolManager;
- import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
- import net.sf.l2j.gameserver.util.Broadcast;
- import net.sf.l2j.util.Rnd;
- /**
- * @author Drake2wow
- *
- */
- public class RandomFights
- {
- protected static ArrayList<L2PcInstance> _participants = new ArrayList<>();
- public static void createFight()
- {
- new Fight();
- }
- protected static L2PcInstance pickPlayer()
- {
- if (_participants.isEmpty())
- return null;
- L2PcInstance player = _participants.get(Rnd.get(_participants.size() - 1));
- _participants.remove(player);
- if (player.isInOlympiadMode()) //need more restricts obviously
- return pickPlayer();
- return player;
- }
- protected static int f;
- protected static class Fight
- {
- //configs
- protected static int ListenerSleepTimeSeconds = 5;
- protected static int FightDurationSeconds = 180;
- //
- protected final int id = ++f;
- protected final L2PcInstance player1 = pickPlayer();
- protected final L2PcInstance player2 = pickPlayer();
- protected int timePassedSeconds;
- { //validate this fight
- if (player1 == null || player2 == null) //invalid, XXX more checks?
- {
- if (player1 != null)
- _participants.add(player1); //back on queue
- else if (player2 != null)
- _participants.add(player2); //back on queue
- Broadcast.announceToOnlinePlayers("Fight " + id + ") didn't met the required criteria, canceling...");
- }
- else //its valid, start it
- {
- Broadcast.announceToOnlinePlayers("Fight " + id + ") started, " + player1.getName() + " versus " + player2.getName() + "!");
- //teleport them in and do stuff
- //
- ThreadPoolManager.getInstance().scheduleGeneral(new FightListener(), ListenerSleepTimeSeconds * 1000);
- }
- }
- protected class FightListener implements Runnable
- {
- @Override
- public void run()
- {
- timePassedSeconds += ListenerSleepTimeSeconds;
- if (player1 == null && player2 == null)
- winnerIs(null);
- else if (player1 == null || player1.isDead() && !player2.isDead())
- winnerIs(player2);
- else if (player2 == null || player2.isDead() && !player1.isDead())
- winnerIs(player1);
- else if (timePassedSeconds >= FightDurationSeconds)
- winnerIs(player1.getMaxHp() / player1.getCurrentHp() > player2.getMaxHp() / player2.getCurrentHp() ? player1 : player2);
- else //no event in the meantime reschedule me
- ThreadPoolManager.getInstance().scheduleGeneral(this, ListenerSleepTimeSeconds * 1000);
- }
- }
- protected void winnerIs(L2PcInstance player)
- {
- if (player == null)
- Broadcast.announceToOnlinePlayers("Fight " + id + ") no winner!");
- else
- Broadcast.announceToOnlinePlayers("Fight " + id + ") " + player.getName() + " wins!");
- //reward
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement