Wednesday, September 26, 2012

More PrintStatusLine stuff

Today, I got around to applying my new PrintStatusLine design to status-line-changing library extensions like newautomap.h and newconverse.h. It took some tweaking, but now with the current version of RoodyLib, it involves no finessing at all to have glk-enabled automap working in a game along with top-screen conversation menus. Just because I feel like my PrintStatusLine core code has changed just enough, I'm going to share the entire thing again. It might make some more sense if you are looking at a status-window-drawing extension along with this (like newautomap.h in http://roody.gerynarsabode.org/hbe/newautomap.zip), but maybe you can get some idea through the comments below:

! find_height - property that points to routines for determining status window height
property find_height alias u_to

! draw_window - routines with instructions for drawing the status window
property draw_window alias e_to

!\ bottom_justified - have this return true for status windows where regular
status information shares a window with other information and you want the
regular status information to be printed at the bottom of the window \!
property bottom_justified alias d_to

! terp_type - this property gets set to the current interpreter type automatically
property terp_type alias w_to

!\ status_override - normally, the status window object with the highest
find_height number gets drawn (so have those properties return 0 when not in
use), but sometimes more than one extension *could* be drawn, so we use
status_override to have one override the other (have it return true to initiate
such override) \!
property status_override alias nw_to

!\ chosen_window - set to the window instructions object whose draw_window
property will be executed. authors can ignore this. \!
property chosen_window alias nw_to

! "terp_type" values  0, 2, 4
enumerate step * 2
{
    NORMAL_TERP, GLK_TERP = 2, SIMPLE_TERP
}

!\ a PrintStatusLine object in which we will put our instruction objects in (by
inclusion of extensions and what not. PrintStatusLine will call this directly).
\!
object printstatuslib
{
    find_height
        {
        local highest, i, a
        for i in self
            {
                a = i.find_height
                if i.status_override
                    {
                    self.chosen_window = i
                    highest = i.find_height
                    break
                    }
            if higher(highest,a) = a
                    {
                    self.chosen_window = i
                    highest = i.find_height
                    }
            }
        return highest
        }
    draw_window
        {
        run (self.chosen_window).draw_window
        }
    chosen_window 0
    terp_type NORMAL_TERP
    bottom_justified 1
}

replace PrintStatusline
{
local newstatusheight

! set the "terp_type" value
if IsGlk
    printstatuslib.terp_type = GLK_TERP
elseif system(61) ! minimal port
        printstatuslib.terp_type = SIMPLE_TERP
else
    printstatuslib.terp_type = NORMAL_TERP

#ifset CHEAP
        if cheap and printstatuslib.terp_type ~= SIMPLE_TERP
            {
#if defined GAME_TITLE
            CenterTitle(GAME_TITLE,0,1)
#endif
#if undefined GAME_TITLE
            CenterTitle(CheapTitle,0,1)
#endif
            display.needs_repaint = false
            return
            }
        elseif cheap
            return
#endif ! CHEAP

! figure out the size our window will be
newstatusheight = printstatuslib.find_height

! clear/remove the window if the status window height has changed
if (newstatusheight < display.statusline_height) and not system(61)
    {
    window display.statusline_height
    {cls} ! clear whatever's there
    window 0
    }

display.statusline_height = newstatusheight

Font(BOLD_OFF | ITALIC_OFF | UNDERLINE_OFF | PROP_OFF)
window display.statusline_height
    {
    if printstatuslib.terp_type ~= SIMPLE_TERP
        {
          color SL_TEXTCOLOR, SL_BGCOLOR
        cls
        locate 1,1
        }
    run printstatuslib.draw_window
    }
color TEXTCOLOR, BGCOLOR, INPUTCOLOR
Font(DEFAULT_FONT)
}

! Here is an example status window instruction object. It and its routines
! draw a regular status window
object statuswindow
{
    in printstatuslib
    find_height
        {
        return (call &FindStatusHeight)
        }
    draw_window
        {
        return (call &WriteStatus)
        }
}
!\ Note: These properties *could* just say "return FindStatus". I just used
the above syntax to give a clue as to how one would change a value "mid-game".
If you want statuswindow.find_height to point to *another* routine, you could
have a line like this:
    statuswindow.find_height = call &FindNewStatusHeight  \!


! routine for finding the height of the regular status info
routine FindStatusHeight
{
local a, b
text to _temp_string
! can't start off a string with a space, it seems
!if not location     ! so we'll save this space-writing code for the
!    print "\_";     ! "status-writing" routine
!else
if not light_source
    print "In the dark";
else
    {
        print capital location.name;
         if FORMAT & DESCFORM_F
        print "\_";
    }
text to 0
a = StringLength(_temp_string)

text to _temp_string
select STATUSTYPE
    case 1 : print number score; " / "; number counter;
    case 2 : print HoursMinutes(counter);
    case 3 : print "Score: "; number score; "\_ "; "Moves: "; number counter;
    ! STATUSTYPE case 3 is the "Infocom"-style status
    case 4 : StatusType4 ! routine for configurable statusline
if (FORMAT & DESCFORM_F) and (printstatuslib.terp_type ~= GLK_TERP)
    print "\_";
text to 0
if STATUSTYPE
    b = StringLength(_temp_string)

if (b + a + 4)<display.screenwidth ! let's force a 4 character gap between
    {                              ! the two fields
    return 1
    }
elseif (b + a - 4 ) < display.screenwidth and STATUSTYPE = 3
    {
    text to _temp_string
    print "S: "; number score; "\_ "; "M: "; number counter;
    if (FORMAT & DESCFORM_F) and (printstatuslib.terp_type ~= GLK_TERP)
        print "\_";
    text to 0
    return 1
    }
else
    return 2
}

! Roody's note: Replace this if you want to use the top right area
! for something else ("HUNGRY", "TIRED", or whatever)
routine STATUSTYPE4
{}

! routine for drawing the regular status
routine WriteStatus
{
    if printstatuslib.bottom_justified and
        printstatuslib.terp_type ~= SIMPLE_TERP
        {
        if statuswindow.find_height = 2
            {
            locate 1, (display.windowlines - 1)
            }
        else
            locate 1, display.windowlines
        }
    if not location
        print "\_";
    elseif not light_source
        print "In the dark";
    else
        {
            if FORMAT & DESCFORM_F or (printstatuslib.terp_type = GLK_TERP)
                    print "\_";
            print capital location.name;
        }

    if statuswindow.find_height = 1 and STATUSTYPE
        {
            print to (display.linelength - \
            (StringLength(_temp_string) + \
            ((printstatuslib.terp_type = SIMPLE_TERP)*2) ));
            StringPrint(_temp_string)
        }
    elseif STATUSTYPE and statuswindow.find_height = 2
        {
            if printstatuslib.terp_type ~= SIMPLE_TERP and
            not printstatuslib.bottom_justified
                locate 1, 2
            else
                ""
            if (FORMAT & DESCFORM_F) or (printstatuslib.terp_type = GLK_TERP)
    print "\_";
            StringPrint(_temp_string)
        }
}

Monday, September 17, 2012

new debugging verb

The last time I was looking over roodylib's comments and documentation, I was reminded that I had added a new debugging verb (and had failed to mention it). Anyhow, it's called "verbtest", and it's pretty much a ripoff of something I saw that Juhana Leinonen wrote for Inform 7 the other year. Basically, you use it on an object, and it shows you the response to a ton of default verbs.

Here is an example transcript from a game I'm working on:
>verbtest house
Getting object:
You can't take that.

Wearing object:
You can't wear the house.

Listening to object:
The house is not making a sound.

Eating object:
You can't eat the house.

Drinking object:
You can't drink the house.

Hitting object:
Venting your frustrations on the house won't accomplish much.

Hello-ing object:
That doesn't make any sense.

Examining object:
Dark and silent.

Looking through object:
You can't see through that.

Looking under object:
You don't find anything under the house.

Go-ing object:
No, it's time to go home.

Entering object:
No, it's time to go home.

Sitting on object:
No, it's time to go home.

Exiting object:
You're not in the house.

Moving object:
You can't move the house.

Searching object:
You don't find anything new.

Smelling object:
You don't smell anything unusual.

>
The intent is that it'll remind authors of obvious commands that should have better responses.

Maybe I'll incorporate it into HugoFix at some point (giving it a $vt command or something), but that seems a bit presumptuous for now.

What's my PrintStatusLine?

I've been pretty good lately about coming up with roodylib improvements instead of working on my game. The last idea I had was especially challenging, and I have to admit, it took a couple days of pecking to truly get up the steam to finish it.

In a nutshell..

I've been annoyed that some of my library extensions have to replace PrintStatusLine. What do you do when two of your extensions replace PrintStatusLine? Of course, you hack together a version that works this way in some circumstances and that way in others. Like other parts of roodylib, I wondered if there wasn't a way to make it more modular.

Now, I probably could have done it all with multiple routines and maybe some global variables and arrays, but like my other "settings" in roodylib, I decided to make it object based. Extension PrintStatusLine objects have properties that point to routines that determine the height they need and instructions for drawing their portion of the status window.

Even when you aren't using fancy library extension stuff, you could make the status window bigger than usual and draw whatever you want in the extra space (and you can make the normal status part of the window "bottom justified" if you want).

I just finished this thing so hopefully I'll be able to pretty it up a little so it's easier to understand at some point, but it's uncertain that this version of PrintStatusLine will be readable to anyone but me, unfortunately. Let's take a look at what I wrote:
property find_height alias u_to
property draw_window alias e_to
property bottom_justified alias d_to
property terp_type alias w_to

! "terp_type" values  0, 2, 4
enumerate step * 2
{
    NORMAL_TERP, GLK_TERP = 2, SIMPLE_TERP
}

object printstatuslib
{
    find_height
        {
        local sum, i
        for i in self
            {
            sum += i.find_height
            }
        return sum
        }
    draw_window
        {
        local i
        for i in self
            {
            run i.draw_window
            }
        }
    terp_type NORMAL_TERP
    bottom_justified 0
}

! this object and its properties draw the status window as we normally know it
object statuswindow
{
    in printstatuslib
    find_height
        {
        return (call &FindStatusHeight)
        }
    draw_window
        {
        return (call &WriteStatus)
        }
}

replace PrintStatusline
{
local newstatusheight
#ifset CHEAP
    if cheap
        return
#endif

! set the "terp_type" value
if IsGlk
    printstatuslib.terp_type = GLK_TERP
elseif system(61) ! minimal port
        printstatuslib.terp_type = SIMPLE_TERP
else
    printstatuslib.terp_type = NORMAL_TERP

! figure out the size our window will be
newstatusheight = printstatuslib.find_height

! remove the windows if the status window height has changed
if (newstatusheight < display.statusline_height) and not system(61)
    window 0

display.statusline_height = newstatusheight

Font(BOLD_OFF | ITALIC_OFF | UNDERLINE_OFF | PROP_OFF)
window display.statusline_height
    {
    color SL_TEXTCOLOR, SL_BGCOLOR
    if printstatuslib.terp_type ~= SIMPLE_TERP
        {
        cls
        locate 1,1
        }
    run printstatuslib.draw_window
    }
color TEXTCOLOR, BGCOLOR, INPUTCOLOR
Font(DEFAULT_FONT)
}

! routine for finding the height of the regular status info
replace FindStatusHeight
{
local a, b
text to _temp_string
if not location
    print "\_";
elseif not light_source
    print "In the dark";
else
    {
        if FORMAT & DESCFORM_F:  print "\_";
        print capital location.name;
        print "\_";
    }
text to 0
a = StringLength(_temp_string)

if STATUSTYPE = 1
    {
    text to _temp_string
    if (FORMAT & DESCFORM_F)
        print "\_";
    print number score; " / "; number counter;
    if (FORMAT & DESCFORM_F)
        print "\_";
    text to 0
    }
elseif STATUSTYPE = 3
    {
    text to _temp_string
    if (FORMAT & DESCFORM_F) : print "\_";
    print "Score: "; number score; "\_ "; "Moves: "; number counter;
    if (FORMAT & DESCFORM_F) : print "\_";
    text to 0
    b = StringLength(_temp_string)
    }
elseif STATUSTYPE = 2
    {
    text to _temp_string
    if (FORMAT & DESCFORM_F) : print "\_";
    print HoursMinutes(counter);
    if (FORMAT & DESCFORM_F) : print "\_";
    text to 0
    }
elseif STATUSTYPE = 4
    {
    text to _temp_string
    if (FORMAT & DESCFORM_F) : print "\_";
    STATUSTYPE4 ! routine for configurable statusline
    if (FORMAT & DESCFORM_F) : print "\_";
    text to 0
    }

if (b + a + 4)<display.screenwidth ! let's force a 4 character gap between
    {                               ! the two fields
    return 1
    }
elseif (b + a - 4 ) < display.screenwidth and STATUSTYPE = 3
    {
    text to _temp_string
    if (FORMAT & DESCFORM_F) : print "\_";
    print "S: "; number score; "\_ "; "M: "; number counter;
    if (FORMAT & DESCFORM_F) : print "\_";
    text to 0
    return 1
    }
else
    return 2
}

! Roody's note: Replace this if you want to use the top right area
! for something else ("HUNGRY", "TIRED", or whatever)
replace STATUSTYPE4
{}

! routine for drawing the regular status
routine WriteStatus
{
    if printstatuslib.bottom_justified and
        printstatuslib.terp_type ~= SIMPLE_TERP
        {
        if statuswindow.find_height = 2
            {
            locate 1, (display.windowlines - 1)
            }
        else
            locate 1, display.windowlines
        }
    if not location
        print "\_";
    elseif not light_source
        print "In the dark";
    else
        {
            if FORMAT & DESCFORM_F:  print "\_";
            print capital location.name;
        }

    if statuswindow.find_height = 1 and STATUSTYPE
        {
            print to (display.screenwidth - \
            (StringLength(_temp_string) + \
            ((printstatuslib.terp_type = SIMPLE_TERP)*2)));
            StringPrint(_temp_string)
        }
    elseif STATUSTYPE and statuswindow.find_height = 2
        {
            if printstatuslib.terp_type ~= SIMPLE_TERP and
            not printstatuslib.bottom_justified
                locate 1, 2
            else
                ""
            StringPrint(_temp_string)
        }
}

So, say, if you're using newconverse's version of this (after I update it, that is), it'd point printstatuslib find_height and draw_window properties to either these routines or newconverse's status window instructions, depending on whether the player is in a conversation.

 Hopefully, this isn't too complicated.

One other thing...

I also threw together an extension for score notification, as I never have these things around when I need them. I'll add proper documentation and upload it somewhere at some point:

!::
! Hugo Score Notification extension
!::

!\
Provides text like "You score has gone up by [x] points!"
\!

#ifclear _SCORENOTIFY_H
#SET _SCORENOTIFY_H

#ifset VERSIONS
#message "ScoreNotify.h Version 0.5"
#endif

property score_notify alias d_to

property points alias e_to

object scorenotifylib "scorenotify"
{
    score_notify true
    points 0
#ifset _ROODYLIB_H
    save_info
        {
        select self.score_notify
            case 0 : SaveWordSetting("score_off")
            case 1 : SaveWordSetting("score_on")
        return true
        }
    type settings
    in init_instructions
    execute
        {
        local a
        a = CheckWordSetting("scorenotify")
        if a
            {
            select word[(a-1)]
                case "score_off": self.score_notify = 0
                case "score_on": self.score_notify = 1
            }
        }
#endif
#ifset _NEWMENU_H
    usage_desc
        {
        "\BSCORE NOTIFICATION ON\b- Be notified when you score points."
        Indent
        "\BSCORE NOTIFICATION OFF\b- Play without score notifications."
        }
#endif ! NEWMENU
}

#ifset _ROODYLIB_H
object scorenotifymain
{
    type settings
    in main_instructions
    execute
        {
        ScoreNotify
        }
}
#endif  ! _ROODYLIB_H

routine ScoreNotify
{
      if scorenotifylib.points and scorenotifylib.score_notify
           {
              ""
              Font(BOLD_ON)
           ScoreNotificationMessage(&ScoreNotify, 1, scorenotifylib.points ) ! "[Your score has gone up.]"
              Font(BOLD_OFF
           }
      score += scorenotifylib.points   ! add the points to the score
      scorenotifylib.points = 0    ! reset the point counter
}

! routine to call for the last score of a game (after the winning move), as
! main is not called again
routine LastScore(a)
{
    score += a
}

! otherwise, call this routine to add to the game score
routine AddScore(a)
{
    scorenotifylib.points += a
}

routine DoNotifyOnOff
{
      if scorenotifylib.score_notify
            Perform(&DoNotifyOff)
      else
            Perform(&DoNotifyOn)
}

routine DoNotifyOn
{
      if scorenotifylib.score_notify
           ScoreNotificationMessage(&DoNotifyOn, 1 ) ! "[Score notification already on.]"
      else
           {
           ScoreNotificationMessage(&DoNotifyOn, 2 ) ! "[Score notification on.]"
           scorenotifylib.score_notify = 1
           }
}

routine DoNotifyOff
{
      if not scorenotifylib.score_notify
           ScoreNotificationMessage(&DoNotifyOff, 1 ) ! "[Score notification already off.]"
      else
           {
           ScoreNotificationMessage(&DoNotifyOff, 2 ) ! "[Score notification off.]"
           scorenotifylib.score_notify = 0
           }
}

routine ScoreNotificationMessage(r, num, a, b)
{
    if NewScoreNotificationMessages(r, num, a, b):  return

    select r
        case &DoNotifyOn
            {
            select num
                case 1:  "[Score notification already on.]"
                case 2: "[Score notification on.]"
            }
        case &DoNotifyOff
            {
            select num
                case 1:  "[Score notification already off.]"
                case 2: "[Score notification off.]"
            }
        case &ScoreNotify
            {
            select num
                case 1 : "[Your score has gone up.]"
            }
}

!\ The NewScoreNotificationMessages routine may be REPLACED and should return
true if a replacement message exists for routine <r> \!

routine NewScoreNotificationMessages(r, num, a, b)
{
   select r
!    case &ScoreNotify
!        {
!        select num
!            case 1
!                {
!                print "[Your score has gone up by "; number a; " points.]"
!                }
!        }
   case else : return false
   return true ! this line is only reached if we replaced something
}

#endif _SCORENOTIFY_H
Actually, I haven't even compiled that code yet, so there might be a typo or other kind of bug in there.

Friday, September 14, 2012

library suggestions

So far, roodylib replaces 54 standard library routines or objects (that number might be off by some; I sort of counted carelessly by hand). Some of these cover justified inadequacies or bugs uncovered by others; some are only replaced since an object something inherits from was also replaced. Still, yet, there are those that I replace because I feel there is something wrong with the design of something, and as it is clear that much of Hugo is the result of fine reasoning, it is likely that my changes have flaws I have not yet considered.

Anyway, let's talk about some of these kinds of changes. For starters, I replaced the female_character class. In the standard library, that class gets its own type ('female_character"), so if you have a game where you are checking if objects are NPCs, you have to check for both "character" and "female_character". I changed it so female_character objects are of type "character", as you can always check for the female attribute if you are truly looking for a female NPC.

More recently, I was writing some code that assumed the player_character object was of type "player_character". Funnily enough, this was not the case (it is actually of type "character")! Now, maybe people won't agree with me on this one, but I feel the player_character should get its own type, so in roodylib, it currently does so.

Much further back, I changed up some of the vehicle class code so it'd be (hopefully) easier to change which directions can exit a vehicle:
replace vehicle
{
    type vehicle
    vehicle_verb "drive"            ! default verb
    prep "in", "out"                !   "     prepositions
    vehicle_move true               ! by default, always ready to move

#ifclear NO_VERBS
    before
    {
    parent(player) DoGo
        {
        if word[2] = "out" and object = self
            {
            object = out_obj
            return false
            }
        if (object ~= u_obj, out_obj) and object.type = direction
            {
            ! "To walk, you'll have to get out..."
            OMessage(vehicle, 1, self)
            return true
            }
        else
            return object
        }
    }
#endif
    is enterable, static
}
(DoGo also has some slightly different code, I believe)

Lately, I also was looking at something whose design I disagree with but will do nothing about. I was taking a look at GetInput, which is a short and simple routine:
routine GetInput(p)
{
    print p;
    input
}
Personally, I'd think that you'd only call GetInput when you want to also provide a prompt, so it should be something like this:

routine GetInput(p)
{
    if p
        print p;
    else
        print prompt;
    input
}
 My logic is, hey, only call GetInput when you want a prompt of some point. Otherwise, why not just call input directly? Thing is, the standard library (and other existing code) call GetInput without a prompt argument a lot. I could either change all of those library routines (breaking lots of game source, too, in the process, probably) or just ignore it. I'll just do the latter. I could add a slightly-similar-sounding routine to roodylib that behaves like I feel GetInput should, but hey, I admit this isn't an important problem...

So yeah, that.

In other news, I updated Activate, the routine for starting daemons, so it complains if it is called before the player global has been set (I've run into that problem once or twice):
replace Activate(a, set)                ! <set> is for fuses only
{
    local err
    if not player
        {
        Font(BOLD_ON)
        print "[WARNING:  The player global must be set before
        daemon (object "; number a;") can be activated.]"
        err = true
        }
    a.in_scope = player
    a is active
    if a.type = fuse and not err
    {
        if set
            a.timer = set

        run a.activate_event
    }
    elseif a.type = daemon and not err
    {
        if set and not a.#timer
        {
            Font(BOLD_ON)
            print "[WARNING:  Attempt to set nonexistent timer
                property on daemon (object "; number a; ")]"
            err = true
        }
        else
            a.timer = set

        run a.activate_event
    }
    elseif not err
    {
        Font(BOLD_ON)
        print "[WARNING:  Attempt to activate non-fuse/\
        daemon (object "; number a; ")]"
        err = true
    }

#ifset DEBUG
    if debug_flags & D_FUSES and not err
    {
        print "[Activating "; a.name; " "; number a;
        if a.type = fuse
            print " (timer = "; number a.timer; ")";
        print "]"
    }
#endif
    if err
        {
        Font(BOLD_OFF)
        "\npress a key to continue..."
        HiddenPause
        }
    return (not err)
}
So yeah, that's all some stuff.

Wednesday, September 5, 2012

just saying hey

I don't have any inspired motivation behind this post, but I want this blog to feel active so I'll just throw together a post on recent developments.

First off, I found my first in-Hugo's-innards bug. Like, I've found library bugs or interpreter inconsistencies before, but this was the first bug that goes back to the engine code. Anyhow, it involves the dictionary-word total not being correctly reset between game restarts. It's not a huge bug, so it likely won't prompt a new Hugo release very soon but it's nice to add another reason to have a new official release (still, considering some projects I know of will use adding-to-the-dictionary-table intensively, a sooner fix will be appreciated regardless).

So, other than that, I figured I'd pad this post out with some utility routines I am considering adding to roodylib. I wrote them for my WIP, which should hopefully be ready for testing within a week or so.

The first routine would fall in the category of "auxiliary math routines." When I am doing games with windows and/or graphics, I find myself saying, well, I'd like it to do this if the screen has this aspect ratio and I'd like to do that if it has that aspect ratio. So, I'm like, ok, do this if the screenwidth is between this and that number. Anyhow, after wanting some approximation code for a while, I finally threw together a simple routine for such things:
routine IsNear(fir,sec,range)
{
    if (abs(fir - sec) <= range)
        return true
    else
        return false
}

So you'd do something like IsNear(display.linelength, 40, 10) to find out if the line length is within 10 spots of 40. Anyhow, I figure it'd be useful beyond screen stuff, sometimes.

The other routine I'm considering adding is a line-only version of CenterTitle, that only draws the text given to it center-justified. If the text is too long for the screen width, it just does a normal indentation to show that the text is special.

routine CenterLine(a)
{
    print newline ! make sure we are at the start of a line
    local l
    l = string(_temp_string, a)
    if l < display.linelength
        print to (display.linelength / 2 - l/2);
    else    ! if we don't have enough line width to center it, we'll show the
        Indent    ! line's importance by indenting
    print a
}

Centering when you don't have proportional text off is an uncertain gamble, but still, I thought there might be a demand for such a routine.

When roodylib is much more mature, I'll get around to adding switches or whatever so a lot of these routines are only compiled when they are wanted, but that's for another day.