13

Prism Quest: Advanced Entity Behaviors

ZX Spectrum • Phase 1 • Tier 1

Create sophisticated entity AI behaviors with state machines and adaptive logic! Add intelligent spectral patterns, dimensional responses, and behavioral complexity using creative branching techniques and memory-efficient state management that transforms precision control into dynamic dimensional encounters.

⚪ medium
⏱️ 45-60 minutes
đź’» Code Examples
🛠️ Exercise
🎯

Learning Objectives

  • Create sophisticated entity AI behaviors using state machines and branching logic for intelligent dimensional gameplay
  • Add adaptive spectral responses and behavioral complexity with memory-efficient decision systems
  • Learn conditional programming and state management through practical AI behavior implementation
  • Build dynamic entity systems that respond intelligently to navigator actions and dimensional state
  • Experience creative Spectrum development with optimized AI programming and behavioral state machines
đź§ 

Key Concepts

Entity AI programming and state machines for sophisticated behavioral patterns in dimensional exploration Conditional logic and decision systems optimized for ZX Spectrum's memory constraints and processing capabilities Behavioral state management and adaptive responses using efficient branching algorithms Dynamic entity interaction and intelligent pattern recognition for engaging dimensional encounters Creative Spectrum AI development with memory optimization and performance-conscious behavioral programming

Lesson 13: Prism Quest - Advanced Entity Behaviors

Your dimensional entities need intelligent behaviors! In your precision control system from Lesson 12, you achieved pixel-perfect dimensional navigation. Today, you’ll add sophisticated entity AI behaviors with state machines, adaptive logic, and intelligent responses that transform your static spectrals into dynamic dimensional opponents. You’ll master conditional programming through creative ZX Spectrum techniques that create engaging, adaptive dimensional encounters!

Your Advanced Behaviors Crystallize

Let’s create sophisticated entity AI using state machines and memory-efficient decision systems:

; Prism Quest - Advanced Entity Behaviors System
    ORG $8000
    
start:
    ; Initialize complete Spectrum environment
    CALL clear_complete_screen
    
    ; Set up enhanced dimensional world for advanced behaviors
    CALL init_complete_dimensional_state
    CALL init_entity_multiplication_system
    CALL init_achievement_prism_system
    CALL init_fluid_navigation_system
    CALL init_precision_control_system
    
    ; Initialize new advanced behavior systems
    CALL init_advanced_behavior_system
    CALL init_spectral_state_machines
    CALL init_dimensional_decision_system
    CALL init_adaptive_intelligence_system
    
    ; Create behavior-ready dimensional world
    CALL create_advanced_behavior_world
    
    ; Advanced Behaviors main loop
advanced_behaviors_loop:
    ; Start frame timing for behavioral processing
    CALL start_behavior_frame_timing
    
    ; Process precision dimensional control
    CALL read_precision_keyboard_input
    CALL process_input_polling_optimization
    CALL execute_precision_commands
    
    ; Update navigator with precision control
    CALL update_precision_navigator_movement
    
    ; Process advanced spectral behaviors
    CALL update_spectral_state_machines
    CALL process_dimensional_decision_systems
    CALL execute_adaptive_spectral_behaviors
    CALL update_dimensional_behavioral_interactions
    
    ; Process achievement systems with behavioral context
    CALL process_achievement_events_behavioral
    CALL update_achievement_calculations_behavioral
    
    ; Process advanced collision systems
    CALL process_behavioral_dimensional_collisions
    CALL update_dimensional_achievement_tracking_behavioral
    
    ; Update visual systems with behavioral feedback
    CALL update_behavioral_dimensional_effects
    CALL update_behavioral_attribute_display
    CALL update_behavioral_achievement_display
    
    ; Update audio systems with behavioral timing
    CALL update_dimensional_audio_behavioral
    
    ; Check for behavior-based victory conditions
    CALL check_behavioral_victory_conditions
    
    ; Complete behavioral frame timing
    CALL complete_behavior_frame_timing
    
    JR advanced_behaviors_loop

; Initialize advanced behavior system using creative state machine architecture
init_advanced_behavior_system:
    ; Set up behavioral framework for Spectrum constraints
    CALL init_behavioral_framework
    CALL init_spectral_ai_memory_management
    CALL init_behavioral_timing_system
    CALL init_dimensional_intelligence_parameters
    
    ; Initialize behavior control variables
    LD A, $00           ; Clear all behavior states
    LD (global_spectral_behavior_state), A
    LD (behavior_update_counter), A
    LD (dimensional_intelligence_level), A
    LD (adaptive_response_level), A
    
    ; Initialize behavioral timing variables
    LD (behavior_timing_counter), A
    LD (last_behavior_update), A
    LD (spectral_response_time), A
    
    ; Set behavioral parameters for Spectrum optimization
    LD A, $06           ; Behavior update frequency (every 6 frames for efficiency)
    LD (behavior_update_frequency), A
    LD A, $02           ; Base intelligence level
    LD (base_intelligence_level), A
    LD A, $06           ; Maximum intelligence level (limited for performance)
    LD (max_intelligence_level), A
    
    RET

; Initialize spectral state machines for intelligent dimensional behaviors
init_spectral_state_machines:
    ; Set up state machine framework for spectral entities
    CALL init_spectral_state_machine_framework
    CALL init_spectral_state_arrays
    CALL init_dimensional_state_transition_system
    CALL init_spectral_behavioral_memory_system
    
    ; Initialize state machines for all spectral entities
    LD B, $06           ; Initialize 6 spectral entity state machines
    LD HL, spectral_current_state
    
init_spectral_states_loop:
    LD A, $00           ; State: DORMANT
    LD (HL), A
    INC HL
    LD (HL), A          ; Previous state
    INC HL
    LD (HL), $3C        ; State timer: 60 frames
    INC HL
    LD (HL), $00        ; Behavior flags
    INC HL
    
    ; Set initial behavioral parameters
    LD (HL), $01        ; Default behavior type: DRIFT
    INC HL
    LD (HL), $78        ; State duration: 120 frames (2 seconds)
    INC HL
    LD (HL), $00        ; Clear behavioral memory
    INC HL
    LD (HL), $00        ; Clear dimensional memory
    INC HL
    
    DJNZ init_spectral_states_loop
    
    RET

; Initialize dimensional decision system for adaptive AI
init_dimensional_decision_system:
    ; Set up decision system framework for Spectrum
    CALL init_dimensional_decision_framework
    CALL init_decision_node_system
    CALL init_spectral_decision_memory
    CALL init_dimensional_learning_system
    
    ; Initialize decision system parameters
    LD A, $03           ; Decision tree depth (limited for performance)
    LD (decision_tree_depth), A
    LD A, $06           ; Maximum decision nodes per tree
    LD (max_decision_nodes), A
    LD A, $04           ; Decision evaluation frequency (every 4 updates)
    LD (decision_evaluation_frequency), A
    
    ; Initialize decision variables
    LD A, $00
    LD (current_decision_node), A
    LD (decision_evaluation_counter), A
    LD (last_decision_result), A
    LD (dimensional_learning_factor), A
    
    ; Set up basic dimensional decision trees
    CALL setup_dimensional_decision_trees
    
    RET

; Initialize adaptive intelligence system for dynamic dimensional AI
init_adaptive_intelligence_system:
    ; Set up adaptive intelligence framework
    CALL init_adaptive_intelligence_framework
    CALL init_navigator_behavior_tracking
    CALL init_intelligence_adaptation_system
    CALL init_dimensional_difficulty_scaling_system
    
    ; Initialize adaptive intelligence variables
    LD A, $00
    LD (navigator_exploration_pattern), A
    LD (navigator_evasion_tendency), A
    LD (navigator_interaction_frequency), A
    LD (adaptive_intelligence_strength), A
    
    ; Initialize behavior tracking arrays
    LD HL, navigator_behavior_history
    LD DE, navigator_behavior_history + 1
    LD BC, 11           ; Clear 12 tracking variables
    LD (HL), $00
    LDIR
    
    LD HL, adaptive_intelligence_history
    LD DE, adaptive_intelligence_history + 1
    LD BC, 11           ; Clear 12 tracking variables
    LD (HL), $00
    LDIR
    
    ; Set adaptive parameters for Spectrum optimization
    LD A, $20           ; Learning rate (32/256)
    LD (adaptive_learning_rate), A
    LD A, $08           ; Response sensitivity
    LD (response_sensitivity), A
    
    RET

; Create advanced behavior dimensional world with intelligent zones
create_advanced_behavior_world:
    ; Create dimensional world optimized for behavioral AI
    CALL create_behavioral_dimensional_maze
    
    ; Set up AI behavioral zones
    CALL create_spectral_behavior_zones
    
    ; Initialize intelligent spawn areas
    CALL setup_intelligent_dimensional_spawn_areas
    
    ; Create behavioral feedback systems
    CALL init_dimensional_behavioral_feedback_systems
    
    RET

; Create behavioral dimensional maze with AI optimization zones
create_behavioral_dimensional_maze:
    ; Use optimized maze creation with behavioral zones
    LD HL, $4000        ; Screen memory start
    LD DE, behavioral_maze_pattern
    LD BC, 768          ; Copy maze pattern
    LDIR                ; Fast block copy
    
    ; Set up behavioral attribute zones with intelligence indicators
    LD HL, $5800        ; Attribute memory start
    LD B, 24            ; Number of lines
    
create_behavioral_attributes_loop:
    PUSH BC             ; Save line counter
    LD C, 32            ; Characters per line
    
create_behavioral_line_attributes:
    ; Calculate behavioral zone colors
    LD A, B             ; Line number
    ADD A, C            ; Add column for variation
    AND $07             ; Mask to 8 colors for behavioral zones
    ADD A, $40          ; Base bright colors
    
    ; Special behavioral zones for AI
    LD D, A             ; Save calculated color
    LD A, B             ; Check line number
    CP $05              ; Behavioral zone 1 (lines 5-9)
    JR C, normal_behavioral_attribute
    CP $0A
    JR NC, check_behavioral_zone_2
    
    ; Behavioral zone 1 - high intelligence spectral area
    LD A, $43           ; Cyan on black for high intelligence
    JR store_behavioral_attribute
    
check_behavioral_zone_2:
    CP $0F              ; Behavioral zone 2 (lines 15-19)
    JR C, normal_behavioral_attribute
    CP $14
    JR NC, normal_behavioral_attribute
    
    ; Behavioral zone 2 - adaptive learning zone
    LD A, $45           ; Magenta on black for adaptive zone
    JR store_behavioral_attribute
    
normal_behavioral_attribute:
    LD A, D             ; Restore calculated color
    
store_behavioral_attribute:
    LD (HL), A          ; Set attribute
    INC HL              ; Next character
    DEC C               ; Decrement character counter
    JR NZ, create_behavioral_line_attributes
    
    POP BC              ; Restore line counter
    DJNZ create_behavioral_attributes_loop
    
    RET

; Update spectral state machines for intelligent dimensional behavior
update_spectral_state_machines:
    ; Check if it's time to update behaviors
    LD A, (behavior_update_counter)
    INC A
    LD (behavior_update_counter), A
    
    LD B, A             ; Save counter
    LD A, (behavior_update_frequency)
    CP B                ; Time for behavior update?
    JR NZ, skip_spectral_behavior_update
    
    ; Reset counter and update behaviors
    LD A, $00
    LD (behavior_update_counter), A
    
    ; Process all active spectral state machines
    LD B, $06           ; Check 6 spectral entities
    LD HL, spectral_current_state
    LD DE, spectral_entity_data
    
update_spectral_state_machines_loop:
    ; Check if spectral entity is active
    LD A, (DE)          ; Load entity active flag
    AND A               ; Check if active
    JR Z, next_spectral_state_machine
    
    ; Update individual spectral state machine
    CALL update_individual_spectral_state_machine
    
    ; Apply state-based spectral behaviors
    CALL apply_spectral_state_based_behaviors
    
    ; Check for dimensional state transitions
    CALL check_spectral_state_transitions
    
next_spectral_state_machine:
    ; Move to next spectral entity data
    LD A, $08           ; 8 bytes per entity
    ADD A, E
    LD E, A
    JR NC, no_spectral_data_carry
    INC D
no_spectral_data_carry:
    
    ; Move to next state data
    LD A, $08           ; 8 bytes per state
    ADD A, L
    LD L, A
    JR NC, no_spectral_state_carry
    INC H
no_spectral_state_carry:
    
    DJNZ update_spectral_state_machines_loop
    
skip_spectral_behavior_update:
    RET

; Update individual spectral state machine
update_individual_spectral_state_machine:
    ; HL points to state data, DE points to entity data
    ; Decrement state timer
    INC HL
    INC HL              ; Point to state timer
    DEC (HL)            ; Decrement timer
    JR NZ, spectral_state_timer_active
    
    ; State timer expired - trigger state transition
    CALL trigger_spectral_state_transition
    
spectral_state_timer_active:
    ; Update state-specific behavior
    DEC HL
    DEC HL              ; Point back to current state
    LD A, (HL)          ; Load current state
    
    CP $00              ; DORMANT state
    JR Z, process_dormant_spectral_state
    CP $01              ; DRIFT state
    JR Z, process_drift_spectral_state
    CP $02              ; SEEK state
    JR Z, process_seek_spectral_state
    CP $03              ; ENGAGE state
    JR Z, process_engage_spectral_state
    CP $04              ; RETREAT state
    JR Z, process_retreat_spectral_state
    CP $05              ; PHASE state
    JR Z, process_phase_spectral_state
    
    ; Unknown state - default to drift
    LD (HL), $01        ; Set drift state
    
process_dormant_spectral_state:
    CALL process_dormant_spectral_behavior
    RET
    
process_drift_spectral_state:
    CALL process_drift_spectral_behavior
    RET
    
process_seek_spectral_state:
    CALL process_seek_spectral_behavior
    RET
    
process_engage_spectral_state:
    CALL process_engage_spectral_behavior
    RET
    
process_retreat_spectral_state:
    CALL process_retreat_spectral_behavior
    RET
    
process_phase_spectral_state:
    CALL process_phase_spectral_behavior
    RET

; Process drift spectral behavior with dimensional movement
process_drift_spectral_behavior:
    ; DE points to entity data, HL points to state data
    ; Implement dimensional drift pattern
    PUSH HL             ; Save state pointer
    
    ; Get entity position
    INC DE              ; Point to X position
    LD A, (DE)          ; Load X position
    LD C, A             ; Save in C
    INC DE              ; Point to Y position
    LD A, (DE)          ; Load Y position
    LD B, A             ; Save in B
    
    ; Get drift pattern from behavioral memory
    LD A, $05
    ADD A, E            ; Point to behavioral memory
    LD E, A
    JR NC, no_drift_carry
    INC D
no_drift_carry:
    LD A, (DE)          ; Load behavioral memory
    AND $07             ; Get drift direction (0-7)
    
    ; Apply dimensional drift movement
    CP $00              ; North drift
    JR Z, drift_north
    CP $01              ; Northeast drift
    JR Z, drift_northeast
    CP $02              ; East drift
    JR Z, drift_east
    CP $03              ; Southeast drift
    JR Z, drift_southeast
    CP $04              ; South drift
    JR Z, drift_south
    CP $05              ; Southwest drift
    JR Z, drift_southwest
    CP $06              ; West drift
    JR Z, drift_west
    CP $07              ; Northwest drift
    JR Z, drift_northwest
    
drift_north:
    DEC B               ; Move up
    JR P, store_drift_position
    LD B, $00           ; Clamp to boundary
    ; Change direction
    LD A, (DE)
    AND $F8             ; Clear direction bits
    OR $04              ; Set south direction
    LD (DE), A
    LD B, $17           ; Bottom boundary
    JR store_drift_position
    
drift_northeast:
    DEC B               ; Move up
    INC C               ; Move right
    JR P, check_drift_right_boundary
    LD B, $00           ; Clamp Y
check_drift_right_boundary:
    LD A, C
    CP $20              ; Check right boundary
    JR C, store_drift_position
    LD C, $1F           ; Clamp X
    ; Change direction to northwest
    LD A, (DE)
    AND $F8
    OR $07              ; Set northwest direction
    LD (DE), A
    JR store_drift_position
    
drift_east:
    INC C               ; Move right
    LD A, C
    CP $20              ; Check right boundary
    JR C, store_drift_position
    LD C, $1F           ; Clamp to boundary
    ; Change direction to west
    LD A, (DE)
    AND $F8
    OR $06              ; Set west direction
    LD (DE), A
    LD C, $00           ; Left boundary
    JR store_drift_position
    
drift_southeast:
    INC B               ; Move down
    INC C               ; Move right
    LD A, B
    CP $18              ; Check bottom boundary
    JR C, check_drift_southeast_right
    LD B, $17           ; Clamp Y
check_drift_southeast_right:
    LD A, C
    CP $20              ; Check right boundary
    JR C, store_drift_position
    LD C, $1F           ; Clamp X
    ; Change direction to southwest
    LD A, (DE)
    AND $F8
    OR $05              ; Set southwest direction
    LD (DE), A
    JR store_drift_position
    
drift_south:
    INC B               ; Move down
    LD A, B
    CP $18              ; Check bottom boundary
    JR C, store_drift_position
    LD B, $17           ; Clamp to boundary
    ; Change direction to north
    LD A, (DE)
    AND $F8
    OR $00              ; Set north direction
    LD (DE), A
    LD B, $00           ; Top boundary
    JR store_drift_position
    
drift_southwest:
    INC B               ; Move down
    DEC C               ; Move left
    LD A, B
    CP $18              ; Check bottom boundary
    JR C, check_drift_southwest_left
    LD B, $17           ; Clamp Y
check_drift_southwest_left:
    LD A, C
    CP $00              ; Check left boundary
    JR Z, change_drift_southeast
    JR P, store_drift_position
change_drift_southeast:
    LD C, $00           ; Clamp X
    ; Change direction to southeast
    LD A, (DE)
    AND $F8
    OR $03              ; Set southeast direction
    LD (DE), A
    JR store_drift_position
    
drift_west:
    DEC C               ; Move left
    JR P, store_drift_position
    LD C, $00           ; Clamp to boundary
    ; Change direction to east
    LD A, (DE)
    AND $F8
    OR $02              ; Set east direction
    LD (DE), A
    LD C, $1F           ; Right boundary
    JR store_drift_position
    
drift_northwest:
    DEC B               ; Move up
    DEC C               ; Move left
    JR P, check_drift_northwest_left
    LD B, $00           ; Clamp Y
check_drift_northwest_left:
    LD A, C
    CP $00
    JR Z, change_drift_northeast
    JR P, store_drift_position
change_drift_northeast:
    LD C, $00           ; Clamp X
    ; Change direction to northeast
    LD A, (DE)
    AND $F8
    OR $01              ; Set northeast direction
    LD (DE), A
    
store_drift_position:
    ; Store new position back to entity data
    DEC DE              ; Point back to Y position
    LD (DE), B          ; Store Y
    DEC DE              ; Point to X position
    LD (DE), C          ; Store X
    
    POP HL              ; Restore state pointer
    RET

; Process seek spectral behavior with navigator tracking
process_seek_spectral_behavior:
    ; DE points to entity data, HL points to state data
    ; Calculate distance to navigator for dimensional seeking
    PUSH HL             ; Save state pointer
    
    ; Get entity position
    INC DE              ; Point to X position
    LD A, (DE)          ; Load entity X
    LD C, A             ; Save in C
    INC DE              ; Point to Y position
    LD A, (DE)          ; Load entity Y
    LD B, A             ; Save in B
    
    ; Calculate distance to navigator
    LD HL, (navigator_x) ; Load navigator position
    LD A, L             ; Navigator X
    SUB C               ; Calculate X distance
    JR P, seek_distance_x_positive
    NEG                 ; Make positive
seek_distance_x_positive:
    LD (temp_seek_distance_x), A
    
    LD A, H             ; Navigator Y
    SUB B               ; Calculate Y distance
    JR P, seek_distance_y_positive
    NEG                 ; Make positive
seek_distance_y_positive:
    LD (temp_seek_distance_y), A
    
    ; Move toward navigator intelligently
    LD A, L             ; Navigator X
    CP C                ; Compare with entity X
    JR C, seek_move_left
    JR Z, seek_check_y_movement
    
    ; Move right toward navigator
    INC C
    JR seek_check_y_movement
    
seek_move_left:
    DEC C
    
seek_check_y_movement:
    LD A, H             ; Navigator Y
    CP B                ; Compare with entity Y
    JR C, seek_move_up
    JR Z, seek_movement_complete
    
    ; Move down toward navigator
    INC B
    JR seek_movement_complete
    
seek_move_up:
    DEC B
    
seek_movement_complete:
    ; Store new position
    LD (DE), B          ; Store Y
    DEC DE
    LD (DE), C          ; Store X
    
    ; Check if close enough to engage
    LD A, (temp_seek_distance_x)
    ADD A, (temp_seek_distance_y)
    CP $03              ; Engage range
    JR NC, seek_behavior_complete
    
    ; Close enough - transition to engage state
    POP HL              ; Restore state pointer
    LD (HL), $03        ; ENGAGE state
    INC HL
    INC HL              ; Point to state timer
    LD (HL), $1E        ; Engage duration: 30 frames
    RET
    
seek_behavior_complete:
    POP HL              ; Restore state pointer
    RET

; Process dimensional decision systems for adaptive AI
process_dimensional_decision_systems:
    ; Increment decision evaluation counter
    LD A, (decision_evaluation_counter)
    INC A
    LD (decision_evaluation_counter), A
    
    LD B, A             ; Save counter
    LD A, (decision_evaluation_frequency)
    CP B                ; Time for decision evaluation?
    JR NZ, skip_dimensional_decision_evaluation
    
    ; Reset counter and evaluate decisions
    LD A, $00
    LD (decision_evaluation_counter), A
    
    ; Process decision systems for all active spectral entities
    LD B, $06           ; Check 6 spectral entities
    LD HL, spectral_entity_data
    
dimensional_decision_loop:
    ; Check if spectral entity is active and has decision capability
    LD A, (HL)          ; Load entity active flag
    AND A               ; Check if active
    JR Z, next_dimensional_decision_entity
    
    ; Evaluate decision system for spectral entity
    CALL evaluate_spectral_decision_system
    
next_dimensional_decision_entity:
    ; Move to next entity data
    LD A, $08           ; 8 bytes per entity
    ADD A, L
    LD L, A
    JR NC, no_dimensional_decision_carry
    INC H
no_dimensional_decision_carry:
    
    DJNZ dimensional_decision_loop
    
skip_dimensional_decision_evaluation:
    RET

; Execute adaptive spectral behaviors based on navigator patterns
execute_adaptive_spectral_behaviors:
    ; Track navigator behavior patterns
    CALL track_navigator_behavior_patterns
    
    ; Adjust spectral behaviors based on patterns
    CALL adjust_spectral_behaviors_to_patterns
    
    ; Update adaptive intelligence strength
    CALL update_adaptive_intelligence_strength
    
    RET

; Track navigator behavior patterns for adaptive AI
track_navigator_behavior_patterns:
    ; Analyze navigator movement patterns
    LD BC, (navigator_x)        ; Load current navigator position
    LD HL, (previous_navigator_position)
    
    ; Calculate movement delta
    LD A, C             ; Current X
    SUB L               ; Previous X
    JR P, navigator_x_movement_positive
    NEG                 ; Make positive
navigator_x_movement_positive:
    LD L, A             ; Save X movement
    
    LD A, B             ; Current Y
    SUB H               ; Previous Y
    JR P, navigator_y_movement_positive
    NEG                 ; Make positive
navigator_y_movement_positive:
    ADD A, L            ; Total movement
    
    ; Update movement total
    LD HL, navigator_movement_total
    ADD A, (HL)
    LD (HL), A
    
    ; Track navigator exploration pattern
    LD A, (navigator_exploration_counter)
    INC A
    LD (navigator_exploration_counter), A
    CP $3C              ; Update every second (60 frames)
    JR NZ, exploration_tracking_complete
    
    ; Calculate exploration pattern
    LD A, $00
    LD (navigator_exploration_counter), A
    LD A, (navigator_movement_total)
    SRL A               ; Divide by 2 for average
    LD (navigator_exploration_pattern), A
    LD A, $00
    LD (navigator_movement_total), A
    
exploration_tracking_complete:
    ; Store current position for next frame
    LD BC, (navigator_x)
    LD (previous_navigator_position), BC
    
    RET

; Update behavioral dimensional effects with intelligent feedback
update_behavioral_dimensional_effects:
    ; Update spectral state indicators
    CALL update_spectral_state_indicators
    
    ; Update AI decision visualizations
    CALL update_dimensional_ai_visualizations
    
    ; Update adaptive behavior feedback
    CALL update_adaptive_dimensional_feedback
    
    ; Update intelligence level displays
    CALL update_dimensional_intelligence_displays
    
    RET

; Update spectral state indicators for visual feedback
update_spectral_state_indicators:
    ; Display spectral states using attribute colors
    LD B, $06           ; Check 6 spectral entities
    LD HL, spectral_entity_data
    LD DE, spectral_current_state
    
spectral_state_indicator_loop:
    ; Check if spectral entity is active
    LD A, (HL)          ; Load entity active flag
    AND A               ; Check if active
    JR Z, next_spectral_state_indicator
    
    ; Get entity screen position
    INC HL              ; Point to X position
    LD A, (HL)          ; Load X
    LD (temp_spectral_screen_x), A
    INC HL              ; Point to Y position
    LD A, (HL)          ; Load Y
    LD (temp_spectral_screen_y), A
    DEC HL              ; Point back to X
    DEC HL              ; Point back to active flag
    
    ; Get state color
    LD A, (DE)          ; Load current state
    PUSH HL
    LD HL, spectral_state_colors
    ADD A, L            ; Add state offset
    LD L, A
    JR NC, no_state_color_carry
    INC H
no_state_color_carry:
    LD A, (HL)          ; Load state color
    LD (temp_spectral_state_color), A
    POP HL
    
    ; Display state indicator
    CALL display_spectral_state_indicator
    
next_spectral_state_indicator:
    ; Move to next entity data
    LD A, $08           ; 8 bytes per entity
    ADD A, L
    LD L, A
    JR NC, no_spectral_indicator_carry
    INC H
no_spectral_indicator_carry:
    
    ; Move to next state data
    INC DE
    
    DJNZ spectral_state_indicator_loop
    
    RET

; Advanced behavioral data and variables
global_spectral_behavior_state:     DEFB 0  ; Global spectral behavior state
behavior_update_counter:            DEFB 0  ; Behavior update counter
dimensional_intelligence_level:     DEFB 0  ; Current dimensional intelligence level
adaptive_response_level:            DEFB 0  ; Adaptive response level
behavior_timing_counter:            DEFB 0  ; Behavior timing counter
last_behavior_update:               DEFB 0  ; Last behavior update frame
spectral_response_time:             DEFB 0  ; Spectral response time
behavior_update_frequency:          DEFB 6  ; Behavior update frequency
base_intelligence_level:            DEFB 2  ; Base intelligence level
max_intelligence_level:             DEFB 6  ; Maximum intelligence level

; Spectral state machine data (6 entities Ă— 8 bytes each)
spectral_current_state:             DEFS 6  ; Current state for each spectral
spectral_previous_state:            DEFS 6  ; Previous state for each spectral
spectral_state_timer:               DEFS 6  ; State timer for each spectral
spectral_behavior_flags:            DEFS 6  ; Behavior flags for each spectral
spectral_behavior_type:             DEFS 6  ; Behavior type for each spectral
spectral_state_duration:            DEFS 6  ; State duration for each spectral
spectral_behavioral_memory:         DEFS 6  ; Behavioral memory for each spectral
spectral_dimensional_memory:        DEFS 6  ; Dimensional memory for each spectral

; Spectral entity data (6 entities Ă— 8 bytes each)
spectral_entity_data:               DEFS 48 ; Complete spectral entity data

; Decision system variables
decision_tree_depth:                DEFB 3  ; Decision tree depth
max_decision_nodes:                 DEFB 6  ; Maximum decision nodes
decision_evaluation_frequency:      DEFB 4  ; Decision evaluation frequency
current_decision_node:              DEFB 0  ; Current decision node
decision_evaluation_counter:        DEFB 0  ; Decision evaluation counter
last_decision_result:               DEFB 0  ; Last decision result
dimensional_learning_factor:        DEFB 0  ; Dimensional learning factor

; Navigator behavior tracking
navigator_exploration_pattern:      DEFB 0  ; Navigator exploration pattern
navigator_evasion_tendency:         DEFB 0  ; Navigator evasion tendency
navigator_interaction_frequency:    DEFB 0  ; Navigator interaction frequency
adaptive_intelligence_strength:     DEFB 0  ; Adaptive intelligence strength
navigator_behavior_history:         DEFS 12 ; Navigator behavior history
adaptive_intelligence_history:      DEFS 12 ; Adaptive intelligence history
adaptive_learning_rate:             DEFB 32 ; Adaptive learning rate
response_sensitivity:               DEFB 8  ; Response sensitivity

; Navigator pattern tracking
previous_navigator_position:        DEFW 0  ; Previous navigator position
navigator_movement_total:           DEFB 0  ; Navigator movement total
navigator_exploration_counter:      DEFB 0  ; Navigator exploration counter

; Temporary calculation variables
temp_seek_distance_x:               DEFB 0  ; Temporary seek distance X
temp_seek_distance_y:               DEFB 0  ; Temporary seek distance Y
temp_spectral_screen_x:             DEFB 0  ; Temporary spectral screen X
temp_spectral_screen_y:             DEFB 0  ; Temporary spectral screen Y
temp_spectral_state_color:          DEFB 0  ; Temporary spectral state color

; State color table for visual feedback
spectral_state_colors:
    DEFB $40            ; DORMANT - black on red
    DEFB $42            ; DRIFT - green on red
    DEFB $43            ; SEEK - cyan on red
    DEFB $44            ; ENGAGE - yellow on red
    DEFB $45            ; RETREAT - magenta on red
    DEFB $46            ; PHASE - blue on red

; Maze pattern data
behavioral_maze_pattern:            DEFS 768 ; Behavioral maze pattern data

Your Advanced Behaviors Achievement

Congratulations! You’ve transformed your Prism Quest into a sophisticated dimensional experience with intelligent AI behaviors that adapt and respond to your navigation patterns! Your behavior enhancement adds:

Sophisticated Spectral AI Systems

  • Dimensional State Machines: Professional spectral behavior management with DORMANT, DRIFT, SEEK, ENGAGE, RETREAT, and PHASE states
  • Adaptive Decision Systems: Intelligent branching logic for dimensional responses based on navigator behavior
  • Pattern Recognition: Spectral AI that learns from navigator patterns and adjusts dimensional strategies
  • Behavioral Memory: Spectrals remember past dimensional interactions and adapt their approaches

ZX Spectrum-Optimized AI Programming

  • Memory-Efficient Design: Complex AI behaviors within 48KB constraints using creative optimization techniques
  • Performance-Conscious Logic: Smooth AI processing maintaining 25fps gameplay with intelligent update scheduling
  • Creative State Management: Professional state transition systems optimized for Spectrum’s architectural limitations
  • Adaptive Intelligence: Dynamic difficulty adjustment based on navigator exploration patterns and dimensional skill

Dynamic Dimensional Interactions

  • Intelligent Dimensional Movement: Spectrals that navigate dimensional space with purpose and strategy
  • Contextual Responses: Entities that respond intelligently to dimensional conditions and navigator behavior
  • Visual Intelligence Feedback: Real-time AI state indicators showing spectral intelligence and decision-making
  • Dimensional Adaptation: AI systems that evolve and improve through dimensional encounters

Technical Mastery Gained

Advanced Programming Concepts

You’ve learned creative AI and conditional programming techniques:

  • State Machine Architecture: Robust behavioral systems adapted for ZX Spectrum’s memory constraints
  • Decision System Implementation: Efficient branching logic for complex AI within hardware limitations
  • Adaptive Algorithms: Learning systems that adjust behavior using creative memory management
  • Constraint Innovation: Professional AI development within Spectrum’s unique architectural challenges

Creative Game Development

Your advanced behavior system demonstrates innovative Spectrum programming:

  • Behavioral Complexity: Multi-layered AI systems creating engaging dimensional gameplay within constraints
  • Performance Innovation: Intelligent update scheduling maintaining smooth gameplay with complex AI
  • Player Engagement: Adaptive systems that scale dimensional challenge and maintain exploration interest
  • Authentic Implementation: Creative ZX Spectrum techniques pushing the boundaries of 8-bit AI

Take It Further

Your Advanced Entity Behaviors foundation enables incredible future enhancements:

  • Swarm Intelligence: Coordinated spectral behaviors and collective dimensional decision-making
  • Dimensional Personality Systems: Individual spectral characteristics affecting dimensional interactions
  • Advanced Dimensional Tactics: Sophisticated AI strategies using dimensional space manipulation
  • Learning Networks: Spectral entities that share knowledge and improve collective intelligence
  • Dynamic Dimensional Narrative: AI-driven dimensional story elements responding to navigator choices

You’ve mastered conditional programming and AI development through creative ZX Spectrum techniques, creating the foundation for the advanced dimensional systems coming in future lessons!

Your precision control and fluid navigation from Lessons 10-12 now have intelligent dimensional opposition - every spectral encounter feels dynamic and challenging, transforming your Prism Quest into an engaging experience that adapts and evolves with navigator skill within the unique constraints of the ZX Spectrum!