hedgewars/uFLAmmo.pas
author unc0rr
Tue, 09 Feb 2016 21:11:16 +0300
branchqmlfrontend
changeset 11544 b69f5f22a3ba
parent 11456 ab77e2ea2f82
permissions -rw-r--r--
Merge default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10890
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
     1
unit uFLAmmo;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
     2
interface
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
     3
uses uFLTypes;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
     4
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
     5
function getAmmosList: PPChar; cdecl;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
     6
procedure freeAmmosList;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
     7
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
     8
function ammoByName(s: shortstring): PAmmo;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
     9
procedure sendAmmoConfig(var ammo: TAmmo);
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    10
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    11
implementation
11456
ab77e2ea2f82 uFLData was renamed, update "uses"
sheepluva
parents: 11437
diff changeset
    12
uses uFLUtils, uFLIPC, uPhysFSLayer, uFLThemes;
10890
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    13
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    14
const MAX_AMMO_NAMES = 64;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    15
type
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    16
    TAmmoArray = array [0..0] of TAmmo;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    17
    PAmmoArray = ^TAmmoArray;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    18
var
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    19
    ammoList: PAmmo;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    20
    ammoNumber: LongInt;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    21
    listOfAmmoNames: array[0..MAX_AMMO_NAMES] of PChar;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    22
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    23
procedure loadAmmo;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    24
var f: PFSFile;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    25
    ammo: PAmmo;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    26
    ammos: PAmmoArray;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    27
    s: ansistring;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    28
    i: Longword;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    29
begin
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    30
    f:= pfsOpenRead('/Config/weapons.ini');
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    31
    ammoNumber:= 0;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    32
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    33
    if f <> nil then
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    34
    begin
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    35
        while (not pfsEOF(f)) do
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    36
        begin
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    37
            pfsReadLnA(f, s);
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    38
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    39
            if (length(s) > 0) and (s[1] <> '[') then
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    40
                inc(ammoNumber);
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    41
        end;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    42
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    43
        //inc(ammoNumber); // add some default ammo
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    44
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    45
        ammoList:= GetMem(sizeof(ammoList^) * (ammoNumber + 1));
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    46
        ammo:= PAmmo(ammoList);
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    47
        pfsSeek(f, 0);
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    48
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    49
        while (not pfsEOF(f)) do
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    50
        begin
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    51
            pfsReadLnA(f, s);
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    52
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    53
            i:= 1;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    54
            while(i <= length(s)) and (s[i] <> '=') do inc(i);
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    55
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    56
            if i < length(s) then
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    57
            begin
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    58
                ammo^.ammoName:= copy(s, 1, i - 1);
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    59
                delete(s, 1, i);
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    60
                // TODO: split into 4 shortstrings
10892
83a99e2f8b00 Make ammo scheme work
unc0rr
parents: 10890
diff changeset
    61
                i:= length(s) div 4;
83a99e2f8b00 Make ammo scheme work
unc0rr
parents: 10890
diff changeset
    62
                ammo^.a:= copy(s, 1, i);
83a99e2f8b00 Make ammo scheme work
unc0rr
parents: 10890
diff changeset
    63
                ammo^.b:= copy(s, i + 1, i);
83a99e2f8b00 Make ammo scheme work
unc0rr
parents: 10890
diff changeset
    64
                ammo^.c:= copy(s, i * 2 + 1, i);
83a99e2f8b00 Make ammo scheme work
unc0rr
parents: 10890
diff changeset
    65
                ammo^.d:= copy(s, i * 3 + 1, i);
10890
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    66
                inc(ammo)
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    67
            end;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    68
        end;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    69
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    70
        pfsClose(f)
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    71
    end;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    72
end;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    73
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    74
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    75
function getAmmosList: PPChar; cdecl;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    76
var i, t, l: Longword;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    77
    ammo: PAmmo;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    78
begin
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    79
    if ammoList = nil then
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    80
        loadAmmo;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    81
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    82
    t:= ammoNumber;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    83
    if t >= MAX_AMMO_NAMES then 
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    84
        t:= MAX_AMMO_NAMES;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    85
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    86
    ammo:= ammoList;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    87
    for i:= 0 to Pred(t) do
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    88
    begin
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    89
        l:= length(ammo^.ammoName);
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    90
        if l >= 255 then l:= 254;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    91
        ammo^.ammoName[l + 1]:= #0;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    92
        listOfAmmoNames[i]:= @ammo^.ammoName[1];
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    93
        inc(ammo)
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    94
    end;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    95
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    96
    listOfAmmoNames[t]:= nil;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    97
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    98
    getAmmosList:= listOfAmmoNames
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
    99
end;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   100
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   101
function ammoByName(s: shortstring): PAmmo;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   102
var i: Longword;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   103
    ammo: PAmmo;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   104
begin
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   105
    ammo:= ammoList;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   106
    i:= 0;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   107
    while (i < ammoNumber) and (ammo^.ammoName <> s) do
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   108
    begin
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   109
        inc(ammo);
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   110
        inc(i)
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   111
    end;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   112
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   113
    if i < ammoNumber then ammoByName:= ammo else ammoByName:= nil
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   114
end;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   115
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   116
procedure freeAmmosList;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   117
begin
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   118
    if ammoList <> nil then
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   119
        FreeMem(ammoList, sizeof(ammoList^) * (ammoNumber + 1))
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   120
end;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   121
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   122
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   123
procedure sendAmmoConfig(var ammo: TAmmo);
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   124
var i: Longword;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   125
begin
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   126
    with ammo do
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   127
    begin
10892
83a99e2f8b00 Make ammo scheme work
unc0rr
parents: 10890
diff changeset
   128
        ipcToEngine('eammloadt ' + ammo.a);
83a99e2f8b00 Make ammo scheme work
unc0rr
parents: 10890
diff changeset
   129
        ipcToEngine('eammprob '  + ammo.b);
83a99e2f8b00 Make ammo scheme work
unc0rr
parents: 10890
diff changeset
   130
        ipcToEngine('eammdelay ' + ammo.c);
83a99e2f8b00 Make ammo scheme work
unc0rr
parents: 10890
diff changeset
   131
        ipcToEngine('eammreinf ' + ammo.d);
11437
6e641b5453f9 Accept ammo scheme from network
unc0rr
parents: 10892
diff changeset
   132
        ipcToEngine('eammstore');
10890
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   133
    end
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   134
end;
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   135
24e68c7d2e79 Oops, forgot this file
unc0rr
parents:
diff changeset
   136
end.