I haven’t done that level, but after looking at your code I was wondering if maybe what’s happening is that the peasants start building when you call the
for-loop, and then the code proceeds into your first
while-loop, which has your hero disperse them if an enemy shows up, but never calls them back.
(I also don’t think it should be terminating, so I don’t think you’re even getting to the second
while-loop - is your hero attacking the enemies? If he’s not, then this may be the problem.)
If I’m right, the basic story is that you need a way to get your hero to attack in the same loop, and then, when there aren’t any enemies, to command the peasants to get back in place.
Hope that helps!