mirror of https://github.com/OpenTTD/OpenTTD.git
(svn r2105) -Codechange: Added a cache for the first vehicle of a chain to increase performance, especially with many long trains
This commit is contained in:
parent
3a8665f796
commit
c68cfdeeca
11
train_cmd.c
11
train_cmd.c
|
@ -736,6 +736,14 @@ int32 CmdMoveRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
||||||
dst_head = NULL;
|
dst_head = NULL;
|
||||||
if (dst != NULL) dst_head = GetFirstVehicleInChain(dst);
|
if (dst != NULL) dst_head = GetFirstVehicleInChain(dst);
|
||||||
|
|
||||||
|
/* clear the ->first cache */
|
||||||
|
{
|
||||||
|
Vehicle *u;
|
||||||
|
|
||||||
|
for (u = src_head; u != NULL; u = u->next) u->first = NULL;
|
||||||
|
for (u = dst_head; u != NULL; u = u->next) u->first = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
/* check if all vehicles in the source train are stopped */
|
/* check if all vehicles in the source train are stopped */
|
||||||
if (CheckTrainStoppedInDepot(src_head) < 0)
|
if (CheckTrainStoppedInDepot(src_head) < 0)
|
||||||
return CMD_ERROR;
|
return CMD_ERROR;
|
||||||
|
@ -939,6 +947,9 @@ int32 CmdSellRailWagon(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
||||||
|
|
||||||
// when selling an attached locomotive. we need to delete its window.
|
// when selling an attached locomotive. we need to delete its window.
|
||||||
if (v->subtype == TS_Front_Engine) {
|
if (v->subtype == TS_Front_Engine) {
|
||||||
|
Vehicle *u;
|
||||||
|
|
||||||
|
for (u = v; u != NULL; u = u->next) u->first = NULL;
|
||||||
DeleteWindowById(WC_VEHICLE_VIEW, v->index);
|
DeleteWindowById(WC_VEHICLE_VIEW, v->index);
|
||||||
|
|
||||||
// rearrange all vehicles that follow to separate lines.
|
// rearrange all vehicles that follow to separate lines.
|
||||||
|
|
16
vehicle.c
16
vehicle.c
|
@ -14,6 +14,7 @@
|
||||||
#include "player.h"
|
#include "player.h"
|
||||||
#include "engine.h"
|
#include "engine.h"
|
||||||
#include "sound.h"
|
#include "sound.h"
|
||||||
|
#include "debug.h"
|
||||||
|
|
||||||
#define INVALID_COORD (-0x8000)
|
#define INVALID_COORD (-0x8000)
|
||||||
#define GEN_HASH(x,y) (((x & 0x1F80)>>7) + ((y & 0xFC0)))
|
#define GEN_HASH(x,y) (((x & 0x1F80)>>7) + ((y & 0xFC0)))
|
||||||
|
@ -165,6 +166,7 @@ void AfterLoadVehicles(void)
|
||||||
Vehicle *v;
|
Vehicle *v;
|
||||||
|
|
||||||
FOR_ALL_VEHICLES(v) {
|
FOR_ALL_VEHICLES(v) {
|
||||||
|
v->first = NULL;
|
||||||
if (v->type != 0) {
|
if (v->type != 0) {
|
||||||
v->left_coord = INVALID_COORD;
|
v->left_coord = INVALID_COORD;
|
||||||
VehiclePositionChanged(v);
|
VehiclePositionChanged(v);
|
||||||
|
@ -186,6 +188,7 @@ static Vehicle *InitializeVehicle(Vehicle *v)
|
||||||
assert(v->orders == NULL);
|
assert(v->orders == NULL);
|
||||||
|
|
||||||
v->left_coord = INVALID_COORD;
|
v->left_coord = INVALID_COORD;
|
||||||
|
v->first = NULL;
|
||||||
v->next = NULL;
|
v->next = NULL;
|
||||||
v->next_hash = 0xffff;
|
v->next_hash = 0xffff;
|
||||||
v->string_id = 0;
|
v->string_id = 0;
|
||||||
|
@ -352,10 +355,21 @@ Vehicle *GetPrevVehicleInChain(const Vehicle *v)
|
||||||
|
|
||||||
Vehicle *GetFirstVehicleInChain(const Vehicle *v)
|
Vehicle *GetFirstVehicleInChain(const Vehicle *v)
|
||||||
{
|
{
|
||||||
const Vehicle* u;
|
Vehicle* u;
|
||||||
|
|
||||||
|
if (v->first != NULL) {
|
||||||
|
if (v->first->subtype == TS_Front_Engine) {
|
||||||
|
return v->first;
|
||||||
|
} else {
|
||||||
|
DEBUG(misc, 0) ("v->first cache faulty. We shouldn't be here");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
while ((u = GetPrevVehicleInChain(v)) != NULL) v = u;
|
while ((u = GetPrevVehicleInChain(v)) != NULL) v = u;
|
||||||
|
|
||||||
|
if (v->subtype == TS_Front_Engine)
|
||||||
|
for (u = (Vehicle *)v; u != NULL; u = u->next) u->first = (Vehicle *)v;
|
||||||
|
|
||||||
return (Vehicle*)v;
|
return (Vehicle*)v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -97,6 +97,7 @@ struct Vehicle {
|
||||||
uint16 index; // NOSAVE: Index in vehicle array
|
uint16 index; // NOSAVE: Index in vehicle array
|
||||||
|
|
||||||
Vehicle *next; // next
|
Vehicle *next; // next
|
||||||
|
Vehicle *first; // NOSAVE: pointer to the first vehicle in the chain
|
||||||
|
|
||||||
StringID string_id; // Displayed string
|
StringID string_id; // Displayed string
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue