+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;
+
+ 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
+ return (anim_event == mask ||
+ anim_event == mask_anim_only);
+}
+
+static boolean isClickablePart(struct GlobalAnimPartControlInfo *part, int mask)
+{
+ struct GraphicInfo *c = &part->control_info;
+ int num_init_events = GetGlobalAnimEventValueCount(c->init_event);
+ int num_anim_events = GetGlobalAnimEventValueCount(c->anim_event);
+ int i;
+
+ for (i = 0; i < num_init_events; i++)
+ {
+ int init_event = GetGlobalAnimEventValue(c->init_event, i);
+
+ if (checkGlobalAnimEvent(init_event, mask))
+ return TRUE;
+ }
+
+ 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 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))
+ 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 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)