Advertisement
Guest User

Untitled

a guest
May 10th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 22.15 KB | None | 0 0
  1. package assault;
  2. import java.sql.*;
  3. import java.text.DecimalFormat;
  4. import java.util.*;
  5. import java.io.FileOutputStream;
  6.  
  7.  
  8. public class Assault
  9. {
  10.     public static long gentime;
  11.     public static boolean debugmode;
  12.     private static String dbhost = "localhost";
  13.     private static String dbdatabase = "ogame";
  14.     private static String username = "root";
  15.     private static String dbpasswd = "";
  16.     private static String prefix = "na_";
  17.     public static int assaultid = 1;
  18.     public static int planetid = 0;
  19.     private static int[][] rapidfire = new int[100][100];
  20.     public static Party party;
  21.     public static int assaultResult;
  22.     public static int metal = 0;
  23.     public static int silicon = 0;
  24.     public static int hydrogen = 0;
  25.     private static int shotsAtter;
  26.     private static int shotsDefender;
  27.     private static int atterPower;
  28.     private static int defenderPower;
  29.     private static int shieldAtter;
  30.     private static int shieldDefender;
  31.     private static String assaultReport;
  32.     private static String quantity = "";
  33.     private static String guns = "";
  34.     private static String shields = "";
  35.     private static String shells = "";
  36.     private static double atterUnitsLost = 0.0;
  37.     private static double defenderUnitsLost = 0.0;
  38.     private static double debrisMetal = 0.0;
  39.     private static double debrisSilicon = 0.0;
  40.     private static double moonChance = 0.0;
  41.     private static boolean moon = false;
  42.     private static boolean ismoon = false;
  43.     public static String key;
  44.     public static int time;
  45.     private static final QuickRandom random = new QuickRandom();
  46.     private static DecimalFormat decFormatter = new DecimalFormat(",###");
  47.     public static Map<String, Integer> defenseRepaired = new HashMap<String, Integer>();
  48.     public static boolean defenseIntoDebris;
  49.     public static double[] bulkIntoDebris = new double[10];
  50.     public static double defenseRepairMin;
  51.     public static double defenseRepairMax;
  52.     public static int haulMetal = 0;
  53.     public static int haulSilicon = 0;
  54.     public static int haulHydrogen = 0;
  55.     private static boolean defenderZero = false;
  56.    
  57.     /**
  58.      * @param args
  59.      */
  60.     public static void main(String[] args)
  61.     {
  62.         gentime = System.currentTimeMillis();
  63.         if(args.length > 0)
  64.         {
  65.             dbhost = args[0];
  66.             dbdatabase = args[3];
  67.             username = args[1];
  68.             dbpasswd = args[2];
  69.             prefix = args[4];
  70.             assaultid = Integer.valueOf(args[5]);
  71.         }
  72.        
  73.         // Assault configuration
  74.         defenseIntoDebris = false;
  75.         bulkIntoDebris[3] = 0.3; // Fleet
  76.         bulkIntoDebris[4] = 0.0; // Defense
  77.         defenseRepairMin = 0.6;
  78.         defenseRepairMax = 0.8;
  79.         debugmode = false; // On: Will proceed database updates. Off: Will output report in console and create report file.
  80.        
  81.         // Random key to protect the access
  82.         key = generateKey(4);
  83.        
  84.         party = new Party(); // Initialize party
  85.        
  86.         assaultReport = "<center>";
  87.         assaultReport += String.format("{embedded[ASSAULT_TIME]}%ta %td. %tb %tY, %tT{/embedded}<br />\n<br /><br />\n", Calendar.getInstance(), Calendar.getInstance(), Calendar.getInstance(), Calendar.getInstance(), Calendar.getInstance());
  88.        
  89.         /**
  90.          * Read in users for this assault.
  91.          */
  92.         int userid = 0;
  93.         ResultSet rs = null;
  94.         try {
  95.             Statement stmt = Database.createStatement();
  96.             rs = stmt.executeQuery("SELECT a.planetid, a.time, tp.metal, tp.silicon, tp.hydrogen, tp.ismoon, pp.participantid, pp.userid, pp.mode, pp.consumption, pp.preloaded, u.username, g.galaxy, g.system, g.position, g.moonid FROM "+prefix+"assaultparticipant pp LEFT JOIN "+prefix+"assault a ON (a.assaultid = pp.assaultid) LEFT JOIN "+prefix+"planet tp ON (tp.planetid = a.planetid) LEFT JOIN "+prefix+"user u ON (u.userid = pp.userid) LEFT JOIN "+prefix+"galaxy g ON (g.planetid = u.hp) WHERE pp.assaultid = '"+assaultid+"' ORDER BY pp.participantid ASC");
  97.             while(rs.next())
  98.             {
  99.                 if(planetid == 0)
  100.                 {
  101.                     planetid = rs.getInt("planetid");
  102.                     // If planet has moon or planet is moon
  103.                     if(rs.getInt("ismoon") == 1 || rs.getInt("moonid") > 0) { ismoon = true; }
  104.                     metal = (int) Math.floor(rs.getFloat("metal") / 2); // The available haul
  105.                     silicon = (int) Math.floor(rs.getFloat("silicon") / 2); // The available haul
  106.                     hydrogen = (int) Math.floor(rs.getFloat("hydrogen") / 2); // The available haul
  107.                     time = rs.getInt("time"); // Assault time
  108.                 }
  109.                 userid = rs.getInt("userid");
  110.                 if(userid > 0)
  111.                 {
  112.                     Participant participant = new Participant(userid, rs.getInt("mode"), rs.getString("username"));
  113.                     participant.setGalaxy(rs.getInt("galaxy"));
  114.                     participant.setSystem(rs.getInt("system"));
  115.                     participant.setPosition(rs.getInt("position"));
  116.                     participant.setParticipantId(rs.getInt("participantid"));
  117.                     participant.setConsumption(rs.getInt("consumption"));
  118.                     participant.setPreloaded(rs.getInt("preloaded"));
  119.                     if(rs.getInt("mode") == 1)
  120.                     {
  121.                         party.addAtter(participant);
  122.                     }
  123.                     else
  124.                     {
  125.                         party.addDefender(participant);
  126.                     }
  127.                 }
  128.             }
  129.         } catch (SQLException e) {
  130.             System.err.println(e.getMessage());
  131.         }
  132.  
  133.         if(party.defenderHasNoFleet())
  134.         {
  135.             defenderZero = true;
  136.             assaultResult = 1;
  137.         }
  138.         else
  139.         {
  140.             /**
  141.              * Load rapid fire.
  142.              */
  143.             rs = null;
  144.             try {
  145.                 Statement stmt = Database.createStatement();
  146.                 rs = stmt.executeQuery("SELECT unitid, target, value FROM "+prefix+"rapidfire ORDER BY unitid ASC, target ASC");
  147.                 while(rs.next())
  148.                 {
  149.                     rapidfire[rs.getInt("unitid")][rs.getInt("target")] = rs.getInt("value");
  150.                 }
  151.             } catch (SQLException e) {
  152.                 System.err.println(e.getMessage());
  153.             }
  154.         }
  155.        
  156.         /**
  157.          * Here begins the assault calculations.
  158.          */
  159.         for(int turn = 1; turn < 7; turn++)
  160.         {
  161.             if(defenderZero) { break; }
  162.            
  163.             assaultReport += "<strong>{lang}TURN{/lang}: "+turn+"</strong><br />\n";
  164.            
  165.             // Flush turn variables
  166.             shotsAtter = 0;
  167.             shotsDefender = 0;
  168.             atterPower = 0;
  169.             defenderPower = 0;
  170.             shieldAtter = 0;
  171.             shieldDefender = 0;
  172.            
  173.             // Attackers shoot
  174.             for(Iterator<Participant> iter = party.atter.iterator(); iter.hasNext();)
  175.             {
  176.                 Participant participant = iter.next();
  177.                 assaultReport += "{lang}ATTACKER{/lang} " + participant.getUsername() + " [" + participant.getGalaxy() + ":" + participant.getSystem() + ":" + participant.getPosition() + "]<br />\n";
  178.                 assaultReport += String.format("{lang}GUN_POWER{/lang}: %.0f&#037; {lang}SHIELD_POWER{/lang}: %.0f&#037; {lang}ARMORING{/lang}: %.0f&#037;<br />\n", participant.getAttack() * 10, participant.getShield() * 10, participant.getShell() * 10);
  179.                 assaultReport += "<table class=\"atable\"><tr><th>{lang}TYPE{/lang}</th>";
  180.                 resetBuffer();
  181.                 for(Iterator<Unit> fleetIter = participant.fleet.iterator(); fleetIter.hasNext();)
  182.                 {
  183.                     Unit unit = fleetIter.next();
  184.                     if(unit.getQuantity() > 0)
  185.                     {
  186.                         assaultReport += "<th>{lang}"+unit.getName()+"{/lang}</th>";   
  187.                         quantity += "<td>"+decFormatter.format(unit.getQuantity())+"</td>";
  188.                         guns += String.format("<td>%s</td>", decFormatter.format(unit.getAttack()));
  189.                         shields += String.format("<td>%s</td>", decFormatter.format(unit.getShield()));
  190.                         shells += String.format("<td>%s</td>", decFormatter.format(unit.getShell()));
  191.                         shipShoots(unit, participant.getMode()); // Actual calculations
  192.                     }
  193.                 }
  194.                 assaultReport += quantity+guns+shields+shells;
  195.                 assaultReport += "</tr></table><br />\n";
  196.             }
  197.            
  198.             // Defenders shoot
  199.             for(Iterator<Participant> iter = party.defender.iterator(); iter.hasNext();)
  200.             {
  201.                 Participant participant = iter.next();
  202.                 assaultReport += "{lang}DEFENDER{/lang} " + participant.getUsername() + " [" + participant.getGalaxy() + ":" + participant.getSystem() + ":" + participant.getPosition() + "]<br />\n";
  203.                 assaultReport += String.format("{lang}GUN_POWER{/lang}: %.0f&#037; {lang}SHIELD_POWER{/lang}: %.0f&#037; {lang}ARMORING{/lang}: %.0f&#037;<br />\n", participant.getAttack() * 10, participant.getShield() * 10, participant.getShell() * 10);
  204.                 assaultReport += "<table class=\"atable\"><tr><th>{lang}TYPE{/lang}</th>";
  205.                 resetBuffer();
  206.                 for(Iterator<Unit> fleetIter = participant.fleet.iterator(); fleetIter.hasNext();)
  207.                 {
  208.                     Unit unit = fleetIter.next();
  209.                     if(unit.getQuantity() > 0)
  210.                     {
  211.                         assaultReport += "<th>{lang}"+unit.getName()+"{/lang}</th>";
  212.                         quantity += "<td>"+decFormatter.format(unit.getQuantity())+"</td>";
  213.                         guns += String.format("<td>%s</td>", decFormatter.format(unit.getAttack()));
  214.                         shields += String.format("<td>%s</td>", decFormatter.format(unit.getShield()));
  215.                         shells += String.format("<td>%s</td>", decFormatter.format(unit.getShell()));
  216.                         shipShoots(unit, participant.getMode()); // Actual calculations
  217.                     }
  218.                 }
  219.                 assaultReport += quantity+guns+shields+shells;
  220.                 assaultReport += "</tr></table><br />\n";
  221.             }
  222.            
  223.             // Get values of this turn
  224.             assaultReport += "<br />\n";
  225.             assaultReport += String.format("{embedded[ATTACKER_SHOTS]}%s{/embedded} {embedded[ATTACKER_POWER]}%s{/embedded} {embedded[DEFENDER_SHIELD]}%s{/embedded}<br />\n", decFormatter.format(shotsAtter), decFormatter.format(atterPower), decFormatter.format(shieldDefender));
  226.             assaultReport += String.format("{embedded[DEFENDER_SHOTS]}%s{/embedded} {embedded[DEFENDER_POWER]}%s{/embedded} {embedded[ATTACKER_SHIELD]}%s{/embedded}<br />\n<br />\n", decFormatter.format(shotsDefender), decFormatter.format(defenderPower), decFormatter.format(shieldAtter));
  227.            
  228.             party.renew(); // Renew the party: Reload shields and remove ships with explosion flag
  229.            
  230.             // Check if attacker or defender has still fleet to battle
  231.             boolean atterNoFleet = party.atterHasNoFleet();
  232.             boolean defenderNoFleet = party.defenderHasNoFleet();
  233.             if(atterNoFleet || defenderNoFleet)
  234.             {
  235.                 if(atterNoFleet && defenderNoFleet)
  236.                 {
  237.                     assaultResult = 0; // Draw
  238.                 }
  239.                 else if(defenderNoFleet)
  240.                 {
  241.                     assaultResult = 1; // Attacker won
  242.                 }
  243.                 else
  244.                 {
  245.                     assaultResult = 2; // Defender won
  246.                 }
  247.                 break;
  248.             }
  249.             else if(turn == 6)
  250.             {
  251.                 assaultResult = 0; // Draw
  252.             }
  253.         }
  254.        
  255.         // Final result of remaining ships
  256.         // Attackers
  257.         for(Iterator<Participant> iter = party.atter.iterator(); iter.hasNext();)
  258.         {
  259.             Participant participant = iter.next();
  260.             assaultReport += "{lang}ATTACKER{/lang} " + participant.getUsername() + " [" + participant.getGalaxy() + ":" + participant.getSystem() + ":" + participant.getPosition() + "]<br />\n";
  261.             if(assaultResult != 2)
  262.             {
  263.                 assaultReport += "<table class=\"atable\"><tr><th>{lang}TYPE{/lang}</th>";
  264.                 resetBuffer();
  265.                 for(Iterator<Unit> fleetIter = participant.fleet.iterator(); fleetIter.hasNext();)
  266.                 {
  267.                     Unit unit = fleetIter.next();
  268.                     if(unit.getQuantity() > 0)
  269.                     {
  270.                         assaultReport += "<th>{lang}"+unit.getName()+"{/lang}</th>";
  271.                         quantity += "<td>"+decFormatter.format(unit.getQuantity())+"</td>";
  272.                         guns += String.format("<td>%s</td>", decFormatter.format(unit.getAttack()));
  273.                         shields += String.format("<td>%s</td>", decFormatter.format(unit.getShield()));
  274.                         shells += String.format("<td>%s</td>", decFormatter.format(unit.getShell()));
  275.                     }
  276.                 }
  277.                 assaultReport += quantity+guns+shields+shells;
  278.                 assaultReport += "</tr></table><br />\n";
  279.             }
  280.             else { assaultReport += "<strong>{lang}DESTROYED{/lang}</strong><br />\n"; }
  281.             participant.finish();
  282.             debrisMetal += participant.getMetal();
  283.             debrisSilicon += participant.getSilicon();
  284.             atterUnitsLost += participant.getLostUnits();
  285.         }
  286.        
  287.         // Defenders
  288.         assaultReport += "<br />\n";
  289.         for(Iterator<Participant> iter = party.defender.iterator(); iter.hasNext();)
  290.         {
  291.             Participant participant = iter.next();
  292.             assaultReport += "{lang}DEFENDER{/lang} " + participant.getUsername() + " [" + participant.getGalaxy() + ":" + participant.getSystem() + ":" + participant.getPosition() + "]<br />\n";
  293.             if(assaultResult != 1)
  294.             {
  295.                 assaultReport += "<table class=\"atable\"><tr><th>{lang}TYPE{/lang}</th>";
  296.                 resetBuffer();
  297.                 for(Iterator<Unit> fleetIter = participant.fleet.iterator(); fleetIter.hasNext();)
  298.                 {
  299.                     Unit unit = fleetIter.next();
  300.                     if(unit.getQuantity() > 0)
  301.                     {
  302.                         assaultReport += "<th>{lang}"+unit.getName()+"{/lang}</th>";
  303.                         quantity += "<td>"+decFormatter.format(unit.getQuantity())+"</td>";
  304.                         guns += String.format("<td>%s</td>", decFormatter.format(unit.getAttack()));
  305.                         shields += String.format("<td>%s</td>", decFormatter.format(unit.getShield()));
  306.                         shells += String.format("<td>%s</td>", decFormatter.format(unit.getShell()));
  307.                     }
  308.                 }
  309.                 assaultReport += quantity+guns+shields+shells;
  310.                 assaultReport += "</tr></table><br />\n";
  311.             }
  312.             else { assaultReport += "<strong>{lang}DESTROYED{/lang}</strong><br />\n"; }
  313.             participant.finish(); // Finish this participant
  314.             debrisMetal += participant.getMetal(); // Metal of this participant add to debris
  315.             debrisSilicon += participant.getSilicon(); // Silicon of this participant add to debris
  316.             defenderUnitsLost += participant.getLostUnits();
  317.         }
  318.        
  319.         // Assault result out steam
  320.         assaultReport += "<br />\n";
  321.         switch(assaultResult)
  322.         {
  323.             case 0:
  324.                 assaultReport += "{lang}BATTLE_DRAW{/lang}<br />\n<br />\n";
  325.             break;
  326.             case 1:
  327.                 assaultReport += "{lang}ATTACKER_WON{/lang}<br />\n";
  328.                 assaultReport += "{lang}ATTACKER_HAUL{/lang}<br />\n";
  329.                 assaultReport += decFormatter.format(haulMetal)+" {lang}METAL{/lang}, "+decFormatter.format(haulSilicon)+" {lang}SILICON{/lang} {lang}AND{/lang} "+decFormatter.format(haulHydrogen)+" {lang}HYDROGEN{/lang}<br />\n<br />\n";
  330.             break;
  331.             case 2:
  332.                 assaultReport += "{lang}DEFENDER_WON{/lang}<br />\n";
  333.             break;
  334.         }
  335.        
  336.         // Lost units and debris out stream
  337.         assaultReport += String.format("{embedded[ATTACKER_LOST_UNITS]}%s{/embedded}<br />\n", decFormatter.format(atterUnitsLost));
  338.         assaultReport += String.format("{embedded[DEFENDER_LOST_UNITS]}%s{/embedded}<br />\n<br />\n", decFormatter.format(defenderUnitsLost));
  339.         if(debrisMetal > 0.0 || debrisSilicon > 0.0) { assaultReport += String.format("{lang}DEBRIS{/lang} %s {lang}METAL{/lang} {lang}AND{/lang} %s {lang}SILICON{/lang}.<br />\n", decFormatter.format(debrisMetal), decFormatter.format(debrisSilicon)); }
  340.        
  341.         // Get chance of moon appearance
  342.         moonChance = (debrisMetal + debrisSilicon) / 100000;
  343.         if(moonChance < 1) { moonChance = 0; }
  344.         else if(moonChance > 20) { moonChance = 20; }
  345.        
  346.         if(moonChance > 0 && !ismoon)
  347.         {
  348.             assaultReport += String.format("{embedded[MOON_CHANCE]}%s{/embedded}<br />\n", decFormatter.format(moonChance));
  349.             if(rand(1, 100) <= moonChance)
  350.             {
  351.                 moon = true;
  352.                 assaultReport += "<strong>{lang}MOON{/lang}</strong><br />\n";
  353.             }
  354.         }
  355.        
  356.         // Repaired defense out stream
  357.         String repaired = "";
  358.         if(defenseRepaired.size() > 0)
  359.         {
  360.             Set<String> keyset = defenseRepaired.keySet();
  361.             for(Iterator<String> iter = keyset.iterator(); iter.hasNext();)
  362.             {
  363.                 String unitname = iter.next();
  364.                 repaired += decFormatter.format(defenseRepaired.get(unitname))+" {lang}"+unitname+"{/lang}, ";
  365.             }
  366.             repaired = repaired.substring(0, repaired.length() - 2);
  367.         }
  368.        
  369.         assaultReport += "{lang}REPAIRED_UNITS{/lang}: "+repaired;
  370.         assaultReport += "</center>";
  371.        
  372.         if(debugmode)
  373.         {
  374.             try { FileOutputStream output = new FileOutputStream("kb.html");
  375.             for (int i=0; i < assaultReport.length(); i++){
  376.                 output.write((byte)assaultReport.charAt(i));
  377.             }
  378.             output.close(); }
  379.             catch(Exception e) {}
  380.             System.out.println(assaultReport);
  381.         }
  382.         else
  383.         {
  384.             try { finish(); }
  385.             catch(SQLException e)
  386.             {
  387.                 e.printStackTrace();
  388.             }
  389.         }
  390.  
  391.         System.out.println("Finished");
  392.         return;
  393.     }
  394.    
  395.     public static int rand(int min, int max)
  396.     {
  397.         int rand = random.nextInt();
  398.         if(rand < 0) { rand *= (-1); };
  399.         return (rand % (min - max)) + min;
  400.     }
  401.    
  402.     /**
  403.      * Lets a ship shooting to hit defenders.
  404.      * ASSAULT CORE
  405.      *
  406.      * @param Participant
  407.      */
  408.     private static void shipShoots(Unit unit, int mode)
  409.     {
  410.         boolean shootsAgain;
  411.         if(unit.quantity == 0) { return; }
  412.        
  413.         // Set generic variables
  414.         double explodingChance = 0;
  415.         double damageToShell = 0;
  416.         int targetUnit = 0; // Represents a single unit
  417.         double sShield = 0; // Shield of this unit
  418.         double sShell = 0; // Shell of this unit
  419.         boolean dead = false;
  420.        
  421.         for(int i = 1; i <= unit.getQuantity(); i++)
  422.         {
  423.             shootsAgain = true;
  424.  
  425.             // Shot loop
  426.             while(shootsAgain)
  427.             {
  428.                 shootsAgain = false;
  429.                 // Chose random target user
  430.                 Participant targetUser;
  431.                 if(mode == 1) // Attacking users
  432.                 {
  433.                     targetUser = party.getRandomDefender();
  434.                 }
  435.                 else // Defending users
  436.                 {
  437.                     targetUser = party.getRandomAtter();
  438.                 }
  439.                
  440.                 // Chose random target unit
  441.                 Unit target = targetUser.getRandomUnit();
  442.                 if(target.sUnit.size() <= 0) { dead = true; }
  443.                 if(!dead)
  444.                 {
  445.                     // Rapidfire
  446.                     shootsAgain = canShootAgain(unit, target);
  447.                     targetUnit = target.getRandomSingleUnit();
  448.  
  449.                     // Add turn values
  450.                     if(mode == 1)
  451.                     {
  452.                         shotsAtter++;
  453.                         atterPower += unit.getAttack();
  454.                     }
  455.                     else
  456.                     {
  457.                         shotsDefender++;
  458.                         defenderPower += unit.getAttack();
  459.                     }
  460.                    
  461.                     // Get shell and shield of selected ship
  462.                     sShield = target.sShield.get(targetUnit);
  463.                     sShell = target.sShell.get(targetUnit);
  464.                    
  465.                     // Damage of lesser than 1% to the shield will be ignored
  466.                     if(unit.getAttack() <= target.getShield() / 100)
  467.                     {
  468.                         if(mode == 1) { shieldDefender += unit.getAttack(); }
  469.                         else { shieldAtter += unit.getAttack(); }
  470.                         damageToShell = 0; // Shield blocks all damage
  471.                     }
  472.                     else
  473.                     {
  474.                         // Shield destroyed?
  475.                         if(unit.getAttack() > sShield)
  476.                         {
  477.                             if(mode == 1) { shieldDefender += sShield; }
  478.                             else { shieldAtter += sShield; }
  479.                             damageToShell = unit.getAttack() - sShield;
  480.  
  481.                             // Shield has been destroyed.
  482.                             sShield = 0;
  483.  
  484.                             // Calculate damage to shell.
  485.                             sShell = sShell - damageToShell;
  486.                             target.sShell.put(targetUnit, sShell);
  487.                         }
  488.                         // Shield sustains damage
  489.                         else
  490.                         {
  491.                             if(mode == 1) { shieldDefender += unit.getAttack(); }
  492.                             else { shieldAtter += unit.getAttack(); }
  493.                             sShield -= unit.getAttack(); // Decrease shield
  494.                             damageToShell = 0; // Shell remains untouched
  495.                         }
  496.                         // Save damage to shield
  497.                         target.sShield.put(targetUnit, sShield);
  498.                     }
  499.  
  500.                     // If there's still damage to shell
  501.                     if(damageToShell > 0)
  502.                     {
  503.                         sShell -= damageToShell; // Decrease shell
  504.                         // Shell destroyed?
  505.                         if(sShell <= 0)
  506.                         {
  507.                             sShell = 0; // Shell destroyed
  508.                             // Mark this unit with explosion flag. Ship will be removed at the end of a turn.
  509.                             if(!target.explosionFlag.contains(targetUnit)) { target.explosionFlag.add(targetUnit); }
  510.                         }
  511.                         else
  512.                         {
  513.                             // Explosion chance, if the unit's shell is 30% or higher destroyed
  514.                             if(target.getShell() * 0.7 >= sShell)
  515.                             {
  516.                                 explodingChance = 100 - Math.ceil(sShell / (target.getShell() / 100));
  517.                                 int random = rand(1, 100);
  518.                                 if(random <= explodingChance && explodingChance >= 30)
  519.                                 {
  520.                                     // Ships explodes due to perforated shell
  521.                                     // Mark this unit with explosion flag. Ship will be removed at the end of a turn.
  522.                                     if(!target.explosionFlag.contains(targetUnit)) { target.explosionFlag.add(targetUnit); }
  523.                                 }
  524.                             }
  525.                         }
  526.                         target.sShell.put(targetUnit, sShell); // Save shell
  527.                     }
  528.                 }
  529.             }
  530.         }
  531.         return;
  532.     }
  533.    
  534.     private static boolean canShootAgain(Unit unit, Unit target)
  535.     {
  536.         // Get rapidfire
  537.         int rf = rapidfire[unit.unitid][target.unitid];
  538.         if(rf == 0) { return false; } // If rapidfire expired, stop shooting
  539.         // Random chance of shot again
  540.         double chance = 100 * (rf - 1) / rf;
  541.         if(rand(1,100) <= chance)
  542.         {
  543.             return true;
  544.         }
  545.         return false;
  546.     }
  547.    
  548.     public static String getDBHost()
  549.     {
  550.         return "jdbc:mysql://"+dbhost+"/"+dbdatabase;
  551.     }
  552.    
  553.     public static String getUsername()
  554.     {
  555.         return username;
  556.     }
  557.    
  558.     public static String getPassword()
  559.     {
  560.         return dbpasswd;
  561.     }
  562.  
  563.     public static String getPrefix() {
  564.         return prefix;
  565.     }
  566.  
  567.     public static String getAssaultid() {
  568.         return String.valueOf(assaultid);
  569.     }
  570.  
  571.     public static String getPlanetid() {
  572.         return String.valueOf(planetid);
  573.     }
  574.    
  575.     private static void resetBuffer() {
  576.         quantity = "<tr><th>{lang}QUANTITY{/lang}</th>";
  577.         guns = "</tr><tr><th>{lang}GUNS{/lang}</th>";
  578.         shields = "</tr><tr><th>{lang}SHIELDS{/lang}</th>";
  579.         shells = "</tr><tr><th>{lang}ARMOR{/lang}</th>";
  580.         return;
  581.     }
  582.    
  583.     private static void finish() throws SQLException {
  584.         int _moon;
  585.         if(moon) { _moon = 1; } else { _moon = 0; }
  586.         Statement stmt = Database.createStatement();
  587.        
  588.         // Set debris
  589.         stmt.execute("UPDATE "+prefix+"galaxy SET metal = metal + '"+debrisMetal+"', silicon = silicon + '"+debrisSilicon+"' WHERE planetid = '"+planetid+"' OR moonid = '"+planetid+"'");
  590.        
  591.         // Subtract haul from planet
  592.         if(assaultResult == 1)
  593.         {
  594.             stmt.execute("UPDATE "+prefix+"planet SET metal = metal - '"+haulMetal+"', silicon = silicon - '"+haulSilicon+"', hydrogen = hydrogen - '"+haulHydrogen+"' WHERE planetid = '"+planetid+"'");
  595.         }
  596.        
  597.         // Set final data for this assault
  598.         gentime = System.currentTimeMillis() - gentime;
  599.         stmt.execute("UPDATE "+prefix+"assault SET `key` = '"+key+"', `result` = '"+assaultResult+"', moonchance = '"+Math.floor(moonChance)+"', moon = '"+_moon+"', lostunits_attacker = '"+atterUnitsLost+"', lostunits_defender = '"+defenderUnitsLost+"', gentime = '"+gentime+"', accomplished = '1', report = '"+assaultReport+"' WHERE assaultid = '"+assaultid+"'");
  600.         return;
  601.     }
  602.    
  603.     private static String generateKey(int length)
  604.     {
  605.         String sKey = "";
  606.         long r1 = random.nextLong();
  607.         long r2 = random.nextLong();
  608.         String hash1 = Long.toHexString(r1);
  609.         String hash2 = Long.toHexString(r2);
  610.         sKey = hash1 + hash2;
  611.         if(sKey.length() > length)
  612.         {
  613.             sKey = sKey.substring(0, length);
  614.         }
  615.         return sKey.toLowerCase();
  616.     }
  617.    
  618.     public static double randDouble(double min, double max)
  619.     {
  620.         return (random.nextDouble() % (min - max)) + min;
  621.     }
  622. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement