File size: 23,850 Bytes
1ecc382
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94e4b64
 
 
 
 
 
 
 
 
1ecc382
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
/**
 * Multi-Piclet Battle Engine
 * Supports up to 4 Piclets on the field at once (2 per side)
 * Extends the single-Piclet battle system with party management and multi-targeting
 */

import {
  MultiBattleState,
  MultiBattleAction,
  MultiMoveAction,
  MultiSwitchAction,
  TurnActions,
  PicletTarget,
  BattleSide,
  FieldPosition,
  MultiBattleConfig,
  ActionPriority,
  VictoryCondition,
  MultiEffectTarget
} from './multi-piclet-types';

import {
  BattlePiclet,
  PicletDefinition,
  BattleEffect,
  Move,
  BaseStats,
  StatusEffect
} from './types';

import { getEffectivenessMultiplier } from '../types/picletTypes';

export class MultiBattleEngine {
  private state: MultiBattleState;
  private victoryCondition: VictoryCondition;

  constructor(config: MultiBattleConfig, victoryCondition: VictoryCondition = { type: 'allFainted' }) {
    this.victoryCondition = victoryCondition;
    this.state = this.initializeBattle(config);
    this.log('Multi-Piclet battle started!');
    this.logActivePiclets();
  }

  private initializeBattle(config: MultiBattleConfig): MultiBattleState {
    // Initialize active Piclets from parties
    const playerActive: Array<BattlePiclet | null> = [null, null];
    const opponentActive: Array<BattlePiclet | null> = [null, null];

    // Set up initial active Piclets
    for (let i = 0; i < config.playerActiveCount && i < config.playerParty.length; i++) {
      playerActive[i] = this.createBattlePiclet(config.playerParty[i], 50);
    }

    for (let i = 0; i < config.opponentActiveCount && i < config.opponentParty.length; i++) {
      opponentActive[i] = this.createBattlePiclet(config.opponentParty[i], 50);
    }

    return {
      turn: 1,
      phase: 'selection',
      activePiclets: {
        player: playerActive,
        opponent: opponentActive
      },
      parties: {
        player: config.playerParty,
        opponent: config.opponentParty
      },
      fieldEffects: [],
      log: [],
      winner: undefined
    };
  }

  private createBattlePiclet(definition: PicletDefinition, level: number): BattlePiclet {
    // Same logic as original BattleEngine
    const statMultiplier = 1 + (level - 50) * 0.02;
    
    const hp = Math.floor(definition.baseStats.hp * statMultiplier);
    const attack = Math.floor(definition.baseStats.attack * statMultiplier);
    const defense = Math.floor(definition.baseStats.defense * statMultiplier);
    const speed = Math.floor(definition.baseStats.speed * statMultiplier);

    return {
      definition,
      currentHp: hp,
      maxHp: hp,
      level,
      attack,
      defense,
      speed,
      accuracy: 100,
      statusEffects: [],
      moves: definition.movepool.slice(0, 4).map(move => ({
        move,
        currentPP: move.pp
      })),
      statModifiers: {},
      temporaryEffects: []
    };
  }

  public getState(): MultiBattleState {
    return JSON.parse(JSON.stringify(this.state));
  }

  public isGameOver(): boolean {
    return this.state.phase === 'ended';
  }

  public getWinner(): 'player' | 'opponent' | 'draw' | undefined {
    return this.state.winner;
  }

  public getActivePiclets(): { player: BattlePiclet[], opponent: BattlePiclet[] } {
    return {
      player: this.state.activePiclets.player.filter(p => p !== null) as BattlePiclet[],
      opponent: this.state.activePiclets.opponent.filter(p => p !== null) as BattlePiclet[]
    };
  }

  public getAvailableSwitches(side: BattleSide): Array<{ partyIndex: number, piclet: PicletDefinition }> {
    const available: Array<{ partyIndex: number, piclet: PicletDefinition }> = [];
    const activePicletNames = this.state.activePiclets[side]
      .filter(p => p !== null)
      .map(p => p!.definition.name);

    this.state.parties[side].forEach((partyMember, index) => {
      if (!activePicletNames.includes(partyMember.name)) {
        available.push({ partyIndex: index, piclet: partyMember });
      }
    });

    return available;
  }

  public getValidActions(side: BattleSide): MultiBattleAction[] {
    const actions: MultiBattleAction[] = [];
    const activePiclets = this.state.activePiclets[side];

    // Add move actions for each active Piclet
    activePiclets.forEach((piclet, position) => {
      if (piclet) {
        piclet.moves.forEach((moveData, moveIndex) => {
          if (moveData.currentPP > 0) {
            actions.push({
              type: 'move',
              side,
              position: position as FieldPosition,
              moveIndex
            });
          }
        });
      }
    });

    // Add switch actions for empty slots or when Piclets can be switched
    this.state.parties[side].forEach((partyMember, partyIndex) => {
      // Check if this party member is not currently active
      const isActive = activePiclets.some(active => 
        active?.definition.name === partyMember.name
      );
      
      if (!isActive) {
        // Can switch into any position that has a Piclet (replacement) or empty slot
        activePiclets.forEach((slot, position) => {
          actions.push({
            type: 'switch',
            side,
            position: position as FieldPosition,
            partyIndex
          });
        });
      }
    });

    return actions;
  }

  public executeTurn(turnActions: TurnActions): void {
    if (this.state.phase !== 'selection') {
      throw new Error('Cannot execute turn - battle is not in selection phase');
    }

    this.state.phase = 'execution';
    this.log(`Turn ${this.state.turn} - Executing actions`);

    // Determine action order based on priority and speed
    const allActions = this.determineActionOrder(turnActions);

    // Execute actions in order
    for (const actionPriority of allActions) {
      if (this.state.phase === 'ended') break;
      this.executeAction(actionPriority.action, actionPriority.side, actionPriority.position);
    }

    // End of turn processing
    this.processTurnEnd();

    // Check for battle end
    this.checkBattleEnd();

    if (this.state.phase !== 'ended') {
      this.state.turn++;
      this.state.phase = 'selection';
    }
  }

  private determineActionOrder(turnActions: TurnActions): ActionPriority[] {
    const allActionPriorities: ActionPriority[] = [];

    // Process player actions
    turnActions.player.forEach(action => {
      const priority = this.getActionPriority(action);
      const piclet = this.state.activePiclets.player[action.position];
      allActionPriorities.push({
        action,
        side: 'player',
        position: action.position,
        priority,
        speed: piclet?.speed || 0,
        randomTiebreaker: Math.random()
      });
    });

    // Process opponent actions
    turnActions.opponent.forEach(action => {
      const priority = this.getActionPriority(action);
      const piclet = this.state.activePiclets.opponent[action.position];
      allActionPriorities.push({
        action,
        side: 'opponent',
        position: action.position,
        priority,
        speed: piclet?.speed || 0,
        randomTiebreaker: Math.random()
      });
    });

    // Sort by priority (higher first), then speed (higher first), then random
    return allActionPriorities.sort((a, b) => {
      if (a.priority !== b.priority) return b.priority - a.priority;
      if (a.speed !== b.speed) return b.speed - a.speed;
      return a.randomTiebreaker - b.randomTiebreaker;
    });
  }

  private getActionPriority(action: MultiBattleAction): number {
    if (action.type === 'switch') return 6; // Switches have highest priority
    
    const piclet = this.state.activePiclets[action.side][action.position];
    if (!piclet) return 0;
    
    const move = piclet.moves[action.moveIndex]?.move;
    return move?.priority || 0;
  }

  private executeAction(action: MultiBattleAction, side: BattleSide, position: FieldPosition): void {
    const piclet = this.state.activePiclets[side][position];
    if (!piclet) return;

    if (action.type === 'move') {
      this.executeMove(action as MultiMoveAction, side, position);
    } else if (action.type === 'switch') {
      this.executeSwitch(action as MultiSwitchAction, side, position);
    }
  }

  private executeMove(action: MultiMoveAction, side: BattleSide, position: FieldPosition): void {
    const attacker = this.state.activePiclets[side][position];
    if (!attacker) return;

    const moveData = attacker.moves[action.moveIndex];
    if (!moveData || moveData.currentPP <= 0) {
      this.log(`${attacker.definition.name} has no PP left for that move!`);
      return;
    }

    const move = moveData.move;
    this.log(`${attacker.definition.name} used ${move.name}!`);

    // Consume PP
    moveData.currentPP--;

    // Check if move hits (simplified for now)
    if (!this.checkMoveHits(move, attacker)) {
      this.log(`${attacker.definition.name}'s attack missed!`);
      return;
    }

    // Process effects for each target
    const targets = this.resolveTargets(move, side, position, action.targets);
    
    for (const effect of move.effects) {
      this.processMultiEffect(effect, attacker, targets, move);
    }
  }

  private executeSwitch(action: MultiSwitchAction, side: BattleSide, position: FieldPosition): void {
    const currentPiclet = this.state.activePiclets[side][position];
    const newPiclet = this.state.parties[side][action.partyIndex];
    
    if (!newPiclet) return;

    // Create battle instance of new Piclet
    const battlePiclet = this.createBattlePiclet(newPiclet, 50);
    
    // Switch out current Piclet (if any)
    if (currentPiclet) {
      this.log(`${currentPiclet.definition.name} switched out!`);
      // Trigger switch-out abilities here
    }

    // Switch in new Piclet
    this.state.activePiclets[side][position] = battlePiclet;
    this.log(`${battlePiclet.definition.name} switched in!`);
    
    // Trigger switch-in abilities here
    this.processAbilityTrigger(battlePiclet, 'onSwitchIn');
  }

  private resolveTargets(move: Move, attackerSide: BattleSide, attackerPosition: FieldPosition, targetOverride?: any): BattlePiclet[] {
    const targets: BattlePiclet[] = [];
    const attacker = this.state.activePiclets[attackerSide][attackerPosition];
    if (!attacker) return targets;

    // Check if move effects specify targets, default to opponent
    const effectTargets = move.effects.map(e => (e as any).target).filter(t => t);
    const primaryTarget = effectTargets[0] || 'opponent';

    switch (primaryTarget) {
      case 'self':
        targets.push(attacker);
        break;
        
      case 'opponent':
        // Target first available opponent (can be enhanced for player choice)
        const opponentSide = attackerSide === 'player' ? 'opponent' : 'player';
        const opponents = this.state.activePiclets[opponentSide].filter(p => p !== null) as BattlePiclet[];
        if (opponents.length > 0) {
          targets.push(opponents[0]);
        }
        break;
        
      case 'allOpponents':
        const oppSide = attackerSide === 'player' ? 'opponent' : 'player';
        const allOpponents = this.state.activePiclets[oppSide].filter(p => p !== null) as BattlePiclet[];
        targets.push(...allOpponents);
        break;
        
      case 'ally':
        // Target ally (for double battles)
        const allies = this.state.activePiclets[attackerSide].filter(p => p !== null && p !== attacker) as BattlePiclet[];
        if (allies.length > 0) {
          targets.push(allies[0]);
        }
        break;
        
      case 'allAllies':
        const allAllies = this.state.activePiclets[attackerSide].filter(p => p !== null && p !== attacker) as BattlePiclet[];
        targets.push(...allAllies);
        break;
        
      case 'all':
        // Target all active Piclets
        for (const side of ['player', 'opponent'] as BattleSide[]) {
          const activePiclets = this.state.activePiclets[side].filter(p => p !== null) as BattlePiclet[];
          targets.push(...activePiclets);
        }
        break;
        
      case 'random':
        // Target random active Piclet
        const allActive: BattlePiclet[] = [];
        for (const side of ['player', 'opponent'] as BattleSide[]) {
          const activePiclets = this.state.activePiclets[side].filter(p => p !== null) as BattlePiclet[];
          allActive.push(...activePiclets);
        }
        if (allActive.length > 0) {
          const randomIndex = Math.floor(Math.random() * allActive.length);
          targets.push(allActive[randomIndex]);
        }
        break;
        
      case 'weakest':
        // Target Piclet with lowest HP percentage
        const allActivePiclets: BattlePiclet[] = [];
        for (const side of ['player', 'opponent'] as BattleSide[]) {
          const activePiclets = this.state.activePiclets[side].filter(p => p !== null) as BattlePiclet[];
          allActivePiclets.push(...activePiclets);
        }
        if (allActivePiclets.length > 0) {
          const weakest = allActivePiclets.reduce((weak, current) => 
            (current.currentHp / current.maxHp) < (weak.currentHp / weak.maxHp) ? current : weak
          );
          targets.push(weakest);
        }
        break;
        
      case 'strongest':
        // Target Piclet with highest HP percentage
        const allActiveForStrongest: BattlePiclet[] = [];
        for (const side of ['player', 'opponent'] as BattleSide[]) {
          const activePiclets = this.state.activePiclets[side].filter(p => p !== null) as BattlePiclet[];
          allActiveForStrongest.push(...activePiclets);
        }
        if (allActiveForStrongest.length > 0) {
          const strongest = allActiveForStrongest.reduce((strong, current) => 
            (current.currentHp / current.maxHp) > (strong.currentHp / strong.maxHp) ? current : strong
          );
          targets.push(strongest);
        }
        break;
        
      default:
        // Fallback to first opponent
        const defaultOpponentSide = attackerSide === 'player' ? 'opponent' : 'player';
        const defaultOpponents = this.state.activePiclets[defaultOpponentSide].filter(p => p !== null) as BattlePiclet[];
        if (defaultOpponents.length > 0) {
          targets.push(defaultOpponents[0]);
        }
    }

    return targets;
  }

  private processMultiEffect(effect: BattleEffect, attacker: BattlePiclet, targets: BattlePiclet[], move: Move): void {
    // Process effect on each target
    for (const target of targets) {
      this.processEffect(effect, attacker, target, move);
    }
  }

  private processEffect(effect: BattleEffect, attacker: BattlePiclet, target: BattlePiclet, move: Move): void {
    // Check condition
    if (effect.condition && !this.checkCondition(effect.condition, attacker, target)) {
      return;
    }

    switch (effect.type) {
      case 'damage':
        this.processDamageEffect(effect, attacker, target, move);
        break;
      case 'heal':
        this.processHealEffect(effect, target);
        break;
      case 'modifyStats':
        this.processModifyStatsEffect(effect, target);
        break;
      case 'applyStatus':
        this.processApplyStatusEffect(effect, target);
        break;
      // Add other effect types as needed
      default:
        this.log(`Effect ${effect.type} not implemented in multi-battle yet`);
    }
  }

  // Simplified effect processors (can be expanded)
  private processDamageEffect(effect: any, attacker: BattlePiclet, target: BattlePiclet, move: Move): void {
    const damage = this.calculateDamage(attacker, target, move);
    target.currentHp = Math.max(0, target.currentHp - damage);
    this.log(`${target.definition.name} took ${damage} damage!`);
  }

  private processHealEffect(effect: any, target: BattlePiclet): void {
    const healAmount = Math.floor(target.maxHp * 0.5); // Simplified
    const oldHp = target.currentHp;
    target.currentHp = Math.min(target.maxHp, target.currentHp + healAmount);
    const actualHeal = target.currentHp - oldHp;
    
    if (actualHeal > 0) {
      this.log(`${target.definition.name} recovered ${actualHeal} HP!`);
    }
  }

  private processModifyStatsEffect(effect: any, target: BattlePiclet): void {
    // Simplified stat modification
    if (effect.stats?.attack === 'increase') {
      target.attack = Math.floor(target.attack * 1.25);
      this.log(`${target.definition.name}'s attack rose!`);
    }
  }

  private processApplyStatusEffect(effect: any, target: BattlePiclet): void {
    if (!target.statusEffects.includes(effect.status)) {
      target.statusEffects.push(effect.status);
      this.log(`${target.definition.name} was ${effect.status}ed!`);
    }
  }

  private calculateDamage(attacker: BattlePiclet, target: BattlePiclet, move: Move): number {
    // Simplified damage calculation
    const baseDamage = move.power || 50;
    const effectiveness = getEffectivenessMultiplier(
      move.type,
      target.definition.primaryType,
      target.definition.secondaryType
    );

    let damage = Math.floor((baseDamage * (attacker.attack / target.defense) * 0.5) + 10);
    damage = Math.floor(damage * effectiveness);
    
    return Math.max(1, damage);
  }

  private checkMoveHits(move: Move, attacker: BattlePiclet): boolean {
    return Math.random() * 100 < move.accuracy;
  }

  private checkCondition(condition: string, attacker: BattlePiclet, target: BattlePiclet): boolean {
    switch (condition) {
      case 'always':
        return true;
      case 'ifLowHp':
        return attacker.currentHp / attacker.maxHp < 0.25;
      default:
        return true;
    }
  }

  private processAbilityTrigger(piclet: BattlePiclet, trigger: string): void {
    // Process special ability triggers
    if (piclet.definition.specialAbility.triggers) {
      for (const abilityTrigger of piclet.definition.specialAbility.triggers) {
        if (abilityTrigger.event === trigger) {
          this.log(`${piclet.definition.name}'s ${piclet.definition.specialAbility.name} activated!`);
          // Process trigger effects
        }
      }
    }
  }

  private processTurnEnd(): void {
    // Process status effects for all active Piclets
    for (const side of ['player', 'opponent'] as BattleSide[]) {
      for (const piclet of this.state.activePiclets[side]) {
        if (piclet) {
          this.processStatusEffects(piclet);
          this.processTemporaryEffects(piclet);
        }
      }
    }

    // Process field effects
    this.processFieldEffects();

    // Handle fainted Piclets
    this.handleFaintedPiclets();
  }

  private handleFaintedPiclets(): void {
    for (const side of ['player', 'opponent'] as BattleSide[]) {
      for (let position = 0; position < this.state.activePiclets[side].length; position++) {
        const piclet = this.state.activePiclets[side][position];
        if (piclet && piclet.currentHp <= 0) {
          this.log(`${piclet.definition.name} fainted!`);
          
          // Remove fainted Piclet from active slot
          this.state.activePiclets[side][position] = null;
          
          // Trigger faint abilities
          this.processAbilityTrigger(piclet, 'onKO');
          
          // For now, we don't auto-switch reserves in this simplified implementation
          // In a full implementation, the player would choose a replacement
        }
      }
    }
  }

  private processStatusEffects(piclet: BattlePiclet): void {
    for (const status of piclet.statusEffects) {
      switch (status) {
        case 'burn':
        case 'poison':
          const damage = Math.floor(piclet.maxHp / 8);
          piclet.currentHp = Math.max(0, piclet.currentHp - damage);
          this.log(`${piclet.definition.name} hurt by ${status}!`);
          break;
      }
    }
  }

  private processTemporaryEffects(piclet: BattlePiclet): void {
    piclet.temporaryEffects = piclet.temporaryEffects.filter(effect => {
      effect.duration--;
      return effect.duration > 0;
    });
  }

  private processFieldEffects(): void {
    this.state.fieldEffects = this.state.fieldEffects.filter(effect => {
      effect.duration--;
      if (effect.duration <= 0) {
        this.log(`Field effect '${effect.name}' ended!`);
        return false;
      }
      return true;
    });
  }

  private checkBattleEnd(): void {
    const winner = this.determineWinner();
    if (winner) {
      this.state.winner = winner;
      this.state.phase = 'ended';
      this.log(`Battle ended! Winner: ${winner}`);
    }
  }

  private determineWinner(): 'player' | 'opponent' | 'draw' | null {
    // Count living active Piclets (not null and HP > 0)
    const playerActiveLiving = this.state.activePiclets.player.filter(p => p !== null && p.currentHp > 0);
    const opponentActiveLiving = this.state.activePiclets.opponent.filter(p => p !== null && p.currentHp > 0);
    
    // Check for healthy reserves
    const playerHealthyReserves = this.getHealthyReserves('player');
    const opponentHealthyReserves = this.getHealthyReserves('opponent');
    
    const playerHasUsablePiclets = playerActiveLiving.length > 0 || playerHealthyReserves.length > 0;
    const opponentHasUsablePiclets = opponentActiveLiving.length > 0 || opponentHealthyReserves.length > 0;

    // Check victory conditions based on type
    switch (this.victoryCondition.type) {
      case 'allFainted':
        if (!playerHasUsablePiclets) {
          if (!opponentHasUsablePiclets) {
            return 'draw';
          }
          return 'opponent';
        }
        if (!opponentHasUsablePiclets) {
          return 'player';
        }
        break;
        
      case 'custom':
        if (this.victoryCondition.customCheck) {
          return this.victoryCondition.customCheck(this.state);
        }
        break;
    }

    return null;
  }

  private getHealthyReserves(side: BattleSide): PicletDefinition[] {
    // Get party members that have never been used in battle
    // We need to track which party members have been on the field
    const usedPicletNames = new Set<string>();
    
    // Add currently active Piclets
    this.state.activePiclets[side].forEach(p => {
      if (p !== null) {
        usedPicletNames.add(p.definition.name);
      }
    });
    
    // For a full implementation, we would also track previously active Piclets that fainted
    // For now, we estimate by checking the initial setup - if there are more party members
    // than active slots, the rest are reserves
    const activeSlots = this.state.activePiclets[side].length;
    const initialActiveCount = Math.min(this.state.parties[side].length, activeSlots);
    
    // Mark the first N party members as "used" (they were initially active)
    for (let i = 0; i < initialActiveCount; i++) {
      if (this.state.parties[side][i]) {
        usedPicletNames.add(this.state.parties[side][i].name);
      }
    }
    
    return this.state.parties[side].filter(partyMember => 
      !usedPicletNames.has(partyMember.name)
    );
  }

  private logActivePiclets(): void {
    const playerActives = this.state.activePiclets.player.filter(p => p !== null) as BattlePiclet[];
    const opponentActives = this.state.activePiclets.opponent.filter(p => p !== null) as BattlePiclet[];
    
    this.log(`Player active: ${playerActives.map(p => p.definition.name).join(', ')}`);
    this.log(`Opponent active: ${opponentActives.map(p => p.definition.name).join(', ')}`);
  }

  private log(message: string): void {
    this.state.log.push(message);
  }

  public getLog(): string[] {
    // Strip battle prefixes from all log messages for display
    return this.state.log.map(message => this.stripBattlePrefixes(message));
  }
  
  private stripBattlePrefixes(message: string): string {
    // Remove player- and enemy- prefixes from messages
    return message
      .replace(/player-/g, '')
      .replace(/enemy-/g, '');
  }
}