Skip to content

Commit

Permalink
Merge pull request #6310 from psikomonkie/issue-2090-link-nova-cews-t…
Browse files Browse the repository at this point in the history
…o-allies

Issue 2090: /nova chat command now works with non-enemy mechs too, as long as you own at least one
  • Loading branch information
HoneySkull authored Dec 25, 2024
2 parents 6480c6d + 3fb19d9 commit 6bb37ff
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 33 deletions.
22 changes: 19 additions & 3 deletions megamek/src/megamek/client/commands/AssignNovaNetworkCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private String strUnlinkID(int id) {
private String strUnlinkAll() {
List<Entity> novaUnits = getMyNovaUnits();
for (Entity e : novaUnits) {
setNewNetworkID(e, e.getOriginalNovaC3NetId());
strUnlinkID(e.getId());
}
return "Everything unlinked";
}
Expand All @@ -196,7 +196,7 @@ private String strListNetworks(boolean planned) {
StringBuilder returnValue = new StringBuilder();

List<Integer> allReadyReported = new LinkedList<>();
List<Entity> novaUnits = getMyNovaUnits();
List<Entity> novaUnits = getAlliedNovaUnits();
List<Entity> network;

for (Entity ent : novaUnits) {
Expand Down Expand Up @@ -255,7 +255,7 @@ private String strListNetwork(int id, boolean planned) {
*/
private List<Entity> listNetwork(Entity e, boolean planned) {
List<Entity> novaNetworkMembers = new LinkedList<>();
List<Entity> novaUnits = getMyNovaUnits();
List<Entity> novaUnits = getAlliedNovaUnits();

for (Entity ent : novaUnits) {
if (planned) {
Expand Down Expand Up @@ -283,4 +283,20 @@ private List<Entity> getMyNovaUnits() {
}
return novaUnits;
}

/**
* @return a list of all nova CEWS units the clients could connect with.
*/
private List<Entity> getAlliedNovaUnits() {
List<Entity> novaUnits = new LinkedList<>();
for (Entity ent : getClient().getEntitiesVector()) {
if (ent.hasNovaCEWS()
&& ((ent.getOwnerId() == getClient().getLocalPlayer().getId())
|| (getClient().getLocalPlayer() != null
&& !getClient().getLocalPlayer().isEnemyOf(ent.getOwner())))){
novaUnits.add(ent);
}
}
return novaUnits;
}
}
80 changes: 50 additions & 30 deletions megamek/src/megamek/server/commands/AssignNovaNetServerCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private void debug(int connID, Server server) {
server.sendServerChat(connID, "Called /nova Debug");
server.sendServerChat(connID,
"Check if server really thinks that stuff is connected");
List<Entity> novaUnits = getMyNovaUnits(connID);
List<Entity> novaUnits = getAlliedNovaUnits(connID);
List<Entity> opponent = gameManager.getGame().getEntitiesVector();
for (Entity e : novaUnits) {
List<Entity> curNetwork = listNetwork(connID, e, false);
Expand Down Expand Up @@ -220,15 +220,15 @@ private String strLink3(int connID, int id1, int id2, int id3) {
return "ID Mismatch!\n";
}

if (ent1.getOwnerId() != connID) {
return "This unit doesn't belong to you!\n";
}
if (ent2.getOwnerId() != connID) {
return "This unit doesn't belong to you!\n";
if (ent1.getOwnerId() == connID || ent2.getOwnerId() == connID || ent3.getOwnerId() == connID) {
if (ent1.isEnemyOf(ent2) || ent1.isEnemyOf(ent3) || ent2.isEnemyOf(ent3)) {
return "These units are not on the same team!";
}
}
if (ent3.getOwnerId() != connID) {
return "This unit doesn't belong to you!\n";
else {
return "None of these units belong to you!\n";
}

if (!ent1.hasActiveNovaCEWS()) {
return "ID: " + id1 + " has no active nova CEWS. Aborting.";
}
Expand All @@ -238,9 +238,9 @@ private String strLink3(int connID, int id1, int id2, int id3) {
if (!ent3.hasActiveNovaCEWS()) {
return "ID: " + id3 + " has no active nova CEWS. Aborting.";
}
rval += strUnlinkID(connID, id1);
rval += strUnlinkID(connID, id2);
rval += strUnlinkID(connID, id3);
rval += strUnlinkID(ent1.getOwnerId(), id1);
rval += strUnlinkID(ent2.getOwnerId(), id2);
rval += strUnlinkID(ent3.getOwnerId(), id3);

setNewNetworkID(connID, ent2, ent1.getNewRoundNovaNetworkString());
setNewNetworkID(connID, ent3, ent1.getNewRoundNovaNetworkString());
Expand All @@ -257,11 +257,13 @@ private String strLink2(int connID, int id1, int id2) {
if ((ent1 == null) || (ent2 == null)) {
return "ID Mismatch!\n";
}
if (ent1.getOwnerId() != connID) {
return "This unit doesn't belong to you!\n";
if (ent1.getOwnerId() == connID || ent2.getOwnerId() == connID) {
if (ent1.isEnemyOf(ent2)) {
return "These units are not on the same team!";
}
}
if (ent2.getOwnerId() != connID) {
return "This unit doesn't belong to you!\n";
else {
return "None of these units belong to you!\n";
}
if (!ent1.hasActiveNovaCEWS()) {
return "ID: " + id1 + " has no active nova CEWS. Aborting.";
Expand All @@ -270,8 +272,8 @@ private String strLink2(int connID, int id1, int id2) {
return "ID: " + id2 + " has no active nova CEWS. Aborting.";
}

rval += strUnlinkID(connID, id1);
rval += strUnlinkID(connID, id2);
rval += strUnlinkID(ent1.getOwnerId(), id1);
rval += strUnlinkID(ent2.getOwnerId(), id2);
setNewNetworkID(connID, ent2, ent1.getNewRoundNovaNetworkString());

return rval + "New Network! Linked Units: " + id1 + ", " + id2 + "\n";
Expand All @@ -291,7 +293,7 @@ private String strUnlinkID(int connID, int id) {
List<Entity> network = listNetwork(connID, ent, true);
if (network.size() < 2) {
// no other member, we're done.
return "Unit " + id + " was allready unlinked\n";
return "Unit " + id + " was already unlinked\n";
}
// there are other members in that network. Need to find a different ID
// for them.
Expand Down Expand Up @@ -320,7 +322,7 @@ private String strUnlinkID(int connID, int id) {
private String strUnlinkAll(int connID) {
List<Entity> novaUnits = getMyNovaUnits(connID);
for (Entity e : novaUnits) {
setNewNetworkID(connID, e, e.getOriginalNovaC3NetId());
strUnlinkID(connID, e.getId());
}
return "Everything unlinked";
}
Expand Down Expand Up @@ -384,7 +386,7 @@ private String strListNetwork(int connID, int id, boolean planned) {
}

if (rval != "") {
rval = "Unit " + id + " is in the Network consisting of:\n";
rval = "Unit " + id + " is in the Network consisting of:\n" + rval;
} else {
rval = "Error. No ID match.\n";
}
Expand All @@ -393,28 +395,28 @@ private String strListNetwork(int connID, int id, boolean planned) {
}

/**
* Returns a list with all members of e 's nova network, including e.
* Returns a list with all members of entity 's nova network, including entity.
*
* @param e
* @param entity
* the entity.
* @param planned
* set this to true if you want to calculate based on next turns
* net.
* @return
*/
private List<Entity> listNetwork(int connID, Entity e, boolean planned) {
private List<Entity> listNetwork(int connID, Entity entity, boolean planned) {
List<Entity> novaNetworkMembers = new LinkedList<>();
List<Entity> novaUnits = getMyNovaUnits(connID);
List<Entity> novaUnits = getAlliedNovaUnits(connID);

for (Entity ent : novaUnits) {
for (Entity novaUnit : novaUnits) {
if (planned) {
if (ent.getNewRoundNovaNetworkString() == e
.getNewRoundNovaNetworkString()) {
novaNetworkMembers.add(ent);
if (novaUnit.getNewRoundNovaNetworkString().equals(entity
.getNewRoundNovaNetworkString())) {
novaNetworkMembers.add(novaUnit);
}
} else {
if (ent.getC3NetId() == e.getC3NetId()) {
novaNetworkMembers.add(ent);
if (novaUnit.getC3NetId().equals(entity.getC3NetId())) {
novaNetworkMembers.add(novaUnit);
}
}
}
Expand All @@ -436,4 +438,22 @@ private List<Entity> getMyNovaUnits(int connID) {
return novaUnits;
}

/**
* Return a list of all nova CEWS units the clients units could connect with.
*
* @return
*/
private List<Entity> getAlliedNovaUnits(int connID) {
List<Entity> novaUnits = new LinkedList<>();
for (Entity ent : gameManager.getGame().getEntitiesVector()) {
if (ent.hasNovaCEWS()
&& ((ent.getOwnerId() == connID)
|| (gameManager.getGame().getPlayer(connID) != null
&& !gameManager.getGame().getPlayer(connID).isEnemyOf(ent.getOwner())))){
novaUnits.add(ent);
}
}
return novaUnits;
}

}

0 comments on commit 6bb37ff

Please sign in to comment.