Files @ 495f65b04716
Branch filter:

Location: games/Awakening/battlefield.lua

silverwing
Some texts added to the end of the book
  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
_kh.bf_calcWalkDistance = function(s, x1, y1, x2, y2)
    local closed = {};
    local open = {
        {p = {{x1, y1}}, l = 0}
    };

    while #open > 0 do
        local item = table.remove (open, 1);

        local node = item.p[#item.p];
        local inClosed = false;
        for i = 1, #closed do
            if (closed[i][1] == node[1] and closed[i][2] == node[2]) then
                inClosed = true;
                break;
            end;
        end;
        if (not inClosed) then
            if (node[1] == x2 and node[2] == y2) then
                return item;
            end;
            table.insert(closed, node);

            local pos = 1;
            while (#open >= pos and open[pos].l <= item.l) do
                pos = pos + 1;
            end;

            if s:isPassable(node[1], node[2] - 1) or 
                (node[1] == x2 and node[2] - 1 == y2) then
                local p = {};
                for i = 1, #item.p do
                    table.insert(p, item.p[i]);
                end;
                table.insert(p, {node[1], node[2] - 1});
                table.insert(open, pos, {
                    p = p,
                    l = item.l + 1
                });
            end;

            if s:isPassable(node[1], node[2] + 1) or 
                (node[1] == x2 and node[2] + 1 == y2) then
                local p = {};
                for i = 1, #item.p do
                    table.insert(p, item.p[i]);
                end;
                table.insert(p, {node[1], node[2] + 1});
                table.insert(open, pos, {
                    p = p,
                    l = item.l + 1
                });
            end;

            if s:isPassable(node[1] - 1, node[2]) or 
                (node[1] - 1 == x2 and node[2] == y2) then
                local p = {};
                for i = 1, #item.p do
                    table.insert(p, item.p[i]);
                end;
                table.insert(p, {node[1] - 1, node[2]});
                table.insert(open, pos, {
                    p = p,
                    l = item.l + 1
                });
            end;

            if s:isPassable(node[1] + 1, node[2]) or 
                (node[1] + 1 == x2 and node[2] == y2) then
                local p = {};
                for i = 1, #item.p do
                    table.insert(p, item.p[i]);
                end;
                table.insert(p, {node[1] + 1, node[2]});
                table.insert(open, pos, {
                    p = p,
                    l = item.l + 1
                });
            end;
        end;
    end;

    return nil;
end;

_kh.bf_calcShootDistance = function(x1, y1, x2, y2)
    --return math.max(math.abs(x2 - x1), math.abs(y2 - y1));
    return math.floor(math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)));
end;

_kh.bf_calcDirection = function(x1, y1, x2, y2)
    -- 0 - close by, 1-8 - north to north-west clockwise, 9 - too far
    local dist = _kh.bf_calcDistance(x1,y1,x2,y2);
    if (dist == 0) then
        return 0;
    elseif (dist == 1) then
        if (x1 == x2 and y1 > y2) then
            return 1;
        elseif (x1 < x2 and y1 > y2) then
            return 2;
        elseif (x1 < x2 and y1 == y2) then
            return 3;
        elseif (x1 < x2 and y1 < y2) then
            return 4;
        elseif (x1 == x2 and y1 < y2) then
            return 5;
        elseif (x1 > x2 and y1 < y2) then
            return 6;
        elseif (x1 > x2 and y1 == y2) then
            return 7;
        elseif (x1 > x2 and y1 > y2) then
            return 8;
        end;
    else
        return 9;
    end;
end;

hp_indicator = obj {
    nam = "hp_indicator";
    disp = function(s)
        return "Здоровье:" .. pl.hp .. "/10";
    end;
};

hp_indicator:disable();


battlefield = function(tab)
    local make_turn = tab.make_turn;
    tab.showhp = true;
    tab.nosave = true;
    tab.noautosave = true;
    tab.not_follow = true;
    tab.battlefield = true;
    tab.ui_state = nil;

    tab.click = function(s, x, y)
        local tx = math.floor(x / 32) + 1;
        local ty = math.floor(y / 32) + 1;
        if s.ui_state == nil then
            -- No tile selected
            if s.plX == tx and s.plY == ty then
                pr [[Это вы. ]];
                if pl.hp > 9 then
                    pr [[Вы не ранены. ]];
                elseif pl.hp > 7 then
                    pr [[Вы легко ранены. ]];
                elseif pl.hp > 3 then
                    pr [[Вы ранены. ]];
                else
                    pr [[Вы тяжело ранены. ]];
                end;
                pr [[Нажмите еще раз, чтобы ждать. ]];
                s.ui_state = 1;
                return;
            end;
            for i = 1, #s.obj do
                if not disabled(s.obj[i]) and (s.obj[i].hp == nil or s.obj[i].hp > 0) then
                    if (s.obj[i].x == tx and s.obj[i].y == ty) then
                        pr('Это ' .. s.obj[i].disp);
                        if s.obj[i].enemy then
                            pr(', ваш противник. ');
                        elseif s.obj[i].ally then
                            pr(', ваш союзник. ');
                        else
                            pr('. ');
                        end;

                        pr(EngineUtils.getValue(s.obj[i].state, s.obj[i]));

                        if s.obj[i].enemy and EngineUtils.getValue(s.obj[i].canshoot, s.obj[i]) then
                            if s.obj[i]:canhit() or (s.obj[i]:canthrust() and have(item_harpoon)) then
                                s.ui_state = {tx, ty, 'hit', s.obj[i]};
                                if have(item_harpoon) then
                                    pr('Нажмите еще раз, чтобы ударить гарпуном. ');
                                else
                                    pr('Нажмите еще раз, чтобы ударить. ');
                                end;
                            elseif s:canShoot(s.plX, s.plY, tx, ty) and (have(item_colt) and not s.underwater) or (have(item_harpoon)) then
                                if have(item_colt) and not s.underwater and item_colt.bullets == 0 then
                                    pr [[Ваш револьвер не заряжен. ]];
                                elseif have(item_harpoon) and item_harpoon.charge == 0 then
                                    pr [[Ваше ружье не заряжено. ]];
                                else
                                    s.ui_state = {tx, ty, 'shoot', s.obj[i]};
                                    pr('Нажмите еще раз, чтобы выстрелить. ');
                                end;
                            end;
                        end;
                        return;
                    end;
                end;
            end;
            if math.abs(s.plX - tx) + math.abs(s.plY - ty) < 2 then
                if not s:isPassable(tx, ty) then
                    return
                end;
                s.ui_state = {tx, ty, 'walk'};
                pr('Нажмите еще раз, чтобы идти. ');
            end;
        elseif s.ui_state == 1 then
            -- tile selected. Click same tile to confirm action, or elsewhere to cancel
            s.ui_state = nil;
            if s.plX == tx and s.plY == ty then
                p("Вы ждете. ");
                here():make_turn();
            else
                pr [[Действие отменено. ]];
            end;
        else
            if tx == s.ui_state[1] and ty == s.ui_state[2] then
                if s.ui_state[3] == 'shoot' then
                    if have(item_colt) and not s.underwater and item_colt.bullets > 0 then
                        pr(item_colt:use(s.ui_state[4]));
                    elseif have(item_harpoon) and item_harpoon.charge > 0 then
                        pr(item_harpoon:use(s.ui_state[4]));
                    else
                        pr [[Если вы видите это сообщение -- это баг. ]];
                    end;
                elseif s.ui_state[3] == 'hit' then
                    if have(item_harpoon) then
                        pr(item_harpoon:use(s.ui_state[4]));
                    else
                        pr(s.ui_state[4]:act());
                    end;
                elseif s.ui_state[3] == 'walk' then
                    here().plX = s.ui_state[1];
                    here().plY = s.ui_state[2];
                    here():make_turn();
                end;
            else
                pr [[Действие отменено. ]];
            end;
            s.ui_state = nil;
        end;
    end;

    tab.isPassable = function(s, x, y)
        if (x < 1 or x > 7 or y < 1 or y > 7 or tab.map[y][x] > 0) then
            return false;
        end;
    
        for i = 1, #s.obj do
            local obst = EngineUtils.getValue(s.obj[i].obst, s.obj[i]);
            if (obst and obst > 0) then
                if (s.obj[i].x == x and s.obj[i].y == y) then
                    return false;
                end;
            end;
        end;
        
        return true;
    end;
    
    tab.isShootable = function(s, x, y)
        if (x < 1 or x > 7 or y < 1 or y > 7 or tab.map[y][x] > 1) then
            return false;
        end;
    
        for i = 1, #s.obj do
            local obst = EngineUtils.getValue(s.obj[i].obst, s.obj[i]);
            if (obst and obst > 1) then
                if (s.obj[i].x == x and s.obj[i].y == y) then
                    return false;
                end;
            end;
        end;
        
        return true;
    end;
    
    tab.canShoot = function(s, x1, y1, x2, y2)
        local dx = math.abs(x1 - x2);
        local dy = math.abs(y1 - y2);
        local invx = x1 > x2;
        local invy = y1 > y2;
        local xc = 0;
        local yc = 0;
        local error = 0;

        if (dx > dy) then
            --horizontal line
            local y = 0;
            for x = 0, dx do 
                if invx then
                    xc = x1 - x;
                else
                    xc = x1 + x;
                end;
                if invy then
                    yc = y1 - y;
                else
                    yc = y1 + y;
                end;
                if ((xc ~= x1 or yc ~= y1) and not s:isShootable(xc, yc) and (xc ~= x2 or yc ~= y2)) then
                    return false;
                end;
                
                error = error + dy;
                if (2 * error > dx) then
                    y = y + 1
                    error = error - dx;
                end;
            end;
        else
            --vertical line
            local x = 0;
            for y = 0, dy do
                if invx then
                    xc = x1 - x;
                else
                    xc = x1 + x;
                end;
                if invy then
                    yc = y1 - y;
                else
                    yc = y1 + y;
                end;
                if ((xc ~= x1 or yc ~= y1) and not s:isShootable(xc, yc) and (xc ~= x2 or yc ~= y2)) then
                    return false;
                end;
                
                error = error + dx;
                if (2 * error > dy) then
                    x = x + 1;
                    error = error - dy;
                end;
            end;
        end;
        
        return true;
    end;
    
    tab.check_walk = function(s)
        bf_north:enable();
        bf_east:enable();
        bf_south:enable();
        bf_west:enable();
        
        if (not s:isPassable(s.plX, s.plY - 1)) then
            bf_north:disable();
        end;
        if (not s:isPassable(s.plX, s.plY + 1)) then
            bf_south:disable();
        end;
        if (not s:isPassable(s.plX - 1, s.plY)) then
            bf_west:disable();
        end;
        if (not s:isPassable(s.plX + 1, s.plY)) then
            bf_east:disable();
        end;
    end;

    local entered = tab.entered;

    tab.entered = function(s)
        s:check_walk();
        if (type(entered) == 'function') then
            return entered(s);
        else
            return entered;
        end;
    end;

    tab.onreload = function(s)
        here():make_turn();
    end;

    if not tab.check_win then
        tab.check_win = function(s)
            for i = 1, #s.obj do
                if (s.obj[i].enemy and s.obj[i].hp > 0) then
                    return;
                end;
            end;

            walk(s.win);
        end;
    end;

    tab.make_turn = function(s)
        -- A bit peggish but works
        pl.x = s.plX;
        pl.y = s.plY;
        for i = 1, #s.obj do
            if (type(s.obj[i].make_turn) == 'function' and s.obj[i].hp > 0) then
                s.obj[i]:make_turn();
            end;
        end;

        if (make_turn) then
            make_turn(s);
        end;

        if (pl.hp <= 0) then
            walk(s.lose);
        end;

        s:check_walk();

        s:check_win();
    end;
    
    tab.getWalkDistance = _kh.bf_calcWalkDistance;
    tab.getShootDistance = _kh.bf_calcShootDistance;
    tab.getDirection = _kh.bf_calcDirection;
    
    if not tab.wall then
        tab.wall = "images/battle_wall.png";
    end;

    if not tab.obstacle then
        tab.obstacle = "images/battle_obstacle.png";
    end;

    if (not tab.obj) then
        tab.obj = {};
    end;

    if not tab.bcg_pic then
        tab.bcg_pic = 'images/battle_bg.png';
    end;

    if not tab.player_pic then
        tab.player_pic = 'images/player.png';
        end;

    if (not tab.pic) then
        tab.pic = function(s)
            local v = s.bcg_pic .. ";" .. s.player_pic .. "@" .. tostring(s.plX * 32 - 32) .. "," .. tostring(s.plY * 32 - 32);
            for i = 1, #s.obj do
                if ((s.obj[i].hp) and (s.obj[i].hp > 0) and not(s.obj[i]:disabled()) and s.obj[i].x and s.obj[i].y) then
                    if s.obj[i].pic then
                        local pic = EngineUtils.getValue(s.obj[i].pic, s.obj[i]);
                        if type(pic) ~= 'table' then
                            pic = {pic};
                        end;
                        for x = 1, #pic do
                            v = v .. ";" .. pic[x] .. "@" .. tostring(s.obj[i].x * 32 - 32) .. "," .. tostring(s.obj[i].y * 32 - 32);
                        end;
                    end;
                end;
            end;
            
            for i = 1, 7 do
                for j = 1, 7 do
                    if (tab.map[i][j] == 2) then
                        v = v.. ";" .. tab.wall .. "@" .. tostring(j * 32 - 32) .. "," .. tostring(i * 32 - 32);
                    elseif (tab.map[i][j] == 1) then
                        v = v.. ";" .. tab.obstacle .. "@" .. tostring(j * 32 - 32) .. "," .. tostring(i * 32 - 32);
                    end;
                end;
            end;

            if s.ui_state == nil then
            elseif s.ui_state == 1 then
                v = v .. ';images/' .. 'wait_cursor.png' .. '@' .. tostring(s.plX * 32 - 32) .. "," .. tostring(s.plY * 32 - 32);
            else
                v = v .. ';images/' .. s.ui_state[3] .. '_cursor.png' .. '@' .. tostring(s.ui_state[1] * 32 - 32) .. "," .. tostring(s.ui_state[2] * 32 - 32);
            end;

            return v;
        end;
    end;
    
    table.insert(tab.obj, 'bf_north');
    --table.insert(tab.obj, 'bf_northeast');
    table.insert(tab.obj, 'bf_east');
    --table.insert(tab.obj, 'bf_southeast');
    table.insert(tab.obj, 'bf_south');
    --table.insert(tab.obj, 'bf_southwest');
    table.insert(tab.obj, 'bf_west');
    --table.insert(tab.obj, 'bf_northwest');
    table.insert(tab.obj, 'bf_wait');

    return room(tab);
end;

bf_north = obj {
    nam = "north",
    dsc = "{На север}",
    act = function(s)
        here().plY = here().plY - 1;
        p("Вы идете на север");
        here():make_turn();
    end;
};

bf_east = obj {
    nam = "east",
    dsc = "{На восток}",
    act = function(s)
        here().plX = here().plX + 1;
        p("Вы идете на восток.");
        here():make_turn();
    end;
};

bf_south = obj {
    nam = "south",
    dsc = "{На юг}",
    act = function(s)
        here().plY = here().plY + 1;
        p("Вы идете на юг.");
        here():make_turn();
    end;
};

bf_west = obj {
    nam = "west",
    dsc = "{На запад}",
    act = function(s)
        here().plX = here().plX - 1;
        p("Вы идете на запад.");
        here():make_turn();
    end;
};

bf_wait = obj {
    nam = "wait",
    dsc = "{Ждать}",
    act = function(s)
        p("Вы ждете. ");
        here():make_turn();
    end;
};

combatant = function(tab)
    if (tab.canshoot == nil) then
        tab.canshoot = function(s)
            local dist = math.sqrt((s.x - here().plX) * (s.x - here().plX) + (s.y - here().plY) * (s.y - here().plY));
            return dist < 6 and not tab.ally;
        end;
    end;
    
    if (tab.onthrust == nil) then
        tab.onthrust = function(s)
            local dist = math.sqrt((s.x - here().plX) * (s.x - here().plX) + (s.y - here().plY) * (s.y - here().plY));
            if (rnd(4) > dist - 2) then
                tab.hp = tab.hp - 2;
                if (tab.hp > 0) then
                    p(tab.thrustHit);
                else
                    if (tab.onkill) then
                        tab:onkill();
                    end;
                    p(tab.thrustKill);
                end;
            else
                p(tab.thrustMiss);
            end;
            
            here():make_turn();
        end;
    end;
    
    if (tab.onshoot == nil) then
        tab.onshoot = function(s)
            local dist = math.sqrt((s.x - here().plX) * (s.x - here().plX) + (s.y - here().plY) * (s.y - here().plY));
            if (rnd(4) > dist - 2) then
                if (dist == 0) then
                    tab.hp = tab.hp - 2;
                end;
                
                tab.hp = tab.hp - 1;
                if (tab.hp > 0) then
                    p(tab.shootHit);
                else
                    if (tab.onkill) then
                        tab:onkill();
                    end;
                    p(tab.shootKill);
                end;
            else
                p(tab.shootMiss);
            end;
            
            here():make_turn();
        end;
    end;
    
    if (tab.canthrust == nil) then
        tab.canthrust = function(s)
            return math.abs(s.x - here().plX) + math.abs(s.y - here().plY) <= 2 and not s.ally;
        end;
    end;
    
    if (tab.canhit == nil) then
        tab.canhit = function(s)
            return (math.abs(s.x - here().plX) == 1 and s.y == here().plY or
                    s.x == here().plX and math.abs(s.y - here().plY) == 1) and not tab.ally;
        end;
    end;
    
    if (tab.onhit == nil) then
        tab.onhit = function(s)
            if (math.abs(s.x - here().plX) == 1 and s.y == here().plY or
                    s.x == here().plX and math.abs(s.y - here().plY) == 1) then
                tab.hp = tab.hp - 2;
                if (tab.hp > 0) then
                    p(tab.wpnHit);
                else
                    if (tab.onkill) then
                        tab:onkill();
                    end;
                    p(tab.wpnKill);
                end;
                
                here():make_turn();
            else
                p(tab.wpnFar);
            end;
        end;
    end;
    
    if (not tab.act) then
        tab.act = function(s)
            if tab.ally then
                return tab.nohit;
            end;

            if (math.abs(s.x - here().plX) == 1 and s.y == here().plY or
                s.x == here().plX and math.abs(s.y - here().plY) == 1) then
                tab.hp = tab.hp - 1;
                if (tab.hp > 0) then
                    p(tab.handHit);
                else
                    if (tab.onkill) then
                        tab:onkill();
                    end;
                    p(tab.handKill);
                end;

                here():make_turn();
            else
                p(tab.handFar);
            end;
        end;
    end;
    if (not tab.make_turn) then
        tab.make_turn = function(s)
        end;
    end;
    if (not tab.obst) then
        tab.obst = function(s)
            if (s.hp > 0) then
                return 2;
            else
                return 0;
            end;
        end;
    end;
    
    return obj(tab);
end;