Advertisement
Guest User

Untitled

a guest
May 6th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 24.11 KB | None | 0 0
  1. package assault;
  2.  
  3. import java.sql.*;
  4. import java.text.DecimalFormat;
  5. import java.util.*;
  6. import java.io.FileOutputStream;
  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 = 54;
  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
  80.                             // output report in console and create report file.
  81.  
  82.         // Random key to protect the access
  83.         key = generateKey(4);
  84.  
  85.         party = new Party(); // Initialize party
  86.  
  87.         assaultReport = "<center>";
  88.         assaultReport += String
  89.                 .format(
  90.                         "{embedded[ASSAULT_TIME]}%ta %td. %tb %tY, %tT{/embedded}<br />\n<br /><br />\n",
  91.                         Calendar.getInstance(), Calendar.getInstance(),
  92.                         Calendar.getInstance(), Calendar.getInstance(),
  93.                         Calendar.getInstance());
  94.  
  95.         /**
  96.          * Load planet data
  97.          */
  98.         int userid = 0;
  99.         ResultSet rs = null;
  100.         try
  101.         {
  102.             Statement stmt = Database.createStatement();
  103.             rs = stmt
  104.                     .executeQuery("SELECT a.time, a.accomplished, p.planetid, p.metal, p.silicon, p.hydrogen, p.ismoon, g.moonid FROM "
  105.                             + prefix
  106.                             + "assault a LEFT JOIN "
  107.                             + prefix
  108.                             + "planet p ON (p.planetid = a.planetid) LEFT JOIN "
  109.                             + prefix
  110.                             + "galaxy g ON (a.planetid = g.planetid) WHERE a.assaultid = '"
  111.                             + assaultid + "' LIMIT 1");
  112.             if(rs.next())
  113.             {
  114.                 if(rs.getInt("accomplished") == 1)
  115.                 {
  116.                     System.err.println("Combat is already accomplished.");
  117.                     System.exit(1);
  118.                 }
  119.                 planetid = rs.getInt("planetid");
  120.                 metal = (int) Math.floor(rs.getFloat("metal") / 2); // The
  121.                                                                     // available
  122.                                                                     // haul
  123.                 silicon = (int) Math.floor(rs.getFloat("silicon") / 2); // The
  124.                                                                         // available
  125.                                                                         // haul
  126.                 hydrogen = (int) Math.floor(rs.getFloat("hydrogen") / 2); // The
  127.                                                                             // available
  128.                                                                             // haul
  129.                 if(rs.getInt("ismoon") == 1 || rs.getInt("moonid") > 0)
  130.                 {
  131.                     ismoon = true;
  132.                 }
  133.                 time = rs.getInt("time"); // Assault time
  134.             }
  135.         }
  136.         catch(SQLException e)
  137.         {
  138.             System.err.println(e.getMessage());
  139.         }
  140.  
  141.         /**
  142.          * Read in users for this assault.
  143.          */
  144.         try
  145.         {
  146.             Statement stmt = Database.createStatement();
  147.             rs = stmt
  148.                     .executeQuery("SELECT u.userid, u.username, pp.mode, pp.participantid, pp.preloaded, pp.consumption, g.galaxy, g.system, g.position FROM "
  149.                             + prefix
  150.                             + "assaultparticipant pp LEFT JOIN "
  151.                             + prefix
  152.                             + "user u ON (u.userid = pp.userid) LEFT JOIN "
  153.                             + prefix
  154.                             + "galaxy g ON (g.planetid = u.hp) WHERE pp.assaultid = '"
  155.                             + assaultid + "' ORDER BY pp.participantid ASC");
  156.             while(rs.next())
  157.             {
  158.                 userid = rs.getInt("userid");
  159.                 if(userid > 0)
  160.                 {
  161.                     Participant participant = new Participant(userid, rs
  162.                             .getInt("mode"), rs.getString("username"));
  163.                     participant.setGalaxy(rs.getInt("galaxy"));
  164.                     participant.setSystem(rs.getInt("system"));
  165.                     participant.setPosition(rs.getInt("position"));
  166.                     participant.setParticipantId(rs.getInt("participantid"));
  167.                     participant.setConsumption(rs.getInt("consumption"));
  168.                     participant.setPreloaded(rs.getInt("preloaded"));
  169.                     if(rs.getInt("mode") == 1)
  170.                     {
  171.                         party.addAtter(participant);
  172.                     }
  173.                     else
  174.                     {
  175.                         party.addDefender(participant);
  176.                     }
  177.                 }
  178.             }
  179.         }
  180.         catch(SQLException e)
  181.         {
  182.             System.err.println(e.getMessage());
  183.         }
  184.  
  185.         if(party.defenderHasNoFleet())
  186.         {
  187.             defenderZero = true;
  188.             assaultResult = 1;
  189.         }
  190.         else
  191.         {
  192.             /**
  193.              * Load rapid fire.
  194.              */
  195.             rs = null;
  196.             try
  197.             {
  198.                 Statement stmt = Database.createStatement();
  199.                 rs = stmt.executeQuery("SELECT unitid, target, value FROM "
  200.                         + prefix + "rapidfire ORDER BY unitid ASC, target ASC");
  201.                 while(rs.next())
  202.                 {
  203.                     rapidfire[rs.getInt("unitid")][rs.getInt("target")] = rs
  204.                             .getInt("value");
  205.                 }
  206.             }
  207.             catch(SQLException e)
  208.             {
  209.                 System.err.println(e.getMessage());
  210.             }
  211.         }
  212.  
  213.         /**
  214.          * Here begins the assault calculations.
  215.          */
  216.         for(int turn = 1; turn < 7; turn++)
  217.         {
  218.             if(defenderZero)
  219.             {
  220.                 break;
  221.             }
  222.  
  223.             assaultReport += "<strong>{lang}TURN{/lang}: " + turn
  224.                     + "</strong><br />\n";
  225.  
  226.             // Flush turn variables
  227.             shotsAtter = 0;
  228.             shotsDefender = 0;
  229.             atterPower = 0;
  230.             defenderPower = 0;
  231.             shieldAtter = 0;
  232.             shieldDefender = 0;
  233.  
  234.             // Attackers shoot
  235.             for(Iterator<Participant> iter = party.atter.iterator(); iter
  236.                     .hasNext();)
  237.             {
  238.                 Participant participant = iter.next();
  239.                 assaultReport += "{lang}ATTACKER{/lang} "
  240.                         + participant.getUsername() + " ["
  241.                         + participant.getGalaxy() + ":"
  242.                         + participant.getSystem() + ":"
  243.                         + participant.getPosition() + "]<br />\n";
  244.                 assaultReport += String
  245.                         .format(
  246.                                 "{lang}GUN_POWER{/lang}: %.0f&#037; {lang}SHIELD_POWER{/lang}: %.0f&#037; {lang}ARMORING{/lang}: %.0f&#037;<br />\n",
  247.                                 participant.getAttack() * 10, participant
  248.                                         .getShield() * 10, participant
  249.                                         .getShell() * 10);
  250.                 assaultReport += "<table class=\"atable\"><tr><th>{lang}TYPE{/lang}</th>";
  251.                 resetBuffer();
  252.                 for(Iterator<Unit> fleetIter = participant.fleet.iterator(); fleetIter
  253.                         .hasNext();)
  254.                 {
  255.                     Unit unit = fleetIter.next();
  256.                     if(unit.getQuantity() > 0)
  257.                     {
  258.                         assaultReport += "<th>{lang}" + unit.getName()
  259.                                 + "{/lang}</th>";
  260.                         quantity += "<td>"
  261.                                 + decFormatter.format(unit.getQuantity())
  262.                                 + "</td>";
  263.                         guns += String.format("<td>%s</td>", decFormatter
  264.                                 .format(unit.getAttack()));
  265.                         shields += String.format("<td>%s</td>", decFormatter
  266.                                 .format(unit.getShield()));
  267.                         shells += String.format("<td>%s</td>", decFormatter
  268.                                 .format(unit.getShell()));
  269.                         shipShoots(unit, participant.getMode()); // Actual
  270.                                                                     // calculations
  271.                     }
  272.                 }
  273.                 assaultReport += quantity + guns + shields + shells;
  274.                 assaultReport += "</tr></table><br />\n";
  275.             }
  276.  
  277.             // Defenders shoot
  278.             for(Iterator<Participant> iter = party.defender.iterator(); iter
  279.                     .hasNext();)
  280.             {
  281.                 Participant participant = iter.next();
  282.                 assaultReport += "{lang}DEFENDER{/lang} "
  283.                         + participant.getUsername() + " ["
  284.                         + participant.getGalaxy() + ":"
  285.                         + participant.getSystem() + ":"
  286.                         + participant.getPosition() + "]<br />\n";
  287.                 assaultReport += String
  288.                         .format(
  289.                                 "{lang}GUN_POWER{/lang}: %.0f&#037; {lang}SHIELD_POWER{/lang}: %.0f&#037; {lang}ARMORING{/lang}: %.0f&#037;<br />\n",
  290.                                 participant.getAttack() * 10, participant
  291.                                         .getShield() * 10, participant
  292.                                         .getShell() * 10);
  293.                 assaultReport += "<table class=\"atable\"><tr><th>{lang}TYPE{/lang}</th>";
  294.                 resetBuffer();
  295.                 for(Iterator<Unit> fleetIter = participant.fleet.iterator(); fleetIter
  296.                         .hasNext();)
  297.                 {
  298.                     Unit unit = fleetIter.next();
  299.                     if(unit.getQuantity() > 0)
  300.                     {
  301.                         assaultReport += "<th>{lang}" + unit.getName()
  302.                                 + "{/lang}</th>";
  303.                         quantity += "<td>"
  304.                                 + decFormatter.format(unit.getQuantity())
  305.                                 + "</td>";
  306.                         guns += String.format("<td>%s</td>", decFormatter
  307.                                 .format(unit.getAttack()));
  308.                         shields += String.format("<td>%s</td>", decFormatter
  309.                                 .format(unit.getShield()));
  310.                         shells += String.format("<td>%s</td>", decFormatter
  311.                                 .format(unit.getShell()));
  312.                         shipShoots(unit, participant.getMode()); // Actual
  313.                                                                     // calculations
  314.                     }
  315.                 }
  316.                 assaultReport += quantity + guns + shields + shells;
  317.                 assaultReport += "</tr></table><br />\n";
  318.             }
  319.  
  320.             // Get values of this turn
  321.             assaultReport += "<br />\n";
  322.             assaultReport += String
  323.                     .format(
  324.                             "{embedded[ATTACKER_SHOTS]}%s{/embedded} {embedded[ATTACKER_POWER]}%s{/embedded} {embedded[DEFENDER_SHIELD]}%s{/embedded}<br />\n",
  325.                             decFormatter.format(shotsAtter), decFormatter
  326.                                     .format(atterPower), decFormatter
  327.                                     .format(shieldDefender));
  328.             assaultReport += String
  329.                     .format(
  330.                             "{embedded[DEFENDER_SHOTS]}%s{/embedded} {embedded[DEFENDER_POWER]}%s{/embedded} {embedded[ATTACKER_SHIELD]}%s{/embedded}<br />\n<br />\n",
  331.                             decFormatter.format(shotsDefender), decFormatter
  332.                                     .format(defenderPower), decFormatter
  333.                                     .format(shieldAtter));
  334.  
  335.             party.renew(); // Renew the party: Reload shields and remove ships
  336.                             // with explosion flag
  337.  
  338.             // Check if attacker or defender has still fleet to battle
  339.             boolean atterNoFleet = party.atterHasNoFleet();
  340.             boolean defenderNoFleet = party.defenderHasNoFleet();
  341.             if(atterNoFleet || defenderNoFleet)
  342.             {
  343.                 if(atterNoFleet && defenderNoFleet)
  344.                 {
  345.                     assaultResult = 0; // Draw
  346.                 }
  347.                 else if(defenderNoFleet)
  348.                 {
  349.                     assaultResult = 1; // Attacker won
  350.                 }
  351.                 else
  352.                 {
  353.                     assaultResult = 2; // Defender won
  354.                 }
  355.                 break;
  356.             }
  357.             else if(turn == 6)
  358.             {
  359.                 assaultResult = 0; // Draw
  360.             }
  361.         }
  362.  
  363.         // Final result of remaining ships
  364.         // Attackers
  365.         for(Iterator<Participant> iter = party.atter.iterator(); iter.hasNext();)
  366.         {
  367.             Participant participant = iter.next();
  368.             assaultReport += "{lang}ATTACKER{/lang} "
  369.                     + participant.getUsername() + " ["
  370.                     + participant.getGalaxy() + ":" + participant.getSystem()
  371.                     + ":" + participant.getPosition() + "]<br />\n";
  372.             if(assaultResult != 2)
  373.             {
  374.                 assaultReport += "<table class=\"atable\"><tr><th>{lang}TYPE{/lang}</th>";
  375.                 resetBuffer();
  376.                 for(Iterator<Unit> fleetIter = participant.fleet.iterator(); fleetIter
  377.                         .hasNext();)
  378.                 {
  379.                     Unit unit = fleetIter.next();
  380.                     if(unit.getQuantity() > 0)
  381.                     {
  382.                         assaultReport += "<th>{lang}" + unit.getName()
  383.                                 + "{/lang}</th>";
  384.                         quantity += "<td>"
  385.                                 + decFormatter.format(unit.getQuantity())
  386.                                 + "</td>";
  387.                         guns += String.format("<td>%s</td>", decFormatter
  388.                                 .format(unit.getAttack()));
  389.                         shields += String.format("<td>%s</td>", decFormatter
  390.                                 .format(unit.getShield()));
  391.                         shells += String.format("<td>%s</td>", decFormatter
  392.                                 .format(unit.getShell()));
  393.                     }
  394.                 }
  395.                 assaultReport += quantity + guns + shields + shells;
  396.                 assaultReport += "</tr></table><br />\n";
  397.             }
  398.             else
  399.             {
  400.                 assaultReport += "<strong>{lang}DESTROYED{/lang}</strong><br />\n";
  401.             }
  402.             participant.finish();
  403.             debrisMetal += participant.getMetal();
  404.             debrisSilicon += participant.getSilicon();
  405.             atterUnitsLost += participant.getLostUnits();
  406.         }
  407.  
  408.         // Defenders
  409.         assaultReport += "<br />\n";
  410.         for(Iterator<Participant> iter = party.defender.iterator(); iter
  411.                 .hasNext();)
  412.         {
  413.             Participant participant = iter.next();
  414.             assaultReport += "{lang}DEFENDER{/lang} "
  415.                     + participant.getUsername() + " ["
  416.                     + participant.getGalaxy() + ":" + participant.getSystem()
  417.                     + ":" + participant.getPosition() + "]<br />\n";
  418.             if(assaultResult != 1)
  419.             {
  420.                 assaultReport += "<table class=\"atable\"><tr><th>{lang}TYPE{/lang}</th>";
  421.                 resetBuffer();
  422.                 for(Iterator<Unit> fleetIter = participant.fleet.iterator(); fleetIter
  423.                         .hasNext();)
  424.                 {
  425.                     Unit unit = fleetIter.next();
  426.                     if(unit.getQuantity() > 0)
  427.                     {
  428.                         assaultReport += "<th>{lang}" + unit.getName()
  429.                                 + "{/lang}</th>";
  430.                         quantity += "<td>"
  431.                                 + decFormatter.format(unit.getQuantity())
  432.                                 + "</td>";
  433.                         guns += String.format("<td>%s</td>", decFormatter
  434.                                 .format(unit.getAttack()));
  435.                         shields += String.format("<td>%s</td>", decFormatter
  436.                                 .format(unit.getShield()));
  437.                         shells += String.format("<td>%s</td>", decFormatter
  438.                                 .format(unit.getShell()));
  439.                     }
  440.                 }
  441.                 assaultReport += quantity + guns + shields + shells;
  442.                 assaultReport += "</tr></table><br />\n";
  443.             }
  444.             else
  445.             {
  446.                 assaultReport += "<strong>{lang}DESTROYED{/lang}</strong><br />\n";
  447.             }
  448.             participant.finish(); // Finish this participant
  449.             debrisMetal += participant.getMetal(); // Metal of this participant
  450.                                                     // add to debris
  451.             debrisSilicon += participant.getSilicon(); // Silicon of this
  452.                                                         // participant add to
  453.                                                         // debris
  454.             defenderUnitsLost += participant.getLostUnits();
  455.         }
  456.  
  457.         // Assault result out steam
  458.         assaultReport += "<br />\n";
  459.         switch(assaultResult)
  460.         {
  461.         case 0:
  462.             assaultReport += "{lang}BATTLE_DRAW{/lang}<br />\n<br />\n";
  463.             break;
  464.         case 1:
  465.             assaultReport += "{lang}ATTACKER_WON{/lang}<br />\n";
  466.             assaultReport += "{lang}ATTACKER_HAUL{/lang}<br />\n";
  467.             assaultReport += decFormatter.format(haulMetal)
  468.                     + " {lang}METAL{/lang}, "
  469.                     + decFormatter.format(haulSilicon)
  470.                     + " {lang}SILICON{/lang} {lang}AND{/lang} "
  471.                     + decFormatter.format(haulHydrogen)
  472.                     + " {lang}HYDROGEN{/lang}<br />\n<br />\n";
  473.             break;
  474.         case 2:
  475.             assaultReport += "{lang}DEFENDER_WON{/lang}<br />\n";
  476.             break;
  477.         }
  478.  
  479.         // Lost units and debris out stream
  480.         assaultReport += String.format(
  481.                 "{embedded[ATTACKER_LOST_UNITS]}%s{/embedded}<br />\n",
  482.                 decFormatter.format(atterUnitsLost));
  483.         assaultReport += String.format(
  484.                 "{embedded[DEFENDER_LOST_UNITS]}%s{/embedded}<br />\n<br />\n",
  485.                 decFormatter.format(defenderUnitsLost));
  486.         if(debrisMetal > 0.0 || debrisSilicon > 0.0)
  487.         {
  488.             assaultReport += String
  489.                     .format(
  490.                             "{lang}DEBRIS{/lang} %s {lang}METAL{/lang} {lang}AND{/lang} %s {lang}SILICON{/lang}.<br />\n",
  491.                             decFormatter.format(debrisMetal), decFormatter
  492.                                     .format(debrisSilicon));
  493.         }
  494.  
  495.         // Get chance of moon appearance
  496.         moonChance = Math.floor((debrisMetal + debrisSilicon) / 100000.0);
  497.         if(moonChance < 1.0)
  498.         {
  499.             moonChance = 0.0;
  500.         }
  501.         else if(moonChance > 20.0)
  502.         {
  503.             moonChance = 20.0;
  504.         }
  505.  
  506.         if(moonChance > 0.0 && ismoon == false)
  507.         {
  508.             assaultReport += String.format(
  509.                     "{embedded[MOON_CHANCE]}%s{/embedded}<br />\n",
  510.                     decFormatter.format(moonChance));
  511.             if(rand(1, 100) <= moonChance)
  512.             {
  513.                 moon = true;
  514.                 assaultReport += "<strong>{lang}MOON{/lang}</strong><br />\n";
  515.             }
  516.         }
  517.  
  518.         // Repaired defense out stream
  519.         String repaired = "";
  520.         if(defenseRepaired.size() > 0)
  521.         {
  522.             Set<String> keyset = defenseRepaired.keySet();
  523.             for(Iterator<String> iter = keyset.iterator(); iter.hasNext();)
  524.             {
  525.                 String unitname = iter.next();
  526.                 repaired += decFormatter.format(defenseRepaired.get(unitname))
  527.                         + " {lang}" + unitname + "{/lang}, ";
  528.             }
  529.             repaired = repaired.substring(0, repaired.length() - 2);
  530.         }
  531.  
  532.         assaultReport += "{lang}REPAIRED_UNITS{/lang}: " + repaired;
  533.         assaultReport += "</center>";
  534.  
  535.         if(debugmode)
  536.         {
  537.             try
  538.             {
  539.                 FileOutputStream output = new FileOutputStream("kb.html");
  540.                 for(int i = 0; i < assaultReport.length(); i++)
  541.                 {
  542.                     output.write((byte) assaultReport.charAt(i));
  543.                 }
  544.                 output.close();
  545.             }
  546.             catch(Exception e)
  547.             {
  548.             }
  549.             System.out.println(assaultReport);
  550.         }
  551.         else
  552.         {
  553.             try
  554.             {
  555.                 finish();
  556.             }
  557.             catch(SQLException e)
  558.             {
  559.                 e.printStackTrace();
  560.             }
  561.         }
  562.  
  563.         System.out.println("Finished");
  564.         return;
  565.     }
  566.  
  567.     public static int rand(int min, int max)
  568.     {
  569.         int rand = random.nextInt();
  570.         if(rand < 0)
  571.         {
  572.             rand *= (-1);
  573.         }
  574.         ;
  575.         return (rand % (min - max)) + min;
  576.     }
  577.  
  578.     /**
  579.      * Lets a ship shooting to hit defenders. ASSAULT CORE
  580.      *
  581.      * @param Participant
  582.      */
  583.     private static void shipShoots(Unit unit, int mode)
  584.     {
  585.         boolean shootsAgain;
  586.         if(unit.quantity == 0)
  587.         {
  588.             return;
  589.         }
  590.  
  591.         // Set generic variables
  592.         double explodingChance = 0;
  593.         double damageToShell = 0;
  594.         int targetUnit = 0; // Represents a single unit
  595.         double sShield = 0; // Shield of this unit
  596.         double sShell = 0; // Shell of this unit
  597.         boolean dead = false;
  598.  
  599.         for(int i = 1; i <= unit.getQuantity(); i++)
  600.         {
  601.             shootsAgain = true;
  602.  
  603.             // Shot loop
  604.             while(shootsAgain)
  605.             {
  606.                 shootsAgain = false;
  607.                 // Chose random target user
  608.                 Participant targetUser;
  609.                 if(mode == 1) // Attacking users
  610.                 {
  611.                     targetUser = party.getRandomDefender();
  612.                 }
  613.                 else
  614.                 // Defending users
  615.                 {
  616.                     targetUser = party.getRandomAtter();
  617.                 }
  618.  
  619.                 // Chose random target unit
  620.                 Unit target = targetUser.getRandomUnit();
  621.                 if(target.sUnit.size() <= 0)
  622.                 {
  623.                     dead = true;
  624.                 }
  625.                 if(!dead)
  626.                 {
  627.                     // Rapidfire
  628.                     shootsAgain = canShootAgain(unit, target);
  629.                     targetUnit = target.getRandomSingleUnit();
  630.  
  631.                     // Add turn values
  632.                     if(mode == 1)
  633.                     {
  634.                         shotsAtter++;
  635.                         atterPower += unit.getAttack();
  636.                     }
  637.                     else
  638.                     {
  639.                         shotsDefender++;
  640.                         defenderPower += unit.getAttack();
  641.                     }
  642.  
  643.                     // Get shell and shield of selected ship
  644.                     sShield = target.sShield.get(targetUnit);
  645.                     sShell = target.sShell.get(targetUnit);
  646.  
  647.                     // Damage of lesser than 1% to the shield will be ignored
  648.                     if(unit.getAttack() <= target.getShield() / 100)
  649.                     {
  650.                         if(mode == 1)
  651.                         {
  652.                             shieldDefender += unit.getAttack();
  653.                         }
  654.                         else
  655.                         {
  656.                             shieldAtter += unit.getAttack();
  657.                         }
  658.                         damageToShell = 0; // Shield blocks all damage
  659.                     }
  660.                     else
  661.                     {
  662.                         // Shield destroyed?
  663.                         if(unit.getAttack() > sShield)
  664.                         {
  665.                             if(mode == 1)
  666.                             {
  667.                                 shieldDefender += sShield;
  668.                             }
  669.                             else
  670.                             {
  671.                                 shieldAtter += sShield;
  672.                             }
  673.                             damageToShell = unit.getAttack() - sShield;
  674.  
  675.                             // Shield has been destroyed.
  676.                             sShield = 0;
  677.  
  678.                             // Calculate damage to shell.
  679.                             sShell = sShell - damageToShell;
  680.                             target.sShell.put(targetUnit, sShell);
  681.                         }
  682.                         // Shield sustains damage
  683.                         else
  684.                         {
  685.                             if(mode == 1)
  686.                             {
  687.                                 shieldDefender += new Double(unit.getAttack())
  688.                                         .intValue();
  689.                             }
  690.                             else
  691.                             {
  692.                                 shieldAtter += new Double(unit.getAttack())
  693.                                         .intValue();
  694.                             }
  695.                             sShield -= unit.getAttack(); // Decrease shield
  696.                             damageToShell = 0; // Shell remains untouched
  697.                         }
  698.                         // Save damage to shield
  699.                         target.sShield.put(targetUnit, sShield);
  700.                     }
  701.  
  702.                     // If there's still damage to shell
  703.                     if(damageToShell > 0)
  704.                     {
  705.                         sShell -= damageToShell; // Decrease shell
  706.                         // Shell destroyed?
  707.                         if(sShell <= 0)
  708.                         {
  709.                             sShell = 0; // Shell destroyed
  710.                             // Mark this unit with explosion flag. Ship will be
  711.                             // removed at the end of a turn.
  712.                             if(!target.explosionFlag.contains(targetUnit))
  713.                             {
  714.                                 target.explosionFlag.add(targetUnit);
  715.                             }
  716.                         }
  717.                         else
  718.                         {
  719.                             // Explosion chance, if the unit's shell is 30% or
  720.                             // higher destroyed
  721.                             if(target.getShell() * 0.7 >= sShell)
  722.                             {
  723.                                 explodingChance = 100 - Math.ceil(sShell
  724.                                         / (target.getShell() / 100));
  725.                                 int random = rand(1, 100);
  726.                                 if(random <= explodingChance
  727.                                         && explodingChance >= 30)
  728.                                 {
  729.                                     // Ships explodes due to perforated shell
  730.                                     // Mark this unit with explosion flag. Ship
  731.                                     // will be removed at the end of a turn.
  732.                                     if(!target.explosionFlag
  733.                                             .contains(targetUnit))
  734.                                     {
  735.                                         target.explosionFlag.add(targetUnit);
  736.                                     }
  737.                                 }
  738.                             }
  739.                         }
  740.                         target.sShell.put(targetUnit, sShell); // Save shell
  741.                     }
  742.                 }
  743.             }
  744.         }
  745.         return;
  746.     }
  747.  
  748.     private static boolean canShootAgain(Unit unit, Unit target)
  749.     {
  750.         // Get rapidfire
  751.         int rf = rapidfire[unit.unitid][target.unitid];
  752.         if(rf == 0)
  753.         {
  754.             return false;
  755.         } // If rapidfire expired, stop shooting
  756.         // Random chance of shot again
  757.         double chance = 100 * (rf - 1) / rf;
  758.         if(rand(1, 100) <= chance)
  759.         {
  760.             return true;
  761.         }
  762.         return false;
  763.     }
  764.  
  765.     public static String getDBHost()
  766.     {
  767.         return "jdbc:mysql://" + dbhost + "/" + dbdatabase;
  768.     }
  769.  
  770.     public static String getUsername()
  771.     {
  772.         return username;
  773.     }
  774.  
  775.     public static String getPassword()
  776.     {
  777.         return dbpasswd;
  778.     }
  779.  
  780.     public static String getPrefix()
  781.     {
  782.         return prefix;
  783.     }
  784.  
  785.     public static String getAssaultid()
  786.     {
  787.         return String.valueOf(assaultid);
  788.     }
  789.  
  790.     public static String getPlanetid()
  791.     {
  792.         return String.valueOf(planetid);
  793.     }
  794.  
  795.     private static void resetBuffer()
  796.     {
  797.         quantity = "<tr><th>{lang}QUANTITY{/lang}</th>";
  798.         guns = "</tr><tr><th>{lang}GUNS{/lang}</th>";
  799.         shields = "</tr><tr><th>{lang}SHIELDS{/lang}</th>";
  800.         shells = "</tr><tr><th>{lang}ARMOR{/lang}</th>";
  801.         return;
  802.     }
  803.  
  804.     private static void finish() throws SQLException
  805.     {
  806.         int _moon;
  807.         if(moon)
  808.         {
  809.             _moon = 1;
  810.         }
  811.         else
  812.         {
  813.             _moon = 0;
  814.         }
  815.         Statement stmt = Database.createStatement();
  816.  
  817.         // Set debris
  818.         stmt.execute("UPDATE " + prefix + "galaxy SET metal = metal + '"
  819.                 + debrisMetal + "', silicon = silicon + '" + debrisSilicon
  820.                 + "' WHERE planetid = '" + planetid + "' OR moonid = '"
  821.                 + planetid + "'");
  822.  
  823.         // Subtract haul from planet
  824.         if(assaultResult == 1)
  825.         {
  826.             stmt.execute("UPDATE " + prefix + "planet SET metal = metal - '"
  827.                     + haulMetal + "', silicon = silicon - '" + haulSilicon
  828.                     + "', hydrogen = hydrogen - '" + haulHydrogen
  829.                     + "' WHERE planetid = '" + planetid + "'");
  830.         }
  831.  
  832.         // Set final data for this assault
  833.         gentime = System.currentTimeMillis() - gentime;
  834.         stmt.execute("UPDATE " + prefix + "assault SET `key` = '" + key
  835.                 + "', `result` = '" + assaultResult + "', moonchance = '"
  836.                 + Math.floor(moonChance) + "', moon = '" + _moon
  837.                 + "', lostunits_attacker = '" + atterUnitsLost
  838.                 + "', lostunits_defender = '" + defenderUnitsLost
  839.                 + "', gentime = '" + gentime
  840.                 + "', accomplished = '1', report = '" + assaultReport
  841.                 + "' WHERE assaultid = '" + assaultid + "'");
  842.         return;
  843.     }
  844.  
  845.     private static String generateKey(int length)
  846.     {
  847.         String sKey = "";
  848.         long r1 = random.nextLong();
  849.         long r2 = random.nextLong();
  850.         String hash1 = Long.toHexString(r1);
  851.         String hash2 = Long.toHexString(r2);
  852.         sKey = hash1 + hash2;
  853.         if(sKey.length() > length)
  854.         {
  855.             sKey = sKey.substring(0, length);
  856.         }
  857.         return sKey.toLowerCase();
  858.     }
  859.  
  860.     public static double randDouble(double min, double max)
  861.     {
  862.         return (random.nextDouble() % (min - max)) + min;
  863.     }
  864. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement