GunChleoc has proposed merging 
lp:~widelands-dev/widelands/bug-1625502-tips-in-help into lp:widelands.

Commit message:
Convert editor/game tips to Lua and display them in the in-editor/in-game help.

Requested reviews:
  Widelands Developers (widelands-dev)
Related bugs:
  Bug #1625502 in widelands: "Add game and editor tips to in-game help"
  https://bugs.launchpad.net/widelands/+bug/1625502

For more details, see:
https://code.launchpad.net/~widelands-dev/widelands/bug-1625502-tips-in-help/+merge/312969
-- 
Your team Widelands Developers is requested to review the proposed merge of 
lp:~widelands-dev/widelands/bug-1625502-tips-in-help into lp:widelands.
=== modified file 'data/scripting/editor/editor_help.lua'
--- data/scripting/editor/editor_help.lua	2016-04-02 08:09:19 +0000
+++ data/scripting/editor/editor_help.lua	2016-12-10 16:14:18 +0000
@@ -65,7 +65,14 @@
                title = _"Controls",
                script = "scripting/editor/editor_controls.lua",
                script_parameters = {}
+            },
+            {
+               name = "tips",
+               title = _"Tips",
+               script = "scripting/editor/tips.lua",
+               script_parameters = {}
             }
+
          }
       },
       {

=== added file 'data/scripting/editor/tips.lua'
--- data/scripting/editor/tips.lua	1970-01-01 00:00:00 +0000
+++ data/scripting/editor/tips.lua	2016-12-10 16:14:18 +0000
@@ -0,0 +1,18 @@
+include "scripting/formatting.lua"
+
+function get_editor_tips()
+   include "txts/tips/editor.lua"
+   return tips
+end
+
+local text = paragraphdivider()
+for index, contents in pairs(get_editor_tips()) do
+   text = text .. listitem_bullet(contents["text"])
+end
+text = text .. "</p>"
+
+set_textdomain("widelands_editor")
+return {
+  title = _"Tips",
+  text = rt(text)
+}

=== modified file 'data/scripting/format_scenario.lua'
--- data/scripting/format_scenario.lua	2016-04-02 10:53:01 +0000
+++ data/scripting/format_scenario.lua	2016-12-10 16:14:18 +0000
@@ -48,18 +48,6 @@
 
 
 -- RST
--- .. function:: paragraphdivider()
---
---    Closes a paragraph and opens a new paragraph. Use this when you format a string with the speech function
---    and need to divide the speech into multiple paragraphs.
---
---    :returns: <br></p><p font-size=8><br></p><p line-spacing=3 font-size=12>
-function paragraphdivider()
-   return ("<br></p><p font-size=8><br></p><p line-spacing=3 font-size=12>")
-end
-
-
--- RST
 -- .. function:: objective_text(heading, body)
 --
 --    Provides nice formatting for objective texts.

=== modified file 'data/scripting/formatting.lua'
--- data/scripting/formatting.lua	2016-09-21 08:41:51 +0000
+++ data/scripting/formatting.lua	2016-12-10 16:14:18 +0000
@@ -264,3 +264,14 @@
          t2 .. "<br></p><p font-size=8> <br></p></rt>"
    end
 end
+
+-- RST
+-- .. function:: paragraphdivider()
+--
+--    Closes a paragraph and opens a new paragraph. Use this when you format a string with the speech function
+--    and need to divide the speech into multiple paragraphs.
+--
+--    :returns: <br></p><p font-size=8><br></p><p line-spacing=3 font-size=12>
+function paragraphdivider()
+   return ("<br></p><p font-size=8><br></p><p line-spacing=3 font-size=12>")
+end

=== modified file 'data/tribes/scripting/help/init.lua'
--- data/tribes/scripting/help/init.lua	2016-04-02 08:21:25 +0000
+++ data/tribes/scripting/help/init.lua	2016-12-10 16:14:18 +0000
@@ -42,7 +42,7 @@
 
 -- Main function
 return {
-   func = function(tribename)
+   func = function(tribename, game_type)
       set_textdomain("tribes_encyclopedia")
       return {
          title = _"Tribal Encyclopedia",
@@ -64,6 +64,12 @@
                      title = _"Controls",
                      script = "tribes/scripting/help/controls.lua",
                      script_parameters = {}
+                  },
+                  {
+                     name = "tips",
+                     title = _"Tips",
+                     script = "tribes/scripting/help/tips.lua",
+                     script_parameters = {tribename, game_type}
                   }
                }
             },

=== added file 'data/tribes/scripting/help/tips.lua'
--- data/tribes/scripting/help/tips.lua	1970-01-01 00:00:00 +0000
+++ data/tribes/scripting/help/tips.lua	2016-12-10 16:14:18 +0000
@@ -0,0 +1,76 @@
+include "scripting/formatting.lua"
+
+function get_general_tips()
+   include "txts/tips/general_game.lua"
+   return tips
+end
+
+function get_atlantean_tips()
+   include "txts/tips/atlanteans.lua"
+   return tips
+end
+
+function get_barbarian_tips()
+   include "txts/tips/barbarians.lua"
+   return tips
+end
+
+function get_empire_tips()
+   include "txts/tips/empire.lua"
+   return tips
+end
+
+function get_singleplayer_tips()
+   include "txts/tips/singleplayer.lua"
+   return tips
+end
+
+function get_multiplayer_tips()
+   include "txts/tips/multiplayer.lua"
+   return tips
+end
+
+function format_tips(tips)
+   local text = paragraphdivider()
+   for index, contents in pairs(tips) do
+      text = text .. listitem_bullet(contents["text"])
+   end
+   text = text .. "</p>"
+   return text
+end
+
+return {
+   func = function(tribename, game_type)
+      local text = ""
+      set_textdomain("tribes_encyclopedia")
+      if (tribename == "atlanteans") then
+         text = text .. h2(_"Atlanteans")
+         text = text .. format_tips(get_atlantean_tips())
+      elseif (tribename == "barbarians") then
+         text = text .. h2(_"Barbarians")
+         text = text .. format_tips(get_barbarian_tips())
+      elseif (tribename == "empire") then
+         text = text .. h2(_"Empire")
+         text = text .. format_tips(get_empire_tips())
+      end
+
+      set_textdomain("tribes_encyclopedia")
+      if (game_type == "singleplayer") then
+         text = text .. h2(_"Single Player")
+         text = text .. format_tips(get_singleplayer_tips())
+      else
+         text = text .. h2(_"Multiplayer")
+         text = text .. format_tips(get_multiplayer_tips())
+      end
+
+      set_textdomain("tribes_encyclopedia")
+      text = text .. h2(_"General")
+      text = text .. format_tips(get_general_tips())
+
+      set_textdomain("tribes_encyclopedia")
+      return {
+        title = _"Tips",
+        text = rt(text)
+      }
+   end
+}

=== renamed file 'data/txts/tips/atlanteans.tip' => 'data/txts/tips/atlanteans.lua'
--- data/txts/tips/atlanteans.tip	2015-11-07 08:28:27 +0000
+++ data/txts/tips/atlanteans.lua	2016-12-10 16:14:18 +0000
@@ -1,7 +1,12 @@
-[Tip 1]
-text=_Some buildings, such as the farm, have a very small work area. If the building is surrounded with trees or roads, production will stop.
-sec=7
-
-[Tip 2]
-text=_"Always build a forester’s and a fish breeder’s house, so you will not run out of logs or food."
-sec=5
+set_textdomain("texts")
+tips = {
+   {
+      text = _"Keep an eye on your fishers and build a fish breeder’s house. After fish resources are exhausted in their work area, the fish supply cannot be regenerated.",
+      seconds = 4
+   },
+   {
+      text = _"Always build a forester’s and a woodcutter’s house, so you will not run out of logs.",
+      seconds = 5
+   },
+}
+return tips

=== renamed file 'data/txts/tips/barbarians.tip' => 'data/txts/tips/barbarians.lua'
--- data/txts/tips/barbarians.tip	2015-11-07 08:28:27 +0000
+++ data/txts/tips/barbarians.lua	2016-12-10 16:14:18 +0000
@@ -1,11 +1,12 @@
-[Tip 1]
-text=_Some buildings, such as the farm, have a very small work area. If the building is surrounded with trees or roads, production will stop.
-sec=7
-
-[Tip 2]
-text=_Keep an eye on your fishers. After fish resources are exhausted in their work area, they will stop fishing.
-sec=4
-
-[Tip 3]
-text=_"Always build a ranger’s and a gamekeeper’s hut, so you will not run out of logs or food."
-sec=5
+set_textdomain("texts")
+tips = {
+   {
+      text = _"Keep an eye on your fishers. After fish resources are exhausted in their work area, they will stop fishing.",
+      seconds = 4
+   },
+   {
+      text = _"Always build a ranger’s and a gamekeeper’s hut, so you will not run out of logs.",
+      seconds = 5
+   },
+}
+return tips

=== renamed file 'data/txts/tips/editor.tip' => 'data/txts/tips/editor.lua'
--- data/txts/tips/editor.tip	2016-10-01 07:45:42 +0000
+++ data/txts/tips/editor.lua	2016-12-10 16:14:18 +0000
@@ -1,28 +1,32 @@
-[Tip 1]
-text=_"You can find general help for the editor as well as a short tutorial on our wiki at www.widelands.org"
-sec=6
-
-[Tip 2]
-text=_"You can select multiple animal types if you hold down Ctrl during selection. If you then click on the map, an instance of one of the selected animal types will be placed."
-sec=7
-
-[Tip 3]
-text=_"Just like with animals, you can also select multiple immovables or ground textures with Ctrl + mouse click."
-sec=6
-
-[Tip 4]
-text=_"Holding down Shift switches the first alternative tool on. This tool is in most cases the complete opposite of the normal tool (for example deletion of an object instead of placing one)."
-sec=7
-
-[Tip 5]
-text=_"Holding down Alt switches the second alternative tool on (if it exists)."
-sec=6
-
-[Tip 6]
-text=_"The number keys (0 - 9) allow you to directly set the size of the selected area around your mouse cursor."
-sec=6
-
-[Tip 7]
-text=_"Space key behaves like in normal game. It switches if building spaces are shown on or off."
-sec=6
-
+set_textdomain("texts")
+tips = {
+   {
+      text = _"You can find general help for the editor as well as a short tutorial on our wiki at www.widelands.org",
+      seconds = 6
+   },
+   {
+      text = _"You can select multiple animal types if you hold down Ctrl during selection. If you then click on the map, an instance of one of the selected animal types will be placed.",
+      seconds = 7
+   },
+   {
+      text = _"Just like with animals, you can also select multiple immovables or ground textures with Ctrl + mouse click.",
+      seconds = 6
+   },
+   {
+      text = _"Holding down Shift switches the first alternative tool on. This tool is in most cases the complete opposite of the normal tool (for example deletion of an object instead of placing one).",
+      seconds = 7
+   },
+   {
+      text = _"Holding down Alt switches the second alternative tool on (if it exists).",
+      seconds = 6
+   },
+   {
+      text = _"The number keys (0 - 9) allow you to directly set the size of the selected area around your mouse cursor.",
+      seconds = 6
+   },
+   {
+      text = _"Space key behaves like in normal game. It switches if building spaces are shown on or off.",
+      seconds = 6
+   },
+}
+return tips

=== renamed file 'data/txts/tips/empire.tip' => 'data/txts/tips/empire.lua'
--- data/txts/tips/empire.tip	2015-11-07 08:28:27 +0000
+++ data/txts/tips/empire.lua	2016-12-10 16:14:18 +0000
@@ -1,11 +1,12 @@
-[Tip 1]
-text=_Some buildings, such as the farm, have a very small work area. If the building is surrounded with trees or roads, production will stop.
-sec=7
-
-[Tip 2]
-text=_Keep an eye on your fishers. After fish resources are exhausted in their work area, they will stop fishing.
-sec=4
-
-[Tip 3]
-text=_Always build a forester’s house so you will not run out of logs.
-sec=5
+set_textdomain("texts")
+tips = {
+   {
+      text = _"Keep an eye on your fishers. After fish resources are exhausted in their work area, they will stop fishing.",
+      seconds = 4
+   },
+   {
+      text = _"Always build a forester’s house so you will not run out of logs.",
+      seconds = 5
+   },
+}
+return tips

=== renamed file 'data/txts/tips/general_game.tip' => 'data/txts/tips/general_game.lua'
--- data/txts/tips/general_game.tip	2014-12-14 18:57:27 +0000
+++ data/txts/tips/general_game.lua	2016-12-10 16:14:18 +0000
@@ -1,63 +1,72 @@
-[Tip 1]
-text=_The building process will only start after a road is built to the construction site.
-sec=4
-
-[Tip 2]
-text=_Be sure to send a geologist to survey sites before building a well or a mine.
-sec=4
-
-[Tip 3]
-text=_Your territory can be expanded by building various military buildings – Sentry, Fortress, Blockhouse, etc.
-sec=5
-
-[Tip 4]
-text=_Miners will work only if supplied with food.
-sec=3
-
-[Tip 5]
-text=_Split your roads into shorter sections by planting some flags. This will speed up transportation of your wares.
-sec=6
-
-[Tip 6]
-text=_Any further questions? Our FAQ, the wiki and of course our community forums can be found at www.widelands.org.
-sec=6
-
-[Tip 7]
-text=_Use the priority buttons in building windows to adjust delivery of a specific ware.
-sec=5
-
-[Tip 8]
-text=_Use the ‘configure economy’ menu to adjust the target quantity of wares. It can be accessed via the flag menu.
-sec=7
-
-[Tip 9]
-text=_Manage your messages more efficiently: press ‘N’ for News to access the messages window, then use ‘G’ for Go To, and the Delete key to archive messages.
-sec=6
-
-[Tip 10]
-text=_Use the ‘,’ (comma) and ‘.’ (period) keys to go back and forth between recently visited places in the world.
-sec=4
-
-[Tip 11]
-text=_Press Ctrl+(0-9) to store important locations, then press 0-9 to recall them.
-sec=4
-
-[Tip 12]
-text=_Removing a road while holding Ctrl lets you remove all flags up to the first junction.
-sec=4
-
-[Tip 13]
-text=_New soldiers are recruited in the headquarters or a warehouse if all needed tools are available. Training sites are for improving their abilities.
-sec=6
-
-[Tip 14]
-text=_If you like to live dangerously, pressing Ctrl while clicking the enhance, dismantle or destroy building buttons will skip the confirmation.
-sec=6
-
-[Tip 15]
-text=_Press I to quickly toggle the display of your stock inventory.
-sec=4
-
-[Tip 16]
-text=_An economy consists of all buildings and flags connected by roads. The supplies and demands of buildings are handled in its economy.
-sec=8
+set_textdomain("texts")
+tips = {
+   {
+      text = _"The building process will only start after a road is built to the construction site.",
+      seconds = 4
+   },
+   {
+      text = _"Be sure to send a geologist to survey sites before building a well or a mine.",
+      seconds = 4
+   },
+   {
+      text = _"Your territory can be expanded by building various military buildings – Sentry, Fortress, Blockhouse, etc.",
+      seconds = 5
+   },
+   {
+      text = _"Miners will work only if supplied with food.",
+      seconds = 3
+   },
+   {
+      text = _"Split your roads into shorter sections by planting some flags. This will speed up transportation of your wares.",
+      seconds = 6
+   },
+   {
+      text = _"Any further questions? Our FAQ, the wiki and of course our community forums can be found at www.widelands.org.",
+      seconds = 6
+   },
+   {
+      text = _"Use the priority buttons in building windows to adjust delivery of a specific ware.",
+      seconds = 5
+   },
+   {
+      text = _"Use the ‘configure economy’ menu to adjust the target quantity of wares. It can be accessed via the flag menu.",
+      seconds = 7
+   },
+   {
+      text = _"Manage your messages more efficiently: press ‘N’ for News to access the messages window, then use ‘G’ for Go To, and the Delete key to archive messages.",
+      seconds = 6
+   },
+   {
+      text = _"Use the ‘,’ (comma) and ‘.’ (period) keys to go back and forth between recently visited places in the world.",
+      seconds = 4
+   },
+   {
+      text = _"Press Ctrl+(0-9) to store important locations, then press 0-9 to recall them.",
+      seconds = 4
+   },
+   {
+      text = _"Removing a road while holding Ctrl lets you remove all flags up to the first junction.",
+      seconds = 4
+   },
+   {
+      text = _"New soldiers are recruited in the headquarters or a warehouse if all needed tools are available. Training sites are for improving their abilities.",
+      seconds = 6
+   },
+   {
+      text = _"If you like to live dangerously, pressing Ctrl while clicking the enhance, dismantle or destroy building buttons will skip the confirmation.",
+      seconds = 6
+   },
+   {
+      text = _"Press I to quickly toggle the display of your stock inventory.",
+      seconds = 4
+   },
+   {
+      text = _"An economy consists of all buildings and flags connected by roads. The supplies and demands of buildings are handled in its economy.",
+      seconds = 8
+   },
+   {
+      text = _"Some buildings, such as the farm, have a very small work area. If the building is surrounded with trees or roads, production will stop.",
+      seconds = 7
+   },
+}
+return tips

=== renamed file 'data/txts/tips/multiplayer.tip' => 'data/txts/tips/multiplayer.lua'
--- data/txts/tips/multiplayer.tip	2014-02-25 14:42:19 +0000
+++ data/txts/tips/multiplayer.lua	2016-12-10 16:14:18 +0000
@@ -1,11 +1,16 @@
-[Tip 1]
-text=_"You can use the ’Page up’ and ‘Page down’ keys to adjust your desired speed – the real speed is then democratically set."
-sec=5
-
-[Tip 2]
-text=_"You can use ‘/me’ at the beginning of a chatmessage: ‘/me is tired’ will be printed as ‘-> Nickname is tired’."
-sec=5
-
-[Tip 3]
-text=_"Use ‘@name’ at the beginning of a chat message to send it to only one player."
-sec=5
+set_textdomain("texts")
+tips = {
+   {
+      text = _"You can use the ’Page up’ and ‘Page down’ keys to adjust your desired speed – the real speed is then democratically set.",
+      seconds = 5
+   },
+   {
+      text = _"You can use ‘/me’ at the beginning of a chatmessage: ‘/me is tired’ will be printed as ‘-> Nickname is tired’.",
+      seconds = 5
+   },
+   {
+      text = _"Use ‘@name’ at the beginning of a chat message to send it to only one player.",
+      seconds = 5
+   },
+}
+return tips

=== renamed file 'data/txts/tips/singleplayer.tip' => 'data/txts/tips/singleplayer.lua'
--- data/txts/tips/singleplayer.tip	2009-04-15 12:49:22 +0000
+++ data/txts/tips/singleplayer.lua	2016-12-10 16:14:18 +0000
@@ -1,3 +1,8 @@
-[Tip 1]
-text=_Our computer players are too boring? Visit our website at www.widelands.org and meet other players to play online.
-sec=6
+set_textdomain("texts")
+tips = {
+   {
+      text = _"Our computer players are too boring? Visit our website at www.widelands.org and meet other players to play online.",
+      seconds = 6
+   },
+}
+return tips

=== modified file 'src/scripting/lua_globals.cc'
--- src/scripting/lua_globals.cc	2016-08-06 12:39:38 +0000
+++ src/scripting/lua_globals.cc	2016-12-10 16:14:18 +0000
@@ -239,7 +239,8 @@
 		lua_getfield(L, LUA_REGISTRYINDEX, "lua_interface");
 		LuaInterface* lua = static_cast<LuaInterface*>(lua_touserdata(L, -1));
 		lua_pop(L, 1);  // pop this userdata
-		lua->run_script(script);
+		std::unique_ptr<LuaTable> table(lua->run_script(script));
+		table->do_not_warn_about_unaccessed_keys();
 	} catch (std::exception& e) {
 		report_error(L, "%s", e.what());
 	}

=== modified file 'src/wui/game_tips.cc'
--- src/wui/game_tips.cc	2016-10-24 14:07:28 +0000
+++ src/wui/game_tips.cc	2016-12-10 16:14:18 +0000
@@ -25,9 +25,9 @@
 #include "graphic/rendertarget.h"
 #include "graphic/text_layout.h"
 #include "io/fileread.h"
-#include "profile/profile.h"
+#include "scripting/lua_interface.h"
+#include "scripting/lua_table.h"
 
-#define DEFAULT_INTERVAL 5  // seconds
 #define BG_IMAGE "images/loadscreens/tips_bg.png"
 
 GameTips::GameTips(UI::ProgressWindow& progressWindow, const std::vector<std::string>& names)
@@ -36,7 +36,7 @@
      progressWindow_(progressWindow),
      registered_(false),
      lastTip_(0) {
-	// Loading texts-locals, for translating the tips
+	// Loading the "texts" locale for translating the tips
 	i18n::Textdomain textdomain("texts");
 
 	for (uint8_t i = 0; i < names.size(); ++i)
@@ -54,24 +54,22 @@
 	stop();
 }
 
-/// Loads tips out of \var filename
+/// Loads tips out of \var name
 void GameTips::load_tips(std::string name) {
-	std::string filename = "txts/tips/" + name + ".tip";
 	try {
-		Profile prof(filename.c_str());
-		while (Section* const s = prof.get_next_section(nullptr)) {
-			char const* const text = s->get_string("text");
-			if (nullptr == text)
-				continue;
-
+		LuaInterface lua;
+		std::unique_ptr<LuaTable> table(lua.run_script("txts/tips/" + name + ".lua"));
+		std::unique_ptr<LuaTable> tip_table;
+		for (const int key : table->keys<int>()) {
+			tip_table = table->get_table(key);
 			Tip tip;
-			tip.text = text;
-			tip.interval = s->get_int("sec", DEFAULT_INTERVAL);
+			tip.text = tip_table->get_string("text");
+			tip.interval = tip_table->get_int("seconds");
 			tips_.push_back(tip);
 		}
-	} catch (std::exception&) {
-		// just ignore - tips do not impact game
-		return;
+	} catch (LuaError& err) {
+		log("Error loading tips script for %s:\n%s\n", name.c_str(), err.what());
+		// No further handling necessary - tips do not impact game
 	}
 }
 

=== modified file 'src/wui/game_tips.h'
--- src/wui/game_tips.h	2016-08-04 15:49:05 +0000
+++ src/wui/game_tips.h	2016-12-10 16:14:18 +0000
@@ -39,7 +39,7 @@
 		std::string text;
 		int32_t interval;
 	};
-	void load_tips(std::string);
+	void load_tips(std::string name);
 	void show_tip(int32_t index);
 
 	uint32_t lastUpdated_;

=== modified file 'src/wui/tribal_encyclopedia.cc'
--- src/wui/tribal_encyclopedia.cc	2016-08-04 15:49:05 +0000
+++ src/wui/tribal_encyclopedia.cc	2016-12-10 16:14:18 +0000
@@ -24,6 +24,8 @@
 #include <boost/format.hpp>
 
 #include "base/i18n.h"
+#include "logic/game_controller.h"
+#include "logic/game_controller.h"
 #include "logic/map_objects/tribes/tribe_descr.h"
 #include "logic/player.h"
 #include "scripting/lua_coroutine.h"
@@ -41,6 +43,12 @@
 		std::unique_ptr<LuaTable> table(lua_->run_script("tribes/scripting/help/init.lua"));
 		std::unique_ptr<LuaCoroutine> cr(table->get_coroutine("func"));
 		cr->push_arg(tribe.name());
+		upcast(Widelands::Game, game, &parent.egbase());
+		if (game->game_controller()->get_game_type() == GameController::GameType::SINGLEPLAYER) {
+			cr->push_arg("singleplayer");
+		} else {
+			cr->push_arg("multiplayer");
+		}
 		cr->resume();
 		init(parent, cr->pop_table());
 	} catch (LuaError& err) {

_______________________________________________
Mailing list: https://launchpad.net/~widelands-dev
Post to     : widelands-dev@lists.launchpad.net
Unsubscribe : https://launchpad.net/~widelands-dev
More help   : https://help.launchpad.net/ListHelp

Reply via email to