Mod to Random Units

Discussion of Lua and LuaWML support, development, and ideas.

Moderator: Forum Moderators

Post Reply
bil
Posts: 7
Joined: July 21st, 2021, 9:01 am

Mod to Random Units

Post by bil »

I'm using Random Units in the Ageless Era with the Random Campaign, lots of fun. But I wanted the Random Units to only be for the human side. In Radom_Units.lua at line 188 I added
if wesnoth.sides[wesnoth.current.side].controller == "human" then
So;
onEvent(
'recruit',

function()
if wesnoth.sides[wesnoth.current.side].controller == "human" then
setRandomRecruit(wesnoth.sides[wesnoth.current.side], options) end end)

It works. If anyone knows a better way to do it?
Also, I'm not sure how the community feels about uploading mods to addons, which is why I simply posted the code here, in case anyone else wants to try it.
How would I go about make all the human side recruits start at lvl 1? I figure it's here;
local function levelWeight(level)
return 6 - level
end

Maybe I can simply return 1?
Luther
Posts: 128
Joined: July 28th, 2007, 5:19 pm
Location: USA

Re: Mod to Random Units

Post by Luther »

Also, I'm not sure how the community feels about uploading mods to addons, which is why I simply posted the code here, in case anyone else wants to try it.
This add-on is under the GPL, so you have the right to upload a tweaked version of this code under a different name. I've already ported this to Wesnoth 1.15, so that's the only version I'm maintaining right now. I might be willing to add some options to avoid you having to port your fork when you upgrade Wesnoth. If you're familiar with git, you can clone it from https://github.com/luther9/Random_Units

However, when I look up the controller attribute at https://wiki.wesnoth.org/LuaWML/Sides#wesnoth.sides, I see this: "note: In networked multiplayer, the controller attribute may not be the same on all clients. Be very careful or you'll have OOS errors." I don't know why the controller would be different for different clients, but apparently, it's unsafe to use that attribute in a mod that may be used in multiplayer.
How would I go about make all the human side recruits start at lvl 1? I figure it's here;
No, that function sets a probability weight that's used to make low level units more common than high level ones. Instead, look at W.randomUnits_loadUnitTypes and try changing the if statement to if not unitType.do_not_list and unitType.level == 1 then (untested). Remember that some units are level 0. I assume you want to skip those. We could maybe add a checkbox option that restricts the unit selection to level 1.
User avatar
Ravana
Forum Moderator
Posts: 2933
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Mod to Random Units

Post by Ravana »

It is not explicitly unsafe, but controller has behaved differently in different wesnoth versions. You would need to test if it fits your use case with the version you develop for.

Reasoning for that note is that at some point it was decided to not publish if side is controlled by player, or player has set it to ai. And in such case it might be needed to load controller as that player in unsynced manner, and then sync it later.
bil
Posts: 7
Joined: July 21st, 2021, 9:01 am

Re: Mod to Random Units

Post by bil »

It works!
Now that I understand, I took a wild guess;
if not unitType.do_not_list and unitType.level == 1 and unitType.alignment == "lawful" then
And this seemed to work well in playtesting. I seemed to have only gotten Loyalists and boats. Does it use units from faction addons, or only default?
I'm going to try unitType.level <= 1 and unitType.alignment ~= "chaotic" next.
Luther
Posts: 128
Joined: July 28th, 2007, 5:19 pm
Location: USA

Re: Mod to Random Units

Post by Luther »

Ravana wrote: July 26th, 2021, 9:25 am It is not explicitly unsafe, but controller has behaved differently in different wesnoth versions. You would need to test if it fits your use case with the version you develop for.

Reasoning for that note is that at some point it was decided to not publish if side is controlled by player, or player has set it to ai. And in such case it might be needed to load controller as that player in unsynced manner, and then sync it later.
So how would we test it to see if it works? bil is checking the controller at every recruit event, but it's conceivable that we might only check it at the prestart event. (I'm not sure it would make sense to change the recruit list every time a player enters or leaves the game.)
User avatar
Ravana
Forum Moderator
Posts: 2933
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Mod to Random Units

Post by Ravana »

Prestart would be quite safe time, noone has had chance to droid their side yet.

In general I would test OOS kind of aspect by using wesnoth.message and being in game with 2 clients.
Luther
Posts: 128
Joined: July 28th, 2007, 5:19 pm
Location: USA

Re: Mod to Random Units

Post by Luther »

bil wrote: July 26th, 2021, 9:54 am I seemed to have only gotten Loyalists and boats. Does it use units from faction addons, or only default?
I assume you're on Wesnoth 1.14, because Random Campaign isn't on the 1.15 server yet. That version only includes units from core and Era of Myths. The one for 1.15 only uses core, because EoM wasn't uploaded when I checked.

The units to be included must be hard-coded in _main.cfg, inside [randomUnits_loadUnitTypes]. This is mainly because different players in multiplayer will have different add-ons installed.
I'm going to try unitType.level <= 1 and unitType.alignment ~= "chaotic" next.
Sounds interesting. Let me know what works best for you. This will help me decide what filtering options I might want to add.
bil
Posts: 7
Joined: July 21st, 2021, 9:01 am

Re: Mod to Random Units

Post by bil »

unitType.level <= 1 Didn't seem to do anything, all units were 1. unitType.alignment ~= "chaotic" totally worked. I'm thinking anything that falls under unitType would work as a filter. Gender, Race, Usage, even Cost.
My next change, I'm interested to see if I can get the recruit list to show 2 or 3 different choices. When the choice is made, then the next recruit will show 2 or 3 different options.
Post Reply