Enemy AI CIS 487587 Bruce R Maxim UMDearborn

  • Slides: 26
Download presentation
Enemy AI CIS 487/587 Bruce R. Maxim UM-Dearborn 3/10/2021 1

Enemy AI CIS 487/587 Bruce R. Maxim UM-Dearborn 3/10/2021 1

Slides based on the Code from Advanced 3 D Game Programming by Kenneth Finney

Slides based on the Code from Advanced 3 D Game Programming by Kenneth Finney 3/10/2021 2

Stationary Guard - 1 • Requirements – – – 3/10/2021 Visually detects potential threats

Stationary Guard - 1 • Requirements – – – 3/10/2021 Visually detects potential threats Determines friend or foe Determines threat level Scans assigned area Engages hostile players Detects when being engaged by hostile players 3

Stationary Guard - 2 • Attributes – Maximum vision range • (differs for each

Stationary Guard - 2 • Attributes – Maximum vision range • (differs for each guard) – Minimum vision range • (guaranteed detection distance) – Alertness • (some are better and some are worse) – Attention level • (gets worse with no activity) – Aggressiveness • (not always the same for each guard) 3/10/2021 4

AIDrop. Points • Execute the fps. starter demo and open the World Creator Interface

AIDrop. Points • Execute the fps. starter demo and open the World Creator Interface • On lower right side, drill down and click on Mission. Objects/System/Sim. Group • Call the object AIDrop. Points and switch to the World Editor Inspector (F 3) • Make AIDrop. Points an instant group using the Alt key and left mouse button 3/10/2021 5

Guard • Switch to the World Creator (F 4) • On lower right side,

Guard • Switch to the World Creator (F 4) • On lower right side, drill down and click on Mission. Objects/System/Sim. Group • Call the object Guard and switch to the World Editor Inspector (F 3) • Make Guard an instant group using the Alt key and left mouse button 3/10/2021 6

Spawn. Sphere - 1 • Exit the Mission Editor (F 11) and enter camera

Spawn. Sphere - 1 • Exit the Mission Editor (F 11) and enter camera fly mode (F 8) • Find a good location for an AI character and place it 20 feet off the terrain • Switch back to the Mission Editor (F 11) and enter World Creator (F 4) • Drill down to Shapes/Misc on lower right and click on Spawn. Sphere. Marker • Place at least two more in the world 3/10/2021 7

Spawn. Sphere - 2 • Switch to World Editor Inspector (F 3) • Select

Spawn. Sphere - 2 • Switch to World Editor Inspector (F 3) • Select all 3 Spawn. Spheres in upper left (Ctrl and left mouse click) • Click on expand all • Changes the radius field to 10 and press Apply button 3/10/2021 8

Spawn. Sphere - 3 • Select all 3 Spawn. Spheres in upper left (Ctrl

Spawn. Sphere - 3 • Select all 3 Spawn. Spheres in upper left (Ctrl and left mouse click) • Add the following dynamic field and values to each – – aggression range attention alertness 100 100 • Save the mission and exit to the desktop 3/10/2021 9

Code Modifications • Add this line to the serverscriptgame. cs function on. Server. Created

Code Modifications • Add this line to the serverscriptgame. cs function on. Server. Created exec(“. /ai. Guard. cs”); • At the end of the start. Game() function in the same game. cs file add this line to start the bot placement process Schedule(3000, 0, “Create. Bots”); • Save game. cs and copy the file ai. Guard. cs to the serverscript folder 3/10/2021 10

check. For. Threat function AIGuard. DB: : check. For. Threat(%this, %obj) { Debug. Print(

check. For. Threat function AIGuard. DB: : check. For. Threat(%this, %obj) { Debug. Print( "%this: "@%[email protected]"~AIGuard. DB: : check. For. Threat (from: "@%[email protected]")", "check. For. Threat"); if(!is. Object(%obj)) return; // modified to allow firing %idx = %obj. get. Closest. Enemy(); if (%idx < 0) return 0; %target = Client. Group. get. Object( %idx ); 3/10/2021 11

check. For. Threat if ( !%obj. Check. Arc. Of. Sight(%target. player) ) %test. Range

check. For. Threat if ( !%obj. Check. Arc. Of. Sight(%target. player) ) %test. Range = %obj. range / 2; else return; if ( %obj. Get. Target. Range(%target. player) < { return %target. player; } %test. Range) Debug. Print( "no threat (from: "@%[email protected]")", "check. For. Threat"); return 0; } 3/10/2021 12

Testing Guard AI • When the player avatar gets close enough for the Guard

Testing Guard AI • When the player avatar gets close enough for the Guard to see it, the player will be attacked until killed or the player moves out of range • The Player is not able to inflict any damage on the Guard (a definite need in real game) 3/10/2021 13

Pathed. AI • The fps. starter contains the script serverscriptsai. Player. cs • Add

Pathed. AI • The fps. starter contains the script serverscriptsai. Player. cs • Add the following to the end of ai. Player. cs function Insert. Pathed. AI() { %player = AIPlayer: : spawn. On. Path("Follower", "Mission. Group/Paths/Path. B"); %player. mount. Image(Crossbow. Image, 0); %player. set. Inventory(Crossbow. Ammo, 1000); %player. follow. Path("Mission. Group/Paths/Path. B", -1); } 3/10/2021 14

Path Definition - 1 • Find a relatively flat area where it will be

Path Definition - 1 • Find a relatively flat area where it will be easier to create a path and hit F 11 to open the Mission Editor and switch to "World Editor Creator" mode. • Expand the tree in the lower right half of the screen by clicking the "Mission Objects" entry. Expand it out like so: "Mission Objects->Mission" 3/10/2021 15

Path Definition - 2 • In the "Mission" directory, you should see several entries

Path Definition - 2 • In the "Mission" directory, you should see several entries to choose from. • Click the entry labeled "Path" just once to create a new Path object. Name the Path object, “Path. B". • In the "Mission. Group" tree, which is in the upper right hand side of the editor, expand out the tree and find the node labeled "Path. B". 3/10/2021 16

Path Definition - 3 • Make this node the current selection by holding down

Path Definition - 3 • Make this node the current selection by holding down the "Alt" key while clicking it with the mouse. • If done correctly, the selection color of the "Path. B" entry should change from white to gray. Make sure the path markers get grouped under our new Path object. • Return to the tree control in the lower right hand corner and click the “Path. Marker” in the "Mission" directory. 3/10/2021 17

Path Definition - 4 • Name the new Path. Marker, “wp 1" and check

Path Definition - 4 • Name the new Path. Marker, “wp 1" and check to make sure it was correctly grouped under "Path. B" in the "Mission. Group" tree control. • Repeat the process to create two more Path. Markers called, “wp 2, and “wp 3". • Make sure to place them far enough apart so you can see your bot run around. • Save the mission file and exit to the desktop 3/10/2021 18

Test Pathed. AI • Run the test application again. • Move the player avatar

Test Pathed. AI • Run the test application again. • Move the player avatar to a location near the path defined by Path. B • Open the console and type Insert. Pathed. AI( ); • Close the console window and watch it go 3/10/2021 19

Pathed. AIShooter • Define another path called Path. A • Insert the following line

Pathed. AIShooter • Define another path called Path. A • Insert the following line of code in the ai. Player: : aim. At function %this. set. Aim. Location(%object. get. Position()); after the line %this. set. Aim. Object(%object); • You will also need to add the function Insert. Pathed. AIShooter to the file ai. Player. cs 3/10/2021 20

Insert. Pathed. AIShooter - 1 function Insert. Pathed. AIShooter() { %player = AIPlayer: :

Insert. Pathed. AIShooter - 1 function Insert. Pathed. AIShooter() { %player = AIPlayer: : spawn. On. Path("Shooter", "Mission. Group/Paths/Path. A"); %player. mount. Image(Crossbow. Image, 0); %player. set. Inventory(Crossbow. Ammo, 1000); // Maxim added code %player. follow. Path("Mission. Group/Paths/Path. A", -1); %player. push. Task("play. Thread(0, "celwave")"); %player. push. Task("follow. Path("Mission. Group/Paths/Path. A")"); %player. push. Task("aim. At("Mission. Group/target")"); %player. push. Task("wait(10)"); 3/10/2021 21

Insert. Pathed. AIShooter - 2 %player. push. Task("fire(true)"); %player. push. Task("wait(1)"); %player. push. Task("fire(false)");

Insert. Pathed. AIShooter - 2 %player. push. Task("fire(true)"); %player. push. Task("wait(1)"); %player. push. Task("fire(false)"); %player. push. Task("wait(10)"); %player. push. Task("fire(true)"); %player. push. Task("wait(1)"); %player. push. Task("fire(false)"); %player. push. Task("play. Thread(0, "celwave")"); %player. push. Task("done()"); } 3/10/2021 22

Testing Shooter • Open the World Creator and place a logo some where near

Testing Shooter • Open the World Creator and place a logo some where near Path. A • Label the logo “Target” and save the mission • Start demo and open the console window • Type the following to test the shooter once the player avatar can view Path. A Insert. Pathed. AIShooter( ); 3/10/2021 23

Combining Them • It is possible to have a moving enemy that also shoots

Combining Them • It is possible to have a moving enemy that also shoots at you • I have not finished my version yet, but you might try looking at the AI Guard Unit on the Torque web site (this will require recompiling Torque to add a new class) • Finney’s version does not require recompilation of Torque 3/10/2021 24

Chasing • Add the following code where set. Aim. Object is called if (%the.

Chasing • Add the following code where set. Aim. Object is called if (%the. Role !$= “Guard”) { %obj. set. Move. Speed($MAX_CHASER_SPEED); %obj. set. Move. Destination(%tgt. Player. get. Position( )); %obj. next. Block. Check = %this. schedule($MAX_SCAN_GAP*2), “unblock”, %obj); } 3/10/2021 25

Unblock // used to help AI when they get stuck function AIGuard. DB: :

Unblock // used to help AI when they get stuck function AIGuard. DB: : unblock(%this, obj) { if (!is. Object(%obj)) return; cancel(%obj. next. Block. Check); %this. set. Random. Destinatioin(%obj); } 3/10/2021 26