[Fix] Fixed a possible cargo id collision.
This commit is contained in:
		
							parent
							
								
									75c6d9f11f
								
							
						
					
					
						commit
						22e5393f0c
					
				
							
								
								
									
										25
									
								
								src/pilot.c
									
									
									
									
									
								
							
							
						
						
									
										25
									
								
								src/pilot.c
									
									
									
									
									
								
							| @ -30,7 +30,9 @@ | |||||||
| 
 | 
 | ||||||
| /* ID generators. */ | /* ID generators. */ | ||||||
| static unsigned int pilot_id = PLAYER_ID; /**< Stack of pilod ids to assure uniqueness. */ | static unsigned int pilot_id = PLAYER_ID; /**< Stack of pilod ids to assure uniqueness. */ | ||||||
| static unsigned int mission_cargo_id = 0; /**< ID generator for specail mission cargo. */ | static unsigned int mission_cargo_id = 0; /**< ID generator for special mission cargo.
 | ||||||
|  |                                                Not garanteed to be absolutely unique, | ||||||
|  |                                                only unique for each pilot. */ | ||||||
| 
 | 
 | ||||||
| /* Stack of pilots. */ | /* Stack of pilots. */ | ||||||
| Pilot** pilot_stack = NULL; /**< Not static, it is used in player.c and weapon.c and ai.c */ | Pilot** pilot_stack = NULL; /**< Not static, it is used in player.c and weapon.c and ai.c */ | ||||||
| @ -1222,21 +1224,38 @@ static void pilot_calcCargo(Pilot* pilot) { | |||||||
|  *    @return The mission cargo ID of created cargo. |  *    @return The mission cargo ID of created cargo. | ||||||
|  */ |  */ | ||||||
| unsigned int pilot_addMissionCargo(Pilot* pilot, Commodity* cargo, int quantity) { | unsigned int pilot_addMissionCargo(Pilot* pilot, Commodity* cargo, int quantity) { | ||||||
|  |   int i; | ||||||
|  |   unsigned int id, max_id; | ||||||
|   int q; |   int q; | ||||||
|   q = quantity; |   q = quantity; | ||||||
| 
 | 
 | ||||||
|  |   /* Get ID. */ | ||||||
|  |   id = ++mission_cargo_id; | ||||||
|  | 
 | ||||||
|  |   /* Check for collisions with pilot and set ID generator to the max. */ | ||||||
|  |   max_id = 0; | ||||||
|  |   for(i = 0; i < pilot->ncommodities; i++) | ||||||
|  |     if(pilot->commodities[i].id > max_id) | ||||||
|  |       max_id = pilot->commodities[i].id; | ||||||
|  |   if(max_id > id) | ||||||
|  |     mission_cargo_id = max_id; | ||||||
|  |   id = ++mission_cargo_id; | ||||||
|  | 
 | ||||||
|  |   /* Grow commodities. */ | ||||||
|   pilot->commodities = realloc(pilot->commodities, |   pilot->commodities = realloc(pilot->commodities, | ||||||
|       sizeof(PilotCommodity) * (pilot->ncommodities+1)); |       sizeof(PilotCommodity) * (pilot->ncommodities+1)); | ||||||
|   pilot->commodities[pilot->ncommodities].commodity = cargo; |   pilot->commodities[pilot->ncommodities].commodity = cargo; | ||||||
|  |   /* Add commodity. */ | ||||||
|   if(pilot_cargoFree(pilot) < quantity) |   if(pilot_cargoFree(pilot) < quantity) | ||||||
|     q = pilot_cargoFree(pilot); |     q = pilot_cargoFree(pilot); | ||||||
|   pilot->commodities[pilot->ncommodities].id = ++mission_cargo_id; |   pilot->commodities[pilot->ncommodities].id = id; | ||||||
|   pilot->commodities[pilot->ncommodities].quantity = q; |   pilot->commodities[pilot->ncommodities].quantity = q; | ||||||
|  |   /* Postfixing. */ | ||||||
|   pilot->cargo_free -= q; |   pilot->cargo_free -= q; | ||||||
|   pilot->solid->mass += q; |   pilot->solid->mass += q; | ||||||
|   pilot->ncommodities++; |   pilot->ncommodities++; | ||||||
| 
 | 
 | ||||||
|   return pilot->commodities[pilot->ncommodities-1].id; |   return id; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /**
 | /**
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Allanis
						Allanis