/*
OrpheusMS: MapleStory Private Server based on OdinMS
Copyright (C) 2012 Aaron Weiss <aaron@deviant-core.net>
Patrick Huy <patrick.huy@frz.cc>
Matthias Butz <matze@odinms.de>
Jan Christian Meyer <vimes@odinms.de>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package scripting.reactor;
import java.awt.Point;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import client.Equip;
import client.IItem;
import client.Item;
import client.MapleClient;
import client.MapleInventoryType;
import scripting.AbstractPlayerInteraction;
import server.MapleItemInformationProvider;
import server.life.MapleLifeFactory;
import server.life.MapleNPC;
import server.maps.MapleReactor;
import tools.MaplePacketCreator;
import server.maps.MapMonitor;
import server.maps.ReactorDropEntry;
/**
* @author Lerk
*/
public class ReactorActionManager extends AbstractPlayerInteraction {
private MapleReactor reactor;
private MapleClient client;
public ReactorActionManager(MapleClient c, MapleReactor reactor) {
super(c);
this.reactor = reactor;
this.client = c;
}
public void dropItems() {
dropItems(false, 0, 0, 0, 0);
}
public void dropItems(boolean meso, int mesoChance, int minMeso, int maxMeso) {
dropItems(meso, mesoChance, minMeso, maxMeso, 0);
}
public void dropItems(boolean meso, int mesoChance, int minMeso, int maxMeso, int minItems) {
List<ReactorDropEntry> chances = getDropChances();
List<ReactorDropEntry> items = new LinkedList<ReactorDropEntry>();
int numItems = 0;
if (meso && Math.random() < (1 / (double) mesoChance)) {
items.add(new ReactorDropEntry(0, mesoChance));
}
Iterator<ReactorDropEntry> iter = chances.iterator();
while (iter.hasNext()) {
ReactorDropEntry d = iter.next();
if (Math.random() < (1 / (double) d.chance)) {
numItems++;
items.add(d);
}
}
while (items.size() < minItems) {
items.add(new ReactorDropEntry(0, mesoChance));
numItems++;
}
java.util.Collections.shuffle(items);
final Point dropPos = reactor.getPosition();
dropPos.x -= (12 * numItems);
for (ReactorDropEntry d : items) {
if (d.itemId == 0) {
int range = maxMeso - minMeso;
int displayDrop = (int) (Math.random() * range) + minMeso;
int mesoDrop = (displayDrop * client.getWorldServer().getMesoRate());
reactor.getMap().spawnMesoDrop(mesoDrop, dropPos, reactor, client.getPlayer(), false, (byte) 0);
} else {
IItem drop;
MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
if (ii.getInventoryType(d.itemId) != MapleInventoryType.EQUIP) {
drop = new Item(d.itemId, (byte) 0, (short) 1);
} else {
drop = ii.randomizeStats((Equip) ii.getEquipById(d.itemId));
}
reactor.getMap().spawnItemDrop(reactor, getPlayer(), drop, dropPos, false, true);
}
dropPos.x += 25;
}
}
private List<ReactorDropEntry> getDropChances() {
return ReactorScriptManager.getInstance().getDrops(reactor.getId());
}
public void spawnMonster(int id) {
spawnMonster(id, 1, getPosition());
}
public void createMapMonitor(int mapId, String portal) {
new MapMonitor(client.getChannelServer().getMapFactory().getMap(mapId), portal);
}
public void spawnMonster(int id, int qty) {
spawnMonster(id, qty, getPosition());
}
public void spawnMonster(int id, int qty, int x, int y) {
spawnMonster(id, qty, new Point(x, y));
}
private void spawnMonster(int id, int qty, Point pos) {
for (int i = 0; i < qty; i++) {
reactor.getMap().spawnMonsterOnGroudBelow(MapleLifeFactory.getMonster(id), pos);
}
}
public Point getPosition() {
Point pos = reactor.getPosition();
pos.y -= 10;
return pos;
}
public void spawnNpc(int npcId) {
spawnNpc(npcId, getPosition());
}
public void spawnNpc(int npcId, Point pos) {
MapleNPC npc = MapleLifeFactory.getNPC(npcId);
if (npc != null) {
npc.setPosition(pos);
npc.setCy(pos.y);
npc.setRx0(pos.x + 50);
npc.setRx1(pos.x - 50);
npc.setFh(reactor.getMap().getFootholds().findBelow(pos).getId());
reactor.getMap().addMapObject(npc);
reactor.getMap().broadcastMessage(MaplePacketCreator.spawnNPC(npc));
}
}
public MapleReactor getReactor() {
return reactor;
}
public void spawnFakeMonster(int id) {
reactor.getMap().spawnFakeMonsterOnGroundBelow(MapleLifeFactory.getMonster(id), getPosition());
}
}