1 /**
2  ** Mappatch.cc - Patches to the game map.
3  **
4  ** Written: 10-18-2001
5  **/
6 
7 /*
8 Copyright (C) 2001-2013 The Exult Team
9 
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License
12 as published by the Free Software Foundation; either version 2
13 of the License, or (at your option) any later version.
14 
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23 */
24 
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28 
29 #include "mappatch.h"
30 #include "gamewin.h"
31 #include "objs.h"
32 
33 /*
34  *  Find (first) matching object.
35  */
36 
find()37 Game_object *Map_patch::find(
38 ) {
39 	Game_object_vector vec;     // Pass mask=0xb0 to get any object.
40 	Game_object::find_nearby(vec, spec.loc, spec.shapenum, 0, 0xb0,
41 	                         spec.quality, spec.framenum);
42 	return vec.empty() ? nullptr : vec.front();
43 }
44 
45 /*
46  *  Apply by removing object.
47  *
48  *  Output: false if no object found.
49  */
50 
apply()51 bool Map_patch_remove::apply(
52 ) {
53 	bool found = false;
54 	Game_object *obj;
55 	while ((obj = find()) != nullptr) {
56 		obj->remove_this();
57 		found = true;
58 		if (!all)       // Just one?
59 			return true;
60 	}
61 	return found;
62 }
63 
64 /*
65  *  Apply by moving/changing object.
66  *
67  *  Output: false if no object found.
68  */
69 
apply()70 bool Map_patch_modify::apply(
71 ) {
72 	Game_object *obj = find();
73 	if (!obj)
74 		return false;
75 	Game_object_shared keep;
76 	obj->remove_this(&keep);        // Remove but don't delete.
77 	if (mod.shapenum != c_any_shapenum)
78 		obj->set_shape(mod.shapenum);
79 	if (mod.framenum != c_any_framenum)
80 		obj->change_frame(mod.framenum);
81 	if (mod.quality != c_any_qual)
82 		obj->set_quality(mod.quality);
83 	obj->set_invalid();     // To add it back correctly.
84 	obj->move(mod.loc);
85 	return true;
86 }
87 
88 /*
89  *  Add a new patch.
90  */
91 
add(std::unique_ptr<Map_patch> p)92 void Map_patch_collection::add(
93     std::unique_ptr<Map_patch> p
94 ) {
95 	// Get superchunk coords.
96 	int sx = p->spec.loc.tx / c_tiles_per_schunk;
97 	int sy = p->spec.loc.ty / c_tiles_per_schunk;
98 	// Get superchunk # (0-143).
99 	int schunk = sy * c_num_schunks + sx;
100 	patches[schunk].push_back(std::move(p));
101 }
102 
103 /*
104  *  Apply all patches for a superchunk.
105  */
106 
apply(int schunk)107 void Map_patch_collection::apply(
108     int schunk
109 ) {
110 	auto it1 = patches.find(schunk);
111 	if (it1 != patches.end()) { // Found list for superchunk?
112 		Map_patch_list &lst = it1->second;
113 		for (auto& it2 : lst)
114 			it2->apply();    // Apply each one in list.
115 	}
116 }
117