~widelands-dev/widelands/trunk

« back to all changes in this revision

Viewing changes to src/trigger_building.cc

  • Committer: sigra
  • Date: 2007-01-26 16:12:13 UTC
  • Revision ID: git-v1:f983e08603f59bc6a6e99cb732d9102bfbfb3db8
* Try to optimize the set terrain tool by adding Map::set_height for areas. That means first set the height of every node in the area, then adjust the surrounding nodes ([http://sourceforge.net/tracker/index.php?func=detail&aid=1640305&group_id=40163&atid=427221], "Editor set height tool set to max produces funny mountain" is still present though).
* Use Area and Player_Area.
* Put most of the code from Event_Conquer_Area and Event_Unhide_Area into a common base.
* Initialize the names of events and triggers in their initializer lists.

git-svn-id: https://widelands.svn.sourceforge.net/svnroot/widelands/trunk@1965 37b2a8de-5219-0410-9f54-a31bc463ab9c

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (C) 2002-2004, 2006 by the Widelands Development Team
 
2
 * Copyright (C) 2002-2004, 2006-2007 by the Widelands Development Team
3
3
 *
4
4
 * This program is free software; you can redistribute it and/or
5
5
 * modify it under the terms of the GNU General Public License
18
18
 */
19
19
 
20
20
#include "building.h"
21
 
#include "editor_game_base.h"
22
21
#include "editorinteractive.h"
23
22
#include "error.h"
24
23
#include "filesystem.h"
26
25
#include "i18n.h"
27
26
#include "map.h"
28
27
#include "map_variable_manager.h"
 
28
#include "player.h"
29
29
#include "profile.h"
30
30
#include "trigger_building.h"
31
31
#include "util.h"
33
33
 
34
34
static const int TRIGGER_VERSION = 2;
35
35
 
36
 
/*
37
 
 * Init and cleanup
38
 
 */
39
 
Trigger_Building::Trigger_Building(void) {
40
 
        set_name(_("Building Trigger").c_str());
41
 
   set_trigger(false);
42
 
   m_count=-1;
43
 
   m_area=-1;
44
 
   m_pt.x=0;
45
 
   m_pt.y=0;
46
 
   m_player=-1;
47
 
   m_building=_("<unset>");
48
 
}
49
 
 
50
 
Trigger_Building::~Trigger_Building(void) {
51
 
}
52
 
 
53
 
/*
54
 
 * File Read, File Write
55
 
 */
 
36
Trigger_Building::Trigger_Building()
 
37
:
 
38
Trigger      (_("Building Trigger")),
 
39
m_player_area(0, Area(Coords(0, 0), 0)),
 
40
m_building   (_("<unset>")),
 
41
m_count      (0)
 
42
{set_trigger(false);}
 
43
 
 
44
Trigger_Building::~Trigger_Building() {}
 
45
 
56
46
void Trigger_Building::Read(Section* s, Editor_Game_Base* egbase) {
57
 
        const int version= s->get_safe_int("version");
58
 
 
59
 
        if (1 <= version and version <= TRIGGER_VERSION) {
60
 
                m_pt =
61
 
                        version == 1
62
 
                        ?
63
 
                        Coords(s->get_safe_int("point_x"), s->get_safe_int("point_y"))
64
 
                        :
65
 
                        s->get_safe_Coords("point");
66
 
      set_area( s->get_safe_int( "area" ));
67
 
      int player = s->get_safe_int( "player" );
68
 
      set_player(player);
69
 
                egbase->get_iabase()->reference_player_tribe(player, this);
 
47
        const int trigger_version= s->get_safe_int("version");
 
48
        if (1 <= trigger_version and trigger_version <= TRIGGER_VERSION) {
 
49
                m_player_area = Player_Area
 
50
                        (s->get_safe_int("player"),
 
51
                         Area
 
52
                         (trigger_version == 1
 
53
                          ?
 
54
                          Coords(s->get_safe_int("point_x"), s->get_safe_int("point_y"))
 
55
                          :
 
56
                          s->get_safe_Coords("point"),
 
57
                          s->get_safe_int("area")));
 
58
                egbase->get_iabase()
 
59
                        ->reference_player_tribe(m_player_area.player_number, this);
70
60
      set_building_count( s->get_int( "count" ));
71
61
      set_building( s->get_safe_string( "building" ));
72
 
      return;
73
 
   }
74
 
   throw wexception("Building Trigger with unknown/unhandled version %i in map!\n", version);
 
62
        } else throw wexception
 
63
                ("Building Trigger with unknown/unhandled version %i in map!\n",
 
64
                 trigger_version);
75
65
}
76
66
 
77
67
void Trigger_Building::Write(Section & s) const {
78
68
        s.set_int   ("version",  TRIGGER_VERSION);
79
 
        s.set_Coords("point",    m_pt);
80
 
        s.set_int   ("area",     get_area());
81
 
        s.set_int   ("player",   get_player());
 
69
        s.set_Coords("point",    m_player_area);
 
70
        s.set_int   ("area",     m_player_area.radius);
 
71
        s.set_int   ("player",   m_player_area.player_number);
82
72
        s.set_int   ("count",    get_building_count());
83
73
        s.set_string("building", m_building.c_str());
84
74
}
89
79
void Trigger_Building::check_set_conditions(Game* game) {
90
80
        const Map & map = game->map();
91
81
        if
92
 
                (m_pt.x < 0 or m_pt.x >= map.get_width ()
93
 
                 or
94
 
                 m_pt.y < 0 or m_pt.y >= map.get_height()
95
 
                 or
96
 
                 m_player <= 0 or m_player > MAX_PLAYERS)
 
82
                (m_player_area.x < 0 or map.get_width () <= m_player_area.x
 
83
                 or
 
84
                 m_player_area.y < 0 or map.get_height() <= m_player_area.y
 
85
                 or
 
86
                 m_player_area.player_number <= 0
 
87
                 or
 
88
                 map.get_nrplayers() < m_player_area.player_number)
97
89
                return;
98
90
 
99
91
 
100
 
   int count=0;
101
 
        MapRegion mr(game->map(), m_pt, m_area);
 
92
        uint count = 0;
 
93
        MapRegion mr(game->map(), m_player_area);
102
94
        FCoords fc;
103
95
        while (mr.next(fc)) if
104
 
                (const Building * const b =
 
96
                (const Building * const building =
105
97
                 dynamic_cast<const Building * const>(fc.field->get_immovable()))
106
98
        {
107
 
 
108
 
      if(b->get_owner()!=game->get_player(m_player)) continue;
109
 
      std::string name=b->get_name();
110
 
      if(name != m_building) continue;
111
 
      ++count;
112
 
   }
 
99
                if
 
100
                        (building->owner().get_player_number() == m_player_area.player_number
 
101
                         and
 
102
                         building->name() == m_building)
 
103
                        ++count;
 
104
        }
113
105
 
114
106
   if(count>=m_count) set_trigger(true);
115
107
 
116
108
   // Set MapVariable inttemp
117
 
        MapVariableManager & mvm = game->get_map()->get_mvm();
 
109
        MapVariableManager & mvm = game->map().get_mvm();
118
110
        Int_MapVariable * inttemp = mvm.get_int_variable("inttemp");
119
111
   if( !inttemp ) {
120
112
      inttemp = new Int_MapVariable( false );