falyptus

Faith - Génération de nouvelles cellules de combat

May 19th, 2012
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.63 KB | None | 0 0
  1. /* /!\ ATTENTION; Ici on random sur toutes les maps n'ayant pas pour get_placesStr() "-1", si vous voulez faire en sorte que ce système ne soit actif que sur certaines maps il faut faire une condition supplémentaire! /!\ */
  2.  
  3.  
  4. //Dans la classe CryptManager on remplace la fonction parseStartCell() par celle là
  5.  
  6. public static ArrayList<Case> parseStartCell(Carte map,int num)
  7. {
  8.     ArrayList<Case> list = null;
  9.     String infos = null;
  10.     String placesStr = map.get_placesStr();
  11.        
  12.     if(!placesStr.equalsIgnoreCase("-1"))
  13.     {
  14.         placesStr = map.randomizeFightCell();
  15.         infos = placesStr.split("\\|")[num];
  16.         int a = 0;
  17.         list = new ArrayList<Case>();
  18.         while(a < infos.length())
  19.         {
  20.             list.add(map.getCase((getIntByHashedValue(infos.charAt(a))<<6) + getIntByHashedValue(infos.charAt (a+1))));
  21.             a = a+2;
  22.         }
  23.     }
  24.     return list;
  25. }
  26.  
  27. // Et on ajoute:
  28.  
  29. public static Map<Integer, LinkedList<Integer>> generateCellFight(Carte map)
  30. {
  31.     ArrayList<Integer> possibleCells = new ArrayList<Integer>(map.GetCases().size());
  32.            
  33.     for(int cellID = 0; cellID < 1024; cellID++)//Pour chaque cellID compris entre 0 et 1024
  34.     {
  35.         if(map.getCase(cellID) == null) continue; // On vérifie si la cellule existe
  36.         if(map.getCase(cellID).isWalkable(false)) // On vérifie qu'elle est walkable
  37.         {
  38.             possibleCells.add(cellID); // On l'ajoute à la liste des cellules possibles
  39.         }
  40.     }
  41.    
  42.     if(possibleCells.size() < 20) return null; // Si la liste des cellules possibles contient moins de cellule on retourne null
  43.        
  44.     Map<Integer, LinkedList<Integer>> Cells = new TreeMap<Integer, LinkedList<Integer>>();
  45.    
  46.     int team = 0; // Fighter Team
  47.     LinkedList<Integer> cellsList = new LinkedList<Integer>();
  48.    
  49.     while(team <= 1) // On boucle jusqu'à ce que team == 1
  50.     {
  51.         do
  52.         {
  53.             switch((team == 0 && cellsList.size() == 0) ? 1 : Formulas.getRandomValue(1, 3))
  54.             {
  55.             case 1:
  56.                 //On retourne une cellule aléatoirement
  57.                 int cell = possibleCells.get(Formulas.getRandomValue(0, possibleCells.size() - 1));
  58.                 if(!cellsList.contains(cell)) cellsList.add(cell); //Si elle ne figure pas déjà dans la liste on l'ajoute
  59.             break;
  60.                
  61.             default:
  62.                 int lastCell = 0;
  63.                 if(team == 1 && cellsList.size() == 0) //Si team == 1 et que la liste ne contient aucune cellule alors on prends la dernière valeur de la liste totale
  64.                 {
  65.                     lastCell = Cells.get(0).getLast();
  66.                 } else
  67.                 {
  68.                     lastCell = cellsList.getLast(); //Sinon on prends la derniere valeur de la liste temporaire
  69.                 }
  70.                
  71.                 int cellNext = -1;
  72.                 switch(Formulas.getRandomValue(1, 4))
  73.                 {
  74.                 case 1:
  75.                     cellNext = Pathfinding.GetCaseIDFromDirrection(lastCell, 'a', map, true);
  76.                     if(possibleCells.contains(cellNext) && !cellsList.contains(cellNext)) cellsList.add(cellNext);
  77.                 break;
  78.                
  79.                 case 2:
  80.                     cellNext = Pathfinding.GetCaseIDFromDirrection(lastCell, 'c', map, true);
  81.                     if(possibleCells.contains(cellNext) && !cellsList.contains(cellNext)) cellsList.add(cellNext);
  82.                 break;
  83.                    
  84.                 case 3:
  85.                     cellNext = Pathfinding.GetCaseIDFromDirrection(lastCell, 'e', map, true);
  86.                     if(possibleCells.contains(cellNext) && !cellsList.contains(cellNext)) cellsList.add(cellNext);
  87.                 break;
  88.                    
  89.                 case 4:
  90.                     cellNext = Pathfinding.GetCaseIDFromDirrection(lastCell, 'g', map, true);
  91.                     if(possibleCells.contains(cellNext) && !cellsList.contains(cellNext)) cellsList.add(cellNext);
  92.                 break;
  93.                 }
  94.             break;
  95.             }
  96.         }while(cellsList.size() < 8);
  97.         Cells.put(team, cellsList); // On ajoute toutes les cellules à la LinkedList
  98.         team += 1; // On incrémente de team pour crée les cellules des deux équipes
  99.     }
  100.     return Cells;
  101. }
  102.  
  103.  
  104. //Dans la classe Carte on ajoute la méthode:
  105.  
  106. public String randomizeFightCell()
  107. {
  108.     StringBuilder toReturn = new StringBuilder();
  109.     ArrayList<Integer> cellListTeam0 = new ArrayList<Integer>();
  110.     ArrayList<Integer> cellListTeam1 = new ArrayList<Integer>();
  111.     int team = 0;
  112.    
  113.     if(this != null)
  114.     {
  115.         for(Entry<Integer, LinkedList<Integer>> cellLists : CryptManager.generateCellFight(this).entrySet())
  116.         {
  117.             if(team == 0 && team == cellLists.getKey())
  118.             {
  119.                 cellListTeam0.addAll(cellLists.getValue());
  120.                 for(int cell : cellListTeam0)
  121.                 {
  122.                     toReturn.append(CryptManager.cellID_To_Code(cell));
  123.                 }
  124.             }
  125.         }
  126.        
  127.         team = 1;
  128.         toReturn.append("|");
  129.                        
  130.         for(Entry<Integer, LinkedList<Integer>> cellLists : CryptManager.generateCellFight(this).entrySet())
  131.         {
  132.             if(team == 1 && team == cellLists.getKey())
  133.             {
  134.                 cellListTeam1.addAll(cellLists.getValue());
  135.                 for(int cell : cellListTeam1)
  136.                 {
  137.                     toReturn.append(CryptManager.cellID_To_Code(cell));
  138.                 }
  139.             }
  140.         }
  141.     }
  142.     return toReturn.toString();
  143. }
Advertisement
Add Comment
Please, Sign In to add comment