newgrf.c
changeset 1477 2174a73b11c9
parent 1439 c4b08bda7a43
child 1542 2ca6d1624e6d
--- a/newgrf.c	Wed Mar 09 23:59:18 2005 +0000
+++ b/newgrf.c	Thu Mar 10 07:01:43 2005 +0000
@@ -26,7 +26,8 @@
 extern int _replace_sprites_offset[16];
 extern int _traininfo_vehicle_pitch;
 
-struct GRFFile *_cur_grffile, *_first_grffile;
+GRFFile *_cur_grffile;
+GRFFile *_first_grffile;
 int _grffile_count;
 static int _cur_spriteid;
 static int _cur_stage;
@@ -39,7 +40,7 @@
 static uint32 _ttdpatch_flags[8];
 
 
-enum grfspec_feature {
+typedef enum grfspec_feature {
 	GSF_TRAIN,
 	GSF_ROAD,
 	GSF_SHIP,
@@ -47,7 +48,7 @@
 	GSF_STATION,
 	GSF_BRIDGE,
 	GSF_TOWNHOUSE,
-};
+} grfspec_feature;
 
 
 typedef void (*SpecialSpriteHandler)(byte *buf, int len);
@@ -83,22 +84,27 @@
  */
 
 
-enum grfmsg_severity {
+typedef enum grfmsg_severity {
 	GMS_NOTICE,
 	GMS_WARN,
 	GMS_ERROR,
 	GMS_FATAL,
-};
+} grfmsg_severity;
 
-static void CDECL grfmsg(enum grfmsg_severity severity, const char *str, ...)
+static void CDECL grfmsg(grfmsg_severity severity, const char *str, ...)
 {
-	static const char * const severitystr[4] = { "Notice", "Warning", "Error", "Fatal" };
+	static const char* const severitystr[] = {
+		"Notice",
+		"Warning",
+		"Error",
+		"Fatal"
+	};
 	int export_severity = 0;
 	char buf[1024];
 	va_list va;
 
 	va_start(va, str);
-	vsprintf(buf, str, va);
+	vsnprintf(buf, sizeof(buf), str, va);
 	va_end(va);
 
 	export_severity = 2 - (severity == GMS_FATAL ? 2 : severity);
@@ -144,25 +150,23 @@
 }
 
 
-static struct GRFFile *GetFileByGRFID(uint32 grfid)
+static GRFFile *GetFileByGRFID(uint32 grfid)
 {
-	struct GRFFile *file;
+	GRFFile *file;
 
-	file = _first_grffile;
-	while ((file != NULL) && (file->grfid != grfid))
-		file = file->next;
-
+	for (file = _first_grffile; file != NULL; file = file->next) {
+		if (file->grfid == grfid) break;
+	}
 	return file;
 }
 
-static struct GRFFile *GetFileByFilename(const char *filename)
+static GRFFile *GetFileByFilename(const char *filename)
 {
-	struct GRFFile *file;
+	GRFFile *file;
 
-	file = _first_grffile;
-	while ((file != NULL) && strcmp(file->filename, filename))
-		file = file->next;
-
+	for (file = _first_grffile; file != NULL; file = file->next) {
+		if (strcmp(file->filename, filename) == 0) break;
+	}
 	return file;
 }
 
@@ -683,7 +687,7 @@
 		case 0x08:
 		{	/* Class ID */
 			FOR_EACH_OBJECT {
-				struct StationSpec *stat = &_cur_grffile->stations[stid + i];
+				StationSpec *stat = &_cur_grffile->stations[stid + i];
 				uint32 classid;
 
 				/* classid, for a change, is always little-endian */
@@ -714,7 +718,7 @@
 		case 0x09:
 		{	/* Define sprite layout */
 			FOR_EACH_OBJECT {
-				struct StationSpec *stat = &_cur_grffile->stations[stid + i];
+				StationSpec *stat = &_cur_grffile->stations[stid + i];
 				int t;
 
 				stat->tiles = grf_load_byte(&buf);
@@ -759,9 +763,9 @@
 		case 0x0a:
 		{	/* Copy sprite layout */
 			FOR_EACH_OBJECT {
-				struct StationSpec *stat = &_cur_grffile->stations[stid + i];
+				StationSpec *stat = &_cur_grffile->stations[stid + i];
 				byte srcid = grf_load_byte(&buf);
-				struct StationSpec *srcstat = &_cur_grffile->stations[srcid];
+				StationSpec *srcstat = &_cur_grffile->stations[srcid];
 				int t;
 
 				stat->tiles = srcstat->tiles;
@@ -805,7 +809,7 @@
 		case 0x0C:
 		{	/* Platforms number */
 			FOR_EACH_OBJECT {
-				struct StationSpec *stat = &_cur_grffile->stations[stid + i];
+				StationSpec *stat = &_cur_grffile->stations[stid + i];
 
 				stat->allowed_platforms = ~grf_load_byte(&buf);
 			}
@@ -814,7 +818,7 @@
 		case 0x0D:
 		{	/* Platforms length */
 			FOR_EACH_OBJECT {
-				struct StationSpec *stat = &_cur_grffile->stations[stid + i];
+				StationSpec *stat = &_cur_grffile->stations[stid + i];
 
 				stat->allowed_lengths = ~grf_load_byte(&buf);
 			}
@@ -823,7 +827,7 @@
 		case 0x0e:
 		{	/* Define custom layout */
 			FOR_EACH_OBJECT {
-				struct StationSpec *stat = &_cur_grffile->stations[stid + i];
+				StationSpec *stat = &_cur_grffile->stations[stid + i];
 
 				while (buf < *bufp + len) {
 					byte length = grf_load_byte(&buf);
@@ -917,8 +921,6 @@
 	return ret;
 }
 
-#undef shift_buf
-
 
 /* Action 0x00 */
 static void VehicleChangeInfo(byte *buf, int len)
@@ -1048,7 +1050,6 @@
 		}
 		}
 	}
-#undef shift_buf
 }
 
 #undef FOR_EACH_OBJECT
@@ -1105,8 +1106,8 @@
 	/* XXX: For stations, these two are "little cargo" and "lotsa cargo" sets. */
 	uint8 numloaded;
 	uint8 numloading;
-	struct SpriteGroup *group;
-	struct RealSpriteGroup *rg;
+	SpriteGroup *group;
+	RealSpriteGroup *rg;
 	byte *loaded_ptr;
 	byte *loading_ptr;
 	int i;
@@ -1118,7 +1119,7 @@
 	numloading = buf[4];
 
 	if (numloaded == 0x81 || numloaded == 0x82) {
-		struct DeterministicSpriteGroup *dg;
+		DeterministicSpriteGroup *dg;
 		uint16 groupid;
 		int i;
 
@@ -1132,11 +1133,11 @@
 
 		if (setid >= _cur_grffile->spritegroups_count) {
 			_cur_grffile->spritegroups_count = setid + 1;
-			_cur_grffile->spritegroups = realloc(_cur_grffile->spritegroups, _cur_grffile->spritegroups_count * sizeof(struct SpriteGroup));
+			_cur_grffile->spritegroups = realloc(_cur_grffile->spritegroups, _cur_grffile->spritegroups_count * sizeof(*_cur_grffile->spritegroups));
 		}
 
 		group = &_cur_grffile->spritegroups[setid];
-		memset(group, 0, sizeof(struct SpriteGroup));
+		memset(group, 0, sizeof(*group));
 		group->type = SGT_DETERMINISTIC;
 		dg = &group->g.determ;
 
@@ -1190,7 +1191,7 @@
 		return;
 
 	} else if (numloaded == 0x80 || numloaded == 0x83) {
-		struct RandomizedSpriteGroup *rg;
+		RandomizedSpriteGroup *rg;
 		int i;
 
 		/* This stuff is getting actually evaluated in
@@ -1202,7 +1203,7 @@
 
 		if (setid >= _cur_grffile->spritegroups_count) {
 			_cur_grffile->spritegroups_count = setid + 1;
-			_cur_grffile->spritegroups = realloc(_cur_grffile->spritegroups, _cur_grffile->spritegroups_count * sizeof(struct SpriteGroup));
+			_cur_grffile->spritegroups = realloc(_cur_grffile->spritegroups, _cur_grffile->spritegroups_count * sizeof(*_cur_grffile->spritegroups));
 		}
 
 		group = &_cur_grffile->spritegroups[setid];
@@ -1268,10 +1269,10 @@
 
 	if (setid >= _cur_grffile->spritegroups_count) {
 		_cur_grffile->spritegroups_count = setid + 1;
-		_cur_grffile->spritegroups = realloc(_cur_grffile->spritegroups, _cur_grffile->spritegroups_count * sizeof(struct SpriteGroup));
+		_cur_grffile->spritegroups = realloc(_cur_grffile->spritegroups, _cur_grffile->spritegroups_count * sizeof(*_cur_grffile->spritegroups));
 	}
 	group = &_cur_grffile->spritegroups[setid];
-	memset(group, 0, sizeof(struct SpriteGroup));
+	memset(group, 0, sizeof(*group));
 	group->type = SGT_REAL;
 	rg = &group->g.real;
 
@@ -1347,7 +1348,7 @@
 
 		for (i = 0; i < idcount; i++) {
 			uint8 stid = buf[3 + i];
-			struct StationSpec *stat = &_cur_grffile->stations[stid];
+			StationSpec *stat = &_cur_grffile->stations[stid];
 			byte *bp = &buf[4 + idcount];
 
 			for (c = 0; c < cidcount; c++) {
@@ -1381,7 +1382,7 @@
 
 			for (i = 0; i < idcount; i++) {
 				uint8 stid = buf[3 + i];
-				struct StationSpec *stat = &_cur_grffile->stations[stid];
+				StationSpec *stat = &_cur_grffile->stations[stid];
 
 				stat->spritegroup[0] = _cur_grffile->spritegroups[groupid];
 				stat->grfid = _cur_grffile->grfid;
@@ -1639,7 +1640,7 @@
 			param_val = _opt.road_side << 4;
 			break;
 		case 0x88: {  /* see if specified GRFID is active */
-			struct GRFFile *file;
+			GRFFile *file;
 
 			file = GetFileByGRFID(cond_val);
 			param_val = (file != NULL);
@@ -1959,7 +1960,7 @@
 
 	for (i = 0; i < num; i++) {
 		uint32 grfid = grf_load_dword(&buf);
-		struct GRFFile *file = GetFileByGRFID(grfid);
+		GRFFile *file = GetFileByGRFID(grfid);
 
 		/* Unset activation flag */
 		if (file != NULL) {
@@ -1996,7 +1997,7 @@
 
 void InitNewGRFFile(const char *filename, int sprite_offset)
 {
-	struct GRFFile *newfile;
+	GRFFile *newfile;
 
 	newfile = GetFileByFilename(filename);
 	if (newfile != NULL) {
@@ -2006,7 +2007,7 @@
 		return;
 	}
 
-	newfile = calloc(1, sizeof(struct GRFFile));
+	newfile = calloc(1, sizeof(*newfile));
 
 	if (newfile == NULL)
 		error ("Out of memory");