wesnoth.theme_items.unit_alignment

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

Moderators: Forum Moderators, Developers

wesnoth.theme_items.unit_alignment

Postby Rodrom » June 30th, 2017, 12:38 pm

For a while now I've been working on an "element"-themed multiplayer era for Wesnoth 1.13. It seemed like I knew how to use the LUA script up until now, when I noticed a problem that I can't figure out how to solve. There are four LUA scripts being used:

The first is this script, shown here.
Code: Select all
   [event]
        name=preload
        first_time_only=no
        [lua]
            code=<<
            local old_unit_alignment = wesnoth.theme_items.unit_alignment
                function wesnoth.theme_items.unit_alignment()
                    local u = wesnoth.get_displayed_unit()
                    if not u then return {} end
                    local s = old_unit_alignment()
               if u.status.subarctic or u.status.tropical or u.status.weathered or u.status.grounded then
                  if u.status.subarctic and u.status.sweaty then
                     table.insert(s, { "element", {                           
                        text = ",W<span color='#FF9933' size='small'>(-25%)</span>",
                        tooltip = "water: Water/ice units have a 25% damage increase in cool areas, and a 25% damage decrease in warm areas."
                     } })
                  elseif u.status.subarctic and u.status.frigid then
                     table.insert(s, { "element", {                           
                        text = ",W<span color='#00ff00' size='small'>(+25%)</span>",
                        tooltip = "water: Water/ice units have a 25% damage increase in cool areas, and a 25% damage decrease in warm areas."
                     } })
                  elseif u.status.subarctic then
                     table.insert(s, { "element", {                           
                        text = ",W(+0%)",
                        tooltip = "water: Water/ice units have a 25% damage increase in cool areas, and a 25% damage decrease in warm areas."
                     } })
                  end
                  return s
               end
                end
            >>
        [/lua]
    [/event]


w_template.png
water-themed display


The other three are very similar:

Code: Select all
   [event]
        name=preload
        first_time_only=no
        [lua]
            code=<<
            local old_unit_alignment = wesnoth.theme_items.unit_alignment
                function wesnoth.theme_items.unit_alignment()
                    local u = wesnoth.get_displayed_unit()
                    if not u then return {} end
                    local s = old_unit_alignment()
               if u.status.subarctic or u.status.tropical or u.status.weathered or u.status.grounded then
                  if u.status.grounded and u.status.windy then
                     table.insert(s, { "element", {                           
                        text = ",E<span color='#FF9933' size='small'>(-25%)</span>",
                        tooltip = "earth: Earthen units have a 25% damage increase in dusty areas, and a 25% damage decrease in windy areas."
                     } })
                  elseif u.status.grounded and u.status.dusty then
                     table.insert(s, { "element", {                           
                        text = ",E<span color='#00ff00' size='small'>(+25%)</span>",
                        tooltip = "earth: Earthen units have a 25% damage increase in dusty areas, and a 25% damage decrease in windy areas."
                     } })
                  elseif u.status.grounded then
                     table.insert(s, { "element", {                           
                        text = ",E(+0%)",
                        tooltip = "earth: Earthen units have a 25% damage increase in dusty areas, and a 25% damage decrease in windy areas."
                     } })
                  end
                  return s
               end
                end
            >>
        [/lua]
    [/event]
   

e_template.png
earth-themed display


Code: Select all
   [event]
        name=preload
        first_time_only=no
        [lua]
            code=<<
            local old_unit_alignment = wesnoth.theme_items.unit_alignment
                function wesnoth.theme_items.unit_alignment()
                    local u = wesnoth.get_displayed_unit()
                    if not u then return {} end
                    local s = old_unit_alignment()
               if u.status.subarctic or u.status.tropical or u.status.weathered or u.status.grounded then
                  if u.status.tropical and u.status.frigid then
                     table.insert(s, { "element", {                           
                        text = ",F<span color='#FF9933' size='small'>(-25%)</span>",
                        tooltip = "heat: Sun/heat/fire units have a 25% damage increase in warm areas, and a 25% damage decrease in cool areas."
                     } })
                  elseif u.status.tropical and u.status.sweaty then
                     table.insert(s, { "element", {                           
                        text = ",F<span color='#00ff00' size='small'>(+25%)</span>",
                        tooltip = "heat: Sun/heat/fire units have a 25% damage increase in warm areas, and a 25% damage decrease in cool areas."
                     } })
                  elseif u.status.tropical then
                     table.insert(s, { "element", {                           
                        text = ",F(+0%)",
                        tooltip = "heat: Sun/heat/fire units have a 25% damage increase in warm areas, and a 25% damage decrease in cool areas."
                     } })
                  end
                  return s
               end
            end
            >>
        [/lua]
    [/event]

f_template.png
fire-themed display


Code: Select all
   [event]
        name=preload
        first_time_only=no
        [lua]
            code=<<
            local old_unit_alignment = wesnoth.theme_items.unit_alignment
                function wesnoth.theme_items.unit_alignment()
                    local u = wesnoth.get_displayed_unit()
                    if not u then return {} end
                    local s = old_unit_alignment()
               if u.status.subarctic or u.status.tropical or u.status.weathered or u.status.grounded then
                  if u.status.weathered and u.status.dusty then
                     table.insert(s, { "element", {                           
                        text = ",A<span color='#FF9933' size='small'>(-25%)</span>",
                        tooltip = "air: Air units have a 25% damage increase in windy areas, and a 25% damage decrease in dusty areas."
                     } })
                  elseif u.status.weathered and u.status.windy then
                     table.insert(s, { "element", {                           
                        text = ",A<span color='#00ff00' size='small'>(+25%)</span>",
                        tooltip = "air: Air units have a 25% damage increase in windy areas, and a 25% damage decrease in dusty areas."
                     } })
                  elseif u.status.weathered then
                     table.insert(s, { "element", {                           
                        text = ",A(+0%)",
                        tooltip = "air: Air units have a 25% damage increase in windy areas, and a 25% damage decrease in dusty areas."
                     } })
                  end
                  return s
               end
                end
            >>
        [/lua]
    [/event]

a_template.png
wind-themed display


The problem is that any units that do not have a "themed" ability and status have their alignment obscured on the right-hand panel, like this
n_template.png


I want to be able to use regular units and my themed units together if possible.

There are two ideas I had that seemed like they could be solutions:

#1 add a unique status to "regular" units and then have a fifth LUA script represent them:

I'd use this
Code: Select all
[event]
  name=new turn
  first_time_only=no
     [store_unit]      
         [filter]
            [not]
               ability=tropical,subarctic,freebound,earthbound
            [/not]
         [/filter]
       variable=icon_store
       kill=yes
     [/store_unit]
  {FOREACH icon_store i}
    [set_variable]
      name=icon_store[$i].status.mild
      value=yes   
    [/set_variable]
   [unstore_unit]
     variable=icon_store[$i]
   [/unstore_unit]
  {NEXT i}
  {CLEAR_VARIABLE icon_store}
[/event]
[event]
  name=prerecruit
  first_time_only=no
     [store_unit]      
         [filter]
            [not]
               ability=tropical,subarctic,freebound,earthbound
            [/not]
         [/filter]
       variable=icon_store
       kill=yes
     [/store_unit]
  {FOREACH icon_store i}
    [set_variable]
      name=icon_store[$i].status.mild
      value=yes   
    [/set_variable]
   [unstore_unit]
     variable=icon_store[$i]
   [/unstore_unit]
  {NEXT i}
  {CLEAR_VARIABLE icon_store}
[/event]
[event]
  name=prerecall
  first_time_only=no
     [store_unit]      
         [filter]
            [not]
               ability=tropical,subarctic,freebound,earthbound
            [/not]
         [/filter]
       variable=icon_store
       kill=yes
     [/store_unit]
  {FOREACH icon_store i}
    [set_variable]
      name=icon_store[$i].status.mild
      value=yes   
    [/set_variable]
   [unstore_unit]
     variable=icon_store[$i]
   [/unstore_unit]
  {NEXT i}
  {CLEAR_VARIABLE icon_store}
[/event]


and then something like this. I know it's wrong, but I couldn't figure out how to represent the normal alignment in this situation.
Code: Select all
   [event]
        name=preload
        first_time_only=no
        [lua]
            code=<<
            local old_unit_alignment = wesnoth.theme_items.unit_alignment
                function wesnoth.theme_items.unit_alignment()
                    local u = wesnoth.get_displayed_unit()
                    if not u then return {} end
                    local s = old_unit_alignment()
               if u.status.mild then s = u.old_unit_alignment

               end
               return s
            end
         >>
      [/lua]
   [/event]


#2 Change the first four scripts so that they don't obscure the alignment for "normal" units in the first place. Not sure how to do that.
Rodrom
 
Posts: 41
Joined: March 4th, 2017, 7:00 am
Location: Reach me here: Orwell64123@gmail.com

Re: wesnoth.theme_items.unit_alignment

Postby gfgtdf » July 10th, 2017, 8:38 pm

I didn't read your whole post so i migth be wrong, but if you use codes like this:

Code: Select all
local old_unit_alignment = wesnoth.theme_items.unit_alignment
function wesnoth.theme_items.unit_alignment()
   local u = wesnoth.get_displayed_unit()
   if not u then return {} end
   local s = old_unit_alignment()
   if u.status.subarctic or u.status.tropical or u.status.weathered or u.status.grounded then
      if u.status.subarctic and u.status.sweaty then
         table.insert(s, { "element", {                           
            text = ",W<span color='#FF9933' size='small'>(-25%)</span>",
            tooltip = "water: Water/ice units have a 25% damage increase in cool areas, and a 25% damage decrease in warm areas."
         } })
      elseif u.status.subarctic and u.status.frigid then
         table.insert(s, { "element", {                           
            text = ",W<span color='#00ff00' size='small'>(+25%)</span>",
            tooltip = "water: Water/ice units have a 25% damage increase in cool areas, and a 25% damage decrease in warm areas."
         } })
      elseif u.status.subarctic then
         table.insert(s, { "element", {                           
            text = ",W(+0%)",
            tooltip = "water: Water/ice units have a 25% damage increase in cool areas, and a 25% damage decrease in warm areas."
         } })
      end
      return s
   end
end


(i just reindented that one), then if u.status.subarctic or u.status.tropical or u.status.weathered or u.status.grounded evaluatess to false then the return s line is never reached, and if wesnoth.theme_items.unit_alignment returns nothing the the game will just print nothing in the aligment panel in that case. The solution is then so move the return s line out of that if block so that it is called unconditionallly.


A good code indention really helps you to find these type of errors. Since you are using wesnoth 1.13 where 'requireing download' of addons isn't that much of a problem anymore i really reccomend to put your lua code in external .lua files that are then loaded vial wesnoth.dofile or wesnoth.require . If you then us an editor with autoindention for lua files you don't really need to do much anymore.
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.
gfgtdf
Developer
 
Posts: 869
Joined: February 10th, 2013, 2:25 pm

Re: wesnoth.theme_items.unit_alignment

Postby Rodrom » July 11th, 2017, 3:00 am

Thanks gfgtdf. Changed the LUA scripts and they work fine now! Auto-indentation for LUA in Notepad++ should be doable since there are guides online.

Code: Select all
local old_unit_alignment = wesnoth.theme_items.unit_alignment
function wesnoth.theme_items.unit_alignment()
   local u = wesnoth.get_displayed_unit()
   if not u then return {} end
   local s = old_unit_alignment()
   if u.status.subarctic or u.status.tropical or u.status.weathered or u.status.grounded then
      if u.status.subarctic and u.status.sweaty then
         table.insert(s, { "element", {                           
            text = ",W<span color='#FF9933' size='small'>(-15%)</span>",
            tooltip = "water: Water/ice units have a 15% damage increase in cool areas, and a 15% damage decrease in warm areas."
         } })
      elseif u.status.subarctic and u.status.frigid then
         table.insert(s, { "element", {                           
            text = ",W<span color='#00ff00' size='small'>(+15%)</span>",
            tooltip = "water: Water/ice units have a 15% damage increase in cool areas, and a 15% damage decrease in warm areas."
         } })
      elseif u.status.subarctic then
         table.insert(s, { "element", {                           
            text = ",W(+0%)",
            tooltip = "water: Water/ice units have a 15% damage increase in cool areas, and a 15% damage decrease in warm areas."
         } })
      end
   end
return s
end
   
   
local old_unit_alignment = wesnoth.theme_items.unit_alignment
function wesnoth.theme_items.unit_alignment()
   local u = wesnoth.get_displayed_unit()
   if not u then return {} end
   local s = old_unit_alignment()
   if u.status.subarctic or u.status.tropical or u.status.weathered or u.status.grounded then
      if u.status.grounded and u.status.windy then
         table.insert(s, { "element", {                           
            text = ",E<span color='#FF9933' size='small'>(-15%)</span>",
            tooltip = "earth: Earthen units have a 15% damage increase in dusty areas, and a 15% damage decrease in windy areas."
         } })
      elseif u.status.grounded and u.status.dusty then
         table.insert(s, { "element", {                           
            text = ",E<span color='#00ff00' size='small'>(+15%)</span>",
            tooltip = "earth: Earthen units have a 15% damage increase in dusty areas, and a 15% damage decrease in windy areas."
         } })
      elseif u.status.grounded then
         table.insert(s, { "element", {                           
            text = ",E(+0%)",
            tooltip = "earth: Earthen units have a 15% damage increase in dusty areas, and a 15% damage decrease in windy areas."
         } })
      end
   end
return s
end
   
local old_unit_alignment = wesnoth.theme_items.unit_alignment
function wesnoth.theme_items.unit_alignment()
   local u = wesnoth.get_displayed_unit()
   if not u then return {} end
   local s = old_unit_alignment()
   if u.status.subarctic or u.status.tropical or u.status.weathered or u.status.grounded then
      if u.status.tropical and u.status.sweaty then
         table.insert(s, { "element", {                           
            text = ",F<span color='#00ff00' size='small'>(+15%)</span>",
            tooltip = "heat: Sun/heat/fire units have a 15% damage increase in warm areas, and a 15% damage decrease in cool areas."
         } })
      elseif u.status.tropical and u.status.frigid then
         table.insert(s, { "element", {                           
            text = ",F<span color='#FF9933' size='small'>(-15%)</span>",
            tooltip = "heat: Sun/heat/fire units have a 15% damage increase in warm areas, and a 15% damage decrease in cool areas."
         } })
      elseif u.status.tropical then
         table.insert(s, { "element", {                           
            text = ",F(+0%)",
            tooltip = "heat: Sun/heat/fire units have a 15% damage increase in warm areas, and a 15% damage decrease in cool areas."
         } })
      end
   end
return s
end
   
local old_unit_alignment = wesnoth.theme_items.unit_alignment
function wesnoth.theme_items.unit_alignment()
   local u = wesnoth.get_displayed_unit()
   if not u then return {} end
   local s = old_unit_alignment()
   if u.status.subarctic or u.status.tropical or u.status.weathered or u.status.grounded then
      if u.status.weathered and u.status.dusty then
         table.insert(s, { "element", {                           
            text = ",A<span color='#FF9933' size='small'>(-15%)</span>",
            tooltip = "air: Air units have a 15% damage increase in windy areas, and a 15% damage decrease in dusty areas."
         } })
      elseif u.status.weathered and u.status.windy then
         table.insert(s, { "element", {                           
            text = ",A<span color='#00ff00' size='small'>(+15%)</span>",
            tooltip = "air: Air units have a 15% damage increase in windy areas, and a 15% damage decrease in dusty areas."
         } })
      elseif u.status.weathered then
         table.insert(s, { "element", {                           
            text = ",A(+0%)",
            tooltip = "air: Air units have a 15% damage increase in windy areas, and a 15% damage decrease in dusty areas."
         } })
      end
   end
return s
end
Rodrom
 
Posts: 41
Joined: March 4th, 2017, 7:00 am
Location: Reach me here: Orwell64123@gmail.com

Re: wesnoth.theme_items.unit_alignment

Postby gfgtdf » July 13th, 2017, 7:22 pm

Rodrom wrote:Thanks gfgtdf. Changed the LUA scripts and they work fine now! Auto-indentation for LUA in Notepad++ should be doable since there are guides online.

for notepadd++ you could for example use the indent-by-fold addon. It unfortunatley adds too muhc indention to lines that begin with else though. So you need to do a \telse -> else replace after auto indention if you use that.
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.
gfgtdf
Developer
 
Posts: 869
Joined: February 10th, 2013, 2:25 pm


Return to Lua Labs

Who is online

Users browsing this forum: No registered users and 0 guests