From aee2b0c50a38a1bfdd4fdc3d24a38b038fbc21d3 Mon Sep 17 00:00:00 2001 From: tron Date: Sun, 6 Mar 2005 12:54:19 +0000 Subject: [PATCH] (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ... --- Makefile | 44 ++++++++++------------------------ train_cmd.c | 69 ++++++++++++++++++++++++++--------------------------- 2 files changed, 46 insertions(+), 67 deletions(-) diff --git a/Makefile b/Makefile index 46af1ccbab..892c28369e 100644 --- a/Makefile +++ b/Makefile @@ -687,39 +687,14 @@ LANGS = $(LANG_TXT:%.txt=%.lng) # # If we are verbose, we will show commands prefixed by $(Q) (which acts as -# @ in the non-verbose mode), and we will show the "real" cmds instead of -# their quiet versions (which are used in the non-verbose mode). +# @ in the non-verbose mode) # Inspired by the Linux kernel build system. ifdef VERBOSE Q = - quiet = else Q = @ - quiet = quiet_ endif -# Show the command (quiet or non-quiet version based on the assignment -# just above) and then execute it. -cmd = @$(if $($(quiet)cmd_$(1)),echo $($(quiet)cmd_$(1)) &&) $(cmd_$(1)) - - -# The build commands themselves. Note that if you omit the quiet version, -# nothing will be shown in the non-verbose mode. - -quiet_cmd_compile_link = '===> Compiling and Linking $@' - cmd_compile_link = $(CC) $(BASECFLAGS) $(CDEFS) $< -o $@ - -quiet_cmd_ttd_link = '===> Linking $@' - cmd_ttd_link = $(CC) $(LDFLAGS) $(TTDLDFLAGS) $(OBJS) $(LIBS) -o $@ - -COMPILE_PARAMS=$(CFLAGS) $(CDEFS) -MD -c $< -o $@ - -quiet_cmd_c_compile = '===> Compiling $<' - cmd_c_compile = $(CC) $(COMPILE_PARAMS) - -quiet_cmd_cxx_compile = '===> Compiling $<' - cmd_cxx_compile = $(CXX) $(COMPILE_PARAMS) - ############################################################################## # @@ -742,11 +717,13 @@ endian.h: $(ENDIAN_CHECK) $(Q)./$(ENDIAN_CHECK) > $@ $(ENDIAN_CHECK): endian_check.c - $(call cmd,compile_link) + @echo '===> Compiling and Linking $@' + $(Q)$(CC) $(BASECFLAGS) $(CDEFS) $< -o $@ $(TTD): table/strings.h $(OBJS) $(MAKE_CONFIG) - $(call cmd,ttd_link) + @echo '===> Linking $@' + $(Q)$(CC) $(LDFLAGS) $(TTDLDFLAGS) $(OBJS) $(LIBS) -o $@ $(OSX): $(TTD) $(Q)rm -fr "$(OSXAPP)" @@ -772,7 +749,8 @@ $(64_bit_warnings): $(warning If you see any bugs, include in your bug report that you use a 64 bit CPU) $(STRGEN): strgen/strgen.c endian.h - $(call cmd,compile_link) + @echo '===> Compiling and Linking $@' + $(Q)$(CC) $(BASECFLAGS) $(CDEFS) $< -o $@ table/strings.h: lang/english.txt $(STRGEN) @echo '===> Generating $@' @@ -957,11 +935,13 @@ DEPS_MAGIC := $(shell mkdir .deps > /dev/null 2>&1 || :) # therefore we do not need to watch deps. %.o: %.c $(MAKE_CONFIG) endian.h table/strings.h - $(call cmd,c_compile) + @echo '===> Compiling $<' + $(Q)$(CC) $(CFLAGS) $(CDEFS) -MD -c $< -o $@ @mv $(<:%.c=%.d) $(<:%.c=.deps/%.d) %.o: %.cpp $(MAKE_CONFIG) endian.h table/strings.h - $(call cmd,cxx_compile) + @echo '===> Compiling $<' + $(Q)$(CXX) $(CFLAGS) $(CDEFS) -MD -c $< -o $@ @mv $(<:%.c=%.d) $(<:%.c=.deps/%.d) # Silence stale header dependencies @@ -970,6 +950,6 @@ DEPS_MAGIC := $(shell mkdir .deps > /dev/null 2>&1 || :) info: - @echo 'CFLAGS = $(CFLAGS)' + @echo 'CFLAGS = $(CFLAGS) $(CDEFS)' @echo 'LDFLAGS = $(LDFLAGS)' @echo 'LIBS = $(LIBS)' diff --git a/train_cmd.c b/train_cmd.c index 94327919fc..6a3947a3c9 100644 --- a/train_cmd.c +++ b/train_cmd.c @@ -2270,20 +2270,25 @@ static void TrainMovedChangeSignals(uint tile, int dir) typedef struct TrainCollideChecker { - Vehicle *v, *v_skip; - + const Vehicle *v; + const Vehicle *v_skip; } TrainCollideChecker; -static void *FindTrainCollideEnum(Vehicle *v, TrainCollideChecker *tcc) +static void *FindTrainCollideEnum(Vehicle *v, void *data) { - if (v == tcc->v || v == tcc->v_skip || v->type != VEH_Train || v->u.rail.track==0x80) - return 0; + const TrainCollideChecker* tcc = data; - if ( myabs(v->z_pos - tcc->v->z_pos) > 6 || - myabs(v->x_pos - tcc->v->x_pos) >= 6 || - myabs(v->y_pos - tcc->v->y_pos) >= 6) - return NULL; - return v; + if (v != tcc->v && + v != tcc->v_skip && + v->type == VEH_Train && + v->u.rail.track != 0x80 && + myabs(v->z_pos - tcc->v->z_pos) <= 6 && + myabs(v->x_pos - tcc->v->x_pos) < 6 && + myabs(v->y_pos - tcc->v->y_pos) < 6) { + return v; + } else { + return NULL; + } } static void SetVehicleCrashed(Vehicle *v) @@ -2303,7 +2308,7 @@ static void SetVehicleCrashed(Vehicle *v) InvalidateWindowWidget(WC_VEHICLE_VIEW, u->index, STATUS_BAR); } -static int CountPassengersInTrain(Vehicle *v) +static int CountPassengersInTrain(const Vehicle *v) { int num = 0; BEGIN_ENUM_WAGONS(v) @@ -2313,7 +2318,7 @@ static int CountPassengersInTrain(Vehicle *v) } /* - * Checks whether the specified tried has a collision with another vehicle. If + * Checks whether the specified train has a collision with another vehicle. If * so, destroys this vehicle, and the other vehicle if its subtype is 0 (TS_Front_Engine). * Reports the incident in a flashy news item, modifies station ratings and * plays a sound. @@ -2321,34 +2326,35 @@ static int CountPassengersInTrain(Vehicle *v) static void CheckTrainCollision(Vehicle *v) { TrainCollideChecker tcc; - Vehicle *coll,*realcoll; + Vehicle *coll; + Vehicle *realcoll; int num; /* can't collide in depot */ if (v->u.rail.track == 0x80) return; - if ( !(v->u.rail.track == 0x40) ) - assert((uint)TILE_FROM_XY(v->x_pos, v->y_pos) == v->tile); + assert(v->u.rail.track == 0x40 || TILE_FROM_XY(v->x_pos, v->y_pos) == v->tile); tcc.v = v; tcc.v_skip = v->next; /* find colliding vehicle */ - realcoll = coll = VehicleFromPos(TILE_FROM_XY(v->x_pos, v->y_pos), &tcc, (VehicleFromPosProc*)FindTrainCollideEnum); - if (coll == NULL) + realcoll = VehicleFromPos(TILE_FROM_XY(v->x_pos, v->y_pos), &tcc, FindTrainCollideEnum); + if (realcoll == NULL) return; - coll = GetFirstVehicleInChain(coll); + coll = GetFirstVehicleInChain(realcoll); /* it can't collide with its own wagons */ - if ( (v == coll) || ( (v->u.rail.track & 0x40) && ( (v->direction & 2) != (realcoll->direction & 2) ) ) ) + if (v == coll || + (v->u.rail.track & 0x40 && (v->direction & 2) != (realcoll->direction & 2))) return; //two drivers + passangers killed in train v num = 2 + CountPassengersInTrain(v); - if(!(coll->vehstatus&VS_CRASHED)) + if (!(coll->vehstatus & VS_CRASHED)) //two drivers + passangers killed in train coll (if it was not crashed already) num += 2 + CountPassengersInTrain(coll); @@ -2360,9 +2366,10 @@ static void CheckTrainCollision(Vehicle *v) SetDParam(0, num); AddNewsItem(STR_8868_TRAIN_CRASH_DIE_IN_FIREBALL, - NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ACCIDENT, 0), + NEWS_FLAGS(NM_THIN, NF_VIEWPORT | NF_VEHICLE, NT_ACCIDENT, 0), v->index, - 0); + 0 + ); ModifyStationRatingAround(v->tile, v->owner, -160, 30); SndPlayVehicleFx(SND_13_BIG_CRASH, v); @@ -2398,7 +2405,7 @@ static void TrainController(Vehicle *v) byte old_z; /* For every vehicle after and including the given vehicle */ - for(;;) { + for (; v != NULL; prev = v, v = v->next) { BeginVehicleMove(v); if (v->u.rail.track != 0x40) { @@ -2536,7 +2543,7 @@ static void TrainController(Vehicle *v) VehiclePositionChanged(v); if (prev == NULL) CheckTrainCollision(v); - goto next_vehicle; + continue; } common:; @@ -2556,12 +2563,6 @@ common:; AffectSpeedByZChange(v, old_z); CheckTrainCollision(v); } - -next_vehicle:; - /* continue with next vehicle */ - prev = v; - if ((v=v->next) == NULL) - return; } invalid_rail: @@ -2625,10 +2626,7 @@ static void DeleteLastWagon(Vehicle *v) /* Go to the last wagon and delete the link pointing there * *u is then the one-before-last wagon, and *v the last * one which will physicially be removed */ - while (v->next != NULL) { - u = v; - v = v->next; - } + for (; v->next != NULL; v = v->next) u = v; u->next = NULL; InvalidateWindow(WC_VEHICLE_DETAILS, v->index); @@ -2643,7 +2641,8 @@ static void DeleteLastWagon(Vehicle *v) if (!(v->u.rail.track & 0xC0)) SetSignalsOnBothDir(v->tile, FIND_FIRST_BIT(v->u.rail.track)); - /* Check if the wagon was on a road/rail-crossing and disable it if no others are on it */ + /* Check if the wagon was on a road/rail-crossing and disable it if no + * others are on it */ DisableTrainCrossing(v->tile); if (v->u.rail.track == 0x40) { // inside a tunnel