Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -176,18 +176,58 @@ public void clearTreatyNotice(String s) {
treatyNotices.remove(s);
}

/**
* Trade modifiers on the target faction's guilds in this faction's provinces. Each side keeps its own half
* of a trade relation: this faction's record gives what visiting guilds get ("their guilds in our
* territory"), and the target's record towards this faction gives what its own guilds get here ("our
* guilds in their territory"). Reading both records applies one-sided treaties, such as the leader's half
* of an unequal treaty, the way the Diplomacy menu shows them. Both lookups go through
* {@link #getTradeRelation}, so income previews see proposed relations on either side.
*/
public List<FactionModifier> getTradeModifiersFor(String target) {
Faction visitor = FactionManager.getByString(target);
return tradeModifiersFor(target, visitor);
}

/** {@link #getTradeModifiersFor(String)} for a faction already in hand, without the lookup by id. */
public List<FactionModifier> getTradeModifiersFor(Faction visitor) {
if (visitor == null || visitor.getId() == null) {
return new ArrayList<>();
}
return tradeModifiersFor(visitor.getId(), visitor);
}

private List<FactionModifier> tradeModifiersFor(String target, Faction visitor) {
List<FactionModifier> mods = new ArrayList<>();
RelationType r = getTradeRelation(target);
if(r == null) return mods;
if(r.hasTradeEffectsThem()) {
for(FactionModifier mod : r.getTradeEffectsThem()) {
mods.add(new FactionModifier(FactionManager.getByString(target), mod));
}
RelationType ours = getTradeRelation(target);
RelationType theirs = visitor == null || visitor.getDiplomacyHandler() == null || f == null
? null
: visitor.getDiplomacyHandler().getTradeRelation(f.getId());
List<FactionModifier> effects = new ArrayList<>();
if (ours != null && theirs != null && sameRelation(ours, theirs)) {
effects.addAll(ours.getTradeEffectsThemRaw());
effects.addAll(theirs.getTradeEffectsUsRaw());
} else if (ours != null) {
// Unpaired records (an embargo over the other side's old agreement, a missing half) keep the
// owner-only reading, so a stale half never softens an embargo.
effects.addAll(ours.getTradeEffectsThem());
}
for (FactionModifier mod : effects) {
mods.add(new FactionModifier(visitor, mod));
}
return mods;
}

/** True when the two records are the two halves of one relation, as {@code RelationManager} sets them. */
private static boolean sameRelation(RelationType ours, RelationType theirs) {
return (ours.isMutual() && linksTo(ours, theirs)) || (theirs.isMutual() && linksTo(theirs, ours));
}

private static boolean linksTo(RelationType type, RelationType other) {
String link = type.getLinkString();
return link != null && other.getId() != null && link.equalsIgnoreCase(other.getId());
}

public List<FactionModifier> getModifiers() {
List<FactionModifier> mods = new ArrayList<>();
for(Map.Entry<String, Relation> entry : relations.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,11 @@ public List<FactionModifier> getModifiersForGuild(Guild guild) {
mods.addAll(host.getModifiers(guild.getId(), Scope.DOMESTIC_GUILDS, Region.FOREIGN_TERRITORY));
mods.addAll(owner.getModifiers(guild.getId(), Scope.FOREIGN_GUILDS, Region.OUR_TERRITORY));
}
if(owner.getDiplomacyHandler().hasTradeRelation(host.getId())) {
mods.addAll(owner.getDiplomacyHandler().getTradeModifiersFor(host.getId()));
// Both halves of a trade relation count: the owner's record and the visitor's own record.
if (owner.getDiplomacyHandler().hasTradeRelation(host.getId())
|| (host.getDiplomacyHandler() != null
&& host.getDiplomacyHandler().hasTradeRelation(owner.getId()))) {
mods.addAll(owner.getDiplomacyHandler().getTradeModifiersFor(host));
}
} else {
mods.addAll(host.getModifiers(guild.getId(), Scope.DOMESTIC_GUILDS, Region.OUR_TERRITORY));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package net.tfminecraft.simplefactions.diplomacy;

import static org.junit.jupiter.api.Assertions.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import net.tfminecraft.simplefactions.enums.FactionModifiers;
import net.tfminecraft.simplefactions.objects.Faction;
import net.tfminecraft.simplefactions.objects.FactionModifier;
import net.tfminecraft.simplefactions.testsupport.FactionDomainFixture;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
* A guild trading in another faction's province gets the owner's half of their trade relation ("their guilds
* in our territory") plus its own faction's half ("our guilds in their territory"), as the Diplomacy menu
* shows them. The relation types mirror Main's diplomacy.yml.
*/
class TradeRelationHalvesTest {
private FactionDomainFixture domain;
private RelationType leaderSide;
private RelationType subjectSide;
private RelationType agreement;
private RelationType embargo;

@BeforeEach
void setUp() {
domain = new FactionDomainFixture();
agreement =
domain.relationType(
"trade_agreement",
Map.of(
"trade-agreement", true,
"mutual", true,
"trade-effects-us", List.of("trade_power(15.0)", "production(5.0)")));
leaderSide =
domain.relationType(
"unequal_treaty_leader",
Map.of(
"trade-agreement", true,
"mutual", true,
"link", "unequal_treaty_subject",
"trade-effects-us", List.of("trade_power(20.0)", "production(7.5)")));
subjectSide =
domain.relationType(
"unequal_treaty_subject",
Map.of(
"trade-agreement", true,
"lock", true,
"trade-effects-us", List.of("trade_power(5.0)", "production(2.5)")));
embargo =
domain.relationType(
"embargo",
Map.of(
"trade-agreement", true,
"trade-effects-them", List.of("trade_power(-95.0)", "production(-95.0)")));
}

@AfterEach
void tearDown() {
domain.close();
}

@Test
void unequalTreatyGivesTheLeaderItsBonusInTheWeakerSidesLand() {
Faction leader = domain.saved("leader", "LeaderPlayer");
Faction weaker = domain.saved("weaker", "WeakerPlayer");
leader.getDiplomacyHandler().setTradeRelation(weaker, leaderSide);
weaker.getDiplomacyHandler().setTradeRelation(leader, subjectSide);

// The leader's guilds trading in the weaker side's provinces.
assertEquals(
Map.of(FactionModifiers.TRADE_POWER, 20.0, FactionModifiers.PRODUCTION, 7.5),
amounts(weaker.getDiplomacyHandler().getTradeModifiersFor(leader)));
// The weaker side's guilds trading in the leader's provinces.
assertEquals(
Map.of(FactionModifiers.TRADE_POWER, 5.0, FactionModifiers.PRODUCTION, 2.5),
amounts(leader.getDiplomacyHandler().getTradeModifiersFor(weaker)));
assertTrue(
weaker.getDiplomacyHandler().getTradeModifiersFor(leader).stream()
.allMatch(modifier -> modifier.getFrom() == leader));
}

@Test
void tradeAgreementStillGivesEachSideItsBonusOnce() {
Faction first = domain.saved("first", "FirstPlayer");
Faction second = domain.saved("second", "SecondPlayer");
first.getDiplomacyHandler().setTradeRelation(second, agreement);
second.getDiplomacyHandler().setTradeRelation(first, agreement);

Map<FactionModifiers, Double> expected =
Map.of(FactionModifiers.TRADE_POWER, 15.0, FactionModifiers.PRODUCTION, 5.0);
assertEquals(expected, amounts(first.getDiplomacyHandler().getTradeModifiersFor(second)));
assertEquals(expected, amounts(second.getDiplomacyHandler().getTradeModifiersFor(first)));
}

@Test
void embargoStillHitsOnlyTheEmbargoedSide() {
Faction embargoer = domain.saved("embargoer", "EmbargoPlayer");
Faction target = domain.saved("target", "TargetPlayer");
embargoer.getDiplomacyHandler().setTradeRelation(target, embargo);

assertEquals(
Map.of(FactionModifiers.TRADE_POWER, -95.0, FactionModifiers.PRODUCTION, -95.0),
amounts(embargoer.getDiplomacyHandler().getTradeModifiersFor(target)));
assertTrue(target.getDiplomacyHandler().getTradeModifiersFor(embargoer).isEmpty());
}

@Test
void embargoOverTheOtherSidesAgreementKeepsItsFullForceAndOldResults() {
Faction kept = domain.saved("kept", "KeptPlayer");
Faction embargoer = domain.saved("embargoer", "EmbargoPlayer");
// Only the embargoing side's record changes; the other side keeps its old agreement half.
kept.getDiplomacyHandler().setTradeRelation(embargoer, agreement);
embargoer.getDiplomacyHandler().setTradeRelation(kept, embargo);

assertEquals(
Map.of(FactionModifiers.TRADE_POWER, -95.0, FactionModifiers.PRODUCTION, -95.0),
amounts(embargoer.getDiplomacyHandler().getTradeModifiersFor(kept)));
assertEquals(
Map.of(FactionModifiers.TRADE_POWER, 15.0, FactionModifiers.PRODUCTION, 5.0),
amounts(kept.getDiplomacyHandler().getTradeModifiersFor(embargoer)));
}

@Test
void leaderEmbargoingTheWeakerSideIsNotSoftenedByTheOldTreatyHalf() {
Faction leader = domain.saved("leader", "LeaderPlayer");
Faction weaker = domain.saved("weaker", "WeakerPlayer");
leader.getDiplomacyHandler().setTradeRelation(weaker, embargo);
weaker.getDiplomacyHandler().setTradeRelation(leader, subjectSide);

assertEquals(
Map.of(FactionModifiers.TRADE_POWER, -95.0, FactionModifiers.PRODUCTION, -95.0),
amounts(leader.getDiplomacyHandler().getTradeModifiersFor(weaker)));
assertTrue(weaker.getDiplomacyHandler().getTradeModifiersFor(leader).isEmpty());
}

@Test
void noRelationOrUnknownFactionGivesNothing() {
Faction first = domain.saved("first", "FirstPlayer");
Faction second = domain.saved("second", "SecondPlayer");

assertTrue(first.getDiplomacyHandler().getTradeModifiersFor(second).isEmpty());
assertTrue(first.getDiplomacyHandler().getTradeModifiersFor((Faction) null).isEmpty());
assertTrue(first.getDiplomacyHandler().getTradeModifiersFor("unknown").isEmpty());
}

private static Map<FactionModifiers, Double> amounts(List<FactionModifier> modifiers) {
return modifiers.stream()
.collect(Collectors.toMap(FactionModifier::getType, FactionModifier::getAmount, Double::sum));
}
}