File: EngineOutHandler.h

package info (click to toggle)
spring 0.81.2.1%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 28,496 kB
  • ctags: 37,096
  • sloc: cpp: 238,659; ansic: 13,784; java: 12,175; awk: 3,428; python: 1,159; xml: 738; perl: 405; sh: 297; makefile: 267; pascal: 228; objc: 192
file content (184 lines) | stat: -rw-r--r-- 5,919 bytes parent folder | download
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
/*
	Copyright (c) 2008 Robin Vobruba <hoijui.quaero@gmail.com>

	This program is free software {} you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation {} either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY {} without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _ENGINEOUTHANDLER_H
#define _ENGINEOUTHANDLER_H

#include "Object.h"
#include "Sim/Misc/GlobalConstants.h"

#include <map>
#include <vector>
#include <string>

struct Command;
class float3;
class CUnit;
class CGroup;
struct WeaponDef;
class SkirmishAIKey;
class CSkirmishAIWrapper;
struct SSkirmishAICallback;

void handleAIException(const char* description);

class CEngineOutHandler : public CObject {
private:
	CR_DECLARE(CEngineOutHandler);

	CEngineOutHandler();
	~CEngineOutHandler();

public:
	/**
	 * This function initialized a singleton instance,
	 * if not yet done by a call to GetInstance()
	 */
	static void Initialize();
	static CEngineOutHandler* GetInstance();
	static void Destroy();

	void PostLoad();
	/** Called just before all the units are destroyed. */
	void PreDestroy();

	void Update();

	/** Group should return false if it doenst want the unit for some reason. */
	bool UnitAddedToGroup(const CUnit& unit, const CGroup& group);
	/** No way to refuse giving up a unit. */
	void UnitRemovedFromGroup(const CUnit& unit, const CGroup& group);
	void UnitEnteredLos(const CUnit& unit, int allyTeamId);
	void UnitLeftLos(const CUnit& unit, int allyTeamId);
	void UnitEnteredRadar(const CUnit& unit, int allyTeamId);
	void UnitLeftRadar(const CUnit& unit, int allyTeamId);
	void UnitIdle(const CUnit& unit);
	void UnitCreated(const CUnit& unit, const CUnit* builder);
	void UnitFinished(const CUnit& unit);
	void UnitDestroyed(const CUnit& destroyed, const CUnit* attacker);
	void UnitDamaged(const CUnit& damaged, const CUnit* attacker, float damage, int weaponId, bool paralyzer);
	void UnitMoveFailed(const CUnit& unit);
	void UnitCaptured(const CUnit& unit, int newTeamId);
	void UnitGiven(const CUnit& unit, int oldTeamId);

	void SeismicPing(int allyTeamId, const CUnit& unit, const float3& pos, float strength);
	void WeaponFired(const CUnit& unit, const WeaponDef& def);
	void PlayerCommandGiven(const std::vector<int>& selectedUnitIds, const Command& c, int playerId);
	/**
	 * A specific unit has finished a specific command,
	 * might be a good idea to give new orders to it.
	*/
	void CommandFinished(const CUnit& unit, const Command& command);
	void GotChatMsg(const char* msg, int playerId);


	// Skirmish AI stuff
	void CreateSkirmishAI(const size_t skirmishAIId);
	/**
	 * Sets a local Skirmish AI dieing.
	 * Do not call this if you want to kill a local AI, but use
	 * the Skirmish AI Handler instead.
	 * @param skirmishAIId index of the AI to mark as dieing
	 * @see CSkirmishAIHandler::SetSkirmishAIDieing()
	 * @see DestroySkirmishAI()
	 */
	void SetSkirmishAIDieing(const size_t skirmishAIId);
	/**
	 * Destructs a local Skirmish AI for real.
	 * Do not call this if you want to kill a local AI, but use
	 * the Skirmish AI Handler instead.
	 * @param skirmishAIId index of the AI to destroy
	 * @see SetSkirmishAIDieing()
	 * @see CSkirmishAIHandler::SetSkirmishAIDieing()
	 */
	void DestroySkirmishAI(const size_t skirmishAIId);


	void SetCheating(bool enable);
	bool IsCheating() const;


	void Load(std::istream* s);
	void Save(std::ostream* s);

	/**
	 * Should exceptions thrown by AIs be caught by the engine?
	 * In practice, an exception thrown by an AI should be caught in the AI,
	 * or at least the interface plugin, and should therefore never reach
	 * the engine. The reason for this is, that we do not know if the engine
	 * and the AI are compiled with the same compiler/exception system.
	 * Shorlty: catching AI exceptions in the engine is deprecated
	 */
	static bool IsCatchExceptions();
	/**
	 * This is used inside a catch block for a try guarding a call form
	 * the engine into an AI.
	 * There is a macro defined for the catch part, so you should never have
	 * to call this method directly.
	 * @see CATCH_AI_EXCEPTION
	 */
	static void HandleAIException(const char* description);
	/**
	 * Catches a common set of exceptions thrown by AIs.
	 * Use like this:
	 * <code>
	 * try {
	 *     ai->sendEvent(evt);
	 * } CATCH_AI_EXCEPTION
	 * </code>
	 * @see HandleAIException()
	 */
	#define CATCH_AI_EXCEPTION									\
		catch (const std::exception& e) {						\
			CEngineOutHandler::HandleAIException(e.what());		\
			throw e;											\
		} catch (const std::string& s) {						\
			CEngineOutHandler::HandleAIException(s.c_str());	\
			throw s;											\
		} catch (const char* s) {								\
			CEngineOutHandler::HandleAIException(s);			\
			throw s;											\
		} catch (int err) {										\
			const std::string s = IntToString(err);				\
			CEngineOutHandler::HandleAIException(s.c_str());	\
			throw err;											\
		} catch (...) {											\
			CEngineOutHandler::HandleAIException("Unknown");	\
			throw;												\
		}

private:
	static CEngineOutHandler* singleton;

private:
	typedef std::vector<size_t> ids_t;

	typedef std::map<size_t, CSkirmishAIWrapper*> id_ai_t;
	/// Contains all local Skirmish AIs, indexed by their ID
	id_ai_t id_skirmishAI;

	typedef std::map<int, ids_t> team_ais_t;
	/**
	 * Array mapping team IDs to local Skirmish AI instances.
	 * There can be multiple Skirmish AIs per team.
	 */
	team_ais_t team_skirmishAIs;
};

#define eoh CEngineOutHandler::GetInstance()

#endif // _ENGINEOUTHANDLER_H