(svn r11410) -Codechange: implement random triggers for industries.

pull/155/head
rubidium 17 years ago
parent dd6ef7ad49
commit c1cf9f3396

@ -1256,6 +1256,7 @@ static void DeliverGoodsToIndustry(TileIndex xy, CargoID cargo_type, int num_pie
best->produced_cargo_waiting[1] = min(best->produced_cargo_waiting[1] + (num_pieces * indspec->input_cargo_multiplier[accepted_cargo_index][1] / 256), 0xFFFF);
}
TriggerIndustry(best, INDUSTRY_TRIGGER_RECEIVED_CARGO);
StartStopIndustryTileAnimation(best, IAT_INDUSTRY_RECEIVED_CARGO);
}
}

@ -130,6 +130,9 @@ struct Industry : PoolItem<Industry, IndustryID, &_Industry_pool> {
Date last_cargo_accepted_at; ///< Last day cargo was accepted by this industry
byte selected_layout; ///< Which tile layout was used when creating the industry
byte random_triggers; ///< Triggers for the random
uint16 random; ///< Random value used for randomisation of all kinds of things
PersistentStorage psa; ///< Persistent storage for NewGRF industries.
Industry(TileIndex tile = 0) : xy(tile) {}

@ -701,6 +701,8 @@ static void TileLoop_Industry(TileIndex tile)
IndustryGfx newgfx;
IndustryGfx gfx;
TriggerIndustryTile(tile, INDTILE_TRIGGER_TILE_LOOP);
if (!IsIndustryCompleted(tile)) {
MakeIndustryTileBigger(tile);
return;
@ -1042,6 +1044,7 @@ void OnTick_Industry()
if (_game_mode == GM_EDITOR) return;
FOR_ALL_INDUSTRIES(i) {
TriggerIndustry(i, INDUSTRY_TRIGGER_INDUSTRY_TICK);
StartStopIndustryTileAnimation(i, IAT_INDUSTRY_TICK);
ProduceIndustryGoods(i);
}
@ -2137,6 +2140,9 @@ static const SaveLoad _industry_desc[] = {
SLE_CONDARRX(cpp_offsetof(Industry, psa) + cpp_offsetof(Industry::PersistentStorage, storage), SLE_UINT32, 16, 76, SL_MAX_VERSION),
SLE_CONDVAR(Industry, random_triggers, SLE_UINT8, 82, SL_MAX_VERSION),
SLE_CONDVAR(Industry, random, SLE_UINT16, 82, SL_MAX_VERSION),
/* reserve extra space in savegame here. (currently 32 bytes) */
SLE_CONDNULL(32, 2, SL_MAX_VERSION),

@ -16,6 +16,7 @@
#include "newgrf_callbacks.h"
#include "newgrf_spritegroup.h"
#include "newgrf_industries.h"
#include "newgrf_industrytiles.h"
#include "newgrf_commons.h"
#include "newgrf_text.h"
#include "newgrf_town.h"
@ -338,18 +339,18 @@ static const SpriteGroup *IndustryResolveReal(const ResolverObject *object, cons
static uint32 IndustryGetRandomBits(const ResolverObject *object)
{
return object->u.industry.ind == NULL ? 0 : 0; //object->u.industry.ind->random_bits;
return object->u.industry.ind == NULL ? 0 : object->u.industry.ind->random;
}
static uint32 IndustryGetTriggers(const ResolverObject *object)
{
return object->u.industry.ind == NULL ? 0 : 0; //object->u.industry.ind->triggers;
return object->u.industry.ind == NULL ? 0 : object->u.industry.ind->random_triggers;
}
static void IndustrySetTriggers(const ResolverObject *object, int triggers)
{
if (object->u.industry.ind == NULL) return;
//object->u.industry.ind->triggers = triggers;
object->u.industry.ind->random_triggers = triggers;
}
static void NewIndustryResolver(ResolverObject *res, TileIndex tile, Industry *indus)
@ -528,3 +529,19 @@ void IndustryProductionCallback(Industry *ind, int reason)
InvalidateWindow(WC_INDUSTRY_VIEW, ind->index);
}
void DoTriggerIndustry(Industry *ind, IndustryTileTrigger trigger)
{
ResolverObject object;
NewIndustryResolver(&object, ind->xy, ind);
object.callback = CBID_RANDOM_TRIGGER;
object.trigger = trigger;
const SpriteGroup *group = Resolve(GetIndustrySpec(ind->type)->grf_prop.spritegroup, &object);
if (group == NULL) return;
byte new_random_bits = Random();
ind->random &= ~object.reseed;
ind->random |= new_random_bits & object.reseed;
}

@ -119,14 +119,14 @@ static uint32 IndustryTileGetRandomBits(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) ? 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)
@ -134,10 +134,10 @@ static void IndustryTileSetTriggers(const ResolverObject *object, int triggers)
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;
}
}
@ -385,3 +385,43 @@ bool StartStopIndustryTileAnimation(const Industry *ind, IndustryAnimationTrigge
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);
}

@ -21,4 +21,16 @@ void AnimateNewIndustryTile(TileIndex tile);
bool StartStopIndustryTileAnimation(TileIndex tile, IndustryAnimationTrigger iat, uint32 random = Random());
bool StartStopIndustryTileAnimation(const Industry *ind, IndustryAnimationTrigger iat);
enum IndustryTileTrigger {
/* The tile of the industry has been triggered during the tileloop. */
INDTILE_TRIGGER_TILE_LOOP = 0x01,
/* The industry has been triggered via it's tick. */
INDUSTRY_TRIGGER_INDUSTRY_TICK = 0x02,
/* Cargo has been delivered. */
INDUSTRY_TRIGGER_RECEIVED_CARGO = 0x04,
};
void TriggerIndustryTile(TileIndex t, IndustryTileTrigger trigger);
void TriggerIndustry(Industry *ind, IndustryTileTrigger trigger);
#endif /* NEWGRF_INDUSTRYTILES_H */

@ -252,7 +252,7 @@ static inline const SpriteGroup *ResolveRandom(const SpriteGroup *group, Resolve
/* Handle triggers */
/* Magic code that may or may not do the right things... */
byte waiting_triggers = object->GetTriggers(object);
byte match = group->g.random.triggers & (waiting_triggers | object->trigger);
byte match = group->g.random.triggers & (waiting_triggers | object->trigger) || group->g.random.triggers == 0;
bool res;
res = (group->g.random.cmp_mode == RSG_CMP_ANY) ?

@ -29,7 +29,7 @@
#include "strings.h"
#include <list>
extern const uint16 SAVEGAME_VERSION = 81;
extern const uint16 SAVEGAME_VERSION = 82;
uint16 _sl_version; ///< the major savegame version identifier
byte _sl_minor_version; ///< the minor savegame version, DO NOT USE!

Loading…
Cancel
Save