Commit e9674a07 authored by Marc BRUN's avatar Marc BRUN
Browse files

Diplomacy: fixed fake vassal rebellions

parent 0b2ce1df
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
......@@ -88,6 +88,12 @@ public class Diplomacy {
}
public boolean areEnemies(Tribe t1, Tribe t2){
return ((attackers.contains(t1) && defenders.contains(t2)) ||
(defenders.contains(t1) && attackers.contains(t2)));
}
public void checkForEnd(){
double score = getScore();
Tribe winner = null;
......@@ -107,19 +113,15 @@ public class Diplomacy {
switch(goal){
case Vassalize:
boolean hadOverlord = (winner.diplomacy.overlord != null);
if(winner.diplomacy.overlord == loser) winner.diplomacy.becomeVassalOf(loser.diplomacy.overlord);
loser.diplomacy.becomeVassalOf(winner);
if(winner.diplomacy.isUnder(loser))
winner.diplomacy.becomeVassalOf(loser.diplomacy.overlord);
else loser.diplomacy.becomeVassalOf(winner);
ArrayList<War> loserWars = (ArrayList<War>) loser.diplomacy.wars.clone();
for(War war : loserWars){
if(war != this){
war.end(false);
}
}
for(War war : winner.diplomacy.wars){
if(war != this){
war.join(loser, winner);
}
}
if(winner.color == Color.white || hadOverlord) winner.color = colors[colorNumber % colors.length];
colorVassals(winner, winner.color);
colorNumber++;
......@@ -139,7 +141,8 @@ public class Diplomacy {
attacker.diplomacy.makePeaceWithTribe(this, defender);
}
}
if(truce) truces.add(new Truce(attackers.get(0), defenders.get(0), 480));
if(truce)
truces.add(new Truce(attackers.get(0), defenders.get(0), 480));
}
......@@ -174,7 +177,7 @@ public class Diplomacy {
this.tribe = tribe;
this.overlord = null;
this.vassals = new ArrayList<Tribe>();
this.enemies = new ArrayList<Tribe>(); // if t1 attacks t2, all tribes allied with t1 enter war with all tribes allied with t2 and reciprocally
this.enemies = new ArrayList<Tribe>(); // all the tribes we are at war with, as main or side enemies
this.allies = new ArrayList<Tribe>(); // alliances are reciprocal but not transitive
this.wars = new ArrayList<War>();
}
......@@ -256,14 +259,8 @@ public class Diplomacy {
double minDanger = Double.MAX_VALUE;
for(Tribe t : World.tribes){
if(t != tribe && !enemies.contains(t) && (t.diplomacy.overlord == null || t == overlord)){ // valid targets : independent or overlord
boolean truceExists = false;
for(Truce truce : truces){
if((truce.tribe1 == tribe && truce.tribe2 == t) || (truce.tribe1 == t && truce.tribe2 == tribe)){
truceExists = true;
break;
}
}
if(!truceExists){
if(!truceExists(tribe, t)){
double danger = getDangerFrom(t);
if(danger < tribe.dna.diplo.startWarThreshold){
targets.add(t);
......@@ -294,29 +291,42 @@ public class Diplomacy {
}
public boolean truceExists(Tribe t1, Tribe t2){
for(Truce truce : truces){
if((truce.tribe1 == t1 && truce.tribe2 == t2) || (truce.tribe1 == t2 && truce.tribe2 == t1)){
return true;
}
}
return false;
}
public void declareWarToAlliance(Tribe t, War.Goal goal){
if(t == overlord){
overlord = null;
t.diplomacy.vassals.remove(tribe);
goal = War.Goal.Vassalize;
}
ArrayList<Tribe> defenders = new ArrayList<Tribe>();
defenders.add(t);
ArrayList<Tribe> attackers = new ArrayList<Tribe>();
attackers.add(tribe);
// enemy calls allies against us
for(Tribe ally : t.diplomacy.allies){
if(!attackers.contains(ally) && !ally.diplomacy.enemies.contains(tribe))
if(!attackers.contains(ally) && !ally.diplomacy.enemies.contains(tribe) && !truceExists(ally, tribe))
defenders.add(ally);
}
// we call allies against enemy
for(Tribe ally : allies){
if(!defenders.contains(ally) && !ally.diplomacy.enemies.contains(t))
if(!defenders.contains(ally) && !ally.diplomacy.enemies.contains(t) && !truceExists(ally, t))
attackers.add(ally);
}
for(Tribe vassal : t.diplomacy.vassals){
if(!attackers.contains(vassal) && !vassal.diplomacy.enemies.contains(tribe))
defenders.add(vassal);
}
for(Tribe vassal : vassals){
if(!defenders.contains(vassal) && !vassal.diplomacy.enemies.contains(t))
attackers.add(vassal);
}
// enemy calls his vassals against us
t.diplomacy.callVassals(tribe, defenders, attackers);
// we call our vassals against them
callVassals(t, attackers, defenders);
War war = new War(goal, attackers, defenders);
for(Tribe attacker : attackers){
......@@ -326,6 +336,16 @@ public class Diplomacy {
}
}
// calls all vassals into war recursively
public void callVassals(Tribe him, ArrayList<Tribe> us, ArrayList<Tribe> them){
for(Tribe vassal : vassals){
if(!them.contains(vassal) && !vassal.diplomacy.enemies.contains(him) && !truceExists(vassal, him)){
us.add(vassal);
vassal.diplomacy.callVassals(him, us, them);
}
}
}
public void declareWarToTribe(War w, Tribe t){ // reciprocal
allies.remove(t);
......@@ -334,8 +354,10 @@ public class Diplomacy {
t.diplomacy.allies.remove(tribe);
t.diplomacy.enemies.add(tribe);
if(!tribe.diplomacy.wars.contains(w)) tribe.diplomacy.wars.add(w);
if(!t.diplomacy.wars.contains(w)) t.diplomacy.wars.add(w);
if(!tribe.diplomacy.wars.contains(w))
tribe.diplomacy.wars.add(w);
if(!t.diplomacy.wars.contains(w))
t.diplomacy.wars.add(w);
}
......@@ -356,4 +378,14 @@ public class Diplomacy {
t.diplomacy.vassals.add(tribe);
}
// Checks if t is a direct or indirect overlord of self
public boolean isUnder(Tribe t){
if(overlord == null)
return false;
else if(overlord == t)
return true;
else
return overlord.diplomacy.isUnder(t);
}
}
......@@ -32,15 +32,16 @@ public class Military extends Individual{
i.tribe.damageTaken += damage;
World.totalWarDamage += damage;
for(Diplomacy.War war : tribe.diplomacy.wars){
if(war.attackers.contains(tribe) && war.defenders.contains(i.tribe)){
war.attackersScore += damage;
}
if(war.defenders.contains(tribe) && war.attackers.contains(i.tribe)){
war.defendersScore += damage;
if(!(i instanceof Warrior)){
for(Diplomacy.War war : tribe.diplomacy.wars){
if(war.attackers.contains(tribe) && war.defenders.contains(i.tribe)){
war.attackersScore += damage;
}
if(war.defenders.contains(tribe) && war.attackers.contains(i.tribe)){
war.defendersScore += damage;
}
}
}
}
}
......
......@@ -61,10 +61,10 @@ public class Tile {
occupant = null;
switch(type){
case Forest:
resources = new Resources(0, 1000, 0);
resources = new Resources(0, 1500, 0);
break;
case Gold:
resources = new Resources(0, 0, 400);
resources = new Resources(0, 0, 800);
break;
default:
resources = new Resources(0, 0, 0);
......
......@@ -140,7 +140,7 @@ public class World {
// DIPLOMACY
boolean couldStartWar = true;
while(tribe.diplomacy.wars.size() < tribe.dna.diplo.maxWars && couldStartWar && tribe.diplomacy.overlord == null){
while(couldStartWar){
couldStartWar = tribe.diplomacy.startWar();
}
ArrayList<Diplomacy.War> wars = (ArrayList<Diplomacy.War>) tribe.diplomacy.wars.clone();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment