src/newgrf.cpp
changeset 10416 96e869d65b9c
parent 10408 c9baa47d1342
child 10429 1b99254f9607
equal deleted inserted replaced
10415:53ed7c3b441c 10416:96e869d65b9c
   116 static uint32 _grm_engines[256];
   116 static uint32 _grm_engines[256];
   117 
   117 
   118 /* Contains the GRF ID of the owner of a cargo if it has been reserved */
   118 /* Contains the GRF ID of the owner of a cargo if it has been reserved */
   119 static uint32 _grm_cargos[NUM_CARGO * 2];
   119 static uint32 _grm_cargos[NUM_CARGO * 2];
   120 
   120 
       
   121 struct GRMSpriteEntry {
       
   122 	uint32 grfid;
       
   123 	uint32 nfoline;
       
   124 
       
   125 	GRMSpriteEntry(uint32 grfid, uint32 nfoline) : grfid(grfid), nfoline(nfoline) { }
       
   126 
       
   127 	bool operator<(const GRMSpriteEntry &other) const
       
   128 	{
       
   129 		return this->grfid < other.grfid || (this->grfid == other.grfid && this->nfoline < other.nfoline);
       
   130 	}
       
   131 };
       
   132 
       
   133 static std::map<GRMSpriteEntry, SpriteID> _grm_sprites;
       
   134 
   121 /** DEBUG() function dedicated to newGRF debugging messages
   135 /** DEBUG() function dedicated to newGRF debugging messages
   122  * Function is essentialy the same as DEBUG(grf, severity, ...) with the
   136  * Function is essentialy the same as DEBUG(grf, severity, ...) with the
   123  * addition of file:line information when parsing grf files.
   137  * addition of file:line information when parsing grf files.
   124  * NOTE: for the above reason(s) grfmsg() should ONLY be used for
   138  * NOTE: for the above reason(s) grfmsg() should ONLY be used for
   125  * loading/parsing grf files, not for runtime debug messages as there
   139  * loading/parsing grf files, not for runtime debug messages as there
  4353 			if (data == 0x0000FFFF) {
  4367 			if (data == 0x0000FFFF) {
  4354 				/* Patch variables */
  4368 				/* Patch variables */
  4355 				src1 = GetPatchVariable(src1);
  4369 				src1 = GetPatchVariable(src1);
  4356 			} else {
  4370 			} else {
  4357 				/* GRF Resource Management */
  4371 				/* GRF Resource Management */
  4358 				if (_cur_stage != GLS_ACTIVATION) {
  4372 				uint8  op      = src1;
  4359 					/* Ignore GRM during initialization */
  4373 				uint8  feature = GB(data, 8, 8);
       
  4374 				uint16 count   = GB(data, 16, 16);
       
  4375 
       
  4376 				if (_cur_stage == GLS_RESERVE) {
       
  4377 					if (feature == 0x08) {
       
  4378 						/* General sprites */
       
  4379 						if (op == 0) {
       
  4380 							/* Check if the allocated sprites will fit below the original sprite limit */
       
  4381 							if (_cur_spriteid + count >= 16384) {
       
  4382 								grfmsg(0, "ParamSet: GRM: Unable to allocate %d sprites; try changing NewGRF order", count);
       
  4383 								_cur_grfconfig->status = GCS_DISABLED;
       
  4384 
       
  4385 								_skip_sprites = -1;
       
  4386 								return;
       
  4387 							}
       
  4388 
       
  4389 							/* Reserve space at the current sprite ID */
       
  4390 							grfmsg(4, "ParamSet: GRM: Allocated %d sprites at %d", count, _cur_spriteid);
       
  4391 							_grm_sprites[GRMSpriteEntry(_cur_grffile->grfid, _nfo_line)] = _cur_spriteid;
       
  4392 							_cur_spriteid += count;
       
  4393 						}
       
  4394 					}
       
  4395 					/* Ignore GRM result during reservation */
  4360 					src1 = 0;
  4396 					src1 = 0;
  4361 				} else {
  4397 				} else if (_cur_stage == GLS_ACTIVATION) {
  4362 					uint8  op      = src1;
       
  4363 					uint8  feature = GB(data, 8, 8);
       
  4364 					uint16 count   = GB(data, 16, 16);
       
  4365 
       
  4366 					switch (feature) {
  4398 					switch (feature) {
  4367 						case 0x00: // Trains
  4399 						case 0x00: // Trains
  4368 						case 0x01: // Road Vehicles
  4400 						case 0x01: // Road Vehicles
  4369 						case 0x02: // Ships
  4401 						case 0x02: // Ships
  4370 						case 0x03: // Aircraft
  4402 						case 0x03: // Aircraft
  4387 							break;
  4419 							break;
  4388 
  4420 
  4389 						case 0x08: // General sprites
  4421 						case 0x08: // General sprites
  4390 							switch (op) {
  4422 							switch (op) {
  4391 								case 0:
  4423 								case 0:
  4392 									/* Check if the allocated sprites will fit below the original sprite limit */
  4424 									/* Return space reserved during reservation stage */
  4393 									if (_cur_spriteid + count >= 16384) {
  4425 									src1 = _grm_sprites[GRMSpriteEntry(_cur_grffile->grfid, _nfo_line)];
  4394 										grfmsg(0, "ParamSet: GRM: Unable to allocate %d sprites; try changing NewGRF order", count);
  4426 									grfmsg(4, "ParamSet: GRM: Using pre-allocated sprites at %d", src1);
  4395 										_cur_grfconfig->status = GCS_DISABLED;
       
  4396 
       
  4397 										_skip_sprites = -1;
       
  4398 										return;
       
  4399 									}
       
  4400 
       
  4401 									/* 'Reserve' space at the current sprite ID */
       
  4402 									src1 = _cur_spriteid;
       
  4403 									_cur_spriteid += count;
       
  4404 									break;
  4427 									break;
  4405 
  4428 
  4406 								case 1:
  4429 								case 1:
  4407 									src1 = _cur_spriteid;
  4430 									src1 = _cur_spriteid;
  4408 									break;
  4431 									break;
  4419 							if (_skip_sprites == -1) return;
  4442 							if (_skip_sprites == -1) return;
  4420 							break;
  4443 							break;
  4421 
  4444 
  4422 						default: grfmsg(1, "ParamSet: GRM: Unsupported feature 0x%X", feature); return;
  4445 						default: grfmsg(1, "ParamSet: GRM: Unsupported feature 0x%X", feature); return;
  4423 					}
  4446 					}
       
  4447 				} else {
       
  4448 					/* Ignore GRM during initialization */
       
  4449 					src1 = 0;
  4424 				}
  4450 				}
  4425 			}
  4451 			}
  4426 		} else {
  4452 		} else {
  4427 			/* Read another GRF File's parameter */
  4453 			/* Read another GRF File's parameter */
  4428 			const GRFFile *file = GetFileByGRFID(data);
  4454 			const GRFFile *file = GetFileByGRFID(data);
  5933 	/* Load old shore sprites in new position, if they were replaced by ActionA */
  5959 	/* Load old shore sprites in new position, if they were replaced by ActionA */
  5934 	ActivateOldShore();
  5960 	ActivateOldShore();
  5935 
  5961 
  5936 	/* Deallocate temporary loading data */
  5962 	/* Deallocate temporary loading data */
  5937 	free(_gted);
  5963 	free(_gted);
       
  5964 	_grm_sprites.clear();
  5938 }
  5965 }
  5939 
  5966 
  5940 void LoadNewGRF(uint load_index, uint file_index)
  5967 void LoadNewGRF(uint load_index, uint file_index)
  5941 {
  5968 {
  5942 	InitializeGRFSpecial();
  5969 	InitializeGRFSpecial();
  5952 	 */
  5979 	 */
  5953 	for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
  5980 	for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
  5954 		if (c->status != GCS_NOT_FOUND) c->status = GCS_UNKNOWN;
  5981 		if (c->status != GCS_NOT_FOUND) c->status = GCS_UNKNOWN;
  5955 	}
  5982 	}
  5956 
  5983 
       
  5984 	_cur_spriteid = load_index;
       
  5985 
  5957 	/* Load newgrf sprites
  5986 	/* Load newgrf sprites
  5958 	 * in each loading stage, (try to) open each file specified in the config
  5987 	 * in each loading stage, (try to) open each file specified in the config
  5959 	 * and load information from it. */
  5988 	 * and load information from it. */
  5960 	for (GrfLoadingStage stage = GLS_LABELSCAN; stage <= GLS_ACTIVATION; stage++) {
  5989 	for (GrfLoadingStage stage = GLS_LABELSCAN; stage <= GLS_ACTIVATION; stage++) {
  5961 		/* Set activated grfs back to will-be-activated between reservation- and activation-stage.
  5990 		/* Set activated grfs back to will-be-activated between reservation- and activation-stage.
  5965 		}
  5994 		}
  5966 
  5995 
  5967 		uint slot = file_index;
  5996 		uint slot = file_index;
  5968 
  5997 
  5969 		_cur_stage = stage;
  5998 		_cur_stage = stage;
  5970 		_cur_spriteid = load_index;
       
  5971 		for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
  5999 		for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
  5972 			if (c->status == GCS_DISABLED || c->status == GCS_NOT_FOUND) continue;
  6000 			if (c->status == GCS_DISABLED || c->status == GCS_NOT_FOUND) continue;
  5973 			if (stage > GLS_INIT && HasBit(c->flags, GCF_INIT_ONLY)) continue;
  6001 			if (stage > GLS_INIT && HasBit(c->flags, GCF_INIT_ONLY)) continue;
  5974 
  6002 
  5975 			/* @todo usererror() */
  6003 			/* @todo usererror() */