Skip to content

Commit

Permalink
Merge pull request Zukero#120 from Chriz76/randomrequirements
Browse files Browse the repository at this point in the history
Randomrequirements
  • Loading branch information
Zukero authored May 12, 2019
2 parents fc569d8 + 176cf1d commit 97a81e3
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,9 @@ public static boolean canFulfillRequirement(WorldContext world, Requirement requ
case spentGold:
result = stats.getSpentGold() >= requirement.value;
break;
case random:
result = Constants.rollResult(requirement.chance);
break;
case consumedBonemeals:
result = stats.getNumberOfUsedBonemealPotions() >= requirement.value;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
import com.gpl.rpg.AndorsTrail.model.map.TMXMapFileParser.TMXTileSet;
import com.gpl.rpg.AndorsTrail.model.quest.QuestProgress;
import com.gpl.rpg.AndorsTrail.model.script.Requirement;
import com.gpl.rpg.AndorsTrail.resource.parsers.ResourceParserUtils;
import com.gpl.rpg.AndorsTrail.resource.tiles.TileCache;
import com.gpl.rpg.AndorsTrail.util.ConstRange;
import com.gpl.rpg.AndorsTrail.util.Coord;
import com.gpl.rpg.AndorsTrail.util.CoordRect;
import com.gpl.rpg.AndorsTrail.util.L;
Expand Down Expand Up @@ -227,6 +229,7 @@ private static Requirement parseRequirement(TMXObject object) {
String requireId = null;
int requireValue = 0;
boolean requireNegation = false;
ConstRange requireChance = null;
for (TMXProperty p : object.properties) {
if (p.name.equalsIgnoreCase("requireType")) {
try {
Expand All @@ -246,7 +249,13 @@ private static Requirement parseRequirement(TMXObject object) {
}
}
if (requireType == null) return null;
return new Requirement(requireType, requireId, requireValue, requireNegation);
if (requireType == Requirement.RequirementType.random)
{
requireChance = ResourceParserUtils.parseChance(requireId);
requireId = null;
}

return new Requirement(requireType, requireId, requireValue, requireNegation, requireChance);
}

private static CoordRect getTMXObjectPosition(TMXObject object, TMXMap m) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.gpl.rpg.AndorsTrail.model.script;

import com.gpl.rpg.AndorsTrail.model.quest.QuestProgress;
import com.gpl.rpg.AndorsTrail.util.ConstRange;

public final class Requirement {
public static enum RequirementType {
Expand All @@ -17,30 +18,35 @@ public static enum RequirementType {
,consumedBonemeals
,hasActorCondition
,factionScore
,random
}

public final RequirementType requireType;
public final String requireID;
public final int value;
public final boolean negate;
public final ConstRange chance;

public Requirement(
RequirementType requireType
, String requireID
, int value
, boolean negate
, ConstRange chance
) {
this.requireType = requireType;
this.requireID = requireID;
this.value = value;
this.negate = negate;
this.chance = chance;
}

public Requirement(QuestProgress qp) {
this.requireType = RequirementType.questProgress;
this.requireID = qp.questID;
this.value = qp.progress;
this.negate = false;
this.chance = null;
}

public String toString() {
Expand All @@ -52,32 +58,34 @@ public String toString() {
buf.append(value);
return buf.toString();
}

public boolean isValid() {
switch (this.requireType) {
case consumedBonemeals:
return value >= 0;
case hasActorCondition:
return requireID != null;
case inventoryKeep:
case inventoryRemove:
case usedItem:
return requireID != null && value >= 0;
case killedMonster:
return requireID != null && value >= 0;
case questLatestProgress:
case questProgress:
return requireID != null && value >= 0;
case skillLevel:
return requireID != null && value >= 0;
case spentGold:
return value >= 0;
case timerElapsed:
return requireID != null && value >= 0;
case wear:
return requireID != null;
default:
return false;
case consumedBonemeals:
return value >= 0;
case hasActorCondition:
return requireID != null;
case inventoryKeep:
case inventoryRemove:
case usedItem:
return requireID != null && value >= 0;
case killedMonster:
return requireID != null && value >= 0;
case questLatestProgress:
case questProgress:
return requireID != null && value >= 0;
case skillLevel:
return requireID != null && value >= 0;
case spentGold:
return value >= 0;
case random:
return chance != null;
case timerElapsed:
return requireID != null && value >= 0;
case wear:
return requireID != null;
default:
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ public final class ConversationListParser extends JsonCollectionParserFor<Phrase
private final JsonArrayParserFor<Requirement> requirementParser = new JsonArrayParserFor<Requirement>(Requirement.class) {
@Override
protected Requirement parseObject(JSONObject o) throws JSONException {
Requirement.RequirementType type = Requirement.RequirementType.valueOf(o.getString(JsonFieldNames.ReplyRequires.requireType));
String requireID = o.getString(JsonFieldNames.ReplyRequires.requireID);
return new Requirement(
Requirement.RequirementType.valueOf(o.getString(JsonFieldNames.ReplyRequires.requireType))
,o.getString(JsonFieldNames.ReplyRequires.requireID)
type
,type == Requirement.RequirementType.random ? null : requireID
,o.optInt(JsonFieldNames.ReplyRequires.value, 0)
,o.optBoolean(JsonFieldNames.ReplyRequires.negate, false)
,type == Requirement.RequirementType.random ? ResourceParserUtils.parseChance(requireID) : null
);
}
};
Expand Down

0 comments on commit 97a81e3

Please sign in to comment.