Skip to content

Commit d710cae

Browse files
committed
Remove MaterialInfo consts
in favor of using df::builtin_mats enum entries Also adjust some comparisons to use more easily understandable values (e.g. <= CREATURE_200 instead of < HIST_FIG_1)
1 parent 46b2561 commit d710cae

13 files changed

Lines changed: 44 additions & 48 deletions

File tree

docs/changelog.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,13 @@ Template for new versions:
6868
## API
6969

7070
- ``Screen``: new functions ``paintMapPortTile`` and ``readMapPortTile`` to write and read world and region map tiles.
71+
- ``Materials``: ``MaterialInfo`` constants ``NUM_BUILTIN``, ``GROUP_SIZE``, `CREATURE_BASE``, ``FIGURE_BASE``, ``PLANT_BASE``, and ``END_BASE`` removed. New plugins should use appropriate members of the ``df::builtin_mats`` enum.
7172

7273
## Lua
7374

7475
- Added ``Screen::paintMapPortTile`` as ``dfhack.screen.paintMapPortTile``
7576
- Added ``Screen::readMapPortTile`` as ``dfhack.screen.readMapPortTile``
76-
77+
- Deprecated ``gui.materials.CREATURE_BASE`` and ``gui.materials.PLANT_BASE`` - scripts should instead use ``df.builtin_mats.CREATURE_1`` and ``df.builtin_mats.PLANT_1``, respectively.
7778

7879
## Removed
7980

library/LuaApi.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ distribution.
7272
#include "df/building_stockpilest.h"
7373
#include "df/building_tradedepotst.h"
7474
#include "df/building_workshopst.h"
75+
#include "df/builtin_mats.h"
7576
#include "df/burrow.h"
7677
#include "df/caravan_state.h"
7778
#include "df/construction.h"
@@ -491,7 +492,7 @@ static bool decode_matinfo(lua_State *state, MaterialInfo *info, bool numpair =
491492
if (auto item = Lua::GetDFObject<df::item>(state, 1))
492493
return info->decode(item);
493494
if (auto plant = Lua::GetDFObject<df::plant>(state, 1))
494-
return info->decode(MaterialInfo::PLANT_BASE, plant->material);
495+
return info->decode(df::builtin_mats::PLANT_1, plant->material);
495496
if (auto mvec = Lua::GetDFObject<df::material_vec_ref>(state, 1))
496497
return info->decode(*mvec, luaL_checkint(state, 2));
497498
}

library/RemoteTools.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ static command_result ListMaterials(color_ostream &stream,
530530

531531
if (in->builtin())
532532
{
533-
for (int i = 0; i < MaterialInfo::NUM_BUILTIN; i++)
533+
for (int i = 0; i < df::builtin_mats::CREATURE_1; i++)
534534
listMaterial(out, i, -1, mask);
535535
}
536536

@@ -549,7 +549,7 @@ static command_result ListMaterials(color_ostream &stream,
549549
auto praw = vec[i];
550550

551551
for (size_t j = 0; j < praw->material.size(); j++)
552-
listMaterial(out, MaterialInfo::CREATURE_BASE+j, i, mask);
552+
listMaterial(out, df::builtin_mats::CREATURE_1+j, i, mask);
553553
}
554554
}
555555

@@ -561,7 +561,7 @@ static command_result ListMaterials(color_ostream &stream,
561561
auto praw = vec[i];
562562

563563
for (size_t j = 0; j < praw->material.size(); j++)
564-
listMaterial(out, MaterialInfo::PLANT_BASE+j, i, mask);
564+
listMaterial(out, df::builtin_mats::PLANT_1+j, i, mask);
565565
}
566566
}
567567

library/include/modules/Materials.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,6 @@ namespace DFHack
6666

6767
struct DFHACK_EXPORT MaterialInfo
6868
{
69-
static const int NUM_BUILTIN = 19;
70-
static const int GROUP_SIZE = 200;
71-
static const int CREATURE_BASE = NUM_BUILTIN;
72-
static const int FIGURE_BASE = NUM_BUILTIN + GROUP_SIZE;
73-
static const int PLANT_BASE = NUM_BUILTIN + GROUP_SIZE * 2;
74-
static const int END_BASE = NUM_BUILTIN + GROUP_SIZE * 3;
75-
7669
int16_t type;
7770
int32_t index;
7871

library/lua/gui/materials.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ local dlg = require('gui.dialogs')
88

99
ARROW = string.char(26)
1010

11-
CREATURE_BASE = 19
12-
PLANT_BASE = 419
11+
-- For backwards compatibility with older scripts
12+
CREATURE_BASE = df.builtin_mats.CREATURE_1
13+
PLANT_BASE = df.builtin_mats.PLANT_1
1314

1415
MaterialDialog = defclass(MaterialDialog, gui.FramedScreen)
1516

@@ -127,7 +128,7 @@ function MaterialDialog:initCreatureMode()
127128
local choices = {}
128129

129130
for i,v in ipairs(df.global.world.raws.creatures.all) do
130-
self:addObjectChoice(choices, v, v.name[0], CREATURE_BASE, i)
131+
self:addObjectChoice(choices, v, v.name[0], df.builtin_mats.CREATURE_1, i)
131132
end
132133

133134
self:pushContext('Creature materials', choices)
@@ -137,7 +138,7 @@ function MaterialDialog:initPlantMode()
137138
local choices = {}
138139

139140
for i,v in ipairs(df.global.world.raws.plants.all) do
140-
self:addObjectChoice(choices, v, v.name, PLANT_BASE, i)
141+
self:addObjectChoice(choices, v, v.name, df.builtin_mats.PLANT_1, i)
141142
end
142143

143144
self:pushContext('Plant materials', choices)

library/lua/tile-material.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ function GetTreeMat(x, y, z)
194194
for _, tree in ipairs(df.global.world.plants.all) do
195195
if tree.tree_info ~= nil then
196196
if coordInTree(pos, tree) then
197-
return dfhack.matinfo.decode(419, tree.material)
197+
return dfhack.matinfo.decode(df.builtin_mats.PLANT_1, tree.material)
198198
end
199199
end
200200
end
@@ -209,7 +209,7 @@ function GetShrubMat(x, y, z)
209209
for _, shrub in ipairs(df.global.world.plants.all) do
210210
if shrub.tree_info == nil then
211211
if shrub.pos.x == pos.x and shrub.pos.y == pos.y and shrub.pos.z == pos.z then
212-
return dfhack.matinfo.decode(419, shrub.material)
212+
return dfhack.matinfo.decode(df.builtin_mats.PLANT_1, shrub.material)
213213
end
214214
end
215215
end

library/modules/Items.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1756,7 +1756,7 @@ int32_t Items::pickGrowthPrint(int16_t subtype, int16_t mat, int32_t matg)
17561756
{
17571757
int growth_print = -1;
17581758
// Make sure it's made of a valid plant material, then grab its definition
1759-
if (mat >= 419 && mat <= 618 && matg >= 0 && (unsigned)matg < world->raws.plants.all.size())
1759+
if (mat >= df::builtin_mats::PLANT_1 && mat <= df::builtin_mats::PLANT_200 && matg >= 0 && (unsigned)matg < world->raws.plants.all.size())
17601760
{
17611761
auto plant_def = world->raws.plants.all[matg];
17621762
// Make sure it subtype is also valid

library/modules/Kitchen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void Kitchen::debug_print(color_ostream &out)
3939
plotinfo->kitchen.mat_types[i],
4040
plotinfo->kitchen.mat_indices[i],
4141
plotinfo->kitchen.exc_types[i].whole,
42-
(plotinfo->kitchen.mat_types[i] >= 419 && plotinfo->kitchen.mat_types[i] <= 618) ? world->raws.plants.all[plotinfo->kitchen.mat_indices[i]]->id : "n/a"
42+
(plotinfo->kitchen.mat_types[i] >= df::builtin_mats::PLANT_1 && plotinfo->kitchen.mat_types[i] <= df::builtin_mats::PLANT_200) ? world->raws.plants.all[plotinfo->kitchen.mat_indices[i]]->id : "n/a"
4343
);
4444
}
4545
out.print("\n");

library/modules/MapCache.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ t_matpair MapExtras::BlockInfo::getBaseMaterial(df::tiletype tt, df::coord2d pos
943943
case ROOT:
944944
case TREE:
945945
case PLANT:
946-
rv.mat_type = MaterialInfo::PLANT_BASE;
946+
rv.mat_type = df::builtin_mats::PLANT_1;
947947
if (auto plant = plants[block->map_pos + df::coord(x,y,0)])
948948
{
949949
if (auto raw = df::plant_raw::find(plant->material))
@@ -958,7 +958,7 @@ t_matpair MapExtras::BlockInfo::getBaseMaterial(df::tiletype tt, df::coord2d pos
958958
case GRASS_DARK:
959959
case GRASS_DRY:
960960
case GRASS_DEAD:
961-
rv.mat_type = MaterialInfo::PLANT_BASE;
961+
rv.mat_type = df::builtin_mats::PLANT_1;
962962
if (auto raw = df::plant_raw::find(grass[x][y]))
963963
{
964964
rv.mat_type = raw->material_defs.type[plant_material_def::basic_mat];

library/modules/Materials.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,31 +108,31 @@ bool MaterialInfo::decode(int16_t type, int32_t index)
108108
{
109109
material = raws.mat_table.builtin[type];
110110
}
111-
else if (type == 0)
111+
else if (type == df::builtin_mats::INORGANIC)
112112
{
113113
mode = Inorganic;
114114
inorganic = df::inorganic_raw::find(index);
115115
if (!inorganic)
116116
return false;
117117
material = &inorganic->material;
118118
}
119-
else if (type < CREATURE_BASE)
119+
else if (type < df::builtin_mats::CREATURE_1)
120120
{
121121
material = raws.mat_table.builtin[type];
122122
}
123-
else if (type < FIGURE_BASE)
123+
else if (type <= df::builtin_mats::CREATURE_200)
124124
{
125125
mode = Creature;
126-
subtype = type - CREATURE_BASE;
126+
subtype = type - df::builtin_mats::CREATURE_1;
127127
creature = df::creature_raw::find(index);
128128
if (!creature || size_t(subtype) >= creature->material.size())
129129
return false;
130130
material = creature->material[subtype];
131131
}
132-
else if (type < PLANT_BASE)
132+
else if (type <= df::builtin_mats::HIST_FIG_200)
133133
{
134134
mode = Creature;
135-
subtype = type - FIGURE_BASE;
135+
subtype = type - df::builtin_mats::HIST_FIG_1;
136136
figure = df::historical_figure::find(index);
137137
if (!figure)
138138
return false;
@@ -141,10 +141,10 @@ bool MaterialInfo::decode(int16_t type, int32_t index)
141141
return false;
142142
material = creature->material[subtype];
143143
}
144-
else if (type < END_BASE)
144+
else if (type <= df::builtin_mats::PLANT_200)
145145
{
146146
mode = Plant;
147-
subtype = type - PLANT_BASE;
147+
subtype = type - df::builtin_mats::PLANT_1;
148148
plant = df::plant_raw::find(index);
149149
if (!plant || size_t(subtype) >= plant->material.size())
150150
return false;
@@ -219,7 +219,7 @@ bool MaterialInfo::findBuiltin(const std::string& token)
219219
}
220220

221221
auto& raws = world->raws;
222-
for (int i = 0; i < NUM_BUILTIN; i++)
222+
for (int i = 0; i < df::builtin_mats::CREATURE_1; i++)
223223
{
224224
auto obj = raws.mat_table.builtin[i];
225225
if (obj && obj->id == token)
@@ -266,7 +266,7 @@ bool MaterialInfo::findPlant(const std::string& token, const std::string& subtok
266266

267267
for (size_t j = 0; j < p->material.size(); j++)
268268
if (p->material[j]->id == subtoken)
269-
return decode(PLANT_BASE + j, i);
269+
return decode(df::builtin_mats::PLANT_1 + j, i);
270270

271271
break;
272272
}
@@ -286,7 +286,7 @@ bool MaterialInfo::findCreature(const std::string& token, const std::string& sub
286286

287287
for (size_t j = 0; j < p->material.size(); j++)
288288
if (p->material[j]->id == subtoken)
289-
return decode(CREATURE_BASE + j, i);
289+
return decode(df::builtin_mats::CREATURE_1 + j, i);
290290

291291
break;
292292
}

0 commit comments

Comments
 (0)