1 /*
2     C-Dogs SDL
3     A port of the legendary (and fun) action/arcade cdogs.
4     Copyright (c) 2013-2015, 2019 Cong Xu
5     All rights reserved.
6 
7     Redistribution and use in source and binary forms, with or without
8     modification, are permitted provided that the following conditions are met:
9 
10     Redistributions of source code must retain the above copyright notice, this
11     list of conditions and the following disclaimer.
12     Redistributions in binary form must reproduce the above copyright notice,
13     this list of conditions and the following disclaimer in the documentation
14     and/or other materials provided with the distribution.
15 
16     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26     POSSIBILITY OF SUCH DAMAGE.
27 */
28 #pragma once
29 
30 #include "config.h"
31 #include "objective.h"
32 #include "path_cache.h"
33 #include "vector.h"
34 
35 // State data for various AI routines
36 
37 // State for what the AI is currently doing
38 // Used for AI chatter
39 typedef enum
40 {
41 	AI_STATE_NONE,
42 	AI_STATE_IDLE,
43 	AI_STATE_DIE,
44 	AI_STATE_FOLLOW,
45 	AI_STATE_HUNT,
46 	AI_STATE_TRACK,
47 	AI_STATE_FLEE,
48 	AI_STATE_CONFUSED,
49 	AI_STATE_NEXT_OBJECTIVE
50 } AIState;
51 // State for what the AI is doing when confused
52 typedef enum
53 {
54 	AI_CONFUSION_CONFUSED,	// perform a random action
55 	AI_CONFUSION_CORRECT	// perform the right action (correct for confusion)
56 } AIConfusionType;
57 typedef struct
58 {
59 	AIConfusionType Type;
60 	int Cmd;
61 } AIConfusionState;
62 typedef enum
63 {
64 	AI_OBJECTIVE_TYPE_NORMAL,
65 	AI_OBJECTIVE_TYPE_KEY,
66 	AI_OBJECTIVE_TYPE_EXIT,
67 	AI_OBJECTIVE_TYPE_KILL,
68 	AI_OBJECTIVE_TYPE_PICKUP
69 } AIObjectiveType;
70 // State for AI attempting to complete an objective
71 // This is to prevent excessive pathfinding calls
72 typedef struct
73 {
74 	AIObjectiveType Type;
75 	bool IsDestructible;
76 	union
77 	{
78 		const Objective *Obj;
79 		int UID;
80 	} u;
81 	int LastDone;
82 	struct vec2 Goal;
83 } AIObjectiveState;
84 typedef struct
85 {
86 	struct vec2i Goal;
87 	CachedPath Path;
88 	int PathIndex;
89 	bool IsFollowing;
90 } AIGotoContext;
91 typedef struct
92 {
93 	int LastCmd;
94 	// Delay in executing consecutive actions;
95 	// Used to let the AI perform one action for a set amount of time
96 	int Delay;
97 	AIState lastState;
98 	AIState State;
99 
100 	AIConfusionState ConfusionState;
101 	AIObjectiveState ObjectiveState;
102 	struct vec2i LastTile;
103 	bool IsStuckTooLong;
104 	AIGotoContext Goto;
105 	int EnemyId;
106 	double GunRangeScalar;
107 	int OnGunId;
108 } AIContext;
109 
110 AIContext *AIContextNew(void);
111 void AIContextDestroy(AIContext *c);
112 
113 const char *AIStateGetChatterText(const AIState s);
114 bool AIContextShowChatter(const AIChatterFrequency f);
115 bool AIContextSetState(AIContext *c, const AIState s);
116