1 // _________ __ __
2 // / _____// |_____________ _/ |______ ____ __ __ ______
3 // \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
4 // / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
5 // /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
6 // \/ \/ \//_____/ \/
7 // ______________________ ______________________
8 // T H E W A R B E G I N S
9 // Stratagus - A free fantasy real time strategy game engine
10 //
11 /**@name trigger_effect.cpp - The trigger effect source file. */
12 //
13 // (c) Copyright 2019 by Andrettin
14 //
15 // This program is free software; you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation; only version 2 of the License.
18 //
19 // This program is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program; if not, write to the Free Software
26 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 // 02111-1307, USA.
28 //
29
30 /*----------------------------------------------------------------------------
31 -- Includes
32 ----------------------------------------------------------------------------*/
33
34 #include "stratagus.h"
35
36 #include "trigger_effect.h"
37
38 #include "config.h"
39 #include "dialogue.h"
40 #include "unit/unit.h"
41 #include "unit/unittype.h"
42
43 /*----------------------------------------------------------------------------
44 -- Functions
45 ----------------------------------------------------------------------------*/
46
47 /**
48 ** @brief Process data provided by a configuration file
49 **
50 ** @param config_data The configuration data
51 */
ProcessConfigData(const CConfigData * config_data)52 void CCallDialogueTriggerEffect::ProcessConfigData(const CConfigData *config_data)
53 {
54 for (size_t i = 0; i < config_data->Properties.size(); ++i) {
55 std::string key = config_data->Properties[i].first;
56 std::string value = config_data->Properties[i].second;
57
58 if (key == "dialogue") {
59 value = FindAndReplaceString(value, "_", "-");
60 CDialogue *dialogue = CDialogue::GetDialogue(value);
61 if (dialogue) {
62 this->Dialogue = dialogue;
63 }
64 } else {
65 fprintf(stderr, "Invalid trigger property: \"%s\".\n", key.c_str());
66 }
67 }
68
69 if (!this->Dialogue) {
70 fprintf(stderr, "Call dialogue trigger effect has no dialogue.\n");
71 }
72 }
73
Do(CPlayer * player) const74 void CCallDialogueTriggerEffect::Do(CPlayer *player) const
75 {
76 this->Dialogue->Call(player->Index);
77 }
78
79 /**
80 ** @brief Process data provided by a configuration file
81 **
82 ** @param config_data The configuration data
83 */
ProcessConfigData(const CConfigData * config_data)84 void CCreateUnitTriggerEffect::ProcessConfigData(const CConfigData *config_data)
85 {
86 for (size_t i = 0; i < config_data->Properties.size(); ++i) {
87 std::string key = config_data->Properties[i].first;
88 std::string value = config_data->Properties[i].second;
89
90 if (key == "quantity") {
91 this->Quantity = std::stoi(value);
92 } else if (key == "unit_type") {
93 value = FindAndReplaceString(value, "_", "-");
94 CUnitType *unit_type = UnitTypeByIdent(value);
95 if (unit_type) {
96 this->UnitType = unit_type;
97 } else {
98 fprintf(stderr, "Unit type \"%s\" does not exist.\n", value.c_str());
99 }
100 } else {
101 fprintf(stderr, "Invalid trigger property: \"%s\".\n", key.c_str());
102 }
103 }
104
105 if (!this->UnitType) {
106 fprintf(stderr, "Create unit trigger effect has no unit type.\n");
107 }
108 }
109
Do(CPlayer * player) const110 void CCreateUnitTriggerEffect::Do(CPlayer *player) const
111 {
112 CUnit *unit = MakeUnitAndPlace(player->StartPos, *this->UnitType, player, player->StartMapLayer);
113 }
114