File size: 5,739 Bytes
4c208f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Conversion utilities for transforming database types to battle engine types
 */

import type { PicletInstance, BattleMove } from '$lib/db/schema';
import type { PicletDefinition, Move, BaseStats, SpecialAbility } from '$lib/battle-engine/types';
import type { PicletStats, BattleEffect, AbilityTrigger } from '$lib/types';
import { PicletType, AttackType } from '$lib/types/picletTypes';

/**
 * Convert PicletInstance to PicletDefinition for battle engine use
 */
export function picletInstanceToBattleDefinition(instance: PicletInstance): PicletDefinition {
  // Convert base stats from instance level to definition level
  const baseStats: BaseStats = {
    hp: Math.floor(instance.baseHp / 2 - 25), // Reverse the conversion from monsterToPicletInstance
    attack: Math.floor((instance.baseAttack - 30) / 1.5), 
    defense: Math.floor((instance.baseDefense - 30) / 1.5),
    speed: Math.floor((instance.baseSpeed - 30) / 1.5)
  };

  // Convert simple moves to full battle moves
  const movepool: Move[] = instance.moves.map(move => convertBattleMoveToMove(move, instance.primaryType));

  // Create a basic special ability if none exists
  const specialAbility: SpecialAbility = {
    name: "Adaptive",
    description: "A basic ability that adapts to combat situations",
    triggers: [{
      event: "endOfTurn",
      effects: [{
        type: 'heal',
        target: 'self',
        amount: 'small',
        condition: 'ifLowHp'
      }]
    }]
  };

  // Determine tier based on BST (Base Stat Total)
  const bst = baseStats.hp + baseStats.attack + baseStats.defense + baseStats.speed;
  let tier: 'low' | 'medium' | 'high' | 'legendary';
  if (bst <= 300) tier = 'low';
  else if (bst <= 400) tier = 'medium';
  else if (bst <= 500) tier = 'high';
  else tier = 'legendary';

  return {
    name: instance.nickname || instance.typeId,
    description: instance.concept,
    tier,
    primaryType: convertPicletTypeToType(instance.primaryType),
    secondaryType: instance.secondaryType ? convertPicletTypeToType(instance.secondaryType) : undefined,
    baseStats,
    nature: instance.nature,
    specialAbility,
    movepool
  };
}

/**
 * Convert BattleMove to full Move for battle engine
 */
function convertBattleMoveToMove(battleMove: BattleMove, primaryType: PicletType): Move {
  // Create basic effects based on move properties
  const effects: any[] = [];
  
  if (battleMove.power > 0) {
    effects.push({
      type: 'damage',
      target: 'opponent',
      amount: battleMove.power >= 80 ? 'strong' : 
              battleMove.power >= 60 ? 'normal' : 'weak'
    });
  }

  // Add status or stat effects for non-damaging moves
  if (battleMove.power === 0) {
    effects.push({
      type: 'modifyStats',
      target: 'self',
      stats: { attack: 'increase' }
    });
  }

  return {
    name: battleMove.name,
    type: convertAttackTypeToType(battleMove.type),
    power: battleMove.power,
    accuracy: battleMove.accuracy,
    pp: battleMove.pp,
    priority: 0,
    flags: battleMove.power > 0 && battleMove.name.toLowerCase().includes('tackle') ? ['contact'] : [],
    effects
  };
}

/**
 * Convert PicletType to battle engine type
 */
function convertPicletTypeToType(picletType: PicletType): 'beast' | 'bug' | 'aquatic' | 'flora' | 'mineral' | 'space' | 'machina' | 'structure' | 'culture' | 'cuisine' {
  const typeMap: Record<PicletType, any> = {
    [PicletType.BEAST]: 'beast',
    [PicletType.BUG]: 'bug', 
    [PicletType.AQUATIC]: 'aquatic',
    [PicletType.FLORA]: 'flora',
    [PicletType.MINERAL]: 'mineral',
    [PicletType.SPACE]: 'space',
    [PicletType.MACHINA]: 'machina',
    [PicletType.STRUCTURE]: 'structure',
    [PicletType.CULTURE]: 'culture',
    [PicletType.CUISINE]: 'cuisine'
  };
  
  return typeMap[picletType] || 'beast';
}

/**
 * Convert AttackType to battle engine type
 */
function convertAttackTypeToType(attackType: AttackType): 'beast' | 'bug' | 'aquatic' | 'flora' | 'mineral' | 'space' | 'machina' | 'structure' | 'culture' | 'cuisine' | 'normal' {
  // AttackType and PicletType should align, but handle the NORMAL case
  if (attackType === AttackType.NORMAL) {
    return 'normal';
  }
  
  // Map other attack types to piclet types
  const typeMap: Record<string, any> = {
    'beast': 'beast',
    'bug': 'bug',
    'aquatic': 'aquatic', 
    'flora': 'flora',
    'mineral': 'mineral',
    'space': 'space',
    'machina': 'machina',
    'structure': 'structure',
    'culture': 'culture',
    'cuisine': 'cuisine'
  };
  
  return typeMap[attackType.toString()] || 'normal';
}

/**
 * Convert battle engine BattlePiclet back to PicletInstance for state updates
 */
export function battlePicletToInstance(battlePiclet: any, originalInstance: PicletInstance): PicletInstance {
  return {
    ...originalInstance,
    currentHp: battlePiclet.currentHp,
    maxHp: battlePiclet.maxHp,
    level: battlePiclet.level,
    attack: battlePiclet.attack,
    defense: battlePiclet.defense,
    speed: battlePiclet.speed,
    // Update moves with current PP
    moves: battlePiclet.moves.map((moveData: any, index: number) => ({
      ...originalInstance.moves[index],
      currentPp: moveData.currentPP
    }))
  };
}

/**
 * Convert PicletStats (from generation) to PicletDefinition for battle engine
 */
export function picletStatsToBattleDefinition(stats: PicletStats, name: string, concept: string): PicletDefinition {
  return {
    name: stats.name || name,
    description: stats.description || concept,
    tier: stats.tier,
    primaryType: stats.primaryType,
    secondaryType: stats.secondaryType || undefined,
    baseStats: stats.baseStats,
    nature: stats.nature,
    specialAbility: stats.specialAbility,
    movepool: stats.movepool
  };
}