+static void PlayGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
+{
+ // when drawing animations to fading buffer, do not play sounds or music
+ if (drawing_to_fading_buffer)
+ return;
+
+ PlayGlobalAnimSound(part);
+ PlayGlobalAnimMusic(part);
+}
+
+static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
+{
+ StopGlobalAnimSound(part);
+ StopGlobalAnimMusic(part);
+}
+
+static void PlayGlobalAnimSoundIfLoop(struct GlobalAnimPartControlInfo *part)
+{
+ // when drawing animations to fading buffer, do not play sounds
+ if (drawing_to_fading_buffer)
+ return;
+
+ // loop sounds only expire when playing
+ if (game_status != GAME_MODE_PLAYING)
+ return;
+
+ // check if any sound is defined for this animation part
+ if (part->sound == SND_UNDEFINED)
+ return;
+
+ // normal (non-loop) sounds do not expire when playing
+ if (!IS_LOOP_SOUND(part->sound))
+ return;
+
+ // prevent expiring loop sounds when playing
+ PlayGlobalAnimSound(part);
+}
+
+static boolean checkGlobalAnimEvent(int anim_event, int mask)
+{
+ int mask_anim_only = mask & ~ANIM_EVENT_PART_MASK;
+ int mask_ce_only = mask & ~ANIM_EVENT_PAGE_MASK;
+
+ if (mask & ANIM_EVENT_ANY)
+ return (anim_event & ANIM_EVENT_ANY);
+ else if (mask & ANIM_EVENT_SELF)
+ return (anim_event & ANIM_EVENT_SELF);
+ else if (mask & ANIM_EVENT_UNCLICK_ANY)
+ return (anim_event & ANIM_EVENT_UNCLICK_ANY);
+ else if (mask & ANIM_EVENT_CE_CHANGE)
+ return (anim_event == mask ||
+ anim_event == mask_ce_only);
+ else
+ return (anim_event == mask ||
+ anim_event == mask_anim_only);
+}
+
+static boolean isClickablePart(struct GlobalAnimPartControlInfo *part, int mask)
+{
+ struct GraphicInfo *c = &part->control_info;
+ int i;
+
+ if (part->init_event_state)
+ {
+ int num_init_events = GetGlobalAnimEventValueCount(c->init_event);
+
+ for (i = 0; i < num_init_events; i++)
+ {
+ int init_event = GetGlobalAnimEventValue(c->init_event, i);
+
+ if (checkGlobalAnimEvent(init_event, mask))
+ return TRUE;
+ }
+ }
+ else if (part->anim_event_state)
+ {
+ int num_anim_events = GetGlobalAnimEventValueCount(c->anim_event);
+
+ for (i = 0; i < num_anim_events; i++)
+ {
+ int anim_event = GetGlobalAnimEventValue(c->anim_event, i);
+
+ if (checkGlobalAnimEvent(anim_event, mask))
+ return TRUE;
+ }
+ }
+
+ return FALSE;
+}
+
+static boolean isInsidePartStacked(struct GlobalAnimPartControlInfo *part,
+ int mx, int my)
+{
+ struct GraphicInfo *g = &part->graphic_info;
+ struct GraphicInfo *c = &part->control_info;
+ int part_x = part->viewport_x + part->x;
+ int part_y = part->viewport_y + part->y;
+ int part_width = g->width;
+ int part_height = g->height;
+ int x, y;
+
+ for (y = 0; y < c->stacked_yfactor; y++)
+ {
+ for (x = 0; x < c->stacked_xfactor; x++)
+ {
+ int part_stacked_x = part_x + x * (part_width + c->stacked_xoffset);
+ int part_stacked_y = part_y + y * (part_height + c->stacked_yoffset);
+
+ if (mx >= part_stacked_x &&
+ mx < part_stacked_x + part_width &&
+ my >= part_stacked_y &&
+ my < part_stacked_y + part_height)
+ return TRUE;
+ }
+ }
+
+ return FALSE;
+}
+
+static boolean isClickedPart(struct GlobalAnimPartControlInfo *part,
+ int mx, int my, boolean clicked)
+{
+ // check if mouse click was detected at all
+ if (!clicked)
+ return FALSE;
+
+ // check if mouse click is outside the animation part's viewport
+ if (mx < part->viewport_x ||
+ mx >= part->viewport_x + part->viewport_width ||
+ my < part->viewport_y ||
+ my >= part->viewport_y + part->viewport_height)
+ return FALSE;
+
+ // check if mouse click is inside the animation part's (stacked) graphic
+ if (isInsidePartStacked(part, mx, my))
+ return TRUE;
+
+ return FALSE;
+}
+
+static boolean clickBlocked(struct GlobalAnimPartControlInfo *part)
+{
+ return ((part->control_info.style & STYLE_BLOCK) ? TRUE : FALSE);
+}
+
+static boolean clickConsumed(struct GlobalAnimPartControlInfo *part)
+{
+ return ((part->control_info.style & STYLE_PASSTHROUGH) ? FALSE : TRUE);
+}
+
+static void SetGlobalAnimPartTileXY(struct GlobalAnimPartControlInfo *part)
+{
+ // calculate playfield position (with scrolling) for related CE tile
+ // (do not use FX/FY, which are incorrect during envelope requests)
+ int FX0 = 2 * TILEX_VAR; // same as FX during DRAW_TO_FIELDBUFFER
+ int FY0 = 2 * TILEY_VAR; // same as FY during DRAW_TO_FIELDBUFFER
+ int fx = getFieldbufferOffsetX_RND(ScreenMovDir, ScreenGfxPos);
+ int fy = getFieldbufferOffsetY_RND(ScreenMovDir, ScreenGfxPos);
+ int sx = FX0 + SCREENX(part->tile_x) * TILEX_VAR;
+ int sy = FY0 + SCREENY(part->tile_y) * TILEY_VAR;
+ int cx = SX - REAL_SX;
+ int cy = SY - REAL_SY;
+ int x = sx - fx + cx;
+ int y = sy - fy + cy;
+
+ part->tile_xoffset += part->step_xoffset;
+ part->tile_yoffset += part->step_yoffset;
+
+ part->x = x + part->tile_xoffset;
+ part->y = y + part->tile_yoffset;
+}
+
+static void InitGlobalAnim_Triggered(struct GlobalAnimPartControlInfo *part,
+ boolean *click_consumed,
+ boolean *any_event_action,
+ int event_value, char *info_text)
+{
+ struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
+
+ int gic_anim_nr = part->old_anim_nr + 1; // X as in "anim_X"
+ int gic_part_nr = part->old_nr + 1; // Y as in "part_Y"
+ int mask = event_value | (gic_anim_nr << ANIM_EVENT_ANIM_BIT);
+
+ if (!part->is_base)
+ mask |= gic_part_nr << ANIM_EVENT_PART_BIT;
+
+ int anim2_nr;
+
+ for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
+ {
+ struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
+ int part2_nr;
+
+ for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
+ {
+ struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
+
+ if (!(part2->state & (ANIM_STATE_RUNNING | ANIM_STATE_WAITING)))
+ continue;
+
+ if (isClickablePart(part2, mask))
+ {
+ part2->triggered = TRUE;
+ *click_consumed |= clickConsumed(part); // click was on "part"!
+
+#if DEBUG_ANIM_EVENTS
+ Debug("anim:InitGlobalAnim_Triggered",
+ "%d.%d TRIGGERED BY %s OF %d.%d",
+ part2->old_anim_nr + 1, part2->old_nr + 1, info_text,
+ part->old_anim_nr + 1, part->old_nr + 1);
+#endif
+#if 0
+ Debug("anim:InitGlobalAnim_Triggered",
+ "%d.%d TRIGGER CLICKED [%d]", anim2_nr, part2_nr,
+ part2->control_info.anim_event_action);
+#endif
+
+ // after executing event action, ignore any further actions
+ if (!*any_event_action && DoGlobalAnim_EventAction(part2))
+ *any_event_action = TRUE;
+ }
+
+#if 0
+ struct GraphicInfo *c = &part2->control_info;
+
+ if (isClickablePart(part2, mask))
+ Debug("anim:InitGlobalAnim_Triggered",
+ "%d.%d: 0x%08x, 0x%08x [0x%08x] <--- TRIGGERED BY %d.%d",
+ anim2_nr, part2_nr, c->init_event, c->anim_event, mask,
+ anim_nr, part_nr);
+ else
+ Debug("anim:InitGlobalAnim_Triggered",
+ "%d.%d: 0x%08x, 0x%08x [0x%08x]",
+ anim2_nr, part2_nr, c->init_event, c->anim_event, mask);
+#endif
+ }
+ }
+}
+
+static void InitGlobalAnim_Triggered_ByCustomElement(int nr, int page,
+ int x, int y,
+ int trigger_x,
+ int trigger_y)
+{
+ struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[GAME_MODE_PLAYING];
+
+ int event_value = ANIM_EVENT_CE_CHANGE;
+ int event_bits = (nr << ANIM_EVENT_CE_BIT) | (page << ANIM_EVENT_PAGE_BIT);
+ int mask = event_value | event_bits;
+ int anim2_nr;
+
+ for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
+ {
+ struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
+ int part2_nr;
+
+ for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
+ {
+ struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
+
+ if (!(part2->state & (ANIM_STATE_RUNNING | ANIM_STATE_WAITING)))
+ continue;
+
+ if (isClickablePart(part2, mask) && !part2->triggered)
+ {
+ struct GraphicInfo *c = &part2->control_info;
+
+ if (c->position == POS_CE ||
+ c->position == POS_CE_TRIGGER)
+ {
+ // store CE tile and offset position to handle scrolling
+ part2->tile_x = (c->position == POS_CE_TRIGGER ? trigger_x : x);
+ part2->tile_y = (c->position == POS_CE_TRIGGER ? trigger_y : y);
+ part2->tile_xoffset = c->x;
+ part2->tile_yoffset = c->y;
+
+ // set resulting animation position relative to CE tile position
+ // (but only for ".init_event", not ".anim_event" type events)
+ if (part2->init_event_state)
+ SetGlobalAnimPartTileXY(part2);
+
+ // restart animation (by using current sync frame)
+ part2->initial_anim_sync_frame = anim_sync_frame;
+ }
+
+ part2->triggered = TRUE;
+
+ // do not trigger any other animation if CE change event was consumed
+ if (c->style == STYLE_CONSUME_CE_EVENT)
+ return;
+
+#if 0
+ Debug("anim:InitGlobalAnim_Triggered_ByCustomElement",
+ "%d.%d TRIGGERED BY CE %d", anim2_nr, part2_nr, nr + 1);
+#endif
+ }
+ }
+ }
+}
+
+static void HandleGlobalAnimDelay(struct GlobalAnimPartControlInfo *part,
+ int delay_type, char *info_text)
+{
+#if DEBUG_ANIM_DELAY
+ Debug("anim:HandleGlobalAnimDelay", "%d.%d %s",
+ part->old_anim_nr + 1, part->old_nr + 1, info_text);
+#endif
+
+ DoGlobalAnim_DelayAction(part, delay_type);
+}
+
+static void HandleGlobalAnimEvent(struct GlobalAnimPartControlInfo *part,
+ int event_value, char *info_text)
+{
+#if DEBUG_ANIM_EVENTS
+ Debug("anim:HandleGlobalAnimEvent", "%d.%d %s",
+ part->old_anim_nr + 1, part->old_nr + 1, info_text);
+#endif
+
+ boolean click_consumed = FALSE;
+ boolean any_event_action = FALSE;
+
+ // check if this event is defined to trigger other animations
+ InitGlobalAnim_Triggered(part, &click_consumed, &any_event_action,
+ event_value, info_text);
+}
+
+static int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part,
+ int state)
+{
+ if (handle_click && !part->clicked)
+ return state;
+
+ struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
+ struct GlobalAnimMainControlInfo *anim = &ctrl->anim[part->anim_nr];
+ struct GraphicInfo *g = &part->graphic_info;
+ struct GraphicInfo *c = &part->control_info;
+ boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
+ int alpha = (g->alpha != -1 ? g->alpha : SDL_ALPHA_OPAQUE);
+
+ // if game is paused, also pause playfield and door animations
+ if (isPausedOnPlayfieldOrDoor(part))
+ return state;
+
+ if (viewport_changed)
+ state |= ANIM_STATE_RESTART;
+
+ if (state & ANIM_STATE_RESTART)
+ {
+ // when drawing animations to fading buffer, only start fixed animations
+ if (drawing_to_fading_buffer && (c->x == ARG_UNDEFINED_VALUE ||
+ c->y == ARG_UNDEFINED_VALUE))
+ return ANIM_STATE_INACTIVE;
+
+ ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
+
+ part->init_delay_counter =
+ (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
+
+ part->anim_delay_counter =
+ (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
+
+ part->post_delay_counter = 0;
+
+ part->init_event_state = (c->init_event != ANIM_EVENT_UNDEFINED);
+ part->anim_event_state = (c->anim_event != ANIM_EVENT_UNDEFINED);
+
+ part->initial_anim_sync_frame =
+ (g->anim_global_sync || g->anim_global_anim_sync ? 0 :
+ anim_sync_frame + part->init_delay_counter);
+
+ // do not re-initialize random animation frame after fade-in
+ if (part->anim_random_frame == -1)
+ part->anim_random_frame = GetSimpleRandom(g->anim_frames);
+
+ if (c->fade_mode & FADE_MODE_FADE)
+ {
+ // when fading in screen, first frame is 100 % transparent or opaque
+ part->fade_delay_counter = c->fade_delay + 1;
+ part->fade_alpha = (c->fade_mode == FADE_MODE_FADE_IN ? 0 : alpha);
+ }
+ else
+ {
+ part->fade_delay_counter = 0;
+ part->fade_alpha = -1;
+ }
+
+ if (c->direction & MV_HORIZONTAL)
+ {
+ int pos_bottom = part->viewport_height - g->height;
+
+ if (c->position == POS_TOP)
+ part->y = 0;
+ else if (c->position == POS_UPPER)
+ part->y = GetSimpleRandom(pos_bottom / 2);
+ else if (c->position == POS_MIDDLE)
+ part->y = pos_bottom / 2;
+ else if (c->position == POS_LOWER)
+ part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
+ else if (c->position == POS_BOTTOM)
+ part->y = pos_bottom;
+ else
+ part->y = GetSimpleRandom(pos_bottom);
+
+ if (c->direction == MV_RIGHT)
+ {
+ part->step_xoffset = c->step_offset;
+ part->x = -g->width + part->step_xoffset;
+ }
+ else
+ {
+ part->step_xoffset = -c->step_offset;
+ part->x = part->viewport_width + part->step_xoffset;
+ }
+
+ part->step_yoffset = 0;
+ }
+ else if (c->direction & MV_VERTICAL)
+ {
+ int pos_right = part->viewport_width - g->width;
+
+ if (c->position == POS_LEFT)
+ part->x = 0;
+ else if (c->position == POS_RIGHT)
+ part->x = pos_right;
+ else
+ part->x = GetSimpleRandom(pos_right);
+
+ if (c->direction == MV_DOWN)
+ {
+ part->step_yoffset = c->step_offset;