comparison engine/core/model/structures/instance.cpp @ 189:3d0cc4545938

* Applied two patches from icelus * Fix animation reseting on every move()/act() command even if the given animation was already running * Little fix to add boost 1.33 and possibly below compatibility * Added icelus to the AUTHORS file
author nihathrael@33b003aa-7bff-0310-803a-e67f0ece8222
date Thu, 26 Feb 2009 23:33:25 +0000
parents 56ac89189bc4
children 5816ab527da8
comparison
equal deleted inserted replaced
188:06dddc96ce54 189:3d0cc4545938
43 43
44 #include "instance.h" 44 #include "instance.h"
45 45
46 namespace FIFE { 46 namespace FIFE {
47 static Logger _log(LM_INSTANCE); 47 static Logger _log(LM_INSTANCE);
48 48
49 class ActionInfo { 49 class ActionInfo {
50 public: 50 public:
51 ActionInfo(AbstractPather* pather, const Location& curloc): 51 ActionInfo(AbstractPather* pather, const Location& curloc):
52 m_action(NULL), 52 m_action(NULL),
53 m_target(NULL), 53 m_target(NULL),
54 m_speed(0), 54 m_speed(0),
55 m_repeating(false), 55 m_repeating(false),
56 m_action_start_time(0), 56 m_action_start_time(0),
57 m_prev_call_time(0), 57 m_prev_call_time(0),
58 m_pather_session_id(-1), 58 m_pather_session_id(-1),
59 m_pather(pather), 59 m_pather(pather),
84 // pather 84 // pather
85 AbstractPather* m_pather; 85 AbstractPather* m_pather;
86 // leader for follow activity 86 // leader for follow activity
87 Instance* m_leader; 87 Instance* m_leader;
88 }; 88 };
89 89
90 class SayInfo { 90 class SayInfo {
91 public: 91 public:
92 SayInfo(const std::string& txt, unsigned int duration): 92 SayInfo(const std::string& txt, unsigned int duration):
93 m_txt(txt), 93 m_txt(txt),
94 m_duration(duration), 94 m_duration(duration),
95 m_start_time(0) {} 95 m_start_time(0) {}
96 96
97 std::string m_txt; 97 std::string m_txt;
98 unsigned int m_duration; 98 unsigned int m_duration;
99 unsigned int m_start_time; 99 unsigned int m_start_time;
100 }; 100 };
101 101
102 Instance::InstanceActivity::InstanceActivity(Instance& source): 102 Instance::InstanceActivity::InstanceActivity(Instance& source):
103 m_location(source.m_location), 103 m_location(source.m_location),
104 m_facinglocation(), 104 m_facinglocation(),
105 m_action(), 105 m_action(),
106 m_speed(0), 106 m_speed(0),
113 m_timeprovider(NULL) { 113 m_timeprovider(NULL) {
114 if (source.m_facinglocation) { 114 if (source.m_facinglocation) {
115 m_facinglocation = *source.m_facinglocation; 115 m_facinglocation = *source.m_facinglocation;
116 } 116 }
117 } 117 }
118 118
119 Instance::InstanceActivity::~InstanceActivity() { 119 Instance::InstanceActivity::~InstanceActivity() {
120 delete m_actioninfo; 120 delete m_actioninfo;
121 delete m_sayinfo; 121 delete m_sayinfo;
122 delete m_timeprovider; 122 delete m_timeprovider;
123 } 123 }
124 124
125 void Instance::InstanceActivity::update(Instance& source) { 125 void Instance::InstanceActivity::update(Instance& source) {
126 source.m_changeinfo = ICHANGE_NO_CHANGES; 126 source.m_changeinfo = ICHANGE_NO_CHANGES;
127 if (m_location != source.m_location) { 127 if (m_location != source.m_location) {
128 source.m_changeinfo |= ICHANGE_LOC; 128 source.m_changeinfo |= ICHANGE_LOC;
129 m_location = source.m_location; 129 m_location = source.m_location;
146 } 146 }
147 if (m_sayinfo && (m_saytxt != m_sayinfo->m_txt)) { 147 if (m_sayinfo && (m_saytxt != m_sayinfo->m_txt)) {
148 source.m_changeinfo |= ICHANGE_SAYTEXT; 148 source.m_changeinfo |= ICHANGE_SAYTEXT;
149 m_saytxt = m_sayinfo->m_txt; 149 m_saytxt = m_sayinfo->m_txt;
150 } 150 }
151 151
152 if (source.m_changeinfo != ICHANGE_NO_CHANGES) { 152 if (source.m_changeinfo != ICHANGE_NO_CHANGES) {
153 std::vector<InstanceChangeListener*>::iterator i = m_changelisteners.begin(); 153 std::vector<InstanceChangeListener*>::iterator i = m_changelisteners.begin();
154 while (i != m_changelisteners.end()) { 154 while (i != m_changelisteners.end()) {
155 (*i)->onInstanceChanged(&source, source.m_changeinfo); 155 (*i)->onInstanceChanged(&source, source.m_changeinfo);
156 ++i; 156 ++i;
172 Instance::~Instance() { 172 Instance::~Instance() {
173 delete m_activity; 173 delete m_activity;
174 delete m_facinglocation; 174 delete m_facinglocation;
175 delete m_visual; 175 delete m_visual;
176 } 176 }
177 177
178 void Instance::initializeChanges() { 178 void Instance::initializeChanges() {
179 if (!m_activity) { 179 if (!m_activity) {
180 m_activity = new InstanceActivity(*this); 180 m_activity = new InstanceActivity(*this);
181 } 181 }
182 } 182 }
183 183
184 void Instance::setLocation(const Location& loc) { 184 void Instance::setLocation(const Location& loc) {
185 initializeChanges(); 185 initializeChanges();
186 m_location = loc; 186 m_location = loc;
187 bindTimeProvider(); 187 bindTimeProvider();
188 } 188 }
233 } 233 }
234 ++i; 234 ++i;
235 } 235 }
236 FL_WARN(_log, "Cannot remove unknown listener"); 236 FL_WARN(_log, "Cannot remove unknown listener");
237 } 237 }
238 238 void Instance::initializeAction(const std::string& action_name) {
239 void Instance::initalizeAction(const std::string& action_name) {
240 assert(m_object); 239 assert(m_object);
241 assert(m_activity); 240 assert(m_activity);
241 const Action *old_action = m_activity->m_actioninfo ? m_activity->m_actioninfo->m_action : NULL;
242 if (m_activity->m_actioninfo) { 242 if (m_activity->m_actioninfo) {
243 delete m_activity->m_actioninfo; 243 delete m_activity->m_actioninfo;
244 m_activity->m_actioninfo = NULL; 244 m_activity->m_actioninfo = NULL;
245 } 245 }
246 m_activity->m_actioninfo = new ActionInfo(m_object->getPather(), m_location); 246 m_activity->m_actioninfo = new ActionInfo(m_object->getPather(), m_location);
248 if (!m_activity->m_actioninfo->m_action) { 248 if (!m_activity->m_actioninfo->m_action) {
249 delete m_activity->m_actioninfo; 249 delete m_activity->m_actioninfo;
250 m_activity->m_actioninfo = NULL; 250 m_activity->m_actioninfo = NULL;
251 throw NotFound(std::string("action ") + action_name + " not found"); 251 throw NotFound(std::string("action ") + action_name + " not found");
252 } 252 }
253 m_activity->m_actioninfo->m_prev_call_time = m_activity->m_actioninfo->m_action_start_time = getRuntime(); 253 m_activity->m_actioninfo->m_prev_call_time = getRuntime();
254 if (m_activity->m_actioninfo->m_action != old_action) {
255 m_activity->m_actioninfo->m_action_start_time = m_activity->m_actioninfo->m_prev_call_time;
256 }
254 } 257 }
255 258
256 void Instance::move(const std::string& action_name, const Location& target, const double speed) { 259 void Instance::move(const std::string& action_name, const Location& target, const double speed) {
257 initializeChanges(); 260 initializeChanges();
258 initalizeAction(action_name); 261 initializeAction(action_name);
259 m_activity->m_actioninfo->m_target = new Location(target); 262 m_activity->m_actioninfo->m_target = new Location(target);
260 m_activity->m_actioninfo->m_speed = speed; 263 m_activity->m_actioninfo->m_speed = speed;
261 setFacingLocation(target); 264 setFacingLocation(target);
262 FL_DBG(_log, LMsg("starting action ") << action_name << " from" << m_location << " to " << target << " with speed " << speed); 265 FL_DBG(_log, LMsg("starting action ") << action_name << " from" << m_location << " to " << target << " with speed " << speed);
263 } 266 }
264 267
265 void Instance::follow(const std::string& action_name, Instance* leader, const double speed) { 268 void Instance::follow(const std::string& action_name, Instance* leader, const double speed) {
266 initializeChanges(); 269 initializeChanges();
267 initalizeAction(action_name); 270 initializeAction(action_name);
268 m_activity->m_actioninfo->m_target = new Location(leader->getLocationRef()); 271 m_activity->m_actioninfo->m_target = new Location(leader->getLocationRef());
269 m_activity->m_actioninfo->m_speed = speed; 272 m_activity->m_actioninfo->m_speed = speed;
270 m_activity->m_actioninfo->m_leader = leader; 273 m_activity->m_actioninfo->m_leader = leader;
271 setFacingLocation(*m_activity->m_actioninfo->m_target); 274 setFacingLocation(*m_activity->m_actioninfo->m_target);
272 FL_DBG(_log, LMsg("starting action ") << action_name << " from" << m_location << " to " << *m_activity->m_actioninfo->m_target << " with speed " << speed); 275 FL_DBG(_log, LMsg("starting action ") << action_name << " from" << m_location << " to " << *m_activity->m_actioninfo->m_target << " with speed " << speed);
273 } 276 }
274 277
275 void Instance::act(const std::string& action_name, const Location& direction, bool repeating) { 278 void Instance::act(const std::string& action_name, const Location& direction, bool repeating) {
276 initializeChanges(); 279 initializeChanges();
277 initalizeAction(action_name); 280 initializeAction(action_name);
278 m_activity->m_actioninfo->m_repeating = repeating; 281 m_activity->m_actioninfo->m_repeating = repeating;
279 setFacingLocation(direction); 282 setFacingLocation(direction);
280 } 283 }
281 284
282 void Instance::say(const std::string& text, unsigned int duration) { 285 void Instance::say(const std::string& text, unsigned int duration) {
283 initializeChanges(); 286 initializeChanges();
284 delete m_activity->m_sayinfo; 287 delete m_activity->m_sayinfo;
285 m_activity->m_sayinfo = NULL; 288 m_activity->m_sayinfo = NULL;
286 289
287 if (text != "") { 290 if (text != "") {
288 m_activity->m_sayinfo = new SayInfo(text, duration); 291 m_activity->m_sayinfo = new SayInfo(text, duration);
289 m_activity->m_sayinfo->m_start_time = getRuntime(); 292 m_activity->m_sayinfo->m_start_time = getRuntime();
290 } 293 }
291 } 294 }
312 unsigned int timedelta = m_activity->m_timeprovider->getGameTime() - info->m_prev_call_time; 315 unsigned int timedelta = m_activity->m_timeprovider->getGameTime() - info->m_prev_call_time;
313 FL_DBG(_log, LMsg("timedelta ") << timedelta << " prevcalltime " << info->m_prev_call_time); 316 FL_DBG(_log, LMsg("timedelta ") << timedelta << " prevcalltime " << info->m_prev_call_time);
314 // how far we can travel 317 // how far we can travel
315 double distance_to_travel = (static_cast<double>(timedelta) / 1000.0) * info->m_speed; 318 double distance_to_travel = (static_cast<double>(timedelta) / 1000.0) * info->m_speed;
316 FL_DBG(_log, LMsg("dist ") << distance_to_travel); 319 FL_DBG(_log, LMsg("dist ") << distance_to_travel);
317 320
318 Location nextLocation = m_location; 321 Location nextLocation = m_location;
319 info->m_pather_session_id = info->m_pather->getNextLocation( 322 info->m_pather_session_id = info->m_pather->getNextLocation(
320 this, *info->m_target, 323 this, *info->m_target,
321 distance_to_travel, nextLocation, *m_facinglocation, 324 distance_to_travel, nextLocation, *m_facinglocation,
322 info->m_pather_session_id); 325 info->m_pather_session_id);
326 //ExactModelCoordinate b = m_actioninfo->m_target->getMapCoordinates(); 329 //ExactModelCoordinate b = m_actioninfo->m_target->getMapCoordinates();
327 m_location.getLayer()->getInstanceTree()->addInstance(this); 330 m_location.getLayer()->getInstanceTree()->addInstance(this);
328 // return if we are close enough to target to stop 331 // return if we are close enough to target to stop
329 if (info->m_pather_session_id == -1) { 332 if (info->m_pather_session_id == -1) {
330 return true; 333 return true;
331 } 334 }
332 return false; 335 return false;
333 } 336 }
334 337
335 InstanceChangeInfo Instance::update() { 338 InstanceChangeInfo Instance::update() {
336 if (!m_activity) { 339 if (!m_activity) {
442 if (m_activity->m_timeprovider) { 445 if (m_activity->m_timeprovider) {
443 multiplier = m_activity->m_timeprovider->getMultiplier(); 446 multiplier = m_activity->m_timeprovider->getMultiplier();
444 } 447 }
445 delete m_activity->m_timeprovider; 448 delete m_activity->m_timeprovider;
446 m_activity->m_timeprovider = NULL; 449 m_activity->m_timeprovider = NULL;
447 450
448 if (m_location.getLayer()) { 451 if (m_location.getLayer()) {
449 Map* map = m_location.getLayer()->getMap(); 452 Map* map = m_location.getLayer()->getMap();
450 if (map) { 453 if (map) {
451 m_activity->m_timeprovider = new TimeProvider(map->getTimeProvider()); 454 m_activity->m_timeprovider = new TimeProvider(map->getTimeProvider());
452 } 455 }