AI which can change weapons at villages

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

Moderators: Forum Moderators, Developers

Post Reply
Posts: 139
Joined: December 17th, 2015, 10:27 pm

AI which can change weapons at villages

Post by Can-ned_Food » March 19th, 2018, 3:49 am

I don't know if there is a better board in which to categorize this request or discussion. It seems to my albeit limited knowledge that most AIs nowadays are written in Lua.

Situation: With one of my add–ons in development — Food Fighters, — I am writing [event]s which allow users to swap out the weapons equipped when one of their troopers is occupying a village a.k.a. a Weapons Depot + Medical Shack or whatever. A few attacks are always enabled, but only one out of four of them — pistol, long–barrelled rifle, mortarcannon, and bazooka — can be enabled at any one time.

I am wondering if anyone knows of an AI already written which would accomplish the things I'd need:
  • compare the effectiveness of different attacks against a certain target or group of targets
  • weigh those evaluations with the feasibility of reaching a village to change weapon
  • if profitable, plan a path to reach that village and then return to targets
  • re-evaluate as necessary
and so on.

A brief search of the boards — 12 topics which matched my search keywords — returned nothing helpful.

Now, at the risk of sounding too needy, I would also add that I would prefer a non–Lua framework if I need to do major revisions on the thing. For some reason, I simply don't ‘get’ Lua. Of course, by the time I am able to put in some effort to writing this AI procedure, that could be different.
Eh, never mind that. I'll keep this topic focused on Lua AIs.

Posts: 939
Joined: February 10th, 2013, 2:25 pm

Re: AI which can change weapons at villages

Post by gfgtdf » March 20th, 2018, 11:58 am

while it shodul be possibel to write such an ai in lua it's very unlikely that there exists something simlar that was alreaqdy coded.
Scenario with Robots SP scenario (1.11/1.12), allows you to build your units with components, PYR No preperation turn 1.12 mp-mod that allows you to select your units immideately after the game begins.

User avatar
Posts: 1038
Joined: August 3rd, 2012, 11:26 pm

Re: AI which can change weapons at villages

Post by Celtic_Minstrel » March 21st, 2018, 4:18 am

Well, it's technically possible to write Wesnoth AIs in C++, though it's not useful if you intend to share it since you need to compile the AI directly into the engine.

Anyway, a very basic AI that does this seems as if it should be relatively simple to code. By basic I mean something like "when it lands on a village it randomly selects a new weapon". You'd need to register a custom synced command in the new wesnoth.custom_synced_commands table, which is a function that does the actual weapon switch on the unit, then invoke that synced command from your AI (which would probably be a custom candidate action).

Once you get that working you can worry about actually getting units to seek out villages if they feel their current weapon isn't suitable for the current situation, and make informed choices as to which weapon to switch to, and that kind of thing. I think this stuff is largely already done in the engine, though I'm not sure if there's Lua API access to all of it. There might even be some things that are more feasible in FormulaAI? At least, I'm pretty sure FormulaAI still has some things that were never ported to Lua AI. (Unfortunately FormulaAI can't be invoked from Lua though... plus it might be even harder to understand than Lua due to the constness of WFL, though your mileage may vary on that.)
Author of The Black Cross of Aleron campaign and Default++ era.
Maintainer of Steelhive.

Posts: 1008
Joined: October 13th, 2010, 6:14 pm
Location: Wandering, mostly aimlessly

Re: AI which can change weapons at villages

Post by mattsc » March 21st, 2018, 11:26 am

Hi, to answer your closing question first, no, something like that does not yet exist — definitely not in mainline and almost certainly not in UMC. As Celtic_Minstrel wrote, it should be relatively “simple” to write a basic version of it though and getting the weapons-change coded in the AI without causing OOSs is probably the hardest part, so I would follow his suggestion on trying that first. You could probably do all this in WML (as a, say, side_turn event), but if it were me, I’d definitely do it in an AI for the flexibility that offers. Whether you use Lua or WFL is mostly a question of personal preference. I'd use Lua, but that's just because that's what I'm more familiar with these days.

Once you've done those first steps, one possible implementation (there are many different way to accomplish this) of this behavior might look like this:
  • Find all units which are able to switch weapons (maybe that’s all units, in which case you just select all units).
  • For each unit, find which villages it can reach
  • For each of these villages, find which enemies it can reach with reduced MP after getting to the village
  • Also find which enemies it can reach without going to a village first
  • Then, simulate the attack by the unit on all these enemies, with the original weapon(s) and with changed weapons, decide which of those is the best attack, and execute that sequence.
Most of that is pretty simple (at least in principle). In Lua, finding the units can be done with wesnoth.get_units, finding where a unit can go is done with wesnoth.find_reach or wesnoth.find_path, and simulating the attack uses wesnoth.simulate_combat. You also need to come up with some sort of metric to determine which is the best attack, but I’d start with something very simple such as some weighted sum of average damage done and die chance, both of which are in the table returned by simulate_combat. Or you can skip that attack simulation step and just use some general evaluation of the weapon.

Anyways, that’s just the concept (and maybe it’s obvious anyway), the tricky bit is getting this all put together in practice. For that, I’d take a custom AI that also exists somewhere in a mainline campaign or UMC, strip it down to the very basics and then introduce the above one step at a time (using lots of debug output), starting with what Celtic_Minstrel suggested.

Post Reply