hedgewars/uLocale.pas
changeset 2142 48ed98cfd119
parent 2140 75e5c4fcae2a
child 2143 ad05f6b2d1c0
equal deleted inserted replaced
2141:3b06505dbd5d 2142:48ed98cfd119
    31 	TMsgStrId = (sidStartFight, sidDraw, sidWinner, sidVolume, sidPaused,
    31 	TMsgStrId = (sidStartFight, sidDraw, sidWinner, sidVolume, sidPaused,
    32 			sidConfirm, sidSuddenDeath);
    32 			sidConfirm, sidSuddenDeath);
    33 			
    33 			
    34 	TEventId = (eidDied, eidDrowned, eidRoundStart);
    34 	TEventId = (eidDied, eidDrowned, eidRoundStart);
    35 
    35 
       
    36 const MAX_EVENT_STRINGS = 100;
       
    37 
    36 var trammo: array[TAmmoStrId] of string;
    38 var trammo: array[TAmmoStrId] of string;
    37     trmsg: array[TMsgStrId] of string;
    39     trmsg: array[TMsgStrId] of string;
    38 	trevt: array[TEventId] of array[1..100] of string;
       
    39 	trevt_n: array[TEventId] of integer;
       
    40 
    40 
    41 procedure LoadLocale(FileName: string);
    41 procedure LoadLocale(FileName: string);
    42 function Format(fmt: shortstring; var arg: shortstring): shortstring;
    42 function Format(fmt: shortstring; var arg: shortstring): shortstring;
    43 
    43 
    44 function GetEventString(e: TEventId): string;
    44 function GetEventString(e: TEventId): string;
    45 
    45 
    46 implementation
    46 implementation
    47 uses uMisc;
    47 uses uMisc, uRandom;
       
    48 
       
    49 var	trevt: array[TEventId] of array [0..Pred(MAX_EVENT_STRINGS)] of string;
       
    50 	trevt_n: array[TEventId] of integer;
    48 
    51 
    49 procedure LoadLocale(FileName: string);
    52 procedure LoadLocale(FileName: string);
    50 var s: shortstring;
    53 var s: shortstring;
    51     f: textfile;
    54     f: textfile;
    52     a, b, c: LongInt;
    55     a, b, c: LongInt;
       
    56     e: TEventId;
    53 begin
    57 begin
    54 
    58 
    55 // clear event locales
    59 // clear event locales
    56 for a:= 0 to ord(High(TEventId)) do trevt_n[TEventId(a)]:= 0;
    60 for e:= Low(TEventId) to High(TEventId) do trevt_n[e]:= 0;
    57 
    61 
    58 {$I-}
    62 {$I-}
    59 Assign(f, FileName);
    63 Assign(f, FileName);
    60 reset(f);
    64 reset(f);
    61 TryDo(IOResult = 0, 'Cannot load locale "' + FileName + '"', true);
    65 TryDo(IOResult = 0, 'Cannot load locale "' + FileName + '"', true);
    74 	Delete(s, 1, 6);
    78 	Delete(s, 1, 6);
    75 	case a of
    79 	case a of
    76 		0: if (b >=0) and (b <= ord(High(TAmmoStrId))) then trammo[TAmmoStrId(b)]:= s;
    80 		0: if (b >=0) and (b <= ord(High(TAmmoStrId))) then trammo[TAmmoStrId(b)]:= s;
    77 		1: if (b >=0) and (b <= ord(High(TMsgStrId))) then trmsg[TMsgStrId(b)]:= s;
    81 		1: if (b >=0) and (b <= ord(High(TMsgStrId))) then trmsg[TMsgStrId(b)]:= s;
    78 		2: if (b >=0) and (b <= ord(High(TEventId))) then begin
    82 		2: if (b >=0) and (b <= ord(High(TEventId))) then begin
       
    83 			TryDo(trevt_n[TEventId(b)] < MAX_EVENT_STRINGS, 'Too many event strings', true);
       
    84 			trevt[TEventId(b)][trevt_n[TEventId(b)]]:= s;
    79 			inc(trevt_n[TEventId(b)]);
    85 			inc(trevt_n[TEventId(b)]);
    80 			trevt[TEventId(b)][trevt_n[TEventId(b)]]:= s;
       
    81 			end;
    86 			end;
    82 		end;
    87 		end;
    83 	end;
    88 	end;
    84 Close(f)
    89 Close(f)
    85 {$I+}
    90 {$I+}
    88 function GetEventString(e: TEventId): string;
    93 function GetEventString(e: TEventId): string;
    89 begin
    94 begin
    90 	if trevt_n[e] = 0 then // no messages for this event type?
    95 	if trevt_n[e] = 0 then // no messages for this event type?
    91 		GetEventString:= '*missing translation*'
    96 		GetEventString:= '*missing translation*'
    92 	else
    97 	else
    93 		GetEventString:= trevt[e][Random(trevt_n[e]) + 1]; // Pick a random message and return it
    98 		GetEventString:= trevt[e][GetRandom(trevt_n[e])]; // Pick a random message and return it
    94 end;
    99 end;
    95 
   100 
    96 function Format(fmt: shortstring; var arg: shortstring): shortstring;
   101 function Format(fmt: shortstring; var arg: shortstring): shortstring;
    97 var i: LongInt;
   102 var i: LongInt;
    98 begin
   103 begin