changeset 9018:9dcf32675eb5 draft

(svn r12817) -Feature: the ability to play NewGRF sounds for industries and stations.
author rubidium <rubidium@openttd.org>
date Mon, 21 Apr 2008 11:29:01 +0000
parents 09f7bec7a574
children 382abce6450f
files src/newgrf_house.cpp src/newgrf_house.h src/newgrf_industrytiles.cpp src/newgrf_sound.cpp src/newgrf_sound.h src/newgrf_station.cpp src/town_cmd.cpp
diffstat 7 files changed, 35 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/src/newgrf_house.cpp
+++ b/src/newgrf_house.cpp
@@ -430,7 +430,7 @@
 
 			/* If the lower 7 bits of the upper byte of the callback
 			 * result are not empty, it is a sound effect. */
-			if (GB(callback_res, 8, 7) != 0) PlayHouseSound(GB(callback_res, 8, 7), tile);
+			if (GB(callback_res, 8, 7) != 0) PlayTileSound(hs->grffile, GB(callback_res, 8, 7), tile);
 		}
 	}
 
@@ -450,7 +450,7 @@
 	MarkTileDirtyByTile(tile);
 }
 
-void ChangeHouseAnimationFrame(TileIndex tile, uint16 callback_result)
+void ChangeHouseAnimationFrame(const GRFFile *file, TileIndex tile, uint16 callback_result)
 {
 	switch (callback_result & 0xFF) {
 		case 0xFD: /* Do nothing. */         break;
@@ -463,7 +463,7 @@
 	}
 	/* If the lower 7 bits of the upper byte of the callback
 	 * result are not empty, it is a sound effect. */
-	if (GB(callback_result, 8, 7) != 0) PlayHouseSound(GB(callback_result, 8, 7), tile);
+	if (GB(callback_result, 8, 7) != 0) PlayTileSound(file, GB(callback_result, 8, 7), tile);
 }
 
 bool CanDeleteHouse(TileIndex tile)
@@ -491,7 +491,7 @@
 		uint32 param = (hs->extra_flags & SYNCHRONISED_CALLBACK_1B) ? (GB(Random(), 0, 16) | random_bits << 16) : Random();
 		uint16 callback_res = GetHouseCallback(CBID_HOUSE_ANIMATION_START_STOP, param, 0, GetHouseType(tile), GetTownByTile(tile), tile);
 
-		if (callback_res != CALLBACK_FAILED) ChangeHouseAnimationFrame(tile, callback_res);
+		if (callback_res != CALLBACK_FAILED) ChangeHouseAnimationFrame(hs->grffile, tile, callback_res);
 	}
 }
 
--- a/src/newgrf_house.h
+++ b/src/newgrf_house.h
@@ -36,7 +36,7 @@
 
 void DrawNewHouseTile(TileInfo *ti, HouseID house_id);
 void AnimateNewHouseTile(TileIndex tile);
-void ChangeHouseAnimationFrame(TileIndex tile, uint16 callback_result);
+void ChangeHouseAnimationFrame(const struct GRFFile *file, TileIndex tile, uint16 callback_result);
 
 uint16 GetHouseCallback(CallbackID callback, uint32 param1, uint32 param2, HouseID house_id, Town *town, TileIndex tile);
 
--- a/src/newgrf_industrytiles.cpp
+++ b/src/newgrf_industrytiles.cpp
@@ -15,6 +15,7 @@
 #include "newgrf_callbacks.h"
 #include "newgrf_industries.h"
 #include "newgrf_industrytiles.h"
+#include "newgrf_sound.h"
 #include "newgrf_text.h"
 #include "industry_map.h"
 #include "clear_map.h"
@@ -337,6 +338,10 @@
 					frame = callback_res & 0xFF;
 					break;
 			}
+
+			/* If the lower 7 bits of the upper byte of the callback
+			 * result are not empty, it is a sound effect. */
+			if (GB(callback_res, 8, 7) != 0) PlayTileSound(itspec->grf_prop.grffile, GB(callback_res, 8, 7), tile);
 		}
 	}
 
@@ -356,7 +361,7 @@
 	MarkTileDirtyByTile(tile);
 }
 
-static void ChangeIndustryTileAnimationFrame(TileIndex tile, IndustryAnimationTrigger iat, uint32 random_bits, IndustryGfx gfx, Industry *ind)
+static void ChangeIndustryTileAnimationFrame(const IndustryTileSpec *itspec, TileIndex tile, IndustryAnimationTrigger iat, uint32 random_bits, IndustryGfx gfx, Industry *ind)
 {
 	uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_ANIM_START_STOP, random_bits, iat, gfx, ind, tile);
 	if (callback_res == CALLBACK_FAILED) return;
@@ -370,6 +375,10 @@
 			AddAnimatedTile(tile);
 			break;
 	}
+
+	/* If the lower 7 bits of the upper byte of the callback
+	 * result are not empty, it is a sound effect. */
+	if (GB(callback_res, 8, 7) != 0) PlayTileSound(itspec->grf_prop.grffile, GB(callback_res, 8, 7), tile);
 }
 
 bool StartStopIndustryTileAnimation(TileIndex tile, IndustryAnimationTrigger iat, uint32 random)
@@ -380,7 +389,7 @@
 	if (!HasBit(itspec->animation_triggers, iat)) return false;
 
 	Industry *ind = GetIndustryByTile(tile);
-	ChangeIndustryTileAnimationFrame(tile, iat, random, gfx, ind);
+	ChangeIndustryTileAnimationFrame(itspec, tile, iat, random, gfx, ind);
 	return true;
 }
 
--- a/src/newgrf_sound.cpp
+++ b/src/newgrf_sound.cpp
@@ -69,9 +69,11 @@
 	return true;
 }
 
-bool PlayHouseSound(uint16 sound_id, TileIndex tile)
+bool PlayTileSound(const GRFFile *file, uint16 sound_id, TileIndex tile)
 {
-	if (sound_id < GetNumOriginalSounds()) {
+	if (sound_id >= GetNumOriginalSounds()) sound_id += file->sound_offset - GetNumOriginalSounds();
+
+	if (sound_id < GetNumSounds()) {
 		SndPlayTileFx((SoundFx)sound_id, tile);
 		return true;
 	}
--- a/src/newgrf_sound.h
+++ b/src/newgrf_sound.h
@@ -26,6 +26,6 @@
 FileEntry *GetSound(uint index);
 uint GetNumSounds();
 bool PlayVehicleSound(const Vehicle *v, VehicleSoundEvent event);
-bool PlayHouseSound(uint16 sound_id, TileIndex tile);
+bool PlayTileSound(const struct GRFFile *file, uint16 sound_id, TileIndex tile);
 
 #endif /* NEWGRF_SOUND_H */
--- a/src/newgrf_station.cpp
+++ b/src/newgrf_station.cpp
@@ -16,6 +16,7 @@
 #include "newgrf_commons.h"
 #include "newgrf_station.h"
 #include "newgrf_spritegroup.h"
+#include "newgrf_sound.h"
 #include "cargotype.h"
 #include "town_map.h"
 #include "newgrf_town.h"
@@ -901,6 +902,10 @@
 					frame = callback & 0xFF;
 					break;
 			}
+
+			/* If the lower 7 bits of the upper byte of the callback
+			 * result are not empty, it is a sound effect. */
+			if (GB(callback, 8, 7) != 0) PlayTileSound(ss->grffile, GB(callback, 8, 7), tile);
 		}
 	}
 
@@ -935,6 +940,10 @@
 			AddAnimatedTile(tile);
 			break;
 	}
+
+	/* If the lower 7 bits of the upper byte of the callback
+	 * result are not empty, it is a sound effect. */
+	if (GB(callback, 8, 7) != 0) PlayTileSound(ss->grffile, GB(callback, 8, 7), tile);
 }
 
 enum TriggerArea {
--- a/src/town_cmd.cpp
+++ b/src/town_cmd.cpp
@@ -381,16 +381,18 @@
 	IncHouseConstructionTick(tile);
 	if (GetHouseConstructionTick(tile) != 0) return;
 
+	const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile));
+
 	/* Check and/or  */
-	if (HasBit(GetHouseSpecs(GetHouseType(tile))->callback_mask, CBM_HOUSE_CONSTRUCTION_STATE_CHANGE)) {
+	if (HasBit(hs->callback_mask, CBM_HOUSE_CONSTRUCTION_STATE_CHANGE)) {
 		uint16 callback_res = GetHouseCallback(CBID_HOUSE_CONSTRUCTION_STATE_CHANGE, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile);
-		if (callback_res != CALLBACK_FAILED) ChangeHouseAnimationFrame(tile, callback_res);
+		if (callback_res != CALLBACK_FAILED) ChangeHouseAnimationFrame(hs->grffile, tile, callback_res);
 	}
 
 	if (IsHouseCompleted(tile)) {
 		/* Now that construction is complete, we can add the population of the
 		 * building to the town. */
-		ChangePopulation(GetTownByTile(tile), GetHouseSpecs(GetHouseType(tile))->population);
+		ChangePopulation(GetTownByTile(tile), hs->population);
 		SetHouseConstructionYear(tile, _cur_year);
 	}
 	MarkTileDirtyByTile(tile);