I got the puzzle working, thanks!
If you're interested:
By going on various quests you'll develop reputation with different types of units which allows you to hire them to defend your stronghold.
So far I have:
Guards (cheap to hire and good vs bandits)
Archers (good vs unarmoured or slow enemies)
Clerics (will join if you help them, good vs undead)
Paladins (will join after stage in main quest, expensive to hire but good vs most except fast enemies)
Pirates (will join if you help them, good vs fast enemies & ghosts)
Dwarves (will join if you help them, good vs enemies found underground)
and Sprites (will join if you help them, good vs enemies vulnerable to fire magic)
Each enemy requires at least 1 unit to fight them off. There can be up to 4 enemies attacking at once. If you match a unit with the wrong enemy, they lose a life (most have 2 lives before you have to hire replacements).
This is the script code that makes it all happen:
Code:
unitev = $game_variables[4]
unitid = unitev - 1
listenemy = ["Bat","Slime","Bandit","Wisp","Zombie","Giant Spider","Ghoul","Dryad","Golem","Wraith","Fergrul","Dragon"]
listbeat = [4, 1044, 600, 617, 295, 754, 4052] # bitwise &
x = $game_map.events[unitev].x
y = $game_map.events[unitev].y + 2
enemy = $game_map.events_xy(x, y)[0].name
return if listbeat[unitid] & (2 ** listenemy.find_index(enemy)) > 0 #successful match!
$game_variables[3] += 1 #counter for how many units you lost
$game_self_switches[[@map_id, unitev, 'B']] = true #show the unit's 'damage' sprite
$game_variables[61 + unitid] -= 1 #unit loses a life
That gets run for each unit in battle. The listbeat array contains numbers based on binary matches for the enemies they beat. So for example, the paladin is: 001010111111. Meaning they don't beat bats or slimes, but do beat bandits, etc. I convert this binary into an integer (4052) and then use a 'bitwise and' to match it against the enemy that I find opposite that unit. This is a neat trick I learned at college.
I don't know whether this battle puzzle is worth showing off as a tutorial somewhere but anyway, that's all I needed, thanks!