It’s been a long week, and I had to implement two whole sub-systems during this time: AI and Window. Both of them are currently a work in progress, but they have produced tangible results on screen. Building out these two systems, I have faced a fair share of problems which will be discussed below. This will be a fairly long read.
In my last update that was two weeks ago, I had partially drawn the first level. I’m glad to say that now I can draw the complete, as one can see below.
As you can see, I can now:
- Completely Background, Foreground and Grating tiles
- Scroll the map
- Initiate Dialog messages
By far, the AI subsystem has been the biggest subsystem I have encountered so far. With multiple Struct types, constant data and member functions, it defines not only how the characters in the game behave, but also Animation and Drawing code for all of them.
In a nutshell, this is how the AI subsystem works: Each object in the game that is more than just a drawn image is represented by the
AIEntity struct. The definition for this struct can be seen below:
As you can see, it contains Enums for defining Entity Type, State, Direction, Entity position, velocity as well as range of function pointers for Initialization, Drawing and possible Actions. A major portion of the AI subsystem is dedicated towards manipulating Entities of the type AIEntity.
The AI subsystem also maintains a series of lists for processing different events such as Actions, AutoActions etc.
While it is a part of the AI subsystem, I believe it deserves a separate mention. Basically, the Cinematic System is designed to run scripted interactions between Entities - the HDB equivalent cutscenes in a way.
Here’s how it works: The Cinematic System is a giant state machine that manages a queue of
CineCommand values. Each value is equivalent to a call made in the current Lua file. It tells us which interaction is to be executed along with details of the interaction if any.
Once the Lua script call is made, it triggers a function that inserts the corresponding
CineCommand value into the queue. While the Cinematic System is active, at each frame, it will go through each command in the queue and process them. If a command is completed, it will discarded from queue. If not, it just moves on to the next one.
WaitUntilDone command is implemented using this mechanism. If there are any commands before it, it fails to register as completed. Once there are no others before, it registers as complete and the System moves onto the next one if one is there.
The Window class draws all windows, dialogs and messages in the game. I had to extend the
draw-manager with functions like
drawTextto render text on screen. Along the way, I also added getter-setter functions for the Cursor, Kerning and Leading of the font.
Currently, this class is being used to draw Dialog messages as you can see above. If you look closely, you may see that there is a artifact problem with Dialogs which will be described in depth below.
In the future, the Deliveries List, the Inventory and Dialog Choice boxes will be added.
Here is an account of the few problems I’ve had over the past week.
Blitting and Clipping
For the purpose of Blitting Pictures and Tiles, I have introduced a Global Surface. It keeps a track of the of the entire screen and calls the
g_system->copyRectToScreen function which keeps a track of the dirty rects and blits only them.
At one point, the Surfaces started to throw out assertions since the Suface being blitting overstepped the bounds of the Global Surface.
I ran into a series of Read Access Violations due to reading data into initialized struct pointers. As a result, we have developed a rule to add Constructors to all structs that I define.
Struct Arrays instead of Pointer Arrays
In the beginning, I was keeping a number of struct arrays either in the form of
T *arr[size], or using
Common::Array<> with the general understanding that it would be easier to reference the structs by pointers rather the value. However, since then I have realized that they merely serve to hold data and not modify them. Moreover, there are a number of them and initializing each and every one of them was getting hectic. So, I have
Cinematic System and Blocking
As you can see above, the Cinematic System is getting blocked while executing the
C_MOVEENTITY command for Sarge in the opening cinematic. Instead of stopping when she has reached her goal, she keeps on moving forward and never stops, just blocking the Cinematic system. Not sure why this is happening yet, but it needs to be solved.
The Dissolving Screen Problem
In the previous update, it was mentioned that we had implmeneted a hack to get the scene drawing without calling
startMap. While it is still not implemented, the
drawSky functions are now being called directly through the main game loop for now, so the XY-cructch is no longer needed.
However, while implementing
drawSky, a different problem appeared. When it was called, it would creating a strange dissolving effect on screen, wherein the pixels on the scren would drive down with the drawn stars. While bizarre at first, it was being caused because the screen was not being cleared each frame for
drawSky. An obvious mistake perhaps, but something that stumped me nonetheless.
I’ve started to notice a pattern in the original HDB sources. For the purpose of moving data from files to object, they relied heavily on typecasting. They would load and store data in
void* pointers, which would be casted at the time of usage to a particular struct. This might not seem like a big deal at first, but two problems arise:
- Portablity: Due to different architectures, we cannot blindly casted void pointers and expect no problems. We have to explicity define struct types and load them through the ScummVM API.
Void Pointers can hold anything: Since void pointers can hold references of any type, at many places they have been used to hold values whose type could only be determined at runtime. Worse were situations where functions were returning void pointers, whose type could only be determined at runtime.
Due to the static nature of C++, converting such code to explicit types required me to split such functions into multiple variants and introduce union types in their structs.
Font Header Problem
At its core, this was another type problem, except perpretrated by myself. Most of the structs in the game hold positive data, so I have developed a tendency to use unsigned types for them.
This caused a problem here since I wasn’t expecting a negative number in the
_fontHeader data. Moreover, I had been reading the data 16 bits at a time, whereas I should have employed 32 bits. This led to wildly inaccurate header information due to which I couldn’t render the messages.
Dialog artifact problems
If you look closely at the above image of the Dialog box, you might see it has dark black artifacts near the top line. Here’s how those happened.
The Dialog works is composed of 9 smaller images: TopLeft, TopMiddle, TopRight, Left, Middle, Right, BottomLeft, BottomMiddle and BottomRight.
Depending on the length of the message, the Middle images are repeated as set number of times. When I implemented the
drawDialog function, there were transparency artifacts as well. However, replacing some of the
draw calls with
drawMasked solved that.
- Debug and fix the moving code for Entities so Sarge stops walking when she reaches her goal.
- Figure out the correct file for the Dialog image.
- Implement the Bots code