File: format_help.lua

package info (click to toggle)
widelands 2%3A1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 684,084 kB
  • sloc: cpp: 196,737; ansic: 19,395; python: 8,515; sh: 1,734; xml: 700; makefile: 46; lisp: 25
file content (290 lines) | stat: -rw-r--r-- 11,055 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
-- RST
-- format_help.lua
-- -------------------------------------
--
-- Functions used in the ingame help windows for formatting the text and pictures.

include "scripting/richtext.lua"
include "scripting/help.lua"

--  =======================================================
--  *************** Basic helper functions ****************
--  =======================================================

function consumed_items_line(text, images)
   return
      div("width=100%",
         div("width=50%", p(vspace() .. text .. space())) ..
         div("width=*", styles.as_p_with_attr("wui_image_line", "align=right", images))
      )
end

-- RST
-- .. function:: image_line(image, count[, text = nil])
--
--    Aligns the image to a row on the right side with text on the left.
--
--    :arg image: the picture to be aligned to a row.
--    :arg count: length of the picture row.
--    :arg text: if given the text aligned on the left side, formatted via richtext.lua functions.
--    :returns: the text on the left and a picture row on the right.
--
function image_line(image, count, text)
   if not text then
      text = ""
   end
   local images = ""
   for i=1,count do
      images = images .. img(image)
   end

   return consumed_items_line(text, images)
end

-- RST
-- .. function:: plot_size_line(size, size_only)
--
--    Creates a line describing space required on the map.
--    Consists of a header colored text, followed by normal text and an image.
--
--    :arg size: size key. Expected values are "mine", "port", "small, "medium", "big", "none".
--    :arg size_only: size_only key. Optional bool value if size is a space requirement as well.
--    :returns: header followed by normal text and image if a space is required, or empty string.
--
function plot_size_line(size, size_only)
   local text = ""
   local image = ""
   if (size_only == true) then
      if (size == "small") then
         -- TRANSLATORS: Size of a map immovablee
         text = _("Small")
      elseif (size == "medium") then
         -- TRANSLATORS: Size of a map immovable
         text = _("Medium")
      elseif (size == "big") then
         -- TRANSLATORS: Size of a map immovable
         text = _("Big")
      end
      -- TRANSLATORS: Size of a map immovable
      if text ~= "" then
         text = p(styles.as_font_from_p("wui_heading_3", text))
      end
      return text
   else
      if (size == "mine") then
         -- TRANSLATORS: Space on the map required for building a building there
         text = _("Mine plot")
         image = "images/wui/overlays/mine.png"
      elseif (size == "port") then
         -- TRANSLATORS: Space on the map required for building a building there
         text = _("Port plot")
         image = "images/wui/overlays/port.png"
      elseif (size == "small") then
         -- TRANSLATORS: Space on the map required for building a building there
         text = _("Small plot")
         image = "images/wui/overlays/small.png"
      elseif (size == "medium") then
         -- TRANSLATORS: Space on the map required for building a building there
         text = _("Medium plot")
         image = "images/wui/overlays/medium.png"
      elseif (size == "big") then
         -- TRANSLATORS: Space on the map required for building a building there
         text = _("Big plot")
         image = "images/wui/overlays/big.png"
      else
         return ""
      end
   -- TRANSLATORS: Space on the map required for building a building there
      text = p(join_sentences(
         styles.as_font_from_p("wui_heading_3", _("Space required:")),
         text))
      return div("width=100%", div("float=right padding_l=" .. default_gap(), p(img(image)))) .. text
   end
end


--  =======================================================
--  ********** Helper functions for dependencies **********
--  =======================================================

-- RST
-- .. function:: dependencies(items[, text = nil])
--
--    Creates a dependencies line of any length.
--
--    :arg items: ware, worker and/or building descriptions in the correct order
--                from left to right as table (set in {}).
--    :arg text: comment of the image.
--    :returns: a row of pictures connected by arrows.
--
function dependencies(items, text)
   if not text then
      text = ""
   end
   local images = img(items[1].icon_name)
   for k,v in ipairs({table.unpack(items,2)}) do
      images = images .. img("images/richtext/arrow-right.png") ..  img(v.icon_name)
   end
   return div("width=100%",
              styles.as_paragraph("wui_image_line", images .. space() .. text))
end


-- RST
-- .. function:: help_ware_amount_line(ware_description, amount)
--
--    Displays an amount of wares with name and images
--
--    :arg ware_description: The :class:`wl.map.WareDescription` for the ware type to be displayed
--    :arg amount: The amount to show as a number
--    :returns: image_line for the ware type and amount
--
function help_ware_amount_line(ware_description, amount)
   amount = tonumber(amount)
   local image = ware_description.icon_name
   local result = ""
   local imgperline = 6
   local temp_amount = amount

   while (temp_amount > imgperline) do
      result = result .. image_line(image, imgperline)
      temp_amount = temp_amount - imgperline
   end
   -- TRANSLATORS: %1$d is a number, %2$s the name of a ware, e.g. 12x Stone
   result = image_line(image, temp_amount, p(_("%1$dx %2$s"):bformat(amount, linkify_encyclopedia_object(ware_description)))) .. result
   return result
end

-- RST
-- .. function:: help_worker_experience(worker_description, becomes_description)
--
--    Displays needed experience levels for workers
--
--    :arg worker_description: The :class:`wl.map.WorkerDescription` for the lower-level worker
--    :arg becomes_description: The :class:`wl.map.WorkerDescription` for the higher-level worker
--    :returns: text describing the needed experience
--
function help_worker_experience(worker_description, becomes_description)
   local result = ""
   -- TRANSLATORS: EP = Experience Points
   local exp_string = _("%s to %s (%s EP)"):format(
         linkify_encyclopedia_object(worker_description),
         linkify_encyclopedia_object(becomes_description),
         worker_description.needed_experience
      )

   worker_description = becomes_description
   becomes_description = worker_description.becomes
   if(becomes_description) then
     -- TRANSLATORS: EP = Experience Points
      exp_string = exp_string .. "<br>" .. _("%s to %s (%s EP)"):format(
            linkify_encyclopedia_object(worker_description),
            linkify_encyclopedia_object(becomes_description),
            worker_description.needed_experience
         )
   end
   result = result .. p("align=right", exp_string)
   return result
end

-- RST
-- .. function:: help_tool_string(tribe, toolname, no_of_workers)
--
--    Displays tools with an intro text and images
--
--    :arg tribe: The :class:`wl.map.TribeDescription` for the tribe that uses the tools
--    :arg toolnames: e.g. {"shovel", "basket"}.
--    :arg no_of_workers: the number of workers using the tools; for plural formatting.
--    :returns: image_line for the tools
--
function help_tool_string(tribe, toolnames, no_of_workers)
   local result = ""
   local game  = wl.Game();
   for i, toolname in ipairs(toolnames) do
      if (tribe:has_ware(toolname)) then
         local ware_description = game:get_ware_description(toolname)
         result = result .. image_line(ware_description.icon_name, 1, p(linkify_encyclopedia_object(ware_description)))
      elseif (tribe:has_worker(toolname)) then
         local worker_description = game:get_worker_description(toolname)
         result = result .. image_line(worker_description.icon_name, 1, p(linkify_encyclopedia_object(worker_description)))
      end
   end
   return result
end


-- RST
-- .. function:: help_consumed_wares_workers(tribe, building, program_name)
--
--    Returns information for which wares and workers in which amounts are consumed by a production program.
--
--    :arg tribe: The :class:`wl.map.TribeDescription` for the tribe that consumes the ware
--    :arg building: The :class:`wl.map.BuildingDescription` for the building that runs the program
--    :arg program_name: The name of the production program that the info is collected for
--
--    :returns: A "Ware(s) consumed:" section with image_lines
--
function help_consumed_wares_workers(tribe, building, program_name)
   local result = ""
   local consumed_items_string = ""
   local consumed_items_counter = 0
   local consumed_wares_workers = building:consumed_wares_workers(program_name)
   local consumes_wares = false
   local consumes_workers = false
   for countlist, itemlist in pairs(consumed_wares_workers) do
      local consumed_itemnames = {}
      local consumed_images = {}
      local consumed_amount = {}
      local count = 1
      for consumed_item, amount in pairs(itemlist) do
         local description
         if tribe:has_ware(consumed_item) then
            description = wl.Game():get_ware_description(consumed_item)
            consumes_wares = true
         else
            description = wl.Game():get_worker_description(consumed_item)
            consumes_workers = true
         end
         consumed_itemnames[count] = _("%1$dx %2$s"):bformat(amount, linkify_encyclopedia_object(description))
         consumed_images[count] = description.icon_name
         consumed_amount[count] = amount
         count = count + 1
         consumed_items_counter = consumed_items_counter + amount
      end
      local text = localize_list(consumed_itemnames, "or")
      if (countlist > 1) then
         text = _("%s and"):bformat(text)
      end
      local images = ""
      local image_counter = 1
      while (image_counter <= consumed_amount[1]) do
         images = images .. img(consumed_images[1])
         image_counter = image_counter + 1
      end
      for k, v in ipairs({table.unpack(consumed_images,2)}) do
         image_counter = 1
         while (image_counter <= consumed_amount[k + 1]) do
            images = images .. img(v)
            image_counter = image_counter + 1
         end
      end
      consumed_items_string = consumed_items_line(text, images) .. consumed_items_string
   end
   if (consumed_items_counter > 0) then
      local consumed_header = ""
      if (consumes_workers) then
         if (consumes_wares) then
            -- TRANSLATORS: Tribal Encyclopedia: Heading for wares and workers consumed by a productionsite
            consumed_header = _("Wares and workers consumed:")
         else
            -- TRANSLATORS: Tribal Encyclopedia: Heading for workers consumed by a productionsite
            consumed_header = _("Workers consumed:")
         end
      else
         -- TRANSLATORS: Tribal Encyclopedia: Heading for wares consumed by a productionsite
         consumed_header = _("Wares consumed:")
      end
      result = result .. h4(consumed_header) .. consumed_items_string
   end
   return result
end