Chaosforge Forum

Coding => FPC Valkyrie => Topic started by: konijn on March 15, 2007, 07:15

Title: Some Pascal Object help
Post by: konijn on March 15, 2007, 07:15
I could find it on Google I guess,
but my questions here could illicit some design advice as well.

I am planning to have door tiles with a door Entity, so that when the player bumps into the door , the door knows it should change the tile it is on to 'open door' and remove itself from the map ( destroy itself ? ).

Now ( starting from the roguelike tutorial ):

- Is it possible to assign 1 door entity to several different cells to save memory ( in that case I would not destroy the entity )
- How do I know if object x is an instance of class y ? What is the keyword in other words ?
- How do I pass the bumper to the entity ( do I use self or this or still some other keyword ? )

if Cells[tx,ty].Entity <> nil then
  Cells[tx,ty].Entity.bump( this or self or myself or what ? )
  exit(False);
end;

-How does one do casting of an object ?

Cheers,
T.
Title: Re: Some Pascal Object help
Post by: Kornel Kisielewicz on March 15, 2007, 11:47
I could find it on Google I guess,
but my questions here could illicit some design advice as well.

I am planning to have door tiles with a door Entity, so that when the player bumps into the door , the door knows it should change the tile it is on to 'open door' and remove itself from the map ( destroy itself ? ).
First of all -- the biggest error people make with real-world OOP usage is overusing OOP. Why do you need an object for a door at all? Make it a normal tile, add it an "OPENABLE" flag and OpensTo : {TileID} and check it when you check bumping into objects.

Now ( starting from the roguelike tutorial ):

- Is it possible to assign 1 door entity to several different cells to save memory ( in that case I would not destroy the entity )

I keep all cell data in a flat record... Example :
Code: (delphi) [Select]
// Terrain data -- collection of tile definitions. Tile 0 is special -- it's
// not supposed to be used, but is there to prevent and identify errors.
const TerraData : array[0..MAXTILES] of TTerrainData = (
        ( Name : 'void';         Picture : Ord(' ')+256*LightGray;
          DarkPic : Ord(' ')+256*DarkGray; DR: 0; DestroyID : 0; ActID : 0; Flags : [TF_NOMOVE,TF_NOSIGHT]),
        ( Name : 'floor';        Picture : Ord('.')+256*LightGray;
          DarkPic : Ord('.')+256*DarkGray; DR: 0; DestroyID : 0; ActID : 0; Flags : []),
        ( Name : 'wall';         Picture : Ord('#')+256*LightGray;
          DarkPic : Ord('#')+256*DarkGray; DR: 20;DestroyID : 9; ActID : 0; Flags : [TF_NOMOVE,TF_NOSIGHT]),
        ( Name : 'closed door';  Picture : Ord('-')+256*LightGray;
          DarkPic : Ord('-')+256*DarkGray; DR: 0; DestroyID : 0; ActID : 4; Flags : [TF_NOMOVE,TF_NOSIGHT,TF_OPENABLE]),
        ( Name : 'open door';    Picture : Ord('/')+256*LightGray;
          DarkPic : Ord('-')+256*DarkGray; DR: 10;DestroyID : 9; ActID : 3; Flags : []),
          ...

- How do I know if object x is an instance of class y ? What is the keyword in other words ?

Code: (delphi) [Select]
if Node.inheritsFrom(TBeing) then ... ; // True if Node inherits from TBeing
This has the added benefit that if TPlayer descends from TBeing this will also be true.
inheritsFrom is a built-in FreePascal Class function.

- How do I pass the bumper to the entity ( do I use self or this or still some other keyword ? )

Self.

if Cells[tx,ty].Entity <> nil then
  Cells[tx,ty].Entity.bump( this or self or myself or what ? )
  exit(False);
end;
I don't know what you want to achieve here :|

-How does one do casting of an object ?
What do you mean by casting? This?:

Code: (delphi) [Select]
procedure TLevel.Tick;
var Node : TNode;
begin
  Node := Child;
  if Node <> nil then
  repeat
    TBeing(Node).Tick;  // <-- this is casting?
    Node := Node.Next;
  until Node = Child;
end;
Title: Re: Some Pascal Object help
Post by: konijn on March 15, 2007, 13:06
Thanks Kornel!

Wow,

responding with a quote that quotes a post is tough ;)

Anyway,
- Doors in HaH are in a flat structure, the entity just changes closed door to open door, since I didnt want too many flags in my flat structure ( I am now tempted to undo that )
- Flags : [TF_NOMOVE,TF_NOSIGHT] is really interesting, what would be the type of Flags ? a set ?
- Casting , if you get an object x of class TNode, but you know that really it is also of class TPlayer, how do I force it into a variable y of class TPlayer ? just y := TPlayer(x)?

My RL ( I undid my decision for WesnothRL ) is advancing slowly, but it feels good to do Pascal again. The habits are slowly returning. Where are the days I did Pascal homeworks for money ;)

He Who Sheds Light,
T.
Title: Re: Some Pascal Object help
Post by: Kornel Kisielewicz on March 15, 2007, 13:52
- Flags : [TF_NOMOVE,TF_NOSIGHT] is really interesting, what would be the type of Flags ? a set ?
Yeah, there should be even two types in the vutil.pas:
Code: (delphi) [Select]
TFlags = set of Byte; // This one can hold 256 flags and takes 8 bytes memory
TFlags32 = set of [0..31];// This one can hold 32 flags but takes only 4 bytes

The cool thing about it is that you can write things like :
Code: (delphi) [Select]
if FLAG_NOMOVE in Cell.Flags then ...
Include(Flags,FLAG_NOMOVE);
And use other set operators on it (sets are something that C++ people can envy us). See the FP documentation on sets.

- Casting , if you get an object x of class TNode, but you know that really it is also of class TPlayer, how do I force it into a variable y of class TPlayer ? just y := TPlayer(x)?
Yes :).

My RL ( I undid my decision for WesnothRL ) is advancing slowly, but it feels good to do Pascal again. The habits are slowly returning. Where are the days I did Pascal homeworks for money ;)
I'm sorry I didn't update Valkyrie before the 7DRL :(. I feel kinda' guilty :(.
Title: Re: Some Pascal Object help
Post by: konijn on March 17, 2007, 06:32

My RL ( I undid my decision for WesnothRL ) is advancing slowly, but it feels good to do Pascal again. The habits are slowly returning. Where are the days I did Pascal homeworks for money ;)
I'm sorry I didn't update Valkyrie before the 7DRL :(. I feel kinda' guilty :(.

Eh, no worries, I would most likely still not have made it ;)
Also, I was compiling from my USB stick so that I could work on several laptops,
once I started compiling on my hd, it flies ;)

I have a fixed, map, @ walking around, things to pick up, doors, so all I need now is some monsters and los / AI ;)

For TNode I was going to propose the method Has( child: TNode ) but really one could also say
if child.Parent = whoeverIthinkMyParent is. It is up to you ;)

Cheers,
T.
Title: Re: Some Pascal Object help
Post by: Kornel Kisielewicz on March 17, 2007, 11:32
For TNode I was going to propose the method Has( child: TNode ) but really one could also say
if child.Parent = whoeverIthinkMyParent is. It is up to you ;)
For next version I'm considering adding some suga' to it so you could write like if Node in Parent then... xD. But I don't know if I can overload all needed components.