unit rgame;
interface
uses classes;
type
tGame = record
title: string;
load: string;
battle_music: string;
end;
tPlayer = record
attack: string;
block: string;
defense: string;
gold: word;
hp: integer;
map: string;
talk: TStringList;
collected: TStringList;
defeat: TStringList;
item: TStringList;
end;
tMap = record
name: string;
bg: string;
end;
tBlock = record
bg: string;
end;
PStringList = ^TStringList;
var
block: ^tBlock;
game: tGame;
LastBlock: string;
l: boolean;
map: ^tMap;
player: ^tPlayer;
procedure GameInfo;
procedure SpeakNumber(n: integer);
function inList(list: PStringList; name: string; aBlock: string; aMap: string): boolean;
function findItem(name: string): boolean;
procedure DeleteItem(name: string);
function qntItem(name: string): integer;

function subValues(source: string): integer;
function subValue(source: string; n: integer): string;
procedure Rebuild;
implementation
uses rfile, sound,  sysUtils;

procedure Rebuild;
begin
new(player);
new(map); new(block);
player.collected:=TStringList.Create;
player.item:=TStringList.Create;
player.talk:=TStringList.Create;
player.defeat:=TStringList.Create;
GameInfo;
end;

procedure GameInfo;
begin
if not ReadSession(CFGFILE,'OpenRPG') then exit;
game.title:=ReadValue('title');
game.load:=ReadValue('load');
game.battle_music:=readValue('battle_music');
player.gold:=strToInt(readValue('gold'));
player.attack:=readValue('attack');
player.defense:=readValue('defense');
player.map:=game.load;
player.block:=readValue('block');
end;

procedure SpeakNumber(n: integer);
var s: string;
i: integer;
begin
s:=intToStr(n);
if (length(s)=3) then
begin
if (s[2]='0') and (s[3]<>'0') then
begin
play('numbers\'+s[1]+'.wav',0);
play('numbers\hundred.wav',0);
play('numbers\'+s[3]+'.wav',0);
exit;
end;
end;
if n<=20 then
play('numbers\'+s+'.wav',0);
if (n>20) and (n<100) then
begin
play('numbers\'+s[1]+'0.wav',0);
if s[2]<>'0' then play('numbers\'+s[2]+'.wav',0);
end;
if (n>=100) and (n<1000) then
begin
play('numbers\'+s[1]+'.wav',0);
play('numbers\hundred.wav',0);
if (s[2]='0') and (s[3]='0') then exit;
if strToInt(s[2]+s[3])<=20 then
play('numbers\'+s[2]+s[3]+'.wav',0)
else
if (strToInt(s[2]+s[3])>20) then
begin
play('numbers\'+s[2]+'0.wav',0);
if s[3]<>'0' then play('numbers\'+s[3]+'.wav',0);
end;
end;
if n>1000 then
for i:=1 to length(s) do
play('numbers\'+s[i]+'.wav',0);
end;

function ExtractName(source: string): string;
begin
ExtractName:=copy(source,1,pos(':',source)-1);
end;

function ExtractBlock(source: string): string;
var s: string;
begin
s:=copy(source,pos(':',source)+1,length(source));
delete(s,pos('@',s),length(s));
extractBlock:=s;
end;

function ExtractMap(source: string): string;
begin
ExtractMap:=copy(source,pos('@',source)+1,length(source));
end;

function inList(list: PStringList; name: string; aBlock: string; aMap: string): boolean;
var i: integer;
s: string;
begin
inList:=false;
for i:=0 to list^.Count-1 do
begin
s:=list^[i];
if (name=ExtractName(s)) and (aBlock=ExtractBlock(s)) and (aMap=ExtractMap(s)) then
inList:=true;
end;
end;

function findItem(name: string): boolean;
var i: integer;
begin
findItem:=false;
for i:=0 to player.item.Count-1 do
if name=player.item[i] then
findItem:=true;
if player^.attack=name then
finditem:=true;
if player^.defense=name then
finditem:=true;
end;

procedure DeleteItem(name: string);
var i: integer;
begin
for i:=0 to player.item.Count-1 do
if name=player.item[i] then
player.item.Delete(i);
end;

function subValues(source: string): integer;
var i: integer;
begin
subValues:=0;
i:=0;
while pos(',',source)<>0 do
begin
inc(i);
delete(source,1,pos(',',source));
end;
if (source<>'') and (i=0) then
i:=1;
inc(i);
subValues:=i;
end;

function subValue(source: string; n: integer): string;
var i: integer;
s: string;
begin
i:=1;
if pos(',',source)=0 then
begin
subValue:=source;
exit;
end;
if n=1 then
begin
subValue:=copy(source,1,pos(',',source)-1);
exit;
end;
while pos(',',source)<>0 do
begin
if n<>1 then inc(i);
delete(source,1,pos(',',source));
if i=n then
begin
s:=source;
if pos(',',s)<>0 then
delete(s,pos(',',s),length(s));
subValue:=s;
end;
end;
end;

function qntItem(name: string): integer;
var i: integer;
n: integer;
begin
qntItem:=0;
n:=0;
for i:=0 to player.item.Count-1 do
if player.item[i]=name then inc(n);
qntItem:=n;
end;

begin
Rebuild;
end.
