changeset 18763:10757b027360 draft

(svn r23611) -Add: run the begin of the script already while generating, and don't sleep on DoCommand while doing so
author truebrain <truebrain@openttd.org>
date Mon, 19 Dec 2011 20:56:50 +0000
parents c426221fa1a3
children bd2a1a20a792
files src/command.cpp src/genworld.cpp src/genworld.h src/genworld_gui.cpp src/lang/english.txt src/script/api/script_object.cpp src/script/script_instance.hpp
diffstat 7 files changed, 30 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/command.cpp
+++ b/src/command.cpp
@@ -658,7 +658,7 @@
 	 * If we are in network, and the command is not from the network
 	 * send it to the command-queue and abort execution
 	 */
-	if (_networking && !(cmd & CMD_NETWORK_COMMAND)) {
+	if (_networking && !_generating_world && !(cmd & CMD_NETWORK_COMMAND)) {
 		NetworkSendCommand(tile, p1, p2, cmd & ~CMD_FLAGS_MASK, callback, text, _current_company);
 		cur_company.Restore();
 
--- a/src/genworld.cpp
+++ b/src/genworld.cpp
@@ -34,6 +34,7 @@
 #include "progress.h"
 #include "error.h"
 #include "game/game.hpp"
+#include "game/game_instance.hpp"
 
 #include "table/sprites.h"
 
@@ -166,6 +167,17 @@
 
 			if (_game_mode != GM_EDITOR) {
 				Game::StartNew();
+
+				if (Game::GetInstance() != NULL) {
+					SetGeneratingWorldProgress(GWP_RUNSCRIPT, 2500);
+					_generating_world = true;
+					for (i = 0; i < 2500; i++) {
+						Game::GameLoop();
+						IncreaseGeneratingWorldProgress(GWP_RUNSCRIPT);
+						if (Game::GetInstance()->IsSleeping()) break;
+					}
+					_generating_world = false;
+				}
 			}
 		}
 
--- a/src/genworld.h
+++ b/src/genworld.h
@@ -65,6 +65,7 @@
 	GWP_TREE,        ///< Generate trees
 	GWP_GAME_INIT,   ///< Initialize the game
 	GWP_RUNTILELOOP, ///< Runs the tile loop 1280 times to make snow etc
+	GWP_RUNSCRIPT,   ///< Runs the game script at most 2500 times, or when ever the script sleeps
 	GWP_GAME_START,  ///< Really prepare to start the game
 	GWP_CLASS_COUNT
 };
--- a/src/genworld_gui.cpp
+++ b/src/genworld_gui.cpp
@@ -1169,6 +1169,7 @@
 	STR_GENERATION_TREE_GENERATION,
 	STR_GENERATION_SETTINGUP_GAME,
 	STR_GENERATION_PREPARING_TILELOOP,
+	STR_GENERATION_PREPARING_SCRIPT,
 	STR_GENERATION_PREPARING_GAME
 };
 assert_compile(lengthof(_generation_class_table) == GWP_CLASS_COUNT);
@@ -1272,7 +1273,7 @@
 
 static void _SetGeneratingWorldProgress(GenWorldProgress cls, uint progress, uint total)
 {
-	static const int percent_table[] = {0, 5, 14, 17, 20, 40, 60, 65, 80, 85, 99, 100 };
+	static const int percent_table[] = {0, 5, 14, 17, 20, 40, 60, 65, 80, 85, 95, 99, 100 };
 	assert_compile(lengthof(percent_table) == GWP_CLASS_COUNT + 1);
 	assert(cls < GWP_CLASS_COUNT);
 
--- a/src/lang/english.txt
+++ b/src/lang/english.txt
@@ -2413,6 +2413,7 @@
 STR_GENERATION_CLEARING_TILES                                   :{BLACK}Rough and rocky area generation
 STR_GENERATION_SETTINGUP_GAME                                   :{BLACK}Setting up game
 STR_GENERATION_PREPARING_TILELOOP                               :{BLACK}Running tile-loop
+STR_GENERATION_PREPARING_SCRIPT                                 :{BLACK}Running script
 STR_GENERATION_PREPARING_GAME                                   :{BLACK}Preparing game
 
 # NewGRF settings
--- a/src/script/api/script_object.cpp
+++ b/src/script/api/script_object.cpp
@@ -15,6 +15,7 @@
 #include "../../company_func.h"
 #include "../../network/network.h"
 #include "../../tunnelbridge.h"
+#include "../../genworld.h"
 
 #include "../script_storage.hpp"
 #include "../script_instance.hpp"
@@ -233,7 +234,7 @@
 #endif
 
 	/* Try to perform the command. */
-	CommandCost res = ::DoCommandPInternal(tile, p1, p2, cmd, _networking ? ScriptObject::GetActiveInstance()->GetDoCommandCallback() : NULL, text, false, estimate_only);
+	CommandCost res = ::DoCommandPInternal(tile, p1, p2, cmd, (_networking && !_generating_world) ? ScriptObject::GetActiveInstance()->GetDoCommandCallback() : NULL, text, false, estimate_only);
 
 	/* We failed; set the error and bail out */
 	if (res.Failed()) {
@@ -254,7 +255,11 @@
 	SetLastCost(res.GetCost());
 	SetLastCommandRes(true);
 
-	if (_networking) {
+	if (_generating_world) {
+		IncreaseDoCommandCosts(res.GetCost());
+		if (callback != NULL) callback(GetActiveInstance());
+		return true;
+	} else if (_networking) {
 		/* Suspend the AI till the command is really executed. */
 		throw Script_Suspend(-(int)GetDoCommandDelay(), callback);
 	} else {
--- a/src/script/script_instance.hpp
+++ b/src/script/script_instance.hpp
@@ -159,6 +159,12 @@
 	 */
 	void InsertEvent(class ScriptEvent *event);
 
+	/**
+	 * Check if the instance is sleeping, which either happened because the
+	 *  script executed a DoCommand, or executed this.Sleep().
+	 */
+	bool IsSleeping() { return this->suspend != 0; }
+
 protected:
 	class Squirrel *engine;               ///< A wrapper around the squirrel vm.