src/newgrf_industrytiles.cpp
branchNewGRF_ports
changeset 6871 5a9dc001e1ad
parent 6870 ca3fd1fbe311
child 6872 1c4a4a609f85
--- a/src/newgrf_industrytiles.cpp	Sat Oct 06 21:16:00 2007 +0000
+++ b/src/newgrf_industrytiles.cpp	Mon Dec 03 23:39:38 2007 +0000
@@ -22,6 +22,7 @@
 #include "table/sprites.h"
 #include "table/strings.h"
 #include "sprite.h"
+#include "transparency.h"
 
 /**
  * Based on newhouses equivalent, but adapted for newindustries
@@ -118,14 +119,14 @@
 {
 	const TileIndex tile = object->u.industry.tile;
 	if (tile == INVALID_TILE || !IsTileType(tile, MP_INDUSTRY)) return 0;
-	return (object->scope == VSG_SCOPE_SELF) ? GetIndustryRandomBits(tile) : 0; //GetIndustryByTile(tile)->random_bits;
+	return (object->scope == VSG_SCOPE_SELF) ? GetIndustryRandomBits(tile) : GetIndustryByTile(tile)->random;
 }
 
 static uint32 IndustryTileGetTriggers(const ResolverObject *object)
 {
 	const TileIndex tile = object->u.industry.tile;
 	if (tile == INVALID_TILE || !IsTileType(tile, MP_INDUSTRY)) return 0;
-	return (object->scope == VSG_SCOPE_SELF) ? GetIndustryTriggers(tile) : 0; //GetIndustryByTile(tile)->triggers;
+	return (object->scope == VSG_SCOPE_SELF) ? GetIndustryTriggers(tile) : GetIndustryByTile(tile)->random_triggers;
 }
 
 static void IndustryTileSetTriggers(const ResolverObject *object, int triggers)
@@ -133,10 +134,10 @@
 	const TileIndex tile = object->u.industry.tile;
 	if (tile == INVALID_TILE || !IsTileType(tile, MP_INDUSTRY)) return;
 
-	if (object->scope != VSG_SCOPE_SELF) {
+	if (object->scope == VSG_SCOPE_SELF) {
 		SetIndustryTriggers(tile, triggers);
 	} else {
-		//GetIndustryByTile(tile)->triggers = triggers;
+		GetIndustryByTile(tile)->random_triggers = triggers;
 	}
 }
 
@@ -181,8 +182,10 @@
 
 		if (IS_CUSTOM_SPRITE(image)) image += stage;
 
-		if (HASBIT(image, PALETTE_MODIFIER_COLOR)) {
-			pal = GENERAL_SPRITE_COLOR(rnd_color);
+		if (HasBit(image, PALETTE_MODIFIER_COLOR)) {
+			if (pal == 0) {
+				pal = GENERAL_SPRITE_COLOR(rnd_color);
+			}
 		} else {
 			pal = PAL_NONE;
 		}
@@ -193,10 +196,10 @@
 				ti->x + dtss->delta_x, ti->y + dtss->delta_y,
 				dtss->size_x, dtss->size_y,
 				dtss->size_z, ti->z + dtss->delta_z,
-				!HASBIT(image, SPRITE_MODIFIER_OPAQUE) && HASBIT(_transparent_opt, TO_INDUSTRIES)
+				!HasBit(image, SPRITE_MODIFIER_OPAQUE) && IsTransparencySet(TO_INDUSTRIES)
 			);
 		} else {
-			AddChildSpriteScreen(image, pal, dtss->delta_x, dtss->delta_y, HASBIT(_transparent_opt, TO_INDUSTRIES));
+			AddChildSpriteScreen(image, pal, (byte)dtss->delta_x, (byte)dtss->delta_y, IsTransparencySet(TO_INDUSTRIES));
 		}
 	}
 }
@@ -224,7 +227,7 @@
 
 	if (ti->tileh != SLOPE_FLAT) {
 		bool draw_old_one = true;
-		if (HASBIT(inds->callback_flags, CBM_INDT_DRAW_FOUNDATIONS)) {
+		if (HasBit(inds->callback_flags, CBM_INDT_DRAW_FOUNDATIONS)) {
 			/* Called to determine the type (if any) of foundation to draw for industry tile */
 			uint32 callback_res = GetIndustryTileCallback(CBID_INDUSTRY_DRAW_FOUNDATIONS, 0, 0, gfx, i, ti->tile);
 			draw_old_one = callback_res != 0;
@@ -241,7 +244,7 @@
 	} else {
 		/* Limit the building stage to the number of stages supplied. */
 		byte stage = GetIndustryConstructionStage(ti->tile);
-		stage = clamp(stage - 4 + group->g.layout.num_sprites, 0, group->g.layout.num_sprites - 1);
+		stage = Clamp(stage - 4 + group->g.layout.num_sprites, 0, group->g.layout.num_sprites - 1);
 		IndustryDrawTileLayout(ti, group, i->random_color, stage, gfx);
 		return true;
 	}
@@ -265,6 +268,11 @@
 		return callback_res != 0;
 	}
 
+	/* Copy some parameters from the registers to the error message text ref. stack */
+	SwitchToErrorRefStack();
+	PrepareTextRefStackUsage(4);
+	SwitchToNormalRefStack();
+
 	switch (callback_res) {
 		case 0x400: return true;
 		case 0x401: _error_message = STR_0239_SITE_UNSUITABLE;                 return false;
@@ -281,9 +289,9 @@
 	const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
 	byte animation_speed = itspec->animation_speed;
 
-	if (HASBIT(itspec->callback_flags, CBM_INDT_ANIM_SPEED)) {
+	if (HasBit(itspec->callback_flags, CBM_INDT_ANIM_SPEED)) {
 		uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_ANIMATION_SPEED, 0, 0, gfx, ind, tile);
-		if (callback_res != CALLBACK_FAILED) animation_speed = clamp(callback_res & 0xFF, 0, 16);
+		if (callback_res != CALLBACK_FAILED) animation_speed = Clamp(callback_res & 0xFF, 0, 16);
 	}
 
 	/* An animation speed of 2 means the animation frame changes 4 ticks, and
@@ -294,10 +302,10 @@
 
 	bool frame_set_by_callback = false;
 	byte frame = GetIndustryAnimationState(tile);
-	uint16 num_frames = GB(itspec->animation_info, 0, 8) + 1;
+	uint16 num_frames = GB(itspec->animation_info, 0, 8);
 
-	if (HASBIT(itspec->callback_flags, CBM_INDT_ANIM_NEXT_FRAME)) {
-		uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_ANIM_NEXT_FRAME, HASBIT(itspec->animation_special_flags, 0) ? Random() : 0, 0, gfx, ind, tile);
+	if (HasBit(itspec->callback_flags, CBM_INDT_ANIM_NEXT_FRAME)) {
+		uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_ANIM_NEXT_FRAME, HasBit(itspec->animation_special_flags, 0) ? Random() : 0, 0, gfx, ind, tile);
 
 		if (callback_res != CALLBACK_FAILED) {
 			frame_set_by_callback = true;
@@ -354,7 +362,7 @@
 	IndustryGfx gfx = GetIndustryGfx(tile);
 	const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
 
-	if (!HASBIT(itspec->animation_triggers, iat)) return false;
+	if (!HasBit(itspec->animation_triggers, iat)) return false;
 
 	Industry *ind = GetIndustryByTile(tile);
 	ChangeIndustryTileAnimationFrame(tile, iat, random, gfx, ind);
@@ -377,3 +385,43 @@
 
 	return ret;
 }
+
+static void DoTriggerIndustryTile(TileIndex tile, IndustryTileTrigger trigger, Industry *ind)
+{
+	ResolverObject object;
+
+	IndustryGfx gfx = GetIndustryGfx(tile);
+	const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
+
+	NewIndustryTileResolver(&object, gfx, tile, ind);
+
+	object.callback = CBID_RANDOM_TRIGGER;
+	object.trigger = trigger;
+
+	const SpriteGroup *group = Resolve(itspec->grf_prop.spritegroup, &object);
+	if (group == NULL) return;
+
+	byte new_random_bits = Random();
+	byte random_bits = GetIndustryRandomBits(tile);
+	random_bits &= ~object.reseed;
+	random_bits |= new_random_bits & object.reseed;
+	SetIndustryRandomBits(tile, random_bits);
+}
+
+void TriggerIndustryTile(TileIndex tile, IndustryTileTrigger trigger)
+{
+	DoTriggerIndustryTile(tile, trigger, GetIndustryByTile(tile));
+}
+
+extern void DoTriggerIndustry(Industry *ind, IndustryTileTrigger trigger);
+
+void TriggerIndustry(Industry *ind, IndustryTileTrigger trigger)
+{
+	BEGIN_TILE_LOOP(tile, ind->width, ind->height, ind->xy)
+		if (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == ind->index) {
+			DoTriggerIndustryTile(tile, trigger, ind);
+		}
+	END_TILE_LOOP(tile, ind->width, ind->height, ind->xy)
+
+	DoTriggerIndustry(ind, trigger);
+}