26 October 2012

Game modes and MVC, part 1/2

Following a previous article about MVC and event managers, I look at how two veteran pygame developers are using MVC and/or event managers, in particular with respect to switching between modes (e.g. menu vs game vs config).

Example 1: Fool The Bar

In Fool The Bar, an MVC tutorial by Shandy Brown last updated in 2011, a central event manager publishes all the events it receives to the model, the main controller, and the main view. There is a pub/sub in place, but all components subscribing to the event manager receive all the events. The GameStartRequest event is used to switch from the main menu mode to the game mode.

The main controller subscribes to the event manager for all events, and forwards all of them, except the events like GameStartRequest that cause mode changes, to its single current subcontroller. Thus the subcontroller does not subscribe to the event manager, but only publishes to it. And when a GameStartRequest event is fired, the main controller switches its subcontroller from SimpleGUIController for the menu, to MainBarScreenController for the game.

Each subcontroller has its own input mapping. When the SimpleGUIController is active, if the user pushes the DOWN arrow key, the controller fires a GUIFocusNextWidgetEvent. When the MainBarScreenController is active, in game mode, pushing the DOWN arrow key does not fire any event.

Similarly, when the main view receives a GameStartRequest from the event manager, it switches from the menu mode to the game mode. But when the main controller has only one subcontroller active at a time, the main view can have multiple subviews active for one mode. For example, the game mode requires both the MainBarScreen (where the game is displayed) and the MainGUIView (where the HUD with score and possible actions is displayed) to be active at the same time. Thus the main view acts as an event manager for its subviews: subviews subscribe to the main view, and the main view relays events from the event manager to its current subviews. When the mode changes, the main view kills all its current subviews, and instantiates the new subviews.

Example 2: Angry Drunken Dwarves

Angry Drunken Dwarves is a 2004 falling-block puzzle game by Joe Wreschnig. It does not really follow MVC: instead of having distinct models, views, and controllers, modes simply stack up on top of each other. For example, when the player pushes the "Play a game" button, the button's callback stacks the character select mode on top of the main menu mode. When the player has selected a character, the character select mode returns with the selected character, and the callback starts the game mode, passing the character in argument.

Each mode instantiates its own event manager, but unlike Fool The Bar, the event manager is directly polling pygame.events for keyboard or click events. When a component decides that the previous mode should be brought back, such as when the game is over and the main menu should be brought back, that component sends a QuitEvent to pygame.events.

09 October 2012

Balance of Power - Crawford 1986

Notes from Balance of Power, a book by Chris Crawford published in 1986.

Games vs simulations

Games differ from simulations in three ways. First, games carry an artistic message, with unquantifiable concepts and feelings. Second, games simplify reality: they only keep the conflicts inherent in the situation, and unlike in real life, they provide clear and emotionally satisfying resolutions to those conflicts. Yet games maintain a level of realism appropriate to the audience. Finally, games are accessible; there is no need to study the manual.

Balance of power, in short

The player controls one of the two superpowers, the USA or the USSR, during the Cold War. Each turn, both superpowers send money, weapons, troops, or diplomatic pressure onto other countries to trigger insurgencies, coups d'etat, or Finlandization favorable to them. For example, the USSR can send money to Cuba.

If a superpower contest the actions taken by the other superpower, then a crisis testing the player's brinksmanship follows. Either the player stands firm on its ground (whether s/he was contesting or not), and brings the crisis one DEFCON level higher, ie one step closer to World War III and a nuclear holocaust, or s/he pulls back. For example, think about the Cuba missile crisis of 1962.

When a superpower stands firm and wins the crisis, it gains prestige points. A minor country such as Nicaragua is worth 2 points, while a country like East Germany is worth 200 points. The further down in a crisis a superpower backs down, the more it loses prestige. When the highest DEFCON level is reached, the game is lost for both players.

The game ends after eight turns. The superpower with most prestige points win.

Balance of insurgencies

Insurgencies consist of rebels trying to take over the government of a minor country. The strength of any armed faction is measured from its number of soldiers and its number of weapons. If 100 government forces share 1 weapon, then the government's power should be low, and a superpower providing a couple weapons would really increase its strength. Similarly, 1 government force with 100 weapons should be weak. Thus, the strength of an armed faction is the harmonic mean of the number of soldiers and the number of weapons. It is optimal when each soldier has a weapon.

Balance of crises

Nastiness is a game-wide variable; it describes how slippery taking actions in the world has become. When nastiness is high, the AI is more likely to contest the player's actions, start crises, and refuse to back down. Nastiness increases after each crisis or military intervention of a superpower, and decreases slowly every turn.

Pugnacity is a superpower variable; it describes how trustworthy the superpower is considered by minor countries. Pugnacity increases when the superpower is aggressive and wins crises, and decreases when a superpower backs down in crises. If a superpower backs down late in a crisis, it loses a lot of pugnacity.

Combined, nastiness and pugnacity amplify the amplitude of missteps; an error in judgement can cause the end of the game. This is exactly what Crawford was trying to convey about brinksmanship.