From 803e452fab7726cf5959eeb36b4a4030cc061e02 Mon Sep 17 00:00:00 2001 From: rubidium Date: Fri, 18 Sep 2009 07:00:35 +0000 Subject: [PATCH] (svn r17564) [0.7] -Backport from trunk: - Fix: Vehicles waiting for their time table did not load anymore after their initial load was completed [FS#3201] (r17551) - Fix: Aircraft were given an unfair advantage in station rating calculations (r17550) - Fix: [NewGRF] Sign extending of profit calculation did not work (r17546) - Fix: [NoAI] AIs had 'infinite' time when running code from the global scope [FS#3202] (r17545) - Fix: [NoAI] Crash when doing commands in the 'global' scope [FS#3202] (r17544) --- src/ai/ai_instance.cpp | 36 +++++++++++++++++++++++------------- src/economy.cpp | 14 +++++++------- src/script/squirrel.cpp | 2 +- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/ai/ai_instance.cpp b/src/ai/ai_instance.cpp index fdf3809f72..cdd12a9256 100644 --- a/src/ai/ai_instance.cpp +++ b/src/ai/ai_instance.cpp @@ -122,21 +122,31 @@ AIInstance::AIInstance(AIInfo *info) : /* Register the API functions and classes */ this->RegisterAPI(); - /* Load and execute the script for this AI */ - const char *main_script = info->GetMainScript(); - if (strcmp(main_script, "%_dummy") == 0) { - extern void AI_CreateAIDummy(HSQUIRRELVM vm); - AI_CreateAIDummy(this->engine->GetVM()); - } else if (!this->engine->LoadScript(main_script)) { - this->Died(); - return; - } + try { + AIObject::SetAllowDoCommand(false); + /* Load and execute the script for this AI */ + const char *main_script = info->GetMainScript(); + if (strcmp(main_script, "%_dummy") == 0) { + extern void AI_CreateAIDummy(HSQUIRRELVM vm); + AI_CreateAIDummy(this->engine->GetVM()); + } else if (!this->engine->LoadScript(main_script) || this->engine->IsSuspended()) { + if (this->engine->IsSuspended()) AILog::Error("This AI took too long to load script. AI is not started."); + this->Died(); + return; + } - /* Create the main-class */ - this->instance = MallocT(1); - if (!this->engine->CreateClassInstance(info->GetInstanceName(), this->controller, this->instance)) { + /* Create the main-class */ + this->instance = MallocT(1); + if (!this->engine->CreateClassInstance(info->GetInstanceName(), this->controller, this->instance)) { + this->Died(); + return; + } + AIObject::SetAllowDoCommand(true); + } catch (AI_FatalError e) { + this->is_dead = true; + this->engine->ThrowError(e.GetErrorMessage()); + this->engine->ResumeError(); this->Died(); - return; } } diff --git a/src/economy.cpp b/src/economy.cpp index c30c25d2f6..742e02b474 100644 --- a/src/economy.cpp +++ b/src/economy.cpp @@ -1153,7 +1153,7 @@ Money GetTransportedGoodsIncome(uint num_pieces, uint dist, byte transit_days, C int result = GB(callback, 0, 14); /* Simulate a 15 bit signed value */ - if (HasBit(callback, 14)) result = 0x4000 - result; + if (HasBit(callback, 14)) result -= 0x4000; /* "The result should be a signed multiplier that gets multiplied * by the amount of cargo moved and the price factor, then gets @@ -1563,9 +1563,6 @@ static void LoadUnloadVehicle(Vehicle *v, int *cargo_left) { assert(v->current_order.IsType(OT_LOADING)); - /* When we've finished loading we're just staying here till the timetable 'runs' out */ - if (HasBit(v->vehicle_flags, VF_LOADING_FINISHED)) return; - assert(v->load_unload_time_rem != 0); /* We have not waited enough time till the next round of loading/unloading */ @@ -1587,6 +1584,7 @@ static void LoadUnloadVehicle(Vehicle *v, int *cargo_left) /* The train reversed in the station. Take the "easy" way * out and let the train just leave as it always did. */ SetBit(v->vehicle_flags, VF_LOADING_FINISHED); + v->load_unload_time_rem = 1; return; } @@ -1680,9 +1678,11 @@ static void LoadUnloadVehicle(Vehicle *v, int *cargo_left) /* update stats */ int t; switch (u->type) { - case VEH_TRAIN: t = u->u.rail.cached_max_speed; break; - case VEH_ROAD: t = u->max_speed / 2; break; - default: t = u->max_speed; break; + case VEH_TRAIN: t = u->u.rail.cached_max_speed; break; + case VEH_ROAD: t = u->max_speed / 2; break; + case VEH_SHIP: t = u->max_speed; break; + case VEH_AIRCRAFT: t = u->max_speed * 10 / 129; break; // convert to old units + default: NOT_REACHED(); } /* if last speed is 0, we treat that as if no vehicle has ever visited the station. */ diff --git a/src/script/squirrel.cpp b/src/script/squirrel.cpp index 410943b545..c165a1b987 100644 --- a/src/script/squirrel.cpp +++ b/src/script/squirrel.cpp @@ -466,7 +466,7 @@ static SQInteger _io_file_read(SQUserPointer file, SQUserPointer buf, SQInteger /* Load and run the script */ if (SQ_SUCCEEDED(LoadFile(vm, script, SQTrue))) { sq_push(vm, -2); - if (SQ_SUCCEEDED(sq_call(vm, 1, SQFalse, SQTrue))) { + if (SQ_SUCCEEDED(sq_call(vm, 1, SQFalse, SQTrue, 100000))) { sq_pop(vm, 1); return true; }