Skip to content

Commit 603297c

Browse files
rubenwardyest31
authored andcommittedMay 19, 2015
Add texture overriding
1 parent 43702ec commit 603297c

File tree

5 files changed

+111
-5
lines changed

5 files changed

+111
-5
lines changed
 

Diff for: ‎doc/texture_overrides.txt

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Texture Overrides
2+
=================
3+
4+
You can override the textures of a node from a texture pack using
5+
texture overrides. To do this, create a file in a texture pack
6+
called override.txt
7+
8+
Basic Format
9+
------------
10+
11+
Each line in an override.txt file is a rule. It consists of
12+
13+
nodename face-selector texture
14+
15+
For example,
16+
17+
default:dirt_with_grass sides default_stone.png
18+
19+
You can use ^ operators as usual:
20+
21+
default:dirt_with_grass sides default_stone.png^[brighten
22+
23+
Face Selectors
24+
--------------
25+
26+
| face-selector | behavior |
27+
|---------------|---------------------------------------------------|
28+
| left | x- |
29+
| right | x+ |
30+
| front | z- |
31+
| back | z+ |
32+
| top | z+ |
33+
| bottom | z- |
Has conversations. Original line has conversations.
34+
| sides | x-, x+, z-, z+ |
35+
| all | All faces. You can also use '*' instead of 'all'. |

Diff for: ‎src/client.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -1741,6 +1741,9 @@ void Client::afterContentReceived(IrrlichtDevice *device)
17411741
text = wgettext("Initializing nodes...");
17421742
draw_load_screen(text, device, guienv, 0, 72);
17431743
m_nodedef->updateAliases(m_itemdef);
1744+
std::string texture_path = g_settings->get("texture_path");
1745+
if (texture_path != "" && fs::IsDir(texture_path))
1746+
m_nodedef->applyTextureOverrides(texture_path + DIR_DELIM + "override.txt");
17441747
m_nodedef->setNodeRegistrationStatus(true);
17451748
m_nodedef->runNodeResolveCallbacks();
17461749
delete[] text;

Diff for: ‎src/nodedef.cpp

+63-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3333
#include "exceptions.h"
3434
#include "debug.h"
3535
#include "gamedef.h"
36+
#include <fstream> // Used in applyTextureOverrides()
3637

3738
/*
3839
NodeBox
@@ -397,6 +398,7 @@ class CNodeDefManager: public IWritableNodeDefManager {
397398
virtual content_t set(const std::string &name, const ContentFeatures &def);
398399
virtual content_t allocateDummy(const std::string &name);
399400
virtual void updateAliases(IItemDefManager *idef);
401+
virtual void applyTextureOverrides(const std::string &override_filepath);
400402
virtual void updateTextures(IGameDef *gamedef,
401403
void (*progress_cbk)(void *progress_args, u32 progress, u32 max_progress),
402404
void *progress_cbk_args);
@@ -670,7 +672,7 @@ content_t CNodeDefManager::set(const std::string &name, const ContentFeatures &d
670672
j = m_group_to_items.find(group_name);
671673
if (j == m_group_to_items.end()) {
672674
m_group_to_items[group_name].push_back(
673-
std::make_pair(id, i->second));
675+
std::make_pair(id, i->second));
674676
} else {
675677
GroupItems &items = j->second;
676678
items.push_back(std::make_pair(id, i->second));
@@ -700,7 +702,66 @@ void CNodeDefManager::updateAliases(IItemDefManager *idef)
700702
content_t id;
701703
if (m_name_id_mapping.getId(convert_to, id)) {
702704
m_name_id_mapping_with_aliases.insert(
703-
std::make_pair(name, id));
705+
std::make_pair(name, id));
706+
}
707+
}
708+
}
709+
710+
void CNodeDefManager::applyTextureOverrides(const std::string &override_filepath)
711+
{
712+
infostream << "CNodeDefManager::applyTextureOverrides(): Applying "
713+
"overrides to textures from " << override_filepath << std::endl;
714+
715+
std::ifstream infile(override_filepath.c_str());
716+
std::string line;
717+
int line_c = 0;
718+
while (std::getline(infile, line)) {
719+
line_c++;
720+
if (trim(line) == "")
721+
continue;
722+
std::vector<std::string> splitted = str_split(line, ' ');
723+
if (splitted.size() != 3) {
724+
errorstream << override_filepath
725+
<< ":" << line_c << " Could not apply texture override \""
726+
<< line << "\": Syntax error" << std::endl;
727+
continue;
728+
}
729+
730+
content_t id;
731+
if (!getId(splitted[0], id)) {
732+
errorstream << override_filepath
733+
<< ":" << line_c << " Could not apply texture override \""
734+
<< line << "\": Unknown node \""
735+
<< splitted[0] << "\"" << std::endl;
736+
continue;
737+
}
738+
739+
ContentFeatures &nodedef = m_content_features[id];
740+
741+
if (splitted[1] == "top")
742+
nodedef.tiledef[0].name = splitted[2];
743+
else if (splitted[1] == "bottom")
744+
nodedef.tiledef[1].name = splitted[2];
745+
else if (splitted[1] == "right")
746+
nodedef.tiledef[2].name = splitted[2];
747+
else if (splitted[1] == "left")
748+
nodedef.tiledef[3].name = splitted[2];
749+
else if (splitted[1] == "back")
750+
nodedef.tiledef[4].name = splitted[2];
751+
else if (splitted[1] == "front")
752+
nodedef.tiledef[5].name = splitted[2];
753+
else if (splitted[1] == "all" || splitted[1] == "*")
754+
for (int i = 0; i < 6; i++)
755+
nodedef.tiledef[i].name = splitted[2];
756+
else if (splitted[1] == "sides")
757+
for (int i = 2; i < 6; i++)
758+
nodedef.tiledef[i].name = splitted[2];
759+
else {
760+
errorstream << override_filepath
761+
<< ":" << line_c << " Could not apply texture override \""
762+
<< line << "\": Unknown node side \""
763+
<< splitted[1] << "\"" << std::endl;
764+
continue;
704765
}
705766
}
706767
}

Diff for: ‎src/nodedef.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,11 @@ class IWritableNodeDefManager : public INodeDefManager {
335335
*/
336336
virtual void updateAliases(IItemDefManager *idef)=0;
337337

338+
/*
339+
Override textures from servers with ones specified in texturepack/override.txt
340+
*/
341+
virtual void applyTextureOverrides(const std::string &override_filepath)=0;
342+
338343
/*
339344
Update tile textures to latest return values of TextueSource.
340345
*/
@@ -378,4 +383,3 @@ class NodeResolver {
378383
};
379384

380385
#endif
381-

Diff for: ‎src/server.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,11 @@ Server::Server(
335335
// Apply item aliases in the node definition manager
336336
m_nodedef->updateAliases(m_itemdef);
337337

338+
// Apply texture overrides from texturepack/override.txt
339+
std::string texture_path = g_settings->get("texture_path");
340+
if (texture_path != "" && fs::IsDir(texture_path))
341+
m_nodedef->applyTextureOverrides(texture_path + DIR_DELIM + "override.txt");
342+
338343
m_nodedef->setNodeRegistrationStatus(true);
339344

340345
// Perform pending node name resolutions
@@ -3397,5 +3402,3 @@ void dedicated_server_loop(Server &server, bool &kill)
33973402
}
33983403
}
33993404
}
3400-
3401-

1 commit comments

Comments
 (1)

4aiman commented on Jun 3, 2015

@4aiman
Contributor

@everyone
So... instead of replacing some textures now one can use this to change the texturepack?

Please sign in to comment.