Skip to content

Commit

Permalink
added settings window
Browse files Browse the repository at this point in the history
  • Loading branch information
bdew committed Jan 24, 2018
1 parent 4953435 commit ae1625a
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 5 deletions.
86 changes: 86 additions & 0 deletions src/main/java/net/bdew/wurm/customnpc/config/SettingsQuestion.java
@@ -0,0 +1,86 @@
package net.bdew.wurm.customnpc.config;

import com.wurmonline.server.creatures.Communicator;
import com.wurmonline.server.creatures.Creature;
import com.wurmonline.server.questions.Question;
import net.bdew.wurm.customnpc.CustomAIData;
import net.bdew.wurm.customnpc.CustomNpcMod;
import org.gotti.wurmunlimited.modsupport.questions.ModQuestion;
import org.gotti.wurmunlimited.modsupport.questions.ModQuestions;

import java.util.Properties;

public class SettingsQuestion implements ModQuestion {
private final Creature responder, npc;
private final CustomAIData data;

public SettingsQuestion(Creature responder, Creature npc) {
this.responder = responder;
this.npc = npc;
data = (CustomAIData) npc.getCreatureAIData();
}

@Override
public void sendQuestion(Question question) {
final StringBuilder buf = new StringBuilder(ModQuestions.getBmlHeader(question));

buf.append("harray{label{text='Name:'};input{id='name'; text=\"").append(npc.getName()).append("\";maxchars='50'}}");

buf.append("text{text=''}");

buf.append("harray{label{text='Sex:'};");
buf.append("radio{group='sex';id='0';text='Male';").append(npc.getSex() == 0 ? "selected='true'" : "").append("}");
buf.append("radio{group='sex';id='1';text='Female';").append(npc.getSex() == 1 ? "selected='true'" : "").append("}");
buf.append("}");

buf.append("text{text=''}");

data.getConfig().getMovementScript().configBML(npc, buf);

buf.append("text{text=''}");

buf.append(ModQuestions.createAnswerButton2(question, "Save"));

question.getResponder().getCommunicator().sendBml(250, 400, true, true, buf.toString(), 200, 200, 200, question.getTitle());
}

@Override
public void answer(Question question, Properties answers) {
Communicator comm = responder.getCommunicator();

boolean changed = false;

String newName = answers.getProperty("name");
if (newName != null && newName.length() > 0 && !newName.equals(npc.getName())) {
try {
changed = true;
npc.setVisible(false);
npc.setName(newName);
data.getConfig().setName(newName);
data.configUpdated();
} catch (Exception e) {
CustomNpcMod.logException("Error setting name", e);
}
}

byte newSex = Byte.parseByte(answers.getProperty("sex"));
if (newSex != npc.getSex()) {
if (!changed) {
changed = true;
npc.setVisible(false);
}
npc.setSex(newSex);
}

if (changed) {
npc.setVisible(true);
}

data.getConfig().getMovementScript().processConfig(npc, answers);
data.configUpdated();
}

public static void send(Creature player, Creature npc) {
ModQuestions.createQuestion(player, "NPC Settings", "", -10, new SettingsQuestion(player, npc)).sendQuestion();
}
}
Expand Up @@ -25,7 +25,7 @@ public ComeHereAction() {
public boolean action(Action action, Creature performer, Creature target, short num, float counter) {
if (canUse(performer, target)) {
CustomAIData data = (CustomAIData) target.getCreatureAIData();
data.setNextMovement(new MovementPathfind(TilePosLayer.from(performer), performer));
data.setNextMovement(new MovementPathfind(TilePosLayer.from(performer), 2, performer));
}
return propagate(action, FINISH_ACTION, NO_SERVER_PROPAGATION, NO_ACTION_PERFORMER_PROPAGATION);
}
Expand Down
Expand Up @@ -18,6 +18,7 @@ public ManageBehaviourProvider() {
manageActions.add(new EquipmentAction());
manageActions.add(new ComeHereAction());
manageActions.add(new FaceMeAction());
manageActions.add(new SettingsAction());

movementActions = new LinkedList<>();
movementActions.add(new MovementStaticAction());
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/net/bdew/wurm/customnpc/manage/SettingsAction.java
@@ -0,0 +1,29 @@
package net.bdew.wurm.customnpc.manage;

import com.wurmonline.server.behaviours.Action;
import com.wurmonline.server.behaviours.ActionEntry;
import com.wurmonline.server.creatures.Creature;
import net.bdew.wurm.customnpc.config.SettingsQuestion;
import org.gotti.wurmunlimited.modsupport.actions.ModActions;

import static org.gotti.wurmunlimited.modsupport.actions.ActionPropagation.FINISH_ACTION;
import static org.gotti.wurmunlimited.modsupport.actions.ActionPropagation.NO_ACTION_PERFORMER_PROPAGATION;
import static org.gotti.wurmunlimited.modsupport.actions.ActionPropagation.NO_SERVER_PROPAGATION;

public class SettingsAction extends BaseManagementAction {
public SettingsAction() {
super(ActionEntry.createEntry((short) ModActions.getNextActionId(), "Settings", "managing", new int[]{
48 /* ACTION_TYPE_ENEMY_ALWAYS */,
37 /* ACTION_TYPE_NEVER_USE_ACTIVE_ITEM */
}));

}

@Override
public boolean action(Action action, Creature performer, Creature target, short num, float counter) {
if (canUse(performer, target)) {
SettingsQuestion.send(performer, target);
}
return propagate(action, FINISH_ACTION, NO_SERVER_PROPAGATION, NO_ACTION_PERFORMER_PROPAGATION);
}
}
Expand Up @@ -55,6 +55,6 @@ public int hashCode() {

@Override
public String toString() {
return String.format("TilePosLayer{x=%d, y=%d, onSurface=%s, floor=%d}", x, y, onSurface, floor);
return String.format("TilePosLayer(x=%d, y=%d, onSurface=%s, floor=%d)", x, y, onSurface, floor);
}
}
Expand Up @@ -9,11 +9,16 @@

import java.io.PrintStream;
import java.util.Map;
import java.util.Properties;

public interface IMovementScript {
void readFromObject(Map<String, Object> data) throws ConfigLoadError;

void saveToFile(PrintStream file);

IMovementStep getNextStep(Creature creature, CreatureStatus status, CustomAIScript ai, CustomAIData data);

void configBML(Creature creature, StringBuilder buf);

void processConfig(Creature creature, Properties properties);
}
Expand Up @@ -62,6 +62,13 @@ protected TilePosLayer originTile(Creature creature) {
return randomTile(creature);
}

@Override
public void configBML(Creature creature, StringBuilder buf) {
buf.append("label{type='bold';text=\"Movement: Wander in house - ").append(house.getName()).append("\"}");
buf.append("text{text=''}");
super.configBML(creature, buf);
}

@Override
public String toString() {
return String.format("MovementHouse(%s)", this.house.getName());
Expand Down
Expand Up @@ -14,6 +14,7 @@

import java.io.PrintStream;
import java.util.Map;
import java.util.Properties;

abstract public class MovementRandomArea implements IMovementScript {

Expand Down Expand Up @@ -60,9 +61,23 @@ public IMovementStep getNextStep(Creature creature, CreatureStatus status, Custo
CustomNpcMod.logInfo(String.format("NPC %d is outside it's designated zone, teleporting back", creature.getWurmId()));
return new MovementTeleport(originTile(creature));
} else if (Server.rand.nextFloat() <= movementChance) {
return new MovementPathfind(randomTile(creature), null);
return new MovementPathfind(randomTile(creature), movementSpeedMod, null);
} else {
return null;
}
}

@Override
public void configBML(Creature creature, StringBuilder buf) {
buf.append("harray{label{text='Speed Modifier:'};input{id='movementSpeedMod'; text=\"").append(movementSpeedMod).append("\";maxchars='10'}}");
buf.append("harray{label{text='Movement Chance:'};input{id='movementChance'; text=\"").append(movementChance).append("\";maxchars='10'}}");
}

@Override
public void processConfig(Creature creature, Properties properties) {
if (properties.containsKey("movementSpeedMod"))
movementSpeedMod = Float.parseFloat(properties.getProperty("movementSpeedMod"));
if (properties.containsKey("movementChance"))
movementChance = Float.parseFloat(properties.getProperty("movementChance"));
}
}
Expand Up @@ -9,6 +9,7 @@

import java.io.PrintStream;
import java.util.Map;
import java.util.Properties;

public class MovementStatic implements IMovementScript, IMovementStep {
@Override
Expand Down Expand Up @@ -37,4 +38,14 @@ public IMovementStep getNextStep(Creature creature, CreatureStatus status, Custo
public String toString() {
return "MovementStatic()";
}

@Override
public void configBML(Creature creature, StringBuilder buf) {
buf.append("label{type='bold';text='Movement: Static'}");
}

@Override
public void processConfig(Creature creature, Properties properties) {

}
}
Expand Up @@ -68,6 +68,13 @@ protected TilePosLayer originTile(Creature creature) {
}
}

@Override
public void configBML(Creature creature, StringBuilder buf) {
buf.append("label{type='bold';text=\"Movement: Wander in village - ").append(village.getName()).append("\"}");
buf.append("text{text=''}");
super.configBML(creature, buf);
}

@Override
public String toString() {
return String.format("MovementVillage(%s)", this.village.getName());
Expand Down
Expand Up @@ -15,10 +15,12 @@ public class MovementPathfind implements IMovementStep {
private final TilePosLayer target;
private final Creature reportTo;
private int lastTileX, lastTileY, lastCounter = 0;
private float speedMod;

public MovementPathfind(TilePosLayer target, Creature reportTo) {
public MovementPathfind(TilePosLayer target, float speedMod, Creature reportTo) {
this.target = target;
this.reportTo = reportTo;
this.speedMod = speedMod;
}

private void reportFailed(String msg) {
Expand All @@ -39,6 +41,7 @@ public boolean pollMovement(Creature creature, CreatureStatus status, CustomAISc
lastTileX = creature.getTileX();
lastTileY = creature.getTileY();
MovementUtil.clearPath(status);
data.setMovementSpeedModifier(speedMod);
try {
creature.setPathfindcounter(0);
Path path = creature.findPath(target.x, target.y, MovementUtil.pathFinder);
Expand Down Expand Up @@ -70,6 +73,6 @@ public boolean pollMovement(Creature creature, CreatureStatus status, CustomAISc

@Override
public String toString() {
return String.format("MovementPathfind(%s, started=%s)", target, started);
return String.format("MovementPathfind(target=%s, started=%s, reportTo=%s, lastTileX=%d, lastTileY=%d, lastCounter=%d, speedMod=%s)", target, started, reportTo, lastTileX, lastTileY, lastCounter, speedMod);
}
}

0 comments on commit ae1625a

Please sign in to comment.