File size: 6,940 Bytes
076c3cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Adaptive Tactics - Game Models
 * Core data structures for units, weapons, tiles, and map data
 */

/**
 * Weapon data structure
 */
export class Weapon {
    constructor(data) {
        this.id = data.id;
        this.name = data.name;
        this.kind = data.kind; // 'physical', 'magic', or 'heal'
        this.might = data.might;
        this.hit = data.hit;
        this.minRange = data.minRange;
        this.maxRange = data.maxRange;
        this.heal = data.heal || 0;
    }

    /**
     * Check if this weapon can reach a given range
     */
    canReach(range) {
        return range >= this.minRange && range <= this.maxRange;
    }

    /**
     * Check if this is a healing weapon
     */
    isHealingWeapon() {
        return this.kind === 'heal';
    }
}

/**
 * Unit data structure
 */
export class Unit {
    constructor(data, weapons) {
        this.id = data.id;
        this.name = data.name;
        this.title = data.title || '';
        this.sprite = data.sprite;
        this.spriteBlend = data.spriteBlend || false;
        this.faction = data.faction || 'player'; // 'player' or 'enemy'

        // Stats
        this.hp = data.stats.hp;
        this.maxHp = data.stats.maxHp || data.stats.hp;
        this.power = data.stats.power;
        this.skill = data.stats.skill;
        this.speed = data.stats.speed;
        this.def = data.stats.def;
        this.res = data.stats.res;
        this.mov = data.stats.mov;

        // Weapons
        this.weaponIds = data.weapons || [];
        this.equippedWeaponId = data.equippedWeapon || this.weaponIds[0];
        this.weapons = weapons || {};

        // Position (set during battle)
        this.row = 0;
        this.col = 0;

        // State
        this.acted = false;
        this.alive = true;
    }

    /**
     * Get the currently equipped weapon
     */
    getEquippedWeapon() {
        return this.weapons[this.equippedWeaponId] || null;
    }

    /**
     * Get all available weapons
     */
    getAvailableWeapons() {
        return this.weaponIds.map(id => this.weapons[id]).filter(w => w);
    }

    /**
     * Switch to a different weapon
     */
    switchWeapon(weaponId) {
        if (this.weaponIds.includes(weaponId)) {
            this.equippedWeaponId = weaponId;
            return true;
        }
        return false;
    }

    /**
     * Take damage
     */
    takeDamage(amount) {
        this.hp = Math.max(0, this.hp - amount);
        if (this.hp === 0) {
            this.alive = false;
        }
        return this.hp;
    }

    /**
     * Heal
     */
    heal(amount) {
        this.hp = Math.min(this.maxHp, this.hp + amount);
        return this.hp;
    }

    /**
     * Check if unit is a player unit
     */
    isPlayer() {
        return this.faction === 'player';
    }

    /**
     * Check if unit is an enemy
     */
    isEnemy() {
        return this.faction === 'enemy';
    }

    /**
     * Get position as [row, col]
     */
    getPosition() {
        return [this.row, this.col];
    }

    /**
     * Set position
     */
    setPosition(row, col) {
        this.row = row;
        this.col = col;
    }

    /**
     * Get HP percentage
     */
    getHpPercent() {
        return (this.hp / this.maxHp) * 100;
    }
}

/**
 * Tile data structure
 */
export class Tile {
    constructor(row, col, type = 'floor') {
        this.row = row;
        this.col = col;
        this.type = type; // 'floor', 'bush', 'blocked', 'throne'
        this.unit = null;
    }

    /**
     * Check if tile is passable
     */
    isPassable() {
        return this.type !== 'blocked';
    }

    /**
     * Check if tile is occupied
     */
    isOccupied() {
        return this.unit !== null;
    }

    /**
     * Get terrain avoid bonus
     */
    getAvoidBonus() {
        switch (this.type) {
            case 'bush': return 10;
            case 'throne': return 15;
            default: return 0;
        }
    }

    /**
     * Get terrain defense bonus
     */
    getDefBonus() {
        switch (this.type) {
            case 'bush': return 1;
            case 'throne': return 2;
            default: return 0;
        }
    }

    /**
     * Check if this is the throne tile
     */
    isThrone() {
        return this.type === 'throne';
    }

    /**
     * Place a unit on this tile
     */
    placeUnit(unit) {
        this.unit = unit;
        if (unit) {
            unit.setPosition(this.row, this.col);
        }
    }

    /**
     * Remove unit from this tile
     */
    removeUnit() {
        const unit = this.unit;
        this.unit = null;
        return unit;
    }
}

/**
 * Map data structure
 */
export class MapData {
    constructor(data) {
        this.id = data.id;
        this.name = data.name;
        this.rows = data.rows;
        this.cols = data.cols;

        // Store raw tile data for initialization
        this.tileData = data.tiles;
        this.playerSpawn = data.player_spawn;
        this.enemyLayout = data.enemy_layout;
        this.flexNodes = data.flex_nodes;
        this.reinforcementEntries = data.reinforcement_entries;

        // Tiles grid will be built during battle init
        this.tiles = [];
    }

    /**
     * Initialize the tile grid
     */
    initTiles() {
        this.tiles = [];

        // Create all floor tiles first
        for (let row = 1; row <= this.rows; row++) {
            const rowTiles = [];
            for (let col = 1; col <= this.cols; col++) {
                rowTiles.push(new Tile(row, col, 'floor'));
            }
            this.tiles.push(rowTiles);
        }

        // Set blocked tiles
        if (this.tileData.blocked) {
            for (const [row, col] of this.tileData.blocked) {
                this.getTile(row, col).type = 'blocked';
            }
        }

        // Set bush tiles
        if (this.tileData.bush) {
            for (const [row, col] of this.tileData.bush) {
                this.getTile(row, col).type = 'bush';
            }
        }

        // Set throne tile
        if (this.tileData.throne) {
            const [row, col] = this.tileData.throne;
            this.getTile(row, col).type = 'throne';
        }

        return this.tiles;
    }

    /**
     * Get tile at position (1-indexed)
     */
    getTile(row, col) {
        if (row < 1 || row > this.rows || col < 1 || col > this.cols) {
            return null;
        }
        return this.tiles[row - 1]?.[col - 1] || null;
    }

    /**
     * Get all tiles as flat array
     */
    getAllTiles() {
        return this.tiles.flat();
    }

    /**
     * Check if position is valid
     */
    isValidPosition(row, col) {
        return row >= 1 && row <= this.rows && col >= 1 && col <= this.cols;
    }

    /**
     * Get Manhattan distance between two positions
     */
    static getDistance(row1, col1, row2, col2) {
        return Math.abs(row1 - row2) + Math.abs(col1 - col2);
    }
}