Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
illuna-minetest
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Illuna-Minetest
illuna-minetest
Commits
03e0dd33
Commit
03e0dd33
authored
10 years ago
by
gregorycu
Committed by
kwolekr
10 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Optimize minetest.get_(all)_craft_recipe(s)
Signed off by: ShadowNinja, kwolekr
parent
1e4fb80d
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/craftdef.cpp
+28
-75
28 additions, 75 deletions
src/craftdef.cpp
src/craftdef.h
+4
-8
4 additions, 8 deletions
src/craftdef.h
src/script/lua_api/l_craft.cpp
+75
-97
75 additions, 97 deletions
src/script/lua_api/l_craft.cpp
with
107 additions
and
180 deletions
src/craftdef.cpp
+
28
−
75
View file @
03e0dd33
...
...
@@ -27,6 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include
"gamedef.h"
#include
"inventory.h"
#include
"util/serialize.h"
#include
"util/numeric.h"
#include
"strfnd.h"
#include
"exceptions.h"
...
...
@@ -931,85 +932,30 @@ class CCraftDefManager: public IWritableCraftDefManager
}
return
false
;
}
virtual
bool
getCraftRecipe
(
CraftInput
&
input
,
CraftOutput
&
output
,
IGameDef
*
gamedef
)
const
{
CraftOutput
tmpout
;
tmpout
.
item
=
""
;
tmpout
.
time
=
0
;
// If output item is empty, abort.
if
(
output
.
item
.
empty
())
return
false
;
// Walk crafting definitions from back to front, so that later
// definitions can override earlier ones.
for
(
std
::
vector
<
CraftDefinition
*>::
const_reverse_iterator
i
=
m_craft_definitions
.
rbegin
();
i
!=
m_craft_definitions
.
rend
();
i
++
)
{
CraftDefinition
*
def
=
*
i
;
/*infostream<<"Checking "<<input.dump()<<std::endl
<<" against "<<def->dump()<<std::endl;*/
try
{
tmpout
=
def
->
getOutput
(
input
,
gamedef
);
if
((
tmpout
.
item
.
substr
(
0
,
output
.
item
.
length
())
==
output
.
item
)
&&
((
tmpout
.
item
[
output
.
item
.
length
()]
==
0
)
||
(
tmpout
.
item
[
output
.
item
.
length
()]
==
' '
)))
{
// Get output, then decrement input (if requested)
input
=
def
->
getInput
(
output
,
gamedef
);
return
true
;
}
}
catch
(
SerializationError
&
e
)
{
errorstream
<<
"getCraftResult: ERROR: "
<<
"Serialization error in recipe "
<<
def
->
dump
()
<<
std
::
endl
;
// then go on with the next craft definition
}
}
return
false
;
}
virtual
std
::
vector
<
CraftDefinition
*>
getCraftRecipes
(
CraftOutput
&
output
,
IGameDef
*
gamedef
)
const
IGameDef
*
gamedef
,
unsigned
limit
=
0
)
const
{
std
::
vector
<
CraftDefinition
*>
recipes_list
;
CraftInput
input
;
CraftOutput
tmpout
;
tmpout
.
item
=
""
;
tmpout
.
time
=
0
;
std
::
vector
<
CraftDefinition
*>
recipes
;
for
(
std
::
vector
<
CraftDefinition
*>::
const_reverse_iterator
i
=
m_craft_definitions
.
rbegin
();
i
!=
m_craft_definitions
.
rend
();
i
++
)
{
CraftDefinition
*
def
=
*
i
;
std
::
map
<
std
::
string
,
std
::
vector
<
CraftDefinition
*>
>::
const_iterator
vec_iter
=
m_output_craft_definitions
.
find
(
output
.
item
);
/*infostream<<"Checking "<<input.dump()<<std::endl
<<" against "<<def->dump()<<std::endl;*/
if
(
vec_iter
==
m_output_craft_definitions
.
end
())
return
recipes
;
try
{
tmpout
=
def
->
getOutput
(
input
,
gamedef
);
if
(
tmpout
.
item
.
substr
(
0
,
output
.
item
.
length
())
==
output
.
item
)
{
// Get output, then decrement input (if requested)
input
=
def
->
getInput
(
output
,
gamedef
);
recipes_list
.
push_back
(
*
i
);
}
}
catch
(
SerializationError
&
e
)
{
errorstream
<<
"getCraftResult: ERROR: "
<<
"Serialization error in recipe "
<<
def
->
dump
()
<<
std
::
endl
;
// then go on with the next craft definition
}
const
std
::
vector
<
CraftDefinition
*>
&
vec
=
vec_iter
->
second
;
recipes
.
reserve
(
limit
?
MYMIN
(
limit
,
vec
.
size
())
:
vec
.
size
());
for
(
std
::
vector
<
CraftDefinition
*>::
const_reverse_iterator
it
=
vec
.
rbegin
();
it
!=
vec
.
rend
();
++
it
)
{
if
(
limit
&&
recipes
.
size
()
>=
limit
)
break
;
recipes
.
push_back
(
*
it
);
}
return
recipes_list
;
return
recipes
;
}
virtual
std
::
string
dump
()
const
{
...
...
@@ -1023,11 +969,16 @@ class CCraftDefManager: public IWritableCraftDefManager
}
return
os
.
str
();
}
virtual
void
registerCraft
(
CraftDefinition
*
def
)
virtual
void
registerCraft
(
CraftDefinition
*
def
,
IGameDef
*
gamedef
)
{
verbosestream
<<
"registerCraft: registering craft definition: "
<<
def
->
dump
()
<<
std
::
endl
;
m_craft_definitions
.
push_back
(
def
);
CraftInput
input
;
std
::
string
output_name
=
craftGetItemName
(
def
->
getOutput
(
input
,
gamedef
).
item
,
gamedef
);
m_output_craft_definitions
[
output_name
].
push_back
(
def
);
}
virtual
void
clear
()
{
...
...
@@ -1037,6 +988,7 @@ class CCraftDefManager: public IWritableCraftDefManager
delete
*
i
;
}
m_craft_definitions
.
clear
();
m_output_craft_definitions
.
clear
();
}
virtual
void
serialize
(
std
::
ostream
&
os
)
const
{
...
...
@@ -1053,7 +1005,7 @@ class CCraftDefManager: public IWritableCraftDefManager
os
<<
serializeString
(
tmp_os
.
str
());
}
}
virtual
void
deSerialize
(
std
::
istream
&
is
)
virtual
void
deSerialize
(
std
::
istream
&
is
,
IGameDef
*
gamedef
)
{
// Clear everything
clear
();
...
...
@@ -1067,11 +1019,12 @@ class CCraftDefManager: public IWritableCraftDefManager
std
::
istringstream
tmp_is
(
deSerializeString
(
is
),
std
::
ios
::
binary
);
CraftDefinition
*
def
=
CraftDefinition
::
deSerialize
(
tmp_is
);
// Register
registerCraft
(
def
);
registerCraft
(
def
,
gamedef
);
}
}
private
:
std
::
vector
<
CraftDefinition
*>
m_craft_definitions
;
std
::
map
<
std
::
string
,
std
::
vector
<
CraftDefinition
*>
>
m_output_craft_definitions
;
};
IWritableCraftDefManager
*
createCraftDefManager
()
...
...
This diff is collapsed.
Click to expand it.
src/craftdef.h
+
4
−
8
View file @
03e0dd33
...
...
@@ -356,10 +356,8 @@ class ICraftDefManager
// The main crafting function
virtual
bool
getCraftResult
(
CraftInput
&
input
,
CraftOutput
&
output
,
bool
decrementInput
,
IGameDef
*
gamedef
)
const
=
0
;
virtual
bool
getCraftRecipe
(
CraftInput
&
input
,
CraftOutput
&
output
,
IGameDef
*
gamedef
)
const
=
0
;
virtual
std
::
vector
<
CraftDefinition
*>
getCraftRecipes
(
CraftOutput
&
output
,
IGameDef
*
gamedef
)
const
=
0
;
IGameDef
*
gamedef
,
unsigned
limit
=
0
)
const
=
0
;
// Print crafting recipes for debugging
virtual
std
::
string
dump
()
const
=
0
;
...
...
@@ -376,22 +374,20 @@ class IWritableCraftDefManager : public ICraftDefManager
// The main crafting function
virtual
bool
getCraftResult
(
CraftInput
&
input
,
CraftOutput
&
output
,
bool
decrementInput
,
IGameDef
*
gamedef
)
const
=
0
;
virtual
bool
getCraftRecipe
(
CraftInput
&
input
,
CraftOutput
&
output
,
IGameDef
*
gamedef
)
const
=
0
;
virtual
std
::
vector
<
CraftDefinition
*>
getCraftRecipes
(
CraftOutput
&
output
,
IGameDef
*
gamedef
)
const
=
0
;
IGameDef
*
gamedef
,
unsigned
limit
=
0
)
const
=
0
;
// Print crafting recipes for debugging
virtual
std
::
string
dump
()
const
=
0
;
// Add a crafting definition.
// After calling this, the pointer belongs to the manager.
virtual
void
registerCraft
(
CraftDefinition
*
def
)
=
0
;
virtual
void
registerCraft
(
CraftDefinition
*
def
,
IGameDef
*
gamedef
)
=
0
;
// Delete all crafting definitions
virtual
void
clear
()
=
0
;
virtual
void
serialize
(
std
::
ostream
&
os
)
const
=
0
;
virtual
void
deSerialize
(
std
::
istream
&
is
)
=
0
;
virtual
void
deSerialize
(
std
::
istream
&
is
,
IGameDef
*
gamedef
)
=
0
;
};
IWritableCraftDefManager
*
createCraftDefManager
();
...
...
This diff is collapsed.
Click to expand it.
src/script/lua_api/l_craft.cpp
+
75
−
97
View file @
03e0dd33
...
...
@@ -173,7 +173,7 @@ int ModApiCraft::l_register_craft(lua_State *L)
CraftDefinition
*
def
=
new
CraftDefinitionShaped
(
output
,
width
,
recipe
,
replacements
);
craftdef
->
registerCraft
(
def
);
craftdef
->
registerCraft
(
def
,
getServer
(
L
)
);
}
/*
CraftDefinitionShapeless
...
...
@@ -205,7 +205,7 @@ int ModApiCraft::l_register_craft(lua_State *L)
CraftDefinition
*
def
=
new
CraftDefinitionShapeless
(
output
,
recipe
,
replacements
);
craftdef
->
registerCraft
(
def
);
craftdef
->
registerCraft
(
def
,
getServer
(
L
)
);
}
/*
CraftDefinitionToolRepair
...
...
@@ -216,7 +216,7 @@ int ModApiCraft::l_register_craft(lua_State *L)
CraftDefinition
*
def
=
new
CraftDefinitionToolRepair
(
additional_wear
);
craftdef
->
registerCraft
(
def
);
craftdef
->
registerCraft
(
def
,
getServer
(
L
)
);
}
/*
CraftDefinitionCooking
...
...
@@ -246,7 +246,7 @@ int ModApiCraft::l_register_craft(lua_State *L)
CraftDefinition
*
def
=
new
CraftDefinitionCooking
(
output
,
recipe
,
cooktime
,
replacements
);
craftdef
->
registerCraft
(
def
);
craftdef
->
registerCraft
(
def
,
getServer
(
L
)
);
}
/*
CraftDefinitionFuel
...
...
@@ -270,7 +270,7 @@ int ModApiCraft::l_register_craft(lua_State *L)
CraftDefinition
*
def
=
new
CraftDefinitionFuel
(
recipe
,
burntime
,
replacements
);
craftdef
->
registerCraft
(
def
);
craftdef
->
registerCraft
(
def
,
getServer
(
L
)
);
}
else
{
...
...
@@ -326,56 +326,80 @@ int ModApiCraft::l_get_craft_result(lua_State *L)
return
2
;
}
void
push_craft_recipe
(
lua_State
*
L
,
IGameDef
*
gdef
,
const
CraftDefinition
*
recipe
,
const
CraftOutput
&
tmpout
)
{
CraftInput
input
=
recipe
->
getInput
(
tmpout
,
gdef
);
CraftOutput
output
=
recipe
->
getOutput
(
input
,
gdef
);
lua_newtable
(
L
);
// items
std
::
vector
<
ItemStack
>::
const_iterator
iter
=
input
.
items
.
begin
();
for
(
u16
j
=
1
;
iter
!=
input
.
items
.
end
();
iter
++
,
j
++
)
{
if
(
iter
->
empty
())
continue
;
lua_pushstring
(
L
,
iter
->
name
.
c_str
());
lua_rawseti
(
L
,
-
2
,
j
);
}
lua_setfield
(
L
,
-
2
,
"items"
);
setintfield
(
L
,
-
1
,
"width"
,
input
.
width
);
switch
(
input
.
method
)
{
case
CRAFT_METHOD_NORMAL
:
lua_pushstring
(
L
,
"normal"
);
break
;
case
CRAFT_METHOD_COOKING
:
lua_pushstring
(
L
,
"cooking"
);
break
;
case
CRAFT_METHOD_FUEL
:
lua_pushstring
(
L
,
"fuel"
);
break
;
default:
lua_pushstring
(
L
,
"unknown"
);
}
lua_setfield
(
L
,
-
2
,
"type"
);
lua_pushstring
(
L
,
tmpout
.
item
.
c_str
());
lua_setfield
(
L
,
-
2
,
"output"
);
}
void
push_craft_recipes
(
lua_State
*
L
,
IGameDef
*
gdef
,
const
std
::
vector
<
CraftDefinition
*>
&
recipes
,
const
CraftOutput
&
output
)
{
lua_createtable
(
L
,
recipes
.
size
(),
0
);
if
(
recipes
.
empty
())
{
lua_pushnil
(
L
);
return
;
}
std
::
vector
<
CraftDefinition
*>::
const_iterator
it
=
recipes
.
begin
();
for
(
unsigned
i
=
0
;
it
!=
recipes
.
end
();
++
it
)
{
lua_newtable
(
L
);
push_craft_recipe
(
L
,
gdef
,
*
it
,
output
);
lua_rawseti
(
L
,
-
2
,
++
i
);
}
}
// get_craft_recipe(result item)
int
ModApiCraft
::
l_get_craft_recipe
(
lua_State
*
L
)
{
NO_MAP_LOCK_REQUIRED
;
int
k
=
1
;
int
input_i
=
1
;
std
::
string
o_item
=
luaL_checkstring
(
L
,
input_i
);
std
::
string
item
=
luaL_checkstring
(
L
,
1
);
Server
*
server
=
getServer
(
L
);
CraftOutput
output
(
item
,
0
);
std
::
vector
<
CraftDefinition
*>
recipes
=
server
->
cdef
()
->
getCraftRecipes
(
output
,
server
,
1
);
IGameDef
*
gdef
=
getServer
(
L
);
ICraftDefManager
*
cdef
=
gdef
->
cdef
();
CraftInput
input
;
CraftOutput
output
(
o_item
,
0
);
bool
got
=
cdef
->
getCraftRecipe
(
input
,
output
,
gdef
);
lua_newtable
(
L
);
// output table
if
(
got
){
lua_newtable
(
L
);
for
(
std
::
vector
<
ItemStack
>::
const_iterator
i
=
input
.
items
.
begin
();
i
!=
input
.
items
.
end
();
i
++
,
k
++
)
{
if
(
i
->
empty
())
{
continue
;
}
lua_pushinteger
(
L
,
k
);
lua_pushstring
(
L
,
i
->
name
.
c_str
());
lua_settable
(
L
,
-
3
);
}
lua_setfield
(
L
,
-
2
,
"items"
);
setintfield
(
L
,
-
1
,
"width"
,
input
.
width
);
switch
(
input
.
method
)
{
case
CRAFT_METHOD_NORMAL
:
lua_pushstring
(
L
,
"normal"
);
break
;
case
CRAFT_METHOD_COOKING
:
lua_pushstring
(
L
,
"cooking"
);
break
;
case
CRAFT_METHOD_FUEL
:
lua_pushstring
(
L
,
"fuel"
);
break
;
default:
lua_pushstring
(
L
,
"unknown"
);
}
lua_setfield
(
L
,
-
2
,
"type"
);
}
else
{
if
(
recipes
.
empty
())
{
lua_pushnil
(
L
);
lua_setfield
(
L
,
-
2
,
"items"
);
setintfield
(
L
,
-
1
,
"width"
,
0
);
return
1
;
}
push_craft_recipe
(
L
,
server
,
recipes
[
0
],
output
);
return
1
;
}
...
...
@@ -384,59 +408,13 @@ int ModApiCraft::l_get_all_craft_recipes(lua_State *L)
{
NO_MAP_LOCK_REQUIRED
;
std
::
string
o_item
=
luaL_checkstring
(
L
,
1
);
IGameDef
*
gdef
=
getServer
(
L
);
ICraftDefManager
*
cdef
=
gdef
->
cdef
();
CraftInput
input
;
CraftOutput
output
(
o_item
,
0
);
std
::
vector
<
CraftDefinition
*>
recipes_list
;
recipes_list
=
cdef
->
getCraftRecipes
(
output
,
gdef
);
if
(
recipes_list
.
empty
())
{
lua_pushnil
(
L
);
return
1
;
}
std
::
string
item
=
luaL_checkstring
(
L
,
1
);
Server
*
server
=
getServer
(
L
);
CraftOutput
output
(
item
,
0
);
std
::
vector
<
CraftDefinition
*>
recipes
=
server
->
cdef
()
->
getCraftRecipes
(
output
,
server
);
lua_createtable
(
L
,
recipes_list
.
size
(),
0
);
std
::
vector
<
CraftDefinition
*>::
const_iterator
iter
=
recipes_list
.
begin
();
for
(
u16
i
=
0
;
iter
!=
recipes_list
.
end
();
iter
++
)
{
CraftOutput
tmpout
;
tmpout
.
item
=
""
;
tmpout
.
time
=
0
;
tmpout
=
(
*
iter
)
->
getOutput
(
input
,
gdef
);
std
::
string
query
=
tmpout
.
item
;
char
*
fmtpos
,
*
fmt
=
&
query
[
0
];
if
(
strtok_r
(
fmt
,
" "
,
&
fmtpos
)
==
output
.
item
)
{
input
=
(
*
iter
)
->
getInput
(
output
,
gdef
);
lua_newtable
(
L
);
lua_newtable
(
L
);
// items
std
::
vector
<
ItemStack
>::
const_iterator
iter
=
input
.
items
.
begin
();
for
(
u16
j
=
1
;
iter
!=
input
.
items
.
end
();
iter
++
,
j
++
)
{
if
(
iter
->
empty
())
continue
;
lua_pushstring
(
L
,
iter
->
name
.
c_str
());
lua_rawseti
(
L
,
-
2
,
j
);
}
lua_setfield
(
L
,
-
2
,
"items"
);
setintfield
(
L
,
-
1
,
"width"
,
input
.
width
);
switch
(
input
.
method
)
{
case
CRAFT_METHOD_NORMAL
:
lua_pushstring
(
L
,
"normal"
);
break
;
case
CRAFT_METHOD_COOKING
:
lua_pushstring
(
L
,
"cooking"
);
break
;
case
CRAFT_METHOD_FUEL
:
lua_pushstring
(
L
,
"fuel"
);
break
;
default:
lua_pushstring
(
L
,
"unknown"
);
}
lua_setfield
(
L
,
-
2
,
"type"
);
lua_pushstring
(
L
,
&
tmpout
.
item
[
0
]);
lua_setfield
(
L
,
-
2
,
"output"
);
lua_rawseti
(
L
,
-
2
,
++
i
);
}
}
push_craft_recipes
(
L
,
server
,
recipes
,
output
);
return
1
;
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment