File size: 5,125 Bytes
24b81cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**@class		BotTrigger
 **/
class BotTrigger
{
	bool CheckTrigger () { return false; }
};

class MyBotTrigger : BotTrigger
{
	PlayerBase m_Owner;
	void MyBotTrigger (PlayerBase p) { m_Owner = p; }
	override bool CheckTrigger () { return null != m_Owner.GetInventory().FindAttachment(InventorySlots.GetSlotIdFromString("Headgear")); }
};


/**@class		Bot
 **/
class Bot
{
	PlayerBase m_Owner = null;
	protected ref Timer m_Timer = new Timer;
	protected const float c_TriggerTimeoutMS = 1000.0;
	//protected const float c_UpdateMS = 2000.0;
	protected const float c_UpdateMS = 16.0;
	protected ref BotFSM m_FSM = null;
	protected bool m_UseTrigger = false;
	protected bool m_Triggered = false;
	protected DayZPlayerInstanceType m_InstanceType = DayZPlayerInstanceType.INSTANCETYPE_CLIENT;
	protected ref BotTrigger m_BotTrigger = null;
	protected ref BotStateBase m_BotTest = null;

	void Bot (PlayerBase ow)
	{
		m_Owner = ow;
	}
	
	void SetInstanceType (DayZPlayerInstanceType t) { m_InstanceType = t; }

	void Start (bool use_trigger, BotTrigger trigger = null)
	{
		m_UseTrigger = use_trigger;

		InitFSM();

		if (m_UseTrigger)
		{
			if (m_UseTrigger && trigger == null)
				Error("use trigger, but trigger null");

			m_Triggered = false;
			m_BotTrigger = trigger;
			botDebugPrint("[bot] + " + m_Owner + " Bot waiting for trigger...");
			m_Timer.Run(c_TriggerTimeoutMS / 1000.0, this, "OnTrigger", null, true);
		}
		else
		{
			botDebugPrint("[bot] + " + m_Owner + " Bot Started.");
			m_Timer.Run(c_UpdateMS / 1000.0, this, "OnTimer", null, true);
		}
	}

	void DelayedStart (float ms)
	{
		m_Timer.Run(ms / 1000.0, this, "OnDelayedStart", null, false);
	}

	protected void OnDelayedStart ()
	{
		Start(false, null);

		ProcessEvent(new BotEventStart(m_Owner));
		m_UseTrigger = false;
	}

	void Stop ()
	{
		m_Triggered = false;
		m_Timer.Stop();
		m_FSM.Terminate();
	}

	protected void OnTrigger ()
	{
		bool triggered = m_BotTrigger.CheckTrigger();

		if (!m_Triggered)
		{
			if (triggered)
			{
				m_Timer.Stop(); // stop trigger timer
				m_Triggered = true;
				ProcessEvent(new BotEventStart(m_Owner));
				m_Timer.Run(c_UpdateMS / 1000.0, this, "OnUpdate", null, true);

				botDebugPrint("[bot] + " + m_Owner + " Started test!");
			}
		}
		else
		{
			if (!triggered)
			{
				m_Timer.Stop(); // stop update timer
				m_Triggered = false;
				ProcessEvent(new BotEventStop(m_Owner));
				m_Timer.Run(c_TriggerTimeoutMS / 1000.0, this, "OnTrigger", null, true);

				botDebugPrint("[bot] + " + m_Owner + " Stopped test!");
			}
		}
	}

	void OnUpdate (float dt)
	{
		m_FSM.GetCurrentState().OnUpdate(dt);

		OnTrigger(); // to detect trigger stop
	}

	void OnTimer ()
	{
		//m_FSM.GetCurrentState().OnUpdate(c_UpdateMS / 1000.0);

		//OnTrigger(); // to detect trigger stop
	}

	void InitFSM ()
	{
		m_FSM = new BotFSM();

		// basic states
		BotStateBase BotIdle = new BotStateIdle(this, NULL);
		// unstable (intermediate) states
		m_BotTest = new BotTestSpamUserActions(this, NULL);
		//m_BotTest = new BotTestAttachAndDropCycle(this, NULL);
		//m_BotTest = new BotTestItemMoveBackAndForth(this, NULL);
		//m_BotTest = new Bot_TestSpawnOpen(this, NULL);
		//m_BotTest = new Bot_TestSpawnOpenDestroy(this, NULL);
		//m_BotTest = new Bot_TestSpawnOpenEat(this, NULL);
		//m_BotTest = new BotTestSwapG2H(this, NULL);
		//m_BotTest = new BotTestSwapC2H(this, NULL);
		//m_BotTest = new BotTestSwapInternal(this, NULL);

		// events
		BotEventBase ___Bgn__ = new BotEventStart;
		BotEventBase __Stop__ = new BotEventStop;
		BotEventBase ___OK___ = new BotEventEndOK;
		BotEventBase __Fail__ = new BotEventEndFail;
		BotEventBase __Tout__ = new BotEventEndTimeout;
		BotEventBase __IChg__ = new BotEventOnItemInHandsChanged;

		///@{ transition table
		m_FSM.AddTransition(new BotTransition(  BotIdle     , ___Bgn__,   m_BotTest));
		m_FSM.AddTransition(new BotTransition(  BotIdle     , __Stop__,        NULL));

		// causes restart of FSM
		//m_FSM.AddTransition(new BotTransition(m_BotTest     , __IChg__,   m_BotTest));

		//m_FSM.AddTransition(new BotTransition(m_BotTest     , ___OK___,     BotIdle));
		m_FSM.AddTransition(new BotTransition(m_BotTest     , __Fail__,     BotIdle));
		m_FSM.AddTransition(new BotTransition(m_BotTest     , __Tout__,     BotIdle));
		///@} transition table

		m_FSM.SetInitialState(BotIdle);
		m_FSM.Start();
	}
	
	bool ProcessEvent (BotEventBase e)
	{
		if (m_FSM.ProcessEvent(e) == ProcessEventResult.FSM_OK)
		{
			botDebugSpam("[botfsm] Processed event e=" + e.ToString());
			return true;
		}
		else
		{
			botDebugSpam("[botfsm] FSM refused to process event (no transition): src=" + m_FSM.GetCurrentState().ToString() + " event=" + e.ToString());
			return false;
		}
	}
};

void botDebugPrint (string s)
{
#ifdef BOT_DEBUG
	PrintToRPT("" + s); // comment/uncomment to hide/see debug logs
#else
	//Print("" + s); // comment/uncomment to hide/see debug logs
#endif
}

void botDebugSpam (string s)
{
#ifdef BOT_DEBUG_SPAM
	PrintToRPT("" + s); // comment/uncomment to hide/see debug logs
#else
	//Print("" + s); // comment/uncomment to hide/see debug logs
#endif
}