# Palomino - Game Module (Lua and C++)

©2004,2009 Jim E. Brooks http://www.palomino3d.org

- Index
- Overview
- Examples of Game Logic
- Implementation Partitioned Across Lua and C++
- Actors
- Actor Events

- DestroyObject Event
- Collision-Detection Event

- C++ Support for Lua
- HUD
- Missiles

## Overview

[2008/11]

Most of the game logic shall be written in Lua. The central concept is the actor (game entity).

Unlike old versions, there is no C++ Actor class.

## Examples of Game Logic

[2008/10]

### SAM detects and fires at player

1. SAM actor constructed during mission startup
2. SAM actor listens for new actors to appear
3. player is notified by HUD if within SAM's radar radius
4. SAM actor tries to detect enemy aircraft using radar (exclude friendly aircraft)
5. SAM actor selects an enemy
6. player is notified by HUD if SAM has locked-on
7. SAM actor fires missile when radar-lock becomes strong

### New enemy aircraft is spawned

1. AircraftActor is constructed
2. Enemy's avionics builds initial list of hostile actors (includes player)
3. Enemy's avionics tracks certain kinds of actors (aircraft, missiles, radars)
4. Enemy AI reacts to nearest actor
5. Enemy or its friend may destroy nearest actor, so reassess threating actors

## Implementation Partitioned Across Lua and C++

[2008/10]

A goal is to implement the game logic in Lua as much as possible. The C++ side shall have supporting subroutines. But the notion of actors and a game class only exist in Lua.

Writing signficant parts in C++ would be difficult to implement (explained below). Even without those difficulties, the result would be a disorganized implementation.

### Difficulties in alternative implementation based on C++ classes

The C++ simulator and Lua are based on reference-counting. One design that was considered was to implement actor classes in C++ which are exported to Lua, and then Lua would've had classes based on them. Having actor objects existing in both C++ and Lua would have complications. If a C++ MissileActor destroys an AircraftActor, Lua may be holding a reference to the AircraftActor. By design, C++ cannot call Lua, so Lua would have to actively poll a "zombie list" produced by C++ in order for Lua to release references.

## Game Class

[2008/10]

The notion of a Game class exists only in Lua.

### Requirements of the Game class

- is THE container of Actors (C++ doesn't know nor hold references to them)
- can decide to attach/detach Actors
- responds to network commands to attach/detach Actors

## Actors

[2008/10]

### Requirements and characteristics of Actors

- contained in the Game class
- contains an Object or derivative
- aware of each other by the Game class

### Examples of Actor Classes

- generic
- aircraft
- missile
- radar
- SAM
- vehicle

## Actor Events

[2008/11]

### DestroyObject Event

Both C++ and Lua can hold references to an Object. If one side decides to destroy an Object, an event must be broadcast to unreference the Object in all containers.

See DestroyObject Event.

### Collision-Detection Event

C++ does collision-detection. So, when Lua creates a missile actor, Lua has to poll Dyna::IfCollision() of an Actor's underlying Object/Dyna.

## C++ Support for Lua

[2008/10]

The C++ side of the simulator provides subroutines to support the Lua game logic. C++ is used for subroutines that must be executed every frame or cannot be implemented in Lua.

C++ knows nothing about game concepts such as actors. C++ manipulates an Aircraft without knowing it is part of a Lua Actor.

Example list of specific C++ subroutines for game logic:

- HUD widgets (target boxes)
- Controller class that animates an Object every frame (for missiles and guns)
- sends/receives commands across network (which Lua understands and processes)
- etc

## Missiles

[2008/10]

- NOP if no radar lock or Craft has no more missiles
- create Object that inherits launching Craft's matrix
- create SmokeTrailParticleSystem
- register tick functor to animate missile (see Guidance)
- tick functor: create new particles where missile has moved to
- tick functor: do collision-detection of missile
- tick functor: check if target dropped chaff, randomly go off-course
- tick functor: if hit: start ExplosionParticleSystem
- tick functor: if hit: remove target from Game's target list

### Guidance

[2007/10:V2]

A basic guidance algorithm is:

1. Roll left.
2. If angle between up vector (+Y) and target increased, rolling left was a mistake, so roll right.
3. Continue above steps until angle reaches zero.
4. Likewise, try to pitch up or down until the chaser's forward vector (Z) is pointing at the target.

To avoid flip-flopping and jittering, the switching of maneuvers needs to be moderated and have a tolerance for small angles. For N frames, continue the same maneuver.

Last modified: Sun Nov 22 21:22:58 CST 2009