Anyone Can Die

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

Moderator: Forum Moderators

Post Reply
User avatar
Chebmaster
Posts: 1
Joined: April 17th, 2012, 7:26 am
Location: Moscow

Anyone Can Die

Post by Chebmaster »

In short: when your leader dies, an another unit from a pre-defined list takes the mantle and you can fight on. If there's no one left alive from that list, a specific event is called. It's up to you what to do then: proceed to defeat or switch to an another scenario with a spare set of heroes (like in "Sailor Moon: Another Story" if SM is defeated in the end-boss fight, Sailor chibi Moon comes with the Outers for the second chance, but the best ending becomes unavailable).

Realistically, this would be probably suitable only for the very final scenario, where anyone is allowed to die (UtBS would benefit greatly). Otherwise it'd be a coding nightmare (imagine if Deflador would be allowed to die in any map. Or Konrad. The branching for story and dialogue are terrifying)

Even if my breakthrough loses its revolutionary impact (I developed this to hack around the hard-coded defeat on the player-controlled side leader's death) it is still handy for establishing a chain of succession.

There's no guarantee it is bug-free: I wrote it for 1.11.0, then in 1.11.4 it became buggy because treatment of uninitialized variables in Lua has changed. Well, there's one bug less now.

Somewhere in the scenario (you can modify the list according the plot. I place it in my prestart event for simplicity):

Code: Select all

	{VARIABLE available_leaders Bruce,Bob,Jack}
	#a comma-separated list of unit IDs
Somewhere else in the scenario (I suggest installing this via macro in your utils.cfg):

Code: Select all

	[event]
		name=leadership_accept
		first_time_only=no
		[sound]
			name=horn-signals/horn-2.ogg
		[/sound]
		[scroll_to_unit]
			id=$new_leader_id
		[/scroll_to_unit]
		[store_unit]
			[filter]
				id=$new_leader_id
			[/filter]
			variable=ZLSS_store
		[/store_unit]
		[set_variable]
			# by some reasons I was getting errors trying set canrecruit from Lua
			name=ZLSS_store.canrecruit
			value=yes
		[/set_variable]	
		[unstore_unit]
			variable=ZLSS_store
			find_vacant=no
			red,green,blue=255,255,100
			text= _ "Takes the lead!" 
		[/unstore_unit]
	[/event]
	
	[event]
		name=last breath
		first_time_only=no
		[filter]
			side=1
			canrecruit=yes
		[/filter]
		[lua]
			code = <<
				local idlist = {}
				local i=1
				local sep=","
				local inpstr=wesnoth.get_variable("available_leaders")
				for str in string.gmatch(inpstr, "([^"..sep.."]+)") do
					idlist[i] = str
					i = i + 1
				end	
				local unit = wesnoth.get_variable("unit")
				local myid = unit.id
				local succeeded = false
				for k,i in ipairs(idlist) do
					local u = wesnoth.get_units { side = 1, id = i }
					if (#u > 0) and (u[1].id ~= myid) and (u[1].hitpoints > 0) then
						wesnoth.set_variable("new_leader_id", u[1].id)
						succeeded = true
						break
					end
				end
				if succeeded then
					wesnoth.fire_event("leadership_accept")
				else
					wesnoth.fire_event("all_leaders_dead")
				end
			>>
		[/lua]
	[/event]
	[event]
		name=all_leaders_dead
		# do what you need to be done here
		
		
	[/event]
Note that I got a funny bug in 1.11.4: in a 3-side free for all all my leaders were defeated, but as my all_leaders_dead event does nothing, nothing happened and the remaining parties continued going at each other. First they wiped all my remaining units, then kept fighting until one of the sides has been wiped out and its leader killed. Only then did the game remember about me being wiped out and displayed the "defeated" dialog ending the scenario. Lol :lol2:
Why didn't you hear from me before? It's simple: I was developing a doujin campaign in secrecy, based on a well known anime series. Then I thought: screw this, I can develop my own characters! And her I am.
Post Reply