~ubuntu-branches/ubuntu/precise/widelands/precise-backports

« back to all changes in this revision

Viewing changes to src/trigger_time.cc

  • Committer: Bazaar Package Importer
  • Author(s): Martin Quinson
  • Date: 2005-02-14 10:41:12 UTC
  • Revision ID: james.westby@ubuntu.com-20050214104112-6v08iux9fptxpva9
Tags: upstream-build9
Import upstream version build9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2002-4 by the Widelands Development Team
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public License
 
6
 * as published by the Free Software Foundation; either version 2
 
7
 * of the License, or (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
17
 *
 
18
 */
 
19
 
 
20
#include "trigger_time.h"
 
21
#include "error.h"
 
22
#include "filesystem.h"
 
23
#include "game.h"
 
24
 
 
25
static const int TRIGGER_VERSION = 1;
 
26
 
 
27
/*
 
28
 * Init and cleanup
 
29
 */
 
30
Trigger_Time::Trigger_Time(void) {
 
31
   m_last_start_time=0;
 
32
   m_wait_time=60; // defaults to one minute
 
33
   set_name("Time Trigger");
 
34
   set_trigger(false);
 
35
   set_is_one_time_trigger(true);
 
36
}
 
37
 
 
38
Trigger_Time::~Trigger_Time(void) {
 
39
}
 
40
 
 
41
/*
 
42
 * File Read, File Write
 
43
 */
 
44
void Trigger_Time::Read(FileRead* fr, Editor_Game_Base* ) {
 
45
   int version=fr->Unsigned16();
 
46
   if(version <= TRIGGER_VERSION) {
 
47
      set_name(fr->CString());
 
48
      set_is_one_time_trigger(fr->Unsigned8());
 
49
      m_wait_time=fr->Unsigned32();
 
50
      return;
 
51
   }
 
52
   throw wexception("Time Trigger with unknown/unhandled version %i in map!\n", version);
 
53
}
 
54
 
 
55
void Trigger_Time::Write(FileWrite* fw) {
 
56
   // First of all the id
 
57
   fw->Unsigned16(get_id());
 
58
 
 
59
   // Now the version
 
60
   fw->Unsigned16(TRIGGER_VERSION);
 
61
 
 
62
   // Name
 
63
   fw->Data(get_name(), strlen(get_name()));
 
64
   fw->Unsigned8('\0');
 
65
 
 
66
   // triggers only once?
 
67
   fw->Unsigned8(is_one_time_trigger());
 
68
 
 
69
   // Wait time
 
70
   fw->Unsigned32(m_wait_time);
 
71
 
 
72
   // done
 
73
}
 
74
 
 
75
/*
 
76
 * check if trigger conditions are done
 
77
 */
 
78
void Trigger_Time::check_set_conditions(Game* game) {
 
79
   if(((game->get_gametime()-m_last_start_time)/1000) < m_wait_time) return;
 
80
 
 
81
   // Time has come. Set us
 
82
   set_trigger(true);
 
83
}
 
84
 
 
85
/*
 
86
 * Reset this trigger. This is only valid for non one timers
 
87
 */
 
88
void Trigger_Time::reset_trigger(Game* game) {
 
89
   assert(!is_one_time_trigger());
 
90
 
 
91
   // save new start time
 
92
   // NOTE: if it took a while for an event to note us,
 
93
   // this time the trigger wasn't counting
 
94
   m_last_start_time=game->get_gametime();
 
95
 
 
96
   set_trigger(false);
 
97
}