Ok, I don't know where I posted this.
but, this program reads a file in that looks like this:
badtileid,goodtileid
badtiledi,goodtileid
it doesn't have any error checking, so I would add spaces, etc. The tileids are in decimal.
Now, the progam is executed like this:
tileswap tilefile mapmulfile
Where tilefile is the file described above, and the mapmulfile is the map#.mul you want to modify. Bot file names need to have their full location specified. You start the program from the command line!!!!
Make a BACKUP before you try this. UNTESTED.
Code:
#include <fstream>
#include <string>
#include <iostream>
#include <cstdlib>
#include <map>
bool convert(std::string &input, unsigned short &key, unsigned short &value)
{
bool rvalue=false;
std::size_t index=input.find(",");
if (index != std::string::npos)
{
std::string first = input.substr(0,index);
std::string second=input.substr(index+1);
key=std::atoi(first.c_str());
value=std::atoi(second.c_str());
rvalue=true;
}
return rvalue;
}
int main (int argc, char *argv[])
{
std::map<unsigned short,unsigned short> swaptable;
char buffer[1024];
int status=0;
std::map<unsigned short, unsigned short>::const_iterator iter ;
if (argc == 3)
{
std::string tilefilename = argv[1];
std::string mulfile = argv[2];
std::fstream tile;
tile.open(tilefilename.c_str(),std::ios_base::in);
unsigned short key,value;
if (tile.is_open())
{
while (!tile.eof())
{
tile.getline(buffer,1024);
std::string raw=buffer;
if (!raw.empty())
{
if (convert(raw,key,value))
{
swaptable.insert(std::make_pair(key,value));
}
else
std::cerr <<"Invalid line " <<raw<<std::endl;
}
}
// We have the swap buffer ready, now time to look at the data;
tile.close();
unsigned short tileid;
char alt;
int block=0;
tile.open(mulfile.c_str(),std::ios_base::in | std::ios_base::out | std::ios_base::binary);
if (tile.is_open())
{
std::cout <<"Processing " <<mulfile<<std::endl;
while (!tile.eof())
{
// for every block
tile.read(reinterpret_cast<char*>(&tileid),2);
tile.read(reinterpret_cast<char*>(&tileid),2);
std::size_t index;
for (int c=0;c<64;++c)
{
index = tile.tellg();
tile.read(reinterpret_cast<char*>(&tileid),2);
tile.read(&alt,1);
iter = swaptable.find(tileid);
if (iter != swaptable.end())
{
tile.seekp(index);
unsigned short ntile= iter->second;
tile.write(reinterpret_cast<char*>(&ntile),2);
std::cout <<"Tile in block " << block<<", cell " <<c<<" was " <<tileid<<" replaced with " << ntile <<std::endl;
}
}
++block;
}
tile.close();
}
else
std::cerr <<"Unable to open " << mulfile<<std::endl;
}
}
else
{
std::cerr<<"Usage: tileswap tilefilename mapmulfile " <<std::endl;
}
return(status);
}