import java.util.*; /** * @author Nicklas Hjalmarsson * @version 1.0 * * Class for dealing with battles between dragons and other dragons and cities and dragons. */ public class Battle{ int fightcntr = 0; int conqcntr = 0; Fod game; public Battle(){ } public Battle(Fod g){ game = g; fightcntr = 0; } //D1 attacker D2 Defender /** * A fight between two dragons */ public boolean fight(Dragon d1, Dragon d2){ fightcntr++; boolean over = false; Area combatArea = d1.getArea(game.Map); while(!over){ int d1off = d1.attack(); int d1def = d1.defence(); int d2off = d2.attack(); int d2def = d2.defence(); System.out.println(d1.name+" OFF:"+ d1off+" DEF:"+d1def); System.out.println(d2.name+" OFF:"+ d2off+" DEF:"+d2def); int wnd; //D1 attacks wnd = (int)((d1.attack()-d2.defence())*Dice.hitroll()/5); System.out.println(d1.name+" attacks and does "+wnd+" in damage"); if (wnd > 0) d2.Health -= wnd; //D2 attacks wnd = (int)((d2.attack()-d1.defence())*Dice.hitroll()/5); System.out.println(d2.name+" attacks and does "+wnd+" in damage"); if (wnd > 0) d1.Health -= wnd; // You get tired when you fight d1.incWea(7); d2.incWea(7); if(d2.Health <= 0){ System.out.println(d2.name+" died"); // inform owner game.players[d2.Owner].messages += "|@|"+d2.name+" was brutally killed by "+d1.name; game.players[d1.Owner].messages += "|@|The brave "+d1.name+"("+d1.Health+") killed the weak "+d2.name; //ta bort draken ur den area han befann sig i combatArea.removeDragon(d2.name); game.dragons.remove(d2.name); over = true; System.out.println("F return TRUE"); return true; } if(d1.Health <= 0){ System.out.println(d1.name+" died"); // inform owner game.players[d1.Owner].messages += "|@|"+d1.name+" was brutally killed by "+d2.name; game.players[d2.Owner].messages += "|@|The brave "+d2.name+"("+d2.Health+") killed the weak "+d1.name; combatArea.removeDragon(d1.name); game.dragons.remove(d1.name); over = true; System.out.println("F return FALSE"); return false; } else if(d2.flee(d1) && d2.Health > 0){ System.out.println(d2.name+ " Flees the battle"); //Set d2 dest to owner homecastle d2.flee(game); game.players[d1.Owner].messages += "|@|The mighty "+d1.name+"("+d1.Health+") fought "+d2.name+" who cowardly fled the battle"; game.players[d2.Owner].messages += "|@|"+d2.name+"("+d2.Health+") fought "+d1.name+" but had to retreat"; over = true; System.out.println("F return TRUE"); return true; } else if(d1.flee(d2)){ System.out.println(d1.name+ " Flees the battle"); //Set d1 dest to owner homecastle d1.flee(game); game.players[d2.Owner].messages += "|@|The mighty "+d2.name+"("+d2.Health+") fought "+d1.name+" who cowardly fled the battle"; game.players[d1.Owner].messages += "|@|"+d1.name+"("+d1.Health+")fought "+d2.name+" but had to retreat"; over = true; System.out.println("F return FALSE"); return false; } } System.out.println(" BU HAR VI KOMMIT ALLDELSE FÖR LÅNGT"); return false;; } /** * A fight between a city and a dragon */ public void conquer(Dragon d,City c){ conqcntr++; boolean over = false; Area combatArea = c.getArea(game.Map); while(!over){ int breakvalue = (int)(0.7 * c.Population); int doff = d.attack(); int ddef = d.defence(); int coff = c.attack(); int cdef = c.defence(); System.out.println(d.name+" OFF:"+ doff+" DEF:"+ddef); System.out.println(c.Name+" OFF:"+ coff+" DEF:"+cdef); int wnd; //D attacks wnd = (int)((d.attack()-c.defence())*Dice.hitroll()/8); System.out.println(d.name+" attacks and does "+wnd+" in damage"); if (wnd > 0){ c.Population -= wnd; c.Defence = (short)(c.Defence *0.95); } //C attacks wnd = (int)((c.attack()-d.defence())*Dice.hitroll()/8); System.out.println(c.Name+" attacks and does "+wnd+" in damage"); if (wnd > 0) d.Health -= wnd; // You get tired when you fight d.incWea(3); if(c.Population <= breakvalue){ if(c.Population <= 0){ if(c.Owner > 0) game.players[c.Owner].messages += "|@|"+c.Name+" was destroyed by the terrible "+d.name; game.players[d.Owner].messages += "|@|"+c.Name+" was destroyed by your glorius "+d.name; combatArea.removeCity(); game.cities.remove(c.Name); }else{ System.out.println(c.Name+" conquered"); if(c.Owner > 0) game.players[c.Owner].messages += "|@|"+c.Name+" was conquered by the terrible "+d.name; game.players[d.Owner].messages += "|@|"+c.Name+" was conquered by your victorius "+d.name; c.conquer(d.Owner); } over = true; } if(d.Health <= 0){ System.out.println(d.name+" died"); // inform owner game.players[d.Owner].messages += "|@|"+d.name+" was brutally killed by the citizens of "+c.Name; game.players[(c.Owner==-1)?0:c.Owner].messages += "|@|The citizens of "+c.Name+" killed the evil "+d.name; combatArea.removeDragon(d.name); game.dragons.remove(d.name); over = true; } else if(d.flee(c)){ System.out.println(d.name+ " Flees the battle"); game.players[d.Owner].messages += "|@|"+d.name+" had to retreat while attacking "+c.Name; game.players[(c.Owner==-1)?0:c.Owner].messages += "|@|"+c.Name+" was attacked by "+d.name+", but were succesfully defended by your proud subjects"; //Set d dest to owner homecastle d.flee(game); over = true; } } } /** * Use for testing only */ public static void main(String[] args){ Fod f= new Fod(); World world = new World(f); Area [][] Map = world.createWorld(); f.Map = Map; Dragon d = new Dragon(Map[0][1]); d.name = "smaug"; d.Stealth = 50; d.Strength = 100; d.Speed = 100; d.Courage = 100; Dragon d2 = new Dragon(Map[8][4]); d2.name = "Katla"; d2.Owner = 1; d2.Strength = 30; d.Courage = 70; Battle b = new Battle(f); if(b.fight(d,d2)) System.out.println("SMAUG WON"); else System.out.println("SMAUG LOST"); // System.out.println(d); // System.out.println(d2); /* City c = new City(); c.Population = 100; c.Defence = 100; c.Development = 100; b.conquer(d,c); System.out.println("AFTER: "+d); System.out.println("AFTER: "+c); */ } }