Let’s Make: Traffic Department 2192 – Part 26

Prototyping - Loading NPC Ships

We'll load the NPC ships in to the game, which includes assigning their mission path from th game data files. We start by converting the mission data file to a text-friendly format like we did with the maps. Then we create an object that links to each ship. Finally, we load the mission path in to the AI so that the ships will follow their path. By the end of this part, we'll have all the NPC ships in the world and following their scripted task.

Objects

We'll use two new objects in this part. First is an AI specific object that ties to ships. Second is a controller-like object that will have several duties by the end of the prototype

Concept Object Notes
NPC AI obj_ship_ai Each ship gets an associated AI object (even the player ship). This object will pass control commands to the ship like the player does with the keyboard. We'll only make the passive path-following AI for now.
Headquarters obj_hq A multipurpose object that we'll use to control the mission. We'll give it the task of loading the NPC ships from the mission data files.

 

C Code

The mission files have the same problem with ASCII reading. Some of the byte values can't be accurately read as text. We also can't simply shift the byte values up by 0x20 because the byte values are too diverse. They cover both the C0 and C1 control space. We'll use the following program to make the data in to multi-byte format that can be easily read. We'll have to undo the changes in GameMaker later. See the video for more detailed explanations.

NEWMSN.c:

 

GML Code

We have to make it though two new objects this time along with some scripts.

obj_ship_ai: Create 1

We're not registering a keyboard for once! We'll start with some state enumerators for the AI object.

 

obj_ship_ai: Create 2

We'll have some variables including AI mode, associated ship, and tracking information for the world. The AI object starts in the Move mode.

 

obj_ship_ai: Create 3

We'll need some data structures to support the paths and A* algorithm. Note that in the video, we didn't put the A* components in during the video, but I'll put the code in here that we backfill in the AI video.

 

obj_ship_ai: Room Start 1

Set our single reference

 

obj_ship_ai: Room Start 1

Check if the AI is attached to the player, if so, make it inactive. No sense having the player battle for control of the ship from the AI.

 

obj_ship_ai: Begin Step 1

If the AI is in move mode, compare the current location with the path to determine which way to go.

 

obj_ship_ai: Begin Step 2

If the AI has completed the path, then remove the ship from play.

 

obj_hq: Create 1

Set variables for the overall mission, player, and ship handles.

 

obj_hq: Room Start 1

The usual references

 

obj_hq: Room Start 2

This is where we load the ships by reading our data file, to create the ships, then assigning their paths. We'll do the actual ship creation in another script that we'll call here.

 

Script: ship_create

We have this script to encapsulate the ship creation with the AI object creation.

 

Script: get_mission_file

A small script that simply returns the valid file name for the mission data.