src/oldloader.cpp
branchcustombridgeheads
changeset 5650 aefc131bf5ce
parent 5649 55c8267c933f
child 5847 9ce114e1d90d
equal deleted inserted replaced
5649:55c8267c933f 5650:aefc131bf5ce
    78 	OC_TILE      = OC_VAR_U32  | OC_FILE_U16,
    78 	OC_TILE      = OC_VAR_U32  | OC_FILE_U16,
    79 
    79 
    80 	OC_END       = 0 ///< End of the whole chunk, all 32bits set to zero
    80 	OC_END       = 0 ///< End of the whole chunk, all 32bits set to zero
    81 } OldChunkType;
    81 } OldChunkType;
    82 
    82 
       
    83 DECLARE_ENUM_AS_BIT_SET(OldChunkType);
       
    84 
    83 typedef bool OldChunkProc(LoadgameState *ls, int num);
    85 typedef bool OldChunkProc(LoadgameState *ls, int num);
    84 
    86 
    85 typedef struct OldChunks {
    87 typedef struct OldChunks {
    86 	OldChunkType type;   ///< Type of field
    88 	OldChunkType type;   ///< Type of field
    87 	uint32 amount;       ///< Amount of fields
    89 	uint32 amount;       ///< Amount of fields
    95 assert_compile(sizeof(TileIndex) == 4);
    97 assert_compile(sizeof(TileIndex) == 4);
    96 
    98 
    97 static uint32 _bump_assert_value;
    99 static uint32 _bump_assert_value;
    98 static bool   _read_ttdpatch_flags;
   100 static bool   _read_ttdpatch_flags;
    99 
   101 
   100 static OldChunkType GetOldChunkType(OldChunkType type)     {return GB(type, 0, 8);}
   102 static OldChunkType GetOldChunkType(OldChunkType type)     {return (OldChunkType)GB(type, 0, 8);}
   101 static OldChunkType GetOldChunkVarType(OldChunkType type)  {return GB(type, 8, 8) << 8;}
   103 static OldChunkType GetOldChunkVarType(OldChunkType type)  {return (OldChunkType)(GB(type, 8, 8) << 8);}
   102 static OldChunkType GetOldChunkFileType(OldChunkType type) {return GB(type, 16, 8) << 16;}
   104 static OldChunkType GetOldChunkFileType(OldChunkType type) {return (OldChunkType)(GB(type, 16, 8) << 16);}
   103 
   105 
   104 static inline byte CalcOldVarLen(OldChunkType type)
   106 static inline byte CalcOldVarLen(OldChunkType type)
   105 {
   107 {
   106 	static const byte type_mem_size[] = {0, 1, 1, 2, 2, 4, 4, 8};
   108 	static const byte type_mem_size[] = {0, 1, 1, 2, 2, 4, 4, 8};
   107 	byte length = GB(type, 8, 8);
   109 	byte length = GB(type, 8, 8);
   187  *
   189  *
   188  */
   190  */
   189 static bool LoadChunk(LoadgameState *ls, void *base, const OldChunks *chunks)
   191 static bool LoadChunk(LoadgameState *ls, void *base, const OldChunks *chunks)
   190 {
   192 {
   191 	const OldChunks *chunk = chunks;
   193 	const OldChunks *chunk = chunks;
   192 	byte *base_ptr = base;
   194 	byte *base_ptr = (byte*)base;
   193 
   195 
   194 	while (chunk->type != OC_END) {
   196 	while (chunk->type != OC_END) {
   195 		byte* ptr = chunk->ptr;
   197 		byte* ptr = (byte*)chunk->ptr;
   196 		uint i;
   198 		uint i;
   197 
   199 
   198 		for (i = 0; i < chunk->amount; i++) {
   200 		for (i = 0; i < chunk->amount; i++) {
   199 			if (ls->failed) return false;
   201 			if (ls->failed) return false;
   200 
   202 
   957 	OCL_END()
   959 	OCL_END()
   958 };
   960 };
   959 
   961 
   960 static bool LoadOldPlayer(LoadgameState *ls, int num)
   962 static bool LoadOldPlayer(LoadgameState *ls, int num)
   961 {
   963 {
   962 	Player *p = GetPlayer(num);
   964 	Player *p = GetPlayer((PlayerID)num);
   963 
   965 
   964 	_current_player_id = num;
   966 	_current_player_id = (PlayerID)num;
   965 
   967 
   966 	if (!LoadChunk(ls, p, player_chunk)) return false;
   968 	if (!LoadChunk(ls, p, player_chunk)) return false;
   967 
   969 
   968 	p->name_1 = RemapOldStringID(_old_string_id);
   970 	p->name_1 = RemapOldStringID(_old_string_id);
   969 	p->president_name_1 = RemapOldStringID(_old_string_id_2);
   971 	p->president_name_1 = RemapOldStringID(_old_string_id_2);
   994 
   996 
   995 	/* State 20 for AI players is sell vehicle. Since the AI struct is not
   997 	/* State 20 for AI players is sell vehicle. Since the AI struct is not
   996 	 * really figured out as of now, p->ai.cur_veh; needed for 'sell vehicle'
   998 	 * really figured out as of now, p->ai.cur_veh; needed for 'sell vehicle'
   997 	 * is NULL and the function will crash. To fix this, just change the state
   999 	 * is NULL and the function will crash. To fix this, just change the state
   998 	 * to some harmless state, like 'loop vehicle'; 1 */
  1000 	 * to some harmless state, like 'loop vehicle'; 1 */
   999 	if (!IsHumanPlayer(num) && p->ai.state == 20) p->ai.state = 1;
  1001 	if (!IsHumanPlayer((PlayerID)num) && p->ai.state == 20) p->ai.state = 1;
  1000 
  1002 
  1001 	if (p->is_ai && (!_networking || _network_server) && _ai.enabled)
  1003 	if (p->is_ai && (!_networking || _network_server) && _ai.enabled)
  1002 		AI_StartNewAI(p->index);
  1004 		AI_StartNewAI(p->index);
  1003 
  1005 
  1004 	return true;
  1006 	return true;