PDA

View Full Version : Combat tracker script - auto-number names and roll init



SniperDM
June 8th, 2007, 22:42
Thanks to the helpful hint of the getValue() function, I've figured out enough lua syntax to 'make' my own first script. I say 'make' because it's just a slightly modified version of the combat tracker script in the d20 ruleset of fantasygrounds.

The script does the following:

When adding an NPC to the combat tracker, if there is already a combatant with that name, it renames them both numerically. IE if there is already a 'Goblin' on the tracker and you try to add another, their names will be changed to 'Goblin 1 and Goblin 2'. From there, more Goblins will be named "Goblin 3, Goblin 4, etc..."
Automatically rolls hit points for duplicate entries beyond the first. The first monster added will always have the default given hit points.
Automatically rolls initiative for all new NPC entries added, based on the value of the initiative modifier, then sorts the added entry into the tracker based on the rolled initiative.

I make no guarantees to the orderliness or efficiency of this script, or that it's completely functional and without quirk, but it seems to work in my tests so far. Feedback is appreciated.

In order to use this script, you need to run d20unpack and then copy the ruleset from the examples directory to the ruleset directory of fantasygrounds, giving it a custom name. Then just unzip the attached file into the 'scripts' folder of this new ruleset. Of course, your campaign must be using this ruleset to make use of this script.

Here are the parts I changed.

Original:

-- Name
if source.getChild("name") then
newentry.name.setValue(source.getChild("name").getValue());
end

Modified:

-- Name
local namecount = 0;
local highnum = 0;
local check = 0;
for k, v in ipairs(getWindows()) do
if source.getChild("name") then
newentry.name.setValue(source.getChild("name").getValue());
if newentry.name.getValue() == string.sub(getWindows()[k].name.getValue(), 1, string.len(newentry.name.getValue())) then
namecount = namecount + 1;
if string.len(newentry.name.getValue()) == string.len(getWindows()[k].name.getValue()) and string.len(newentry.name.getValue()) > 0 then
for l, w in ipairs(getWindows()) do
if string.sub(getWindows()[l].name.getValue(), 1, string.len(source.getChild("name").getValue())) == source.getChild("name").getValue() then
check = tonumber(string.sub(getWindows()[l].name.getValue(), string.len(source.getChild("name").getValue())+2));
if check and highnum < check then
highnum = check;
end
end
end
getWindows()[k].name.setValue(newentry.name.getValue().." "..highnum+1);
end
end
end
end
if namecount < 2 then
newentry.name.setValue(source.getChild("name").getValue());
end

Original:

-- HP
if source.getChild("hp") then
newentry.hp.setValue(source.getChild("hp").getValue());
end

Modified:

-- HP
if source.getChild("hp") and namecount < 2 then
newentry.hp.setValue(source.getChild("hp").getValue());
else
local hdstring = source.getChild("hd").getValue();
local rollhp = 0;
for v, numdie, dietype, mod in string.gmatch(hdstring, "((%d+)d(%d+)([%+%-]*%d*))") do
if v then
for i = 1, tonumber(numdie) do
rollhp = rollhp + math.random(tonumber(dietype));
end
if tonumber(mod) then
rollhp = rollhp + tonumber(mod);
end
end
end
newentry.hp.setValue(rollhp);
end

Also added to the end of the addNpc() function, before the return statement:

--Roll initiative and sort
newentry.initresult.setValue(math.random(20)+newen try.init.getValue());
applySort(getWindows());
I hope that makes sense. Again, comments/questions are welcome.

EDIT: Removed note about list not sorting upon dropping an NPC intro tracker and file and code blocks updated too. This issue is resolved.

EDIT EDIT: One little problem niggled me that I have now resolved, using an ungodly amount of nested if and for statements, probably more sloppily than necessary. This script now numbers monsters based on the highest number present. If you should have Goblins 1 through 10 and delete all of them except for 10, adding another Goblin will create Goblin 11 instead of Goblin 1 again. On the other hand, if you delete Goblin 10 and leave 1 through 9 intact a new Goblin will be called Goblin 10 once more. This is the best I can do without modifying the way the combat tracker stores data. Files and code blocks updated to match this.

EDIT EDIT EDIT: Added hit point roller to script. Another couple code block added above to demonstrate where it goes, and attached file updated.

Dachannien
June 11th, 2007, 14:02
Doh! I did the automatic renaming thing for my own ruleset last week as well. It didn't even occur to me to roll initiative values automatically, though, which would be extremely useful. I'll have to take a gander at your code (because I'm lazy) and see about doing that :)

SniperDM
June 11th, 2007, 17:27
Great minds think alike, they say... ;)

I'm really curious if you could post your own version of the script. I get the distinct feeling mine is more complex than necessary, and would like to compare code blocks and results if you could indulge me. If you want to add the initiative thing, adding in the bottom code block above to the bottom of the addNPC function should work fine.

I'm also thinking about something to automatically roll NPC hit dice, but I wouldn't want to try it unless I could be sure I could turn it off and on. It would be great for generic NPCs and monsters, but I wouldn't want to change the HPs for main villains and such each time the PCs fight them.

Moon Wizard
June 11th, 2007, 18:47
You could have the script only roll the hit dice for creatures added greater than 1. In that way, if you only add one creature, it uses the default hit points (especially for NPCs). If you add more than 1, then you assume that these are generic creatures that you want to randomize their hit points.

Just some thoughts,
JPG

Dachannien
June 11th, 2007, 22:43
Sure, I'll post it below. I should warn you, though, that it doesn't behave quite the same way yours does. In fact, your version is probably better.

Mine goes through the entry list and counts how many strings start with (for example) "Goblin", and if that number is more than 0, it appends a number that's one greater than whatever the count was. That means that if you have five Goblins on there already, you delete Goblin 2 and Goblin 5, and you add another Goblin, it will number the new one Goblin 4 even though there is already a Goblin 4 on the list. Since I was only programming for my own benefit, I didn't bother making it good enough to satisfy anyone other than me :)

The if-then block under the "Name" comment in addNpc() looks like this in my code:

if source.getChild("name") then
for k,v in ipairs(getWindows()) do
if string.sub(v.name.getValue(), 1, string.len(source.getChild("name").getValue())) == source.getChild("name").getValue() then
which = which + 1;
end
end
if which > 1 then
newentry.name.setValue(source.getChild("name").getValue() .. " " .. tostring(which));
else
newentry.name.setValue(source.getChild("name").getValue());
end
end

BTW, I went ahead and added the automatic initiative roll. That should prove immensely helpful in future sessions :)

Also BTW, you could have the script send the text in the HD field to ChatManager.processDie() whenever a new NPC entry is created. That will generate a secret roll in the chat box for the hit point rolls which you can then drag into the HP box on the tracker, replacing the default HP value that gets put there immediately upon creation of the new entry, if you so choose.

SniperDM
June 13th, 2007, 01:34
Thanks Dachannien! Your script looks much more orderly. Mine was similar early on, but I just wasn't happy with it so I kept adding and adding until it's the abomination you see now. Hardly shop standards by any means. :P Still, I prefer the way it works.

As for the HP roller, having the dice roll in the chat window so they have to be dragged saves a tiny amount of time, but I much prefer a process that's totally automated. I rather like moon_wizard's idea though, so I went ahead and put together a one that fires for entries beyond the first. This means that 'Goblin 1' will always have average hit points, no matter how many you add, but it also means main villains won't have magic fluctuating hp totals. You can see the new section added to the first post, and the downloadable script has been updated too.

It's worth noting that there IS a way to have even "Goblin 1" have random hps while unique NPCs go with the given totals, but it would make the script even messier. I will probably try to rewrite it for cleanliness if I get some free time soon.

sloejack
June 13th, 2007, 15:25
It's worth noting that there IS a way to have even "Goblin 1" have random hps while unique NPCs go with the given totals, but it would make the script even messier. I will probably try to rewrite it for cleanliness if I get some free time soon.

EDIT: I knew it was a dumb question, I just re-read what you said about the unique NPCs part.

Ok, dumb question time, but why not remove the name count from the if check and do the following:

-- HP
if source.getChild("hp") then
local hdstring = source.getChild("hd").getValue();
local rollhp = 0;
for v, numdie, dietype, mod in string.gmatch(hdstring, "((%d+)d(%d+)([%+%-]*%d*))") do
if v then
for i = 1, tonumber(numdie) do
rollhp = rollhp + math.random(tonumber(dietype));
end
if tonumber(mod) then
rollhp = rollhp + tonumber(mod);
end
end
end
newentry.hp.setValue(rollhp);
end

darthfrodo
August 1st, 2007, 22:18
Sniper,
Would you be so good as to give step-by-step installation instructions for this script? It seems like a VERY helpful widget, but I can't decipher where I'm supposed to put what and how.
thanks,
Darth Frodo

Jozie
September 11th, 2007, 05:52
Great Script. Very useful. Thanks for making it available to us.

zifnab69_fr
September 14th, 2007, 10:01
hello, this script look like very usefull, but as i dont read well english i can't understand is the first script in zip file is good or if i must mod it whith the other piece of script there is in the tread...
well is somebody can post the final and working script please ?
Thank you

balth
June 3rd, 2008, 23:08
Hi. I've modded my combattracker.lua to match your edits as listed in the boxes above SAVE for the very last thing about the addNPC function.

I cannot find that data in the combattracker.lua you included in your zip file, nor do I know where to put it. Can you be more specific, please?

Thanks in advance~

(I cannot just replace my .lua file as I am running several other mods besides yours, so copypasta is a must ;) )

EDIT*** I just wanted to clarify I have now triplechecked, and the combattracker.lua you offer for download does NOT have this anywhere in it:


Also added to the end of the addNpc() function, before the return statement:
Code:

--Roll initiative and sort newentry.initresult.setValue(math.random(20)+newen try.init.getValue()); applySort(getWindows());