Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added check when diplomatic relations change with players. #1141

Merged
merged 1 commit into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions src/hu/openig/mechanics/AITrader.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,9 @@ public void prepare() {
fleets.add(tf);
}
}
Set<String> drs = new HashSet<>();
Set<Player> drs = new HashSet<>();
for (DiplomaticRelation dr0 : world.relations) {
if (dr0.first.equals(world.player.id)

|| dr0.second.equals(world.player.id)) {
if (dr0.first == player || dr0.second == player) {
if (dr0.tradeAgreement) {
if (dr0.first.equals(world.player.id)) {
drs.add(dr0.second);
Expand All @@ -175,7 +173,7 @@ public void prepare() {
if (pl.owner == world.player

|| (pl.owner == player)
|| drs.contains(pl.owner.id)) {
|| drs.contains(pl.owner)) {
planets.add(pl);
}
}
Expand Down Expand Up @@ -611,10 +609,10 @@ public void returnToPreviousPlanet(Fleet f) {
fleetTurnedBack.add(f);
Planet pl = lastVisitedPlanet.get(f);
if (pl != null) {
f.targetPlanet(pl);
f.setTargetPlanet(pl);
} else {
Exceptions.add(new AssertionError("Fleet " + f.id + " has no previous planet to return to!"));
f.targetPlanet(nearest(player.world.planets.values(), f));
f.setTargetPlanet(nearest(player.world.planets.values(), f));
}
f.mode = FleetMode.MOVE;
}
Expand Down
4 changes: 2 additions & 2 deletions src/hu/openig/mechanics/AchievementManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,8 @@ public boolean invoke(World t, Player u, AchievementProgress ap) {
ap.displayProgress = false;
ap.max = 1;
for (DiplomaticRelation dr : t.relations) {
if (dr.first.equals(u.id) || dr.second.equals(u.id)) {
Player otherPlayer = dr.first.equals(u.id) ? t.player(dr.second) : t.player(dr.first);
if (dr.first == u || dr.second == u) {
Player otherPlayer = (dr.first == u ? dr.second : dr.first);
if (dr.full
&& !otherPlayer.race.equals(u.race)
&& !otherPlayer.race.equals("traders")
Expand Down
6 changes: 2 additions & 4 deletions src/hu/openig/mechanics/AttackPlanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ AIFleet selectTargetFleet() {
DiplomaticRelation dr = world.relations.get(f.fleet.owner);

if (dr != null && dr.full && !dr.strongAlliance) {
if (dr.value < p.warThreshold && hasActiveAlliance(dr.alliancesAgainst)) {
if (dr.value < p.warThreshold && !hasActiveAlliance(dr.alliancesAgainst)) {
candidates.add(f);
}
}
Expand Down Expand Up @@ -305,9 +305,7 @@ AIPlanet selectTargetPlanet() {
}
DiplomaticRelation dr = world.relations.get(p.owner);
if (dr != null && dr.full && !dr.strongAlliance) {
if (dr.value < this.p.warThreshold

&& !hasActiveAlliance(dr.alliancesAgainst)) {
if (dr.value < this.p.warThreshold && !hasActiveAlliance(dr.alliancesAgainst)) {
if (planetValue(p) > 0) {
candidates.add(p);
enemies.add(p.owner);
Expand Down
5 changes: 2 additions & 3 deletions src/hu/openig/mechanics/Radar.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,10 @@ public static void compute(World world) {
// share radar knowledge
for (Player p : world.players.values()) {
for (DiplomaticRelation dr : world.relations) {
if ((dr.first.equals(p.id) || dr.second.equals(p.id))

if ((dr.first == p || dr.second == p)
&& dr.value >= world.params().radarShareLimit()
&& !dr.alliancesAgainst.isEmpty()) {
Player p2 = world.players.get((dr.first.equals(p.id)) ? dr.second : dr.first);
Player p2 = (dr.first == p ? dr.second : dr.first);

for (Map.Entry<Fleet, FleetKnowledge> fe : p.fleets.entrySet()) {
updateKnowledge(world, p2, fe.getKey(), fe.getValue());
Expand Down
47 changes: 27 additions & 20 deletions src/hu/openig/mechanics/Simulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ public static boolean compute(final World world) {
if (world.level >= 4) {
for (DiplomaticRelation dr : world.relations) {
if (dr != null && dr.full && !dr.strongAlliance && dr.alliancesAgainst.isEmpty()) {
Player p1 = world.players.get(dr.first);
Player p2 = world.players.get(dr.second);
Player p1 = dr.first;
Player p2 = dr.second;
if ((p1.isMainPlayer && !p2.isMainPlayer) || (!p1.isMainPlayer && p2.isMainPlayer)) {
dr.value = Math.max(0, dr.value - world.customBalanceSettings.nonPlayerRelationshipDeterioration / 100d);
dr.updateDrValue(dr.value - world.customBalanceSettings.nonPlayerRelationshipDeterioration / 100d);
}
}
}
Expand Down Expand Up @@ -914,7 +914,7 @@ static boolean moveFleets(List<Fleet> playerFleets, World world) {
f.targetFleet = null;
}
if (clearPlanet) {
f.targetPlanet(null);
f.setTargetPlanet(null);
}
if (f.task == FleetTask.MOVE) {
f.task = FleetTask.IDLE;
Expand All @@ -936,27 +936,34 @@ static boolean moveFleets(List<Fleet> playerFleets, World world) {
/**
* Handle the case when the fleet reached the target planet.
* @param world the world
* @param f the fleet
* @param fleet the fleet
*/
static void handleAttack(World world, Fleet f) {
Planet tp = f.targetPlanet();
Fleet tf = f.targetFleet;
Player o = tp != null ? tp.owner : (tf != null ? tf.owner : null);
if (o != null) {
static void handleAttack(World world, Fleet fleet) {
Planet targetPlanet = fleet.targetPlanet();
Fleet targetFleet = fleet.targetFleet;
Player targetPlayer = targetPlanet != null ? targetPlanet.owner : (targetFleet != null ? targetFleet.owner : null);
fleet.task = FleetTask.IDLE;
if (targetPlayer == null) {
return;
}
DiplomaticRelation dr = world.getRelation(fleet.owner, targetPlayer);
if (dr != null) {
// do not attack a strong ally
DiplomaticRelation dr = world.getRelation(f.owner, o);
if (dr == null || !dr.strongAlliance) {
BattleInfo bi = new BattleInfo();
bi.attacker = f;
bi.targetFleet = tf;
bi.targetPlanet = tp;
f.task = FleetTask.IDLE;

world.pendingBattles.add(bi);
if (dr.strongAlliance) {
return;
}
// cancel attack if relations improved
int endHostilitiesThreshold = Math.max(dr.first.endHostilitiesThreshold, dr.second.endHostilitiesThreshold);
if (dr.value > endHostilitiesThreshold) {
return;
}
}
f.task = FleetTask.IDLE;

BattleInfo bi = new BattleInfo();
bi.attacker = fleet;
bi.targetFleet = targetFleet;
bi.targetPlanet = targetPlanet;
world.pendingBattles.add(bi);
}
/**
* Regenerate the shields and/or health.
Expand Down
9 changes: 4 additions & 5 deletions src/hu/openig/model/AIWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,10 @@ public void assign(Player player) {
now = player.world.time.getTime();

for (DiplomaticRelation dr : player.world.relations) {
if (dr.first.equals(player.id)) {
relations.put(player.world.players.get(dr.second), dr);
} else
if (dr.second.equals(player.id)) {
relations.put(player.world.players.get(dr.first), dr);
if (dr.first == player) {
relations.put(dr.second, dr);
} else if (dr.second == player) {
relations.put(dr.first, dr);
}
}
for (Player p : player.world.players.values()) {
Expand Down
33 changes: 30 additions & 3 deletions src/hu/openig/model/DiplomaticRelation.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
*/
public class DiplomaticRelation {
/** The first player ID who made the contact. */
public String first;
public Player first;
/** The second player ID who made the contact. */
public String second;
public Player second;
/** Indicate that the secondary side has also discovered the first. */
public boolean full;
/** The relation value, 0..100. */
Expand All @@ -45,6 +45,33 @@ public DiplomaticRelation copy() {
dr.assign(this);
return dr;
}
/**
* Update the diplomatic relations value with the given amount.
* Apply any changes that might result from the new relations value.
* @param drValue the amount to current value to
*/
public void updateDrValue(double drValue) {
double oldValue = this.value;
this.value = Math.min(100, Math.max(0, drValue));
// Relations improved
if (oldValue < this.value) {
int endHostilitiesThreshold = Math.max(first.endHostilitiesThreshold, second.endHostilitiesThreshold);
if (this.value > endHostilitiesThreshold) {
for (Fleet fleet : first.ownFleets()) {
if ((fleet.mode == FleetMode.ATTACK || fleet.task == FleetTask.ATTACK)
&& (second.ownPlanets().contains(fleet.targetPlanet()) || second.ownFleets().contains(fleet.targetFleet))) {
fleet.stop();
}
}
for (Fleet fleet : second.ownFleets()) {
if ((fleet.mode == FleetMode.ATTACK || fleet.task == FleetTask.ATTACK)
&& (first.ownPlanets().contains(fleet.targetPlanet()) || first.ownFleets().contains(fleet.targetFleet))) {
fleet.stop();
}
}
}
}
}
/**
* Assign the values from the other diplomatic relation.
* @param other the other relation
Expand All @@ -67,7 +94,7 @@ public void assign(DiplomaticRelation other) {
* or the relation is full.
*/
public boolean knows(String playerId) {
return first.equals(playerId) || (second.equals(playerId) && full);
return first.id.equals(playerId) || (second.id.equals(playerId) && full);
}
/**
* Set the new won't talk value.
Expand Down
2 changes: 1 addition & 1 deletion src/hu/openig/model/Fleet.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public Fleet(Player owner) {
* Set the new target planet and save the current target into {@code arrivedAt}.
* @param p the new target planet
*/
public void targetPlanet(Planet p) {
public void setTargetPlanet(Planet p) {
if (p == null) {
arrivedAt = targetPlanet;
} else {
Expand Down
14 changes: 8 additions & 6 deletions src/hu/openig/model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ public class Player {
public int colonizationLimit = -1;
/** The limit where the AI considers attacking the other party. */
public int warThreshold = 45;
/** The limit above which the AI cancels all ongoing attacks against the other party. */
public int endHostilitiesThreshold = 55;
/** The negotiation offers from players. */
public final Map<String, DiplomaticOffer> offers = new LinkedHashMap<>();
/** The factor for police-to-morale conversion. */
Expand Down Expand Up @@ -340,7 +342,7 @@ public int getStance(Player p) {
*/
public void setStance(Player p, int value) {
DiplomaticRelation dr = world.establishRelation(this, p);
dr.value = value;
dr.updateDrValue(value);
}
/**

Expand All @@ -349,19 +351,19 @@ public void setStance(Player p, int value) {
* @return does this player know the other player? */
public boolean knows(Player other) {
DiplomaticRelation dr = world.getRelation(this, other);
return dr != null && (dr.second.equals(other.id) || (dr.first.equals(other.id) && dr.full));
return dr != null && (dr.second == other || (dr.first == other && dr.full));
}
/**
* @return the set ow known players
*/
public Map<Player, DiplomaticRelation> knownPlayers() {
Map<Player, DiplomaticRelation> result = new LinkedHashMap<>();
for (DiplomaticRelation dr : world.relations) {
if (dr.first.equals(id)) {
result.put(world.players.get(dr.second), dr);
if (dr.first.id.equals(id)) {
result.put(dr.second, dr);
}
if (dr.second.equals(id) && dr.full) {
result.put(world.players.get(dr.first), dr);
if (dr.second.id.equals(id) && dr.full) {
result.put(dr.first, dr);
}
}
return result;
Expand Down
19 changes: 9 additions & 10 deletions src/hu/openig/model/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -2053,7 +2053,7 @@ private void loadFleets(Map<Fleet, Integer> deferredTargets,
}
s0 = xfleet.get("target-planet", null);
if (s0 != null) {
f.targetPlanet(planets.get(s0));
f.setTargetPlanet(planets.get(s0));
}
s0 = xfleet.get("arrived-at", null);
if (s0 != null) {
Expand Down Expand Up @@ -2878,8 +2878,8 @@ public void cureFleets(Planet planet) {
public DiplomaticRelation getRelation(Player first, Player second) {
if (first != null && second != null) {
for (DiplomaticRelation dr : relations) {
if ((dr.first.equals(first.id) && dr.second.equals(second.id))
|| (dr.first.equals(second.id) && dr.second.equals(first.id))) {
if ((dr.first == first && dr.second == second)
|| (dr.first == second && dr.second == first)) {
return dr;
}
}
Expand Down Expand Up @@ -2910,8 +2910,8 @@ public DiplomaticRelation establishRelation(Player first, Player second) {
*/
public DiplomaticRelation createDiplomaticRelation(Player first, Player second) {
DiplomaticRelation dr = new DiplomaticRelation();
dr.first = first.id;
dr.second = second.id;
dr.first = first;
dr.second = second;
dr.value = (first.initialStance + second.initialStance) / 2d;
relations.add(dr);
return dr;
Expand All @@ -2924,8 +2924,8 @@ void saveDiplomaticRelations(XElement xworld) {
XElement xrels = xworld.add("relations");
for (DiplomaticRelation dr : relations) {
XElement xrel = xrels.add("relation");
xrel.set("first", dr.first);
xrel.set("second", dr.second);
xrel.set("first", dr.first.id);
xrel.set("second", dr.second.id);
xrel.set("full", dr.full);
xrel.set("trade-agreement", dr.tradeAgreement);
xrel.set("strong-alliance", dr.strongAlliance);
Expand Down Expand Up @@ -3487,11 +3487,10 @@ public void establishWar(Player p1, Player p2) {
int maxWar = Math.max(p1.warThreshold, p2.warThreshold);
// each attack degrades relations a bit
if (dr.value >= maxWar) {
dr.value = minWar - 1;
dr.updateDrValue(minWar - 1);
} else {
dr.value -= 1;
dr.updateDrValue(dr.value - 1);
}
dr.value = Math.max(0, dr.value);
dr.tradeAgreement = false;
dr.wontTalk(true);
dr.lastContact = time.getTime();
Expand Down
12 changes: 6 additions & 6 deletions src/hu/openig/screen/items/AbandonColonyScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,17 +259,17 @@ boolean abandonPlanet(boolean kill) {
p.morale(p.morale() - 50);
}
for (DiplomaticRelation dr : world().relations) {
if (dr.full && (dr.first.equals(player().id) || dr.second.equals(player().id))) {
dr.value = Math.max(0, dr.value - 5);
if (dr.full && (dr.first == player() || dr.second == player())) {
dr.updateDrValue(dr.value - 5);
}
}
} else {
for (Planet p : player().ownPlanets()) {
p.morale(p.morale() - 20);
}
for (DiplomaticRelation dr : world().relations) {
if (dr.full && (dr.first.equals(player().id) || dr.second.equals(player().id))) {
dr.value = Math.max(0, dr.value - 25);
if (dr.full && (dr.first == player() || dr.second == player())) {
dr.updateDrValue(dr.value - 25);
}
}
}
Expand All @@ -292,8 +292,8 @@ boolean abandonPlanet(boolean kill) {
}
} else {
for (DiplomaticRelation dr : world().relations) {
if (dr.first.equals(player().id) || dr.second.equals(player().id)) {
dr.value = Math.max(0, dr.value - 5);
if (dr.first == player() || dr.second == player()) {
dr.updateDrValue(dr.value - 5);
}
}
List<Planet> ps = new ArrayList<>();
Expand Down
Loading