home *** CD-ROM | disk | FTP | other *** search
- Program ObjAdv;
-
- Uses
- CRT;
-
- Const
- Blank : Char = ' ';
-
- Type
- Location = Object
- X,
- Y : Byte;
- Symbol : Char;
- Constructor Init ( newChar : Char; newX, newY : Byte );
- Function GetX : Byte;
- Function GetY : Byte;
- Procedure Move ( newX, newY : Byte );
- Procedure Show; Virtual;
- Procedure Hide; Virtual;
- End;
- ThingPtr = ^Thing;
- Thing = Object ( Location )
- Name : String;
- Data : ShortInt;
- Constructor Init ( newName : String; newChar : Char; newX,
- newY : Byte );
- Procedure Get;
- Procedure Put ( newX, newY : Byte );
- Function Swing : Byte; Virtual;
- Function GetAC : ShortInt; Virtual;
- Procedure SetAC ( newAC : ShortInt ); Virtual;
- End;
- Weapon = Object ( Thing )
- Constructor Init ( newName : String; newX, newY : Byte;
- newDamage : ShortInt );
- Function Swing : Byte; Virtual;
- End;
- Armor = Object ( Thing )
- Constructor Init ( newName : String; newX, newY : Byte;
- newAC : ShortInt );
- Function GetAC : ShortInt; Virtual;
- Procedure SetAC ( newAC : ShortInt ); Virtual;
- End;
- BeingPtr = ^Being;
- Being = Object ( Location )
- Name : String;
- BaseDamage,
- Health,
- Weilding,
- Wearing : Byte;
- Inventory : Array [1..20] of ThingPtr;
- Constructor Init ( newName : String; newX, newY : Byte;
- newDamage, newHealth, newWeapon,
- newArmor : Byte );
- Procedure TakeHit ( toHit, damage : Byte );
- Function Swing : Byte;
- Procedure Weild ( obj : ThingPtr );
- Procedure Wear ( obj : ThingPtr );
- End;
- Player = Object ( Being )
- Constructor Init ( newName : String; newX, newY : Byte;
- newDamage, newHealth, newWeapon,
- newArmor : Byte );
- End;
- Monster = Object ( Being )
- Constructor Init ( newName : String; newX, newY : Byte;
- newDamage, newHealth, newWeapon,
- newArmor : Byte );
- End;
- Area = Object
- x1, y1,
- x2, y2 : Byte;
- ObjBeing : Array [1..20] of BeingPtr;
- Constructor Init ( newX1, newY1, newX2, newY2 : Byte );
- Procedure AddBeingToRoom ( bPtr : BeingPtr );
- Procedure MoveBeing ( Var creature : Being; posX,
- posY : Byte );
- Function Position ( locX, locY : Byte ) : BeingPtr;
- End;
-
- Var
- mw,
- pw : Weapon;
- ma,
- pa : Armor;
- pp : Player;
- mm : Monster;
- a : Area;
- ch : Char;
- status : Boolean;
- windY : Byte;
-
- Procedure SwitchWindow;
- { This procedure allows the program to switch between the status }
- { window and the playing window. The global variable STATUS is }
- { used to indicate which is the current window. When switching }
- { back to the playing window, WINDY is updated to contain the }
- { current Y position in the status window. }
-
- Begin
- status := Not status;
- If status Then
- Begin
- Window ( 61, 1, 80, 25 ); { Activate the status window }
- TextColor ( Black );
- TextBackground ( LightGray );
- GotoXY ( 1, windY );
- End
- Else
- Begin
- windY := WhereY; { Store the status Window Y }
- Window ( 1, 1, 60, 25 ); { Activate the player window }
- TextColor ( LightGray );
- TextBackground ( Black );
- End;
- End;
-
-
- { /////////////////////////////////////////////////////////////////// }
- { //////////////////////// Location's Methods /////////////////////// }
- { /////////////////////////////////////////////////////////////////// }
- {
- X,
- Y : Byte;
- Symbol : Char;
- }
-
- Constructor Location.Init ( newChar : Char; newX, newY : Byte );
- { Initialize the instance of Location }
- Begin
- X := NewX;
- Y := NewY;
- Symbol := NewChar;
- End;
-
- Procedure Location.Move ( newX, newY : Byte );
- { Move the X,Y position of the instance. There is a special case }
- { involved with an instance of location. If the X and Y values are }
- { 0, then the instance is not to be drawn. The reason for this is }
- { that the instance has been destroyed or is in possesion of the }
- { player. }
- Begin
- Hide;
- X := NewX;
- Y := NewY;
- If NOT ( ( X = 0 ) AND ( Y = 0 ) ) Then
- Show;
- End;
-
- Function Location.GetX : Byte;
- { Return the value of the X field }
- Begin
- GetX := X;
- End;
-
- Function Location.GetY : Byte;
- { Return the value of the Y field }
- Begin
- GetY := Y;
- End;
-
- Procedure Location.Show;
- { Write the symbol of the instance on the screen at the location }
- { stored in fields X and Y. }
- Begin
- GotoXY ( X, Y );
- Write ( Symbol );
- End;
-
- Procedure Location.Hide;
- { Remove the symbol of the instance from the screen at the location }
- { stored in fields X and Y. }
- Begin
- GotoXY ( X, Y );
- Write ( Blank );
- End;
-
- { /////////////////////////////////////////////////////////////////// }
- { ///////////////////////// Thing's Methods ///////////////////////// }
- { /////////////////////////////////////////////////////////////////// }
- {
- X,
- Y : Byte;
- Symbol : Char;
- Name : String;
- Data : ShortInt;
- }
-
- Constructor Thing.Init ( newName : String; newChar : Char; newX,
- newY : Byte );
- { Initialize the instance of Thing. Also, call Thing's immediate }
- { ancestor to initialize it's fields. }
- Begin
- Location.Init ( newChar, newX, newY );
- Name := NewName;
- Data := ShortInt ( $FF );
- End;
-
- Procedure Thing.Get;
- { Unused in this program, but can be used later for picking up }
- { things that are in an Area. }
- Begin
- Location.Move ( 0, 0 );
- End;
-
- Procedure Thing.Put ( newX, newY : Byte );
- { Unused in this program, but can be used later for droping }
- { things in an Area. }
- Begin
- Location.Move ( newX, newY );
- End;
-
- Function Thing.Swing : Byte;
- { Defined as a base value for all things. Default value of a }
- { swing is set to 0. If a descendant object is to be usable }
- { as a weapon, this method should be replaced. }
- Begin
- Swing := 0;
- End;
-
- Function Thing.GetAC : ShortInt;
- { Defined as a base value for all things. Default value of AC }
- { is set to 0. If a descendant object is to be usable as Armor }
- { then this method should be replaced. }
- Begin
- GetAC := 0;
- End;
-
- Procedure Thing.SetAC ( newAC : ShortInt );
- { Unused in this program. This method is a base for all things. }
- { If a descendant object is to be usable as Armor, then this }
- { method should be replaced. }
- Begin
- End;
-
- { /////////////////////////////////////////////////////////////////// }
- { //////////////////////// Weapon's Methods ///////////////////////// }
- { /////////////////////////////////////////////////////////////////// }
- {
- X,
- Y : Byte;
- Symbol : Char;
- Name : String;
- Data : ShortInt;
- }
-
- Constructor Weapon.Init ( NewName : String; NewX, NewY : Byte;
- NewDamage : ShortInt );
- { Initialize the instance of Weapon. Then call Weapon's immediate }
- { ancestor to initialize it's values. }
- Begin
- Thing.Init ( NewName, '|', NewX, NewY );
- Data := NewDamage;
- End;
-
- Function Weapon.Swing : Byte;
- { This is the replacement procedure for Thing. This will compute }
- { a random amount of damage that the swinging of the weapon will }
- { produce. The random value is based on the DATA field of the }
- { object. }
- Begin
- Swing := Random ( data ) + 1;
- End;
-
- { /////////////////////////////////////////////////////////////////// }
- { ///////////////////////// Armor's Methods ///////////////////////// }
- { /////////////////////////////////////////////////////////////////// }
- {
- X,
- Y : Byte;
- Symbol : Char;
- Name : String;
- Data : ShortInt;
- }
-
- Constructor Armor.Init ( NewName : String; NewX, NewY : Byte;
- NewAC : ShortInt );
- { Initialize the instance of Armor. Then call Armor's immediate }
- { ancestor to initialize it's values. }
- Begin
- Thing.Init ( NewName, '#', NewX, NewY );
- Data := NewAC;
- End;
-
- Function Armor.GetAC : ShortInt;
- { This is the replacement procedure for Thing. This will return }
- { the Armor Class of the instance. The AC is stored in the DATA }
- { field of the object. }
- Begin
- GetAC := Data;
- End;
-
- Procedure Armor.SetAC ( newAC : ShortInt );
- { Unused in this program. This is the replacement procedure for }
- { Thing. This could be used to modify the Armor Class of the }
- { instance. The AC is stored in the DATA field of the object. }
- Begin
- Data := newAC;
- End;
-
- { /////////////////////////////////////////////////////////////////// }
- { ///////////////////////// Being's Methods ///////////////////////// }
- { /////////////////////////////////////////////////////////////////// }
- {
- X,
- Y : Byte;
- Symbol : Char;
- Name : String;
- BaseDamage,
- Health,
- Weilding,
- Wearing : Byte;
- Inventory : Array [1..20] of ThingPtr;
- }
-
- Constructor Being.Init ( NewName : String; NewX, NewY : Byte; NewDamage,
- NewHealth, NewWeapon, NewArmor : Byte );
- { Initialize the instance of Being. Also, call Being's immediate }
- { ancestor to initialize it's fields. }
- Begin
- Location.Init ( Symbol, NewX, NewY );
- Name := NewName;
- BaseDamage := NewDamage;
- Health := NewHealth;
- Weilding := NewWeapon;
- Wearing := NewArmor;
- FillChar ( Inventory, SizeOf ( Inventory ), 0 );
- End;
-
- Procedure Being.TakeHit ( ToHit, Damage : Byte );
- { This procedure takes two parameters. The first parameter is }
- { a number from 1-100 that indicates whether this instance of }
- { being will be hit. The second parameter determines how much }
- { damage the instance will take for the hit. }
- Var
- tmp : Byte;
- Begin
- If ( Wearing = 0 ) Then
- tmp := 10
- Else
- tmp := Inventory [Wearing]^.GetAC;
- tmp := 10 * ( 10 - tmp ); { Compute the Armor class of the instance }
- SwitchWindow;
- If ( ToHit <= tmp ) Then
- Begin
- WriteLn ( 'A hit! For ',Damage,' points of damage!' );
- If ( ( health - damage ) <= 0 ) Then
- Begin
- health := 0;
- SwitchWindow;
- hide;
- SwitchWindow;
- WriteLn ( 'You killed the monster.' );
- End
- Else
- health := health - damage;
- End
- Else
- WriteLn ( 'A miss.' );
- SwitchWindow;
- End;
-
- Function Being.Swing : Byte;
- { Return how much damage the instance will give for an attack }
- { with the current weapon of the instance. }
- Var
- tmp : Byte;
- Begin
- tmp := Inventory [Weilding]^.Swing;
- If ( tmp = 0 ) Then
- tmp := Random ( 4 ) + 1;
- Swing := tmp;
- End;
-
- Procedure Being.Weild ( obj : ThingPtr );
- { Set the weapon that the instance is going to weild }
- Var
- tmp : Byte;
- Begin
- tmp := 1;
- While ( tmp <= 20 ) AND ( Inventory [tmp] <> NIL ) Do
- Inc ( tmp );
- If ( tmp > 20 ) Then
- WriteLn ( 'Overloaded, can not weild that item' )
- Else
- Begin
- Inventory [ tmp ] := obj;
- Weilding := tmp;
- End;
- End;
-
- Procedure Being.Wear ( obj : ThingPtr );
- { Set the armor class that the instance will wear }
- Var
- tmp : Byte;
- Begin
- tmp := 1;
- While ( tmp <= 20 ) AND ( Inventory [tmp] <> NIL ) Do
- Inc ( tmp );
- If ( tmp > 20 ) Then
- WriteLn ( 'Overloaded, can not wear that item' )
- Else
- Begin
- Inventory [ tmp ] := obj;
- Wearing := tmp;
- End;
- End;
-
- { /////////////////////////////////////////////////////////////////// }
- { //////////////////////// Player's Methods ///////////////////////// }
- { /////////////////////////////////////////////////////////////////// }
- {
- X,
- Y : Byte;
- Symbol : Char;
- Name : String;
- BaseDamage,
- Health,
- Weilding,
- Wearing : Byte;
- Inventory : Array [1..20] of ThingPtr;
- }
-
- Constructor Player.Init ( newName : String; newX, newY : Byte; newDamage,
- newHealth, newWeapon, newArmor : Byte );
- { Initialize the instance of Player. Also, call Player's immediate }
- { ancestor to initialize it's fields. }
- Begin
- Symbol := '@'; { This is the symbol that the player is represented by }
- Being.Init ( newName, newX, newY, newDamage, newHealth, newWeapon,
- newArmor );
- End;
-
- { /////////////////////////////////////////////////////////////////// }
- { //////////////////////// Monster's Methods //////////////////////// }
- { /////////////////////////////////////////////////////////////////// }
- {
- X,
- Y : Byte;
- Symbol : Char;
- Name : String;
- BaseDamage,
- Health,
- Weilding,
- Wearing : Byte;
- Inventory : Array [1..20] of ThingPtr;
- }
-
- Constructor Monster.Init ( newName : String; newX, newY : Byte; newDamage,
- newHealth, newWeapon, newArmor : Byte );
- { Initialize the instance of Monster. Also, call Monster's immediate }
- { ancestor to initialize it's fields. }
- Begin
- Symbol := '*'; { This is the symbol that the monster is represented by }
- Being.Init ( newName, newX, newY, newDamage, newHealth, newWeapon,
- newArmor );
- End;
-
- { /////////////////////////////////////////////////////////////////// }
- { ////////////////////////// Area's Methods ///////////////////////// }
- { /////////////////////////////////////////////////////////////////// }
- {
- ObjBeing : Array [1..20] of BeingPtr;
- }
-
- Constructor Area.Init ( newX1, newY1, newX2, newY2 : Byte );
- { Initialize the instance of Area. Area represents a room. }
- { The parameters determine the boundries of the room. }
- Begin
- FillChar ( ObjBeing, SizeOf ( ObjBeing ), 0 );
- x1 := newX1;
- y1 := newY1;
- x2 := newX2;
- y2 := newY2;
- End;
-
- Procedure Area.AddBeingToRoom ( bPtr : BeingPtr );
- { Add an instance of Being to the Area. }
- Var
- tmp : Byte;
- Begin
- tmp := 1;
- While ( tmp <= 20 ) AND ( ObjBeing [tmp] <> NIL ) Do
- Inc ( tmp );
- If ( tmp <= 20 ) Then
- ObjBeing [ tmp ] := bPtr;
- End;
-
- Function Area.Position ( locX, locY : Byte ) : BeingPtr;
- { Return an instance of Being (if any) at the X,Y position }
- { specified. NIL will be returned if no instance exists at }
- { the specified location. }
- Var
- tmp : Byte;
- Begin
- tmp := 1;
- While ( tmp <= 20 ) AND NOT ( ( ObjBeing [tmp]^.X = locX ) AND
- ( ObjBeing [tmp]^.Y = locY ) ) AND ( ObjBeing [tmp] <> NIL ) Do
- Inc ( tmp );
- If ( tmp <= 20 ) AND ( ObjBeing [tmp] <> NIL ) AND
- ( ObjBeing [tmp]^.Health > 0 ) Then
- Position := ObjBeing [tmp]
- Else
- Position := NIL;
- End;
-
- Procedure Area.MoveBeing ( Var creature : Being; posX, posY : Byte );
- { Move an instance of Being to a new X,Y location if no other }
- { instance is at that location. If a being is at that location, }
- { then attack it. }
- Var
- tmpNum : Byte;
- tmpObj : BeingPtr;
- Begin
- tmpNum := 1;
- While ( tmpNum <= 20 ) AND ( @creature <> ObjBeing [tmpNum] ) Do
- Inc ( tmpNum );
- If ( tmpNum <= 20 ) Then
- Begin
- If ( posX >= x1 ) AND ( posX <= x2 ) AND
- ( posY >= y1 ) AND ( posY <= y2 ) Then
- Begin
- tmpObj := position ( posX, posY );
- If ( tmpObj = NIL ) Then
- creature.Move ( posX, posY )
- Else
- tmpObj^.TakeHit ( Random ( 100 ), creature.Swing );
- End;
- End;
- End;
-
- Begin
- mw.Init ( 'Long Sword', 0, 0, 8 );
- ma.Init ( 'Leather', 0, 0, 8 );
- mm.Init ( 'Orc', 10, 10, 4, 8, 0, 0 );
- mm.Wear ( @ma );
- mm.Weild ( @mw );
-
- pw.Init ( 'Long Sword', 0, 0, 8 );
- pa.Init ( 'Chain', 0, 0, 6 );
- pp.Init ( 'Elf', 14, 14, 4, 8, 0, 0 );
- pp.Wear ( @pa );
- pp.Weild ( @pw );
-
- a.Init ( 1, 1, 60, 24 );
- a.AddBeingToRoom ( @mm );
- a.AddBeingToRoom ( @pp );
-
- Randomize;
- status := TRUE;
- SwitchWindow; { Initialize the playing window }
- ClrScr;
- SwitchWindow; { Initialize the status window }
- ClrScr;
- SwitchWindow; { Activate the playing window }
- pp.Show;
- mm.Show;
- Repeat
- ch := ReadKey;
- Case ch of
- #0 : Begin
- ch := ReadKey;
- Case ch of
- #72 : Begin { Up }
- a.MoveBeing ( pp, pp.GetX, pp.GetY-1 );
- End;
- #75 : Begin { Left }
- a.MoveBeing ( pp, pp.GetX-1, pp.GetY );
- End;
- #77 : Begin { Right }
- a.MoveBeing ( pp, pp.GetX+1, pp.GetY );
- End;
- #80 : Begin { Down }
- a.MoveBeing ( pp, pp.GetX, pp.GetY+1 );
- End;
- End;
- End;
- End;
- Until ( ch = #27 )
- End.