Advertisement
Guest User

doorTable.java

a guest
May 20th, 2014
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * This program is free software; you can redistribute it and/or modify
  3.  * it under the terms of the GNU General Public License as published by
  4.  * the Free Software Foundation; either version 2, or (at your option)
  5.  * any later version.
  6.  *
  7.  * This program is distributed in the hope that it will be useful,
  8.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.  * GNU General Public License for more details.
  11.  *
  12.  * You should have received a copy of the GNU General Public License
  13.  * along with this program; if not, write to the Free Software
  14.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15.  * 02111-1307, USA.
  16.  *
  17.  * http://www.gnu.org/copyleft/gpl.html
  18.  */
  19. package net.sf.l2j.gameserver.datatables;
  20.  
  21. import java.io.BufferedReader;
  22. import java.io.File;
  23. import java.io.FileNotFoundException;
  24. import java.io.FileReader;
  25. import java.io.IOException;
  26. import java.io.LineNumberReader;
  27. import java.util.Map;
  28. import java.util.StringTokenizer;
  29. import java.util.logging.Logger;
  30.  
  31. import javolution.util.FastMap;
  32. import net.sf.l2j.Config;
  33. import net.sf.l2j.gameserver.idfactory.IdFactory;
  34. import net.sf.l2j.gameserver.instancemanager.ClanHallManager;
  35. import net.sf.l2j.gameserver.model.actor.instance.L2DoorInstance;
  36. import net.sf.l2j.gameserver.model.entity.ClanHall;
  37. import net.sf.l2j.gameserver.pathfinding.AbstractNodeLoc;
  38. import net.sf.l2j.gameserver.templates.L2CharTemplate;
  39. import net.sf.l2j.gameserver.templates.StatsSet;
  40.  
  41. public class DoorTable
  42. {
  43.     private static Logger _log = Logger.getLogger(DoorTable.class.getName());
  44.  
  45.     private Map<Integer,L2DoorInstance> _staticItems;
  46.  
  47.     private static DoorTable _instance;
  48.  
  49.     public static DoorTable getInstance()
  50.     {
  51.         if (_instance == null)
  52.             _instance = new DoorTable();
  53.  
  54.         return _instance;
  55.     }
  56.  
  57.     public DoorTable()
  58.     {
  59.         _staticItems = new FastMap<>();
  60.  
  61.     }
  62.  
  63.  
  64.     public void reloadAll()
  65.     {
  66.         respawn();
  67.  
  68.     }
  69.  
  70.     public void respawn()
  71.     {
  72.         _staticItems = null;
  73.         _instance = null;
  74.         _instance = new DoorTable();
  75.     }
  76.  
  77.     public void parseData()
  78.     {
  79.         LineNumberReader lnr = null;
  80.         try
  81.         {
  82.             File doorData = new File(Config.DATAPACK_ROOT, "data/door.csv");
  83.             lnr = new LineNumberReader(new BufferedReader(new FileReader(doorData)));
  84.  
  85.             String line = null;
  86.             _log.warning("Searching clan halls doors:");
  87.  
  88.             while ((line = lnr.readLine()) != null)
  89.             {
  90.                 if (line.trim().length() == 0 || line.startsWith("#"))
  91.                     continue;
  92.  
  93.                 L2DoorInstance door = parseList(line);
  94.                 putDoor(door);
  95.                 door.spawnMe(door.getX(), door.getY(),door.getZ());
  96.  
  97.                 ClanHall clanhall = ClanHallManager.getInstance().getNearbyClanHall(door.getX(), door.getY(), 500);
  98.  
  99.                 if (clanhall != null)
  100.                 {
  101.                     clanhall.getDoors().add(door);
  102.                     door.setClanHall(clanhall);
  103.  
  104.                     if (Config.DEBUG)
  105.                         _log.warning("door "+door.getDoorName()+" attached to ch "+clanhall.getName());
  106.  
  107.                 }
  108.             }
  109.  
  110.             _log.config("DoorTable: Loaded " + _staticItems.size() + " Door Templates.");
  111.         }
  112.         catch (FileNotFoundException e)
  113.         {
  114.             _initialized = false;
  115.             _log.warning("door.csv is missing in data folder");
  116.         }
  117.         catch (IOException e)
  118.         {
  119.             _initialized = false;
  120.             _log.warning("error while creating door table " + e);
  121.         }
  122.         finally
  123.         {
  124.             try
  125.             {
  126.                 lnr.close();
  127.             }
  128.             catch (Exception e1)
  129.             {
  130.                 /* ignore problems */
  131.             }
  132.         }
  133.     }
  134.  
  135.     public static L2DoorInstance parseList(String line)
  136.     {
  137.         StringTokenizer st = new StringTokenizer(line, ";");
  138.  
  139.         String name = st.nextToken();
  140.         int id = Integer.parseInt(st.nextToken());
  141.         int x = Integer.parseInt(st.nextToken());
  142.         int y = Integer.parseInt(st.nextToken());
  143.         int z = Integer.parseInt(st.nextToken());
  144.         int rangeXMin = Integer.parseInt(st.nextToken());
  145.         int rangeYMin = Integer.parseInt(st.nextToken());
  146.         int rangeZMin = Integer.parseInt(st.nextToken());
  147.         int rangeXMax = Integer.parseInt(st.nextToken());
  148.         int rangeYMax = Integer.parseInt(st.nextToken());
  149.         int rangeZMax = Integer.parseInt(st.nextToken());
  150.  
  151.         int hp = Integer.parseInt(st.nextToken());
  152.         int pdef = Integer.parseInt(st.nextToken());
  153.         int mdef = Integer.parseInt(st.nextToken());
  154.         boolean unlockable = false;
  155.  
  156.         if (st.hasMoreTokens())
  157.             unlockable = Boolean.parseBoolean(st.nextToken());
  158.  
  159.         if (rangeXMin > rangeXMax)
  160.             _log.severe("Error in door data, XMin > XMax, ID:"+id);
  161.         if (rangeYMin > rangeYMax)
  162.             _log.severe("Error in door data, YMin > YMax, ID:"+id);
  163.         if (rangeZMin > rangeZMax)
  164.             _log.severe("Error in door data, ZMin > ZMax, ID:"+id);
  165.  
  166.         int collisionRadius; // (max) radius for movement checks
  167.         if ((rangeXMax - rangeXMin) > (rangeYMax - rangeYMin))
  168.             collisionRadius = rangeYMax - rangeYMin;
  169.         else
  170.             collisionRadius = rangeXMax - rangeXMin;
  171.  
  172.         StatsSet npcDat = new StatsSet();
  173.         npcDat.set("npcId", id);
  174.         npcDat.set("level", 0);
  175.         npcDat.set("jClass", "door");
  176.  
  177.         npcDat.set("baseSTR", 0);
  178.         npcDat.set("baseCON", 0);
  179.         npcDat.set("baseDEX", 0);
  180.         npcDat.set("baseINT", 0);
  181.         npcDat.set("baseWIT", 0);
  182.         npcDat.set("baseMEN", 0);
  183.  
  184.         npcDat.set("baseShldDef", 0);
  185.         npcDat.set("baseShldRate", 0);
  186.         npcDat.set("baseAccCombat", 38);
  187.         npcDat.set("baseEvasRate",  38);
  188.         npcDat.set("baseCritRate",  38);
  189.  
  190.  
  191.         npcDat.set("collision_radius", collisionRadius);
  192.         npcDat.set("collision_height", rangeZMax - rangeZMin);
  193.         npcDat.set("sex", "male");
  194.         npcDat.set("type", "");
  195.         npcDat.set("baseAtkRange", 0);
  196.         npcDat.set("baseMpMax", 0);
  197.  
  198.         npcDat.set("baseCpMax", 0);
  199.         npcDat.set("rewardExp", 0);
  200.         npcDat.set("rewardSp", 0);
  201.         npcDat.set("basePAtk", 0);
  202.         npcDat.set("baseMAtk", 0);
  203.         npcDat.set("basePAtkSpd", 0);
  204.         npcDat.set("aggroRange", 0);
  205.         npcDat.set("baseMAtkSpd", 0);
  206.         npcDat.set("rhand", 0);
  207.         npcDat.set("lhand", 0);
  208.         npcDat.set("armor", 0);
  209.         npcDat.set("baseWalkSpd", 0);
  210.         npcDat.set("baseRunSpd", 0);
  211.         npcDat.set("name", name);
  212.         npcDat.set("baseHpMax", hp);
  213.         npcDat.set("baseHpReg", 3.e-3f);
  214.  
  215.         npcDat.set("baseMpReg", 3.e-3f);
  216.         npcDat.set("basePDef", pdef);
  217.         npcDat.set("baseMDef", mdef);
  218.  
  219.         L2CharTemplate template = new L2CharTemplate(npcDat);
  220.         L2DoorInstance door = new L2DoorInstance(IdFactory.getInstance().getNextId(),template, id, name, unlockable);
  221.         door.setRange(rangeXMin, rangeYMin, rangeZMin, rangeXMax, rangeYMax, rangeZMax);
  222.         try
  223.         {
  224.             door.setMapRegion(MapRegionTable.getInstance().getMapRegion(x,y));
  225.         }
  226.         catch (Exception e)
  227.         {
  228.             _log.severe("Error in door data, ID:"+id);
  229.         }
  230.  
  231.         door.setCurrentHpMp(door.getMaxHp(), door.getMaxMp());
  232.         door.setOpen(1);
  233.         door.setXYZInvisible(x,y,z);
  234.  
  235.         door.setMapRegion(MapRegionTable.getInstance().getMapRegion(x, y));
  236.  
  237.         return door;
  238.     }
  239.  
  240.  
  241.     private boolean _initialized = true;
  242.  
  243.     public boolean isInitialized()
  244.     {
  245.         return _initialized;
  246.     }
  247.  
  248.     public L2DoorInstance getDoor(Integer id)
  249.     {
  250.         return _staticItems.get(id);
  251.     }
  252.  
  253.     public void putDoor(L2DoorInstance door)
  254.  
  255.     {
  256.         _staticItems.put(door.getDoorId(), door);
  257.     }
  258.  
  259.     public L2DoorInstance[] getDoors()
  260.     {
  261.         L2DoorInstance[] _allTemplates = _staticItems.values().toArray(new L2DoorInstance[_staticItems.size()]);
  262.         return _allTemplates;
  263.     }
  264.  
  265.  
  266.     /**
  267.      * Performs a check and sets up a scheduled task for
  268.      * those doors that require auto opening/closing.
  269.      */
  270.     public void checkAutoOpen()
  271.     {
  272.         for (L2DoorInstance doorInst : getDoors())
  273.         {
  274.             // Garden of Eva (every 7 minutes)
  275.             if (doorInst.getDoorName().startsWith("goe"))
  276.                 doorInst.setAutoActionDelay(420000);
  277.        
  278.             // Tower of Insolence (every 5 minutes)
  279.             else if (doorInst.getDoorName().startsWith("aden_tower"))
  280.                 doorInst.setAutoActionDelay(300000);
  281.         }
  282.     }
  283.  
  284.     public boolean checkIfDoorsBetween(AbstractNodeLoc start, AbstractNodeLoc end)
  285.     {
  286.         return checkIfDoorsBetween(start.getX(), start.getY(), start.getZ(), end.getX(), end.getY(), end.getZ());
  287.     }
  288.  
  289.     public boolean checkIfDoorsBetween(int x, int y, int z, int tx, int ty, int tz)
  290.     {
  291.         int region;
  292.         try
  293.         {
  294.             region = MapRegionTable.getInstance().getMapRegion(x,y);
  295.         }
  296.         catch (Exception e)
  297.         {
  298.             return false;
  299.         }
  300.  
  301.         // there are quite many doors, maybe they should be splitted
  302.         for (L2DoorInstance doorInst : getDoors())
  303.         {
  304.             if (doorInst.getMapRegion() != region)
  305.                 continue;
  306.             if (doorInst.getXMax() == 0)
  307.                 continue;
  308.  
  309.             // line segment goes through box
  310.             // first basic checks to stop most calculations short
  311.             // phase 1, x
  312.             if ((x <= doorInst.getXMax() && tx >= doorInst.getXMin()) || (tx <= doorInst.getXMax() && x >= doorInst.getXMin()))
  313.             {
  314.                 //phase 2, y
  315.                 if ((y <= doorInst.getYMax() && ty >= doorInst.getYMin()) || (ty <= doorInst.getYMax() && y >= doorInst.getYMin()))
  316.                 {
  317.                     // phase 3, basically only z remains but now we calculate it with another formula (by rage)
  318.                     // in some cases the direct line check (only) in the beginning isn't sufficient,
  319.                     // when char z changes a lot along the path
  320.                     if (doorInst.getCurrentHp() > 0 && doorInst.getOpen() != 0)
  321.                     {
  322.                         int px1 = doorInst.getXMin();
  323.                         int py1 = doorInst.getYMin();
  324.                         int pz1 = doorInst.getZMin();
  325.                         int px2 = doorInst.getXMax();
  326.                         int py2 = doorInst.getYMax();
  327.                         int pz2 = doorInst.getZMax();
  328.  
  329.                         int l = tx - x;
  330.                         int m = ty - y;
  331.                         int n = tz - z;
  332.  
  333.                         int dk;
  334.  
  335.                         if ((dk = (doorInst.getA() * l + doorInst.getB() * m + doorInst.getC() * n)) == 0)
  336.                             continue; // Parallel
  337.  
  338.                         float p = (float)(doorInst.getA() * x + doorInst.getB() * y + doorInst.getC() * z + doorInst.getD()) / (float)dk;
  339.  
  340.                         int fx = (int)(x - l * p);
  341.                         int fy = (int)(y - m * p);
  342.                         int fz = (int)(z - n * p);
  343.  
  344.                         if ((Math.min(x,tx) <= fx && fx <= Math.max(x,tx)) && (Math.min(y,ty) <= fy && fy <= Math.max(y,ty)) && (Math.min(z,tz) <= fz && fz <= Math.max(z,tz)))
  345.                         {
  346.                             if (((fx >= px1 && fx <= px2) || (fx >= px2 && fx <= px1))
  347.                                      && ((fy >= py1 && fy <= py2) || (fy >= py2 && fy <= py1))
  348.                                      && ((fz >= pz1 && fz <= pz2) || (fz >= pz2 && fz <= pz1)))
  349.                                 return true; // Door between
  350.                         }
  351.                     }
  352.                 }
  353.             }
  354.         }
  355.         return false;
  356.     }
  357. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement