Skip to content

Commit

Permalink
Add extended syntax for modded creatures with unusual names
Browse files Browse the repository at this point in the history
Closes #2
  • Loading branch information
joeyparrish committed Jan 25, 2023
1 parent e804e41 commit 26fba7c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 17 deletions.
74 changes: 57 additions & 17 deletions FarmCounter/FarmCounter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,33 @@ private static void RecomputeAllWorkbenchesInRange() {
}
}

private bool IsSimpleIdentifierCharacter(char ch) {
return (ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z') ||
(ch >= '0' && ch <= '9') ||
(ch == '_');
}

private void ConsumeIdentifier(HashSet<string> identifierSet,
string identifier) {
Logger.LogDebug($"PreparseSignText: identifier \"{identifier}\"");
if (identifier.StartsWith("all_")) {
identifierSet.Add(identifier.Replace("all_", ""));
} else if (identifier.StartsWith("tame_")) {
identifierSet.Add(identifier.Replace("tame_", ""));
} else if (identifier.StartsWith("wild_")) {
identifierSet.Add(identifier.Replace("wild_", ""));
}
}

public void PreparseSignText() {
var signText = sign.GetText();
var identifierSet = new HashSet<string>();
Logger.LogDebug($"PreparseSignText: \"{signText}\"");

bool in_identifier = false;
bool in_simple_identifier = false;
bool in_extended_identifier = false;
string identifier = "";

// Iterate to the character past the end, on purpose. This will allow
Expand All @@ -115,24 +136,31 @@ public void PreparseSignText() {
if (in_identifier == false) {
if (ch == '$') {
in_identifier = true;
in_simple_identifier = false;
in_extended_identifier = false;
identifier = "";
}
} else {
if ((ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z') ||
(ch >= '0' && ch <= '9') ||
(ch == '_')) {
} else if (in_simple_identifier) {
if (IsSimpleIdentifierCharacter(ch)) {
identifier += ch;
} else {
Logger.LogDebug($"PreparseSignText: identifier \"{identifier}\"");
if (identifier.StartsWith("all_")) {
identifierSet.Add(identifier.Replace("all_", ""));
} else if (identifier.StartsWith("tame_")) {
identifierSet.Add(identifier.Replace("tame_", ""));
} else if (identifier.StartsWith("wild_")) {
identifierSet.Add(identifier.Replace("wild_", ""));
}
ConsumeIdentifier(identifierSet, identifier);
in_identifier = false;
}
} else if (in_extended_identifier) {
if (ch == '}') {
ConsumeIdentifier(identifierSet, identifier);
in_identifier = false;
} else {
identifier += ch;
}
} else {
// We don't know what kind of identifier this is yet.
if (ch == '{') {
in_extended_identifier = true;
} else if (IsSimpleIdentifierCharacter(ch)) {
in_simple_identifier = true;
identifier += ch;
}
}
}
Expand Down Expand Up @@ -183,21 +211,33 @@ private string ComputeDisplayedText() {
}

foreach (var character in Character.GetAllCharacters()) {
var rawName = character.m_name;
var baseName = character.m_name.Replace("$enemy_", "");
if (identifiers.Contains(baseName) && IsInsideFarm(character)) {
string nameUsed = null;

if (identifiers.Contains(baseName)) {
nameUsed = baseName;
} else if (identifiers.Contains(rawName)) {
nameUsed = rawName;
}

if (nameUsed != null && IsInsideFarm(character)) {
if (character.IsTamed()) {
countTame[baseName] += 1;
countTame[nameUsed] += 1;
} else {
countWild[baseName] += 1;
countWild[nameUsed] += 1;
}
countAll[baseName] += 1;
countAll[nameUsed] += 1;
}
}

foreach (var key in identifiers) {
signText = signText.Replace($"$wild_{key}", countWild[key].ToString());
signText = signText.Replace($"${{wild_{key}}}", countWild[key].ToString());
signText = signText.Replace($"$tame_{key}", countTame[key].ToString());
signText = signText.Replace($"${{tame_{key}}}", countTame[key].ToString());
signText = signText.Replace($"$all_{key}", countAll[key].ToString());
signText = signText.Replace($"${{all_{key}}}", countAll[key].ToString());
}

// For debugging purposes, show the number of workbenches in range.
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@ a mod like [AllTameable](https://www.nexusmods.com/valheim/mods/478) or
could be considered tame.


## Extended tag syntax for modded creatures

Modded creatures that don't follow the normal `$enemy_` pattern in `m_name` can
still be counted. There is an extended syntax that you can use for
names that contain spaces or don't follow normal patterns.

Extended tags take the form of `${tame_...}`, `${wild_...}`, or `${all_...}`,
where the `...` part is the exact `m_name` for the modded creature. It can contain any character but `}`.

For example, when using the
[OdinHorse](https://valheim.thunderstore.io/package/OdinPlus/OdinHorse/) mod,
there is a creature with `m_name` set to `$odin_horse`. To count tame horses,
use the tag `${tame_$odin_horse}`.

For another example, [AllTameable](https://www.nexusmods.com/valheim/mods/478)
creates baby deer with `m_name` set to `Mini $enemy_deer`. To count tame baby
deer, use the tag `${tame_Mini $enemy_deer}`.


## Defining "nearby"

Valheim defines your "base" as anything in range of your workbenches, so this
Expand All @@ -65,6 +84,9 @@ In vanilla Valheim, workbenches have a 20 meter range. If you're using a mod
such as [Valheim Plus](https://github.com/valheimPlus/ValheimPlus), FarmCounter
will detect the overridden distance from the other mod and use that instead.

To determine the number of nearby workbenches, you can use the special
debugging tag `$all_workbench`.


## Dependencies

Expand Down

0 comments on commit 26fba7c

Please sign in to comment.