Advertisement
Guest User

Untitled

a guest
May 21st, 2012
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.64 KB | None | 0 0
  1. package events.GameOfLife;
  2.  
  3. import java.util.concurrent.ScheduledFuture;
  4.  
  5. import l2p.common.ThreadPoolManager;
  6. import l2p.extensions.scripts.Functions;
  7. import l2p.gameserver.model.L2Spawn;
  8. import l2p.gameserver.model.instances.L2NpcInstance;
  9. import l2p.gameserver.tables.NpcTable;
  10. import l2p.util.Rnd;
  11.  
  12. /**
  13.  * Реализация игры Conway's Game of Life :)
  14.  *
  15.  * @author Gaikotsu
  16.  */
  17. public class GameOfLife extends Functions
  18. {
  19.     private static final int worldWidth = 20;
  20.     private static final int worldHeight = 20;
  21.  
  22.     private static final int cellNpcId = 18927;
  23.  
  24.     private static final int liveCell = 1;
  25.     private static final int deadCell = 2;
  26.  
  27.     private static final int initialLifeCells = 200;
  28.  
  29.     private static final int autoStopSteps = 0;
  30.  
  31.     private static final int posX = 149500;
  32.     private static final int posY = 46200;
  33.     private static final int posZ = -3430;
  34.     private static final int posStep = 50;
  35.  
  36.     private static final int updateWorldPeriod = 2000;
  37.  
  38.     private static ScheduledFuture<?> updateWorldTask = null;
  39.     private static int currentStep;
  40.     private static boolean gamePaused;
  41.  
  42.     private static L2NpcInstance world[][] = new L2NpcInstance[worldWidth][worldHeight];
  43.     private static int worldState[][] = new int[worldWidth][worldHeight];
  44.  
  45.     public void initWorld()
  46.     {
  47.         currentStep = 0;
  48.         gamePaused = true;
  49.  
  50.         if (updateWorldTask != null)
  51.         {
  52.             updateWorldTask.cancel(false);
  53.             updateWorldTask = null;
  54.         }
  55.  
  56.         try
  57.         {
  58.             for (int x = 0; x < worldWidth; x++)
  59.                 for (int y = 0; y < worldHeight; y++)
  60.                 {
  61.                     if (world[x][y] == null)
  62.                     {
  63.                         L2Spawn spawn = new L2Spawn(NpcTable.getTemplate(cellNpcId));
  64.                         spawn.setLocx(posX + x * posStep);
  65.                         spawn.setLocy(posY + y * posStep);
  66.                         spawn.setLocz(posZ);
  67.                         spawn.setAmount(1);
  68.  
  69.                         L2NpcInstance npc = spawn.doSpawn(true);
  70.                         npc.setImobilised(true);
  71.                         npc.setNpcState(deadCell);
  72.  
  73.                         world[x][y] = npc;
  74.                         worldState[x][y] = deadCell;
  75.                     }
  76.                     else
  77.                     {
  78.                         if (world[x][y].getNpcState() != deadCell)
  79.                             world[x][y].setNpcState(deadCell);
  80.  
  81.                         worldState[x][y] = deadCell;
  82.                     }
  83.                 }
  84.  
  85.             int currentIteration = 0;
  86.             int maxIteration = initialLifeCells * 10;
  87.  
  88.             for (int i = 0; i < initialLifeCells; i++)
  89.             {
  90.                 int x = Rnd.get(worldWidth);
  91.                 int y = Rnd.get(worldHeight);
  92.  
  93.                 if (worldState[x][y] == deadCell)
  94.                     worldState[x][y] = liveCell;
  95.                 else
  96.                     i--;
  97.  
  98.                 currentIteration++;
  99.  
  100.                 if (currentIteration > maxIteration)
  101.                     break;
  102.             }
  103.  
  104.             updateNpcState();
  105.         }
  106.         catch (Exception e)
  107.         {
  108.             e.printStackTrace();
  109.         }
  110.     }
  111.  
  112.     public void startGame()
  113.     {
  114.         if (updateWorldTask != null)
  115.             return;
  116.  
  117.         if (autoStopSteps > 0 && currentStep >= autoStopSteps)
  118.             return;
  119.  
  120.         gamePaused = false;
  121.         updateWorldTask = ThreadPoolManager.getInstance().scheduleGeneral(new UpdateWorld(), 0);
  122.     }
  123.  
  124.     public void pauseGame()
  125.     {
  126.         gamePaused = true;
  127.  
  128.         if (updateWorldTask != null)
  129.         {
  130.             updateWorldTask.cancel(false);
  131.             updateWorldTask = null;
  132.         }
  133.     }
  134.  
  135.     private static void updateNpcState()
  136.     {
  137.         for (int x = 0; x < worldWidth; x++)
  138.             for (int y = 0; y < worldHeight; y++)
  139.                 if (world[x][y].getNpcState() != worldState[x][y])
  140.                     world[x][y].setNpcState(worldState[x][y]);
  141.     }
  142.  
  143.     private static class UpdateWorld implements Runnable
  144.     {
  145.         public void run()
  146.         {
  147.             int neighbors = 0;
  148.             int neighborX = 0;
  149.             int neighborY = 0;
  150.  
  151.             for (int x = 0; x < worldWidth; x++)
  152.                 for (int y = 0; y < worldHeight; y++)
  153.                 {
  154.                     neighbors = 0;
  155.  
  156.                     for (int i = -1; i <= 1; i++)
  157.                         for (int j = -1; j <= 1; j++)
  158.                         {
  159.                             if (i == 0 && j == 0)
  160.                                 continue;
  161.  
  162.                             neighborX = x + i;
  163.                             neighborY = y + j;
  164.  
  165.                             if (neighborX < 0)
  166.                                 neighborX = worldWidth - 1;
  167.  
  168.                             if (neighborY < 0)
  169.                                 neighborY = worldHeight - 1;
  170.  
  171.                             if (neighborX >= worldWidth)
  172.                                 neighborX = 0;
  173.  
  174.                             if (neighborY >= worldHeight)
  175.                                 neighborY = 0;
  176.  
  177.                             if (world[neighborX][neighborY].getNpcState() == liveCell)
  178.                                 neighbors++;
  179.                         }
  180.  
  181.                     if (world[x][y].getNpcState() == deadCell && neighbors == 3)
  182.                         worldState[x][y] = liveCell;
  183.                     else
  184.                     {
  185.                         if (world[x][y].getNpcState() == liveCell && (neighbors < 2 || neighbors > 3))
  186.                             worldState[x][y] = deadCell;
  187.                         else
  188.                             worldState[x][y] = world[x][y].getNpcState();
  189.                     }
  190.                 }
  191.  
  192.             updateNpcState();
  193.  
  194.             currentStep++;
  195.  
  196.             if (autoStopSteps > 0 && currentStep >= autoStopSteps)
  197.                 gamePaused = true;
  198.  
  199.             updateWorldTask = !gamePaused ? ThreadPoolManager.getInstance().scheduleGeneral(new UpdateWorld(), updateWorldPeriod) : null;
  200.         }
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement