/*
* 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 2, 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* http://www.gnu.org/copyleft/gpl.html
*/
/**
* @author FBIagent
*/
package net.sf.l2j.gameserver.datatables;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Map;
import javolution.util.FastMap;
import net.sf.l2j.L2DatabaseFactory;
public class PolymorphMonstersTable
{
/** The internal instance of this class (only one instance need) */
private static PolymorphMonstersTable _instance = null;
/** List of monsters who polymorph */
private Map<Integer, PolymorphMonster> _polymorphMonsters = new FastMap<Integer, PolymorphMonster>();
/** Holds all monsters there can polymorph */
private class PolymorphMonster
{
/** monster id */
private int _mobId;
/** List of polymoroph infos */
private ArrayList<PolyInfo> _polyInfos = new ArrayList<PolyInfo>();
/**
* Constructor
* @param mobId
*/
public PolymorphMonster(int mobId)
{
_mobId = mobId;
}
/**
* Add's a polymorph info
* @param polyId
* @param spawnDelay
* @param condition
* @param conditionValue
*/
public void addPolyInfo(int polyId, int spawnDelay, String cond, int condValue)
{
_polyInfos.add(new PolyInfo(polyId, spawnDelay, cond, condValue));
}
/**
* Returns the monster id
* @return int
*/
public int getMobId()
{
return _mobId;
}
/**
* Returns a polymorph info by index
* @param index
* @return int
*/
public PolyInfo getPolyInfo(int index)
{
if (index > _polyInfos.size()-1)
return null;
return _polyInfos.get(index);
}
}
/**
* Constructor
*/
public PolymorphMonstersTable()
{
java.sql.Connection con = null;
try
{
con = L2DatabaseFactory.getInstance().getConnection();
PreparedStatement statement = con.prepareStatement("SELECT `mobId`,`polyId`,`spawnDelay`,`condition`,`conditionValue` FROM `polymorph_monsters` ORDER BY `mobId` ASC, `order`;");
ResultSet resultSet = statement.executeQuery();
PolymorphMonster polymorphMonster = null;
int lastMobId = 0,
nextMobId = 0;
while (resultSet.next())
{
if (polymorphMonster == null)
{
lastMobId = resultSet.getInt("mobId");
polymorphMonster = new PolymorphMonster(lastMobId);
polymorphMonster.addPolyInfo(resultSet.getInt("polyId"), resultSet.getInt("spawnDelay"), resultSet.getString("condition"), resultSet.getInt("conditionValue"));
continue;
}
nextMobId = resultSet.getInt("mobId");
if (nextMobId != lastMobId)
{
_polymorphMonsters.put(lastMobId, polymorphMonster);
polymorphMonster = new PolymorphMonster(nextMobId);
polymorphMonster.addPolyInfo(resultSet.getInt("polyId"), resultSet.getInt("spawnDelay"), resultSet.getString("condition"), resultSet.getInt("conditionValue"));
lastMobId = nextMobId;
continue;
}
polymorphMonster.addPolyInfo(resultSet.getInt("polyId"), resultSet.getInt("spawnDelay"), resultSet.getString("condition"), resultSet.getInt("conditionValue"));
}
_polymorphMonsters.put(nextMobId, polymorphMonster);
polymorphMonster = null;
resultSet.close();
statement.close();
}
catch (Exception e)
{
System.out.println("Error while loading polymorph monsters: " + e);
}
finally
{
try { con.close(); } catch (Exception e) {}
System.out.println("PolymorphMonstersTable: Loaded " + _polymorphMonsters.size() + " polymorph monsters.");
}
}
/**
* Returns instance of PolymorphMonstersTable
* @return PolymorphMonstersTable
*/
public static PolymorphMonstersTable getInstance()
{
if (_instance == null)
_instance = new PolymorphMonstersTable();
return _instance;
}
/**
* Returns a polymorph info by mobId and polymorphed count
* @param mobId
* @param polyCount
* @return int
*/
public PolyInfo getPolyInfo(int mobId, int polyCount)
{
PolymorphMonster polyMonster = _polymorphMonsters.get(mobId);
if (polyMonster == null)
return null;
return polyMonster.getPolyInfo(polyCount);
}
}