InputSystem
[edit] Notes about Egoboo's input and character control system.
Egoboo moves characters around through the use of what it calls 'latches'. The latch variables that actually control the motion are the following:
float chrlatchx[MAXPLAYER];
float chrlatchy[MAXPLAYER];
unsigned char chrlatchbutton[MAXPLAYER];
To move a character or player around, you just need to update those values.
But how do those values get set? There are three kinds of input that affect these values: changes to the waypoints in scripts, pressing buttons or moving a joystick, and network updates from remote computers. Tracking the way that all of these inputs are handled can be quite a task!
Waypoints can be set inside scripts using the ClearWaypoints, AddWaypoint, and (maybe someday) the FindPath functions. Because there is no automatic pathfinding at the moment, only the last waypoint is of any interest and you will see that almost all scripts use the formula "ClearWaypoints, tmpx=..., tmpy=..., AddWaypoint". All the script commands for a given script are processed by calling the let_char_think() function in char.c.
The controls are read through the function set_local_latches(), which calls the function set_one_player_latch(), for each player that is hooked into the module. The function set_one_player_latch() reads the input and translates that into all the possible game commands, including motion commands. So that moving the joystick up (or whatever) is converted into values in a different set of variables:
float plalatchx[MAXPLAYER];
float plalatchy[MAXPLAYER];
unsigned char plalatchbutton[MAXPLAYER];
Network updates also affect this set of variables (since only player latches are sent over the internet). Information is exchanged over the net using the functions buffer_player_latches() which prepares and sends data over the 'net, and unbuffer_player_latches() which receives remote players' latches as well as "receiving" your own latches from all the local players. This is also where the plalatch info is copied over to the chrlatch info. Ahhh, the magic happens...
Anyway, there is still one last step. The function move_characters() translates the chrlatch*[] and chrbutton[] data into the AI/character/item actions and motions you see in the game. This is where the information is transferred from rider to his mount. Very simply, all the rider's latches are copied over the mount's latches.
There it is, the control system dissected in all it's glory!
The biggest area that needs improvement is in the sending and receiving of latches over the 'net. The latches need to be converted to 'deltas', the change between two successive updates. When there is no change we should send no data.
Anyone who has actually played Egoboo over the 'net will tell you that it is a bit sluggish and even the player directly connected to the local computer can experience significant and noticeable lag.
Original article by Jonathan
Heavily edited by BGBirdsey (maybe beyond recognition?)
|