ULTIMA8: Add a helper function to clean up list code

This commit is contained in:
Matthew Duggan 2021-06-19 16:38:56 +09:00
parent adebe08365
commit 6b66d7007a
3 changed files with 18 additions and 18 deletions

View File

@ -77,6 +77,14 @@ public:
_size++;
}
void appenduint16(uint16 val) {
assert(_elementSize == 2);
uint8 buf[2];
buf[0] = static_cast<uint8>(val);
buf[1] = static_cast<uint8>(val >> 8);
append(buf);
}
void remove(const uint8 *e) {
// do we need to erase all occurences of e or just the first one?
// (deleting all, currently)
@ -105,20 +113,23 @@ public:
}
void appendList(const UCList &l) {
// need to check if elementsizes match...
// elementsizes should match...
assert(_elementSize == l.getElementSize());
_elements.reserve(_elementSize * (_size + l._size));
unsigned int lsize = l._size;
for (unsigned int i = 0; i < lsize; i++)
for (unsigned int i = 0; i < l._size; i++)
append(l[i]);
}
void unionList(const UCList &l) { // like append, but remove duplicates
// need to check if elementsizes match...
// elementsizes should match...
assert(_elementSize == l.getElementSize());
_elements.reserve(_elementSize * (_size + l._size));
for (unsigned int i = 0; i < l._size; i++)
if (!inList(l[i]))
append(l[i]);
}
void subtractList(const UCList &l) {
// elementsizes should match...
assert(_elementSize == l.getElementSize());
for (unsigned int i = 0; i < l._size; i++)
remove(l[i]);
}

View File

@ -275,10 +275,7 @@ void Container::containerSearch(UCList *itemlist, const uint8 *loopscript,
if ((*iter)->checkLoopScript(loopscript, scriptsize)) {
assert(itemlist->getElementSize() == 2);
uint16 oId = (*iter)->getObjId();
uint8 buf[2];
buf[0] = static_cast<uint8>(oId);
buf[1] = static_cast<uint8>(oId >> 8);
itemlist->append(buf);
itemlist->appenduint16(oId);
}
if (recurse) {

View File

@ -607,11 +607,7 @@ void CurrentMap::areaSearch(UCList *itemlist, const uint8 *loopscript,
// check item against loopscript
if (item->checkLoopScript(loopscript, scriptsize)) {
assert(itemlist->getElementSize() == 2);
const uint16 objid = item->getObjId();
uint8 buf[2];
buf[0] = static_cast<uint8>(objid);
buf[1] = static_cast<uint8>(objid >> 8);
itemlist->append(buf);
itemlist->appenduint16(item->getObjId());
}
if (recurse) {
@ -696,11 +692,7 @@ void CurrentMap::surfaceSearch(UCList *itemlist, const uint8 *loopscript,
// check item against loopscript
if (item->checkLoopScript(loopscript, scriptsize)) {
assert(itemlist->getElementSize() == 2);
uint16 objid = item->getObjId();
uint8 buf[2];
buf[0] = static_cast<uint8>(objid);
buf[1] = static_cast<uint8>(objid >> 8);
itemlist->append(buf);
itemlist->appenduint16(item->getObjId());
}
}
}