1 /* ResidualVM - A 3D game interpreter
2 *
3 * ResidualVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the AUTHORS
5 * file distributed with this source distribution.
6 *
7 * Additional copyright for this file:
8 * Copyright (C) 1999-2000 Revolution Software Ltd.
9 * This code is based on source code created by Revolution Software,
10 * used with permission.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 *
26 */
27
28 #include "engines/icb/p4.h"
29 #include "engines/icb/icon_list.h"
30 #include "engines/icb/debug.h"
31
32 namespace ICB {
33
34 // Don't use #defines for common strings as it wastes rdata storage (on PSX at least)
35 const char *global_deleted_list = "DELETED_LIST";
36 const char *iconListEmptyIcon = "empty";
37
_icon_list()38 _icon_list::_icon_list() {
39 m_pcListName = const_cast<char *>(ICON_LIST_DELETED_PLACEHOLDER);
40 m_eScope = CURRENT_LOGIC;
41 m_nItemCount = 0;
42 m_bAllowDuplicates = TRUE8;
43 m_nPad1 = 0;
44 m_nPad2 = 0;
45
46 // Note, a hash of 0 means unknown HASH value i.e. NULL_HASH = 0.
47 assert(NULL_HASH == 0);
48 memset((uint8 *)m_pnIconListHash, 0, ICON_LIST_MAX_ICONS * sizeof(uint32));
49 memset((uint8 *)m_pnDuplicateCount, 0, ICON_LIST_MAX_ICONS * sizeof(uint8));
50 memset((uint8 *)m_ppcIconList, 0, ICON_LIST_MAX_ICONS * MAXLEN_ICON_NAME * sizeof(char));
51 }
52
Clone(const _icon_list & oSource)53 void _icon_list::Clone(const _icon_list &oSource) {
54 uint32 i;
55
56 m_pcListName = oSource.m_pcListName;
57
58 m_eScope = oSource.m_eScope;
59 m_nItemCount = oSource.m_nItemCount;
60 m_bAllowDuplicates = oSource.m_bAllowDuplicates;
61
62 for (i = 0; i < m_nItemCount; ++i) {
63 Set_string(const_cast<char *>(oSource.m_ppcIconList[i]), m_ppcIconList[i], MAXLEN_ICON_NAME);
64 m_pnIconListHash[i] = oSource.m_pnIconListHash[i];
65 m_pnDuplicateCount[i] = oSource.m_pnDuplicateCount[i];
66 }
67 }
68
GetDuplicateCount(const char * pcIconName) const69 uint8 _icon_list::GetDuplicateCount(const char *pcIconName) const {
70 uint32 i = 0;
71
72 if (strlen(pcIconName) == 0)
73 Fatal_error("Empty icon name passed into _icon_list::GetDuplicateCount()");
74
75 uint32 nIconNameHash = HashString(pcIconName);
76
77 for (i = 0; i < m_nItemCount; ++i) {
78 if (m_pnIconListHash[i] == nIconNameHash) {
79 if (m_bAllowDuplicates)
80 return (m_pnDuplicateCount[i]);
81 else
82 return (1);
83 }
84 }
85
86 // We fell off the end of the list, which means the icon is not in the list.
87 return (0);
88 }
89
SetAbsoluteIconCount(const char * pcIconName,uint32 nCount)90 void _icon_list::SetAbsoluteIconCount(const char *pcIconName, uint32 nCount) {
91 uint32 i;
92 uint32 nHash;
93
94 // If the list does not allow duplicates, this function is disallowed.
95 if (!m_bAllowDuplicates)
96 Fatal_error("SetAbsoluteIconCount() called for list %s which doesn't allow duplicates", m_pcListName);
97
98 // Also, count must be in range.
99 if (nCount > ICON_MAX_DUPLICATE_COUNT)
100 Fatal_error("Attempt to set %d of icon %s in list %s (maximum=%d)", nCount, pcIconName, m_pcListName, ICON_MAX_DUPLICATE_COUNT);
101
102 // Work out the hash of the icon name.
103 nHash = HashString(pcIconName);
104
105 // Check if it is in the list already.
106 i = 0;
107 while ((i < m_nItemCount) && (m_pnIconListHash[i] != nHash))
108 ++i;
109
110 // Did we find it?
111 if (i == m_nItemCount) {
112 // No, it wasn't in the list. Can't add a new one if the list is full.
113 if (m_nItemCount == ICON_LIST_MAX_ICONS)
114 Fatal_error("_icon_list::SetAbsoluteIconCount(): Icon list %s full adding %s", m_pcListName, pcIconName);
115
116 // Add a fresh entry.
117 Set_string(const_cast<char *>(pcIconName), m_ppcIconList[i], MAXLEN_ICON_NAME);
118
119 m_pnIconListHash[i] = nHash;
120 m_pnDuplicateCount[i] = (uint8)nCount;
121 ++m_nItemCount;
122 } else {
123 // Icon is already in list.
124 m_pnDuplicateCount[i] = (uint8)nCount;
125 }
126 }
127
AddIcon(const char * pcIconName,const uint32 nIconNameHash)128 void _icon_list::AddIcon(const char *pcIconName, const uint32 nIconNameHash) {
129 uint32 i;
130
131 // If the 'empty' icon is the only one in the list, remove it before adding the new one.
132 if ((m_nItemCount == 1) && (strcmp(m_ppcIconList[0], ICON_LIST_EMPTY_ICON) == 0))
133 --m_nItemCount;
134
135 // Check if it is in the list already.
136 i = 0;
137 while ((i < m_nItemCount) && (m_pnIconListHash[i] != nIconNameHash))
138 ++i;
139
140 // Did we find it?
141 if (i == m_nItemCount) {
142 // No, it wasn't in the list.
143 if (m_nItemCount == ICON_LIST_MAX_ICONS)
144 Fatal_error("_icon_list::AddItem(): Icon list %s full", m_pcListName);
145
146 // Add a fresh entry.
147 Set_string(const_cast<char *>(pcIconName), m_ppcIconList[i], MAXLEN_ICON_NAME);
148 m_pnIconListHash[i] = nIconNameHash;
149 m_pnDuplicateCount[i] = 1;
150 ++m_nItemCount;
151 } else {
152 // Yes, it is already in the list, so behaviour depends on whether or not duplicates are allowed. Nothing to do if
153 // duplicates are not allowed.
154 if (m_bAllowDuplicates) {
155 // If the name of the icon is 'return' or 'goback' we don't add a duplicate.
156 if (strcmp(pcIconName, "return") && strcmp(pcIconName, "goback")) {
157 if (m_pnDuplicateCount[i] < ICON_MAX_DUPLICATE_COUNT)
158 ++m_pnDuplicateCount[i];
159 }
160 }
161 }
162 }
163
RemoveIcon(const char * pcIconName,bool8 bForceRemove)164 void _icon_list::RemoveIcon(const char *pcIconName, bool8 bForceRemove) {
165 uint32 i, j;
166 uint32 nIconNameHash = HashString(pcIconName);
167
168 // Find it in the list.
169 i = 0;
170 while ((i < m_nItemCount) && (m_pnIconListHash[i] != nIconNameHash))
171 ++i;
172
173 // Don't do anything if it isn't in the list. And don't do anything if the count is already at zero.
174 if ((i < m_nItemCount) && (m_pnDuplicateCount[i] > 0)) {
175 // Decrement the duplicate count.
176 if (bForceRemove)
177 m_pnDuplicateCount[i] = 0;
178 else
179 --m_pnDuplicateCount[i];
180
181 // We can remove the item if the count hits zero. This is the same whether or not duplicates are allowed.
182 if (m_pnDuplicateCount[i] == 0) {
183 // Might seem inefficient to remove holes from the array, rather than just mark them deleted, but it is
184 // swings and roundabouts: other code ends up less efficient if I don't remove holes, and it's not like
185 // we're dealing with thousands of array elements.
186 for (j = i + 1; j < m_nItemCount; ++j) {
187 strcpy(m_ppcIconList[j - 1], m_ppcIconList[j]);
188 m_pnIconListHash[j - 1] = m_pnIconListHash[j];
189 m_pnDuplicateCount[j - 1] = m_pnDuplicateCount[j];
190 }
191
192 // Don't actually need to delete the item; reducing the count takes care of it.
193 --m_nItemCount;
194 }
195 }
196 }
197
GetDuplicateCount(uint32 nIndex) const198 uint8 _icon_list::GetDuplicateCount(uint32 nIndex) const {
199 if (m_bAllowDuplicates)
200 return (m_pnDuplicateCount[nIndex]);
201 else
202 return ((m_pnDuplicateCount[nIndex] == 0) ? (uint8)0 : (uint8)1);
203 }
204
GetIconPosition(const char * pcIconName) const205 int32 _icon_list::GetIconPosition(const char *pcIconName) const {
206 uint32 i;
207 uint32 nIconNameHash;
208
209 // Get hash we are looking for.
210 nIconNameHash = HashString(pcIconName);
211
212 for (i = 0; i < m_nItemCount; ++i) {
213 if (m_pnIconListHash[i] == nIconNameHash)
214 return (i);
215 }
216
217 // Not found.
218 return (-1);
219 }
220
221 } // End of namespace ICB
222