Merge branch 'master' of https://github.com/erlehmann/minetest-delta.git into upstrea...
[oweals/minetest.git] / src / content_craft.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "content_craft.h"
21 #include "inventory.h"
22 #include "content_mapnode.h"
23 #include "player.h"
24
25 /*
26         items: actually *items[9]
27         return value: allocates a new item, or returns NULL.
28 */
29 InventoryItem *craft_get_result(InventoryItem **items)
30 {
31         // Wood
32         {
33                 ItemSpec specs[9];
34                 specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_TREE);
35                 if(checkItemCombination(items, specs))
36                 {
37                         return new MaterialItem(CONTENT_WOOD, 4);
38                 }
39         }
40
41         // Stick
42         {
43                 ItemSpec specs[9];
44                 specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
45                 if(checkItemCombination(items, specs))
46                 {
47                         return new CraftItem("Stick", 4);
48                 }
49         }
50
51         // Fence
52         {
53                 ItemSpec specs[9];
54                 specs[3] = ItemSpec(ITEM_CRAFT, "Stick");
55                 specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
56                 specs[5] = ItemSpec(ITEM_CRAFT, "Stick");
57                 specs[6] = ItemSpec(ITEM_CRAFT, "Stick");
58                 specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
59                 specs[8] = ItemSpec(ITEM_CRAFT, "Stick");
60                 if(checkItemCombination(items, specs))
61                 {
62                         return new MaterialItem(CONTENT_FENCE, 2);
63                 }
64         }
65
66         // Sign
67         {
68                 ItemSpec specs[9];
69                 specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
70                 specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
71                 specs[2] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
72                 specs[3] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
73                 specs[4] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
74                 specs[5] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
75                 specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
76                 if(checkItemCombination(items, specs))
77                 {
78                         //return new MapBlockObjectItem("Sign");
79                         return new MaterialItem(CONTENT_SIGN_WALL, 1);
80                 }
81         }
82
83         // Torch
84         {
85                 ItemSpec specs[9];
86                 specs[0] = ItemSpec(ITEM_CRAFT, "lump_of_coal");
87                 specs[3] = ItemSpec(ITEM_CRAFT, "Stick");
88                 if(checkItemCombination(items, specs))
89                 {
90                         return new MaterialItem(CONTENT_TORCH, 4);
91                 }
92         }
93
94         // Wooden pick
95         {
96                 ItemSpec specs[9];
97                 specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
98                 specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
99                 specs[2] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
100                 specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
101                 specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
102                 if(checkItemCombination(items, specs))
103                 {
104                         return new ToolItem("WPick", 0);
105                 }
106         }
107
108         // Stone pick
109         {
110                 ItemSpec specs[9];
111                 specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
112                 specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
113                 specs[2] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
114                 specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
115                 specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
116                 if(checkItemCombination(items, specs))
117                 {
118                         return new ToolItem("STPick", 0);
119                 }
120         }
121
122         // Steel pick
123         {
124                 ItemSpec specs[9];
125                 specs[0] = ItemSpec(ITEM_CRAFT, "steel_ingot");
126                 specs[1] = ItemSpec(ITEM_CRAFT, "steel_ingot");
127                 specs[2] = ItemSpec(ITEM_CRAFT, "steel_ingot");
128                 specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
129                 specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
130                 if(checkItemCombination(items, specs))
131                 {
132                         return new ToolItem("SteelPick", 0);
133                 }
134         }
135
136         // Mese pick
137         {
138                 ItemSpec specs[9];
139                 specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_MESE);
140                 specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_MESE);
141                 specs[2] = ItemSpec(ITEM_MATERIAL, CONTENT_MESE);
142                 specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
143                 specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
144                 if(checkItemCombination(items, specs))
145                 {
146                         return new ToolItem("MesePick", 0);
147                 }
148         }
149
150         // Wooden shovel
151         {
152                 ItemSpec specs[9];
153                 specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
154                 specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
155                 specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
156                 if(checkItemCombination(items, specs))
157                 {
158                         return new ToolItem("WShovel", 0);
159                 }
160         }
161
162         // Stone shovel
163         {
164                 ItemSpec specs[9];
165                 specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
166                 specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
167                 specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
168                 if(checkItemCombination(items, specs))
169                 {
170                         return new ToolItem("STShovel", 0);
171                 }
172         }
173
174         // Steel shovel
175         {
176                 ItemSpec specs[9];
177                 specs[1] = ItemSpec(ITEM_CRAFT, "steel_ingot");
178                 specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
179                 specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
180                 if(checkItemCombination(items, specs))
181                 {
182                         return new ToolItem("SteelShovel", 0);
183                 }
184         }
185
186         // Wooden axe
187         {
188                 ItemSpec specs[9];
189                 specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
190                 specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
191                 specs[3] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
192                 specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
193                 specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
194                 if(checkItemCombination(items, specs))
195                 {
196                         return new ToolItem("WAxe", 0);
197                 }
198         }
199
200         // Stone axe
201         {
202                 ItemSpec specs[9];
203                 specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
204                 specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
205                 specs[3] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
206                 specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
207                 specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
208                 if(checkItemCombination(items, specs))
209                 {
210                         return new ToolItem("STAxe", 0);
211                 }
212         }
213
214         // Steel axe
215         {
216                 ItemSpec specs[9];
217                 specs[0] = ItemSpec(ITEM_CRAFT, "steel_ingot");
218                 specs[1] = ItemSpec(ITEM_CRAFT, "steel_ingot");
219                 specs[3] = ItemSpec(ITEM_CRAFT, "steel_ingot");
220                 specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
221                 specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
222                 if(checkItemCombination(items, specs))
223                 {
224                         return new ToolItem("SteelAxe", 0);
225                 }
226         }
227
228         // Wooden sword
229         {
230                 ItemSpec specs[9];
231                 specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
232                 specs[4] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
233                 specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
234                 if(checkItemCombination(items, specs))
235                 {
236                         return new ToolItem("WSword", 0);
237                 }
238         }
239
240         // Stone sword
241         {
242                 ItemSpec specs[9];
243                 specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
244                 specs[4] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
245                 specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
246                 if(checkItemCombination(items, specs))
247                 {
248                         return new ToolItem("STSword", 0);
249                 }
250         }
251
252         // Steel sword
253         {
254                 ItemSpec specs[9];
255                 specs[1] = ItemSpec(ITEM_CRAFT, "steel_ingot");
256                 specs[4] = ItemSpec(ITEM_CRAFT, "steel_ingot");
257                 specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
258                 if(checkItemCombination(items, specs))
259                 {
260                         return new ToolItem("SteelSword", 0);
261                 }
262         }
263
264         // Rail
265         {
266                 ItemSpec specs[9];
267                 specs[0] = ItemSpec(ITEM_CRAFT, "steel_ingot");
268                 specs[1] = ItemSpec(ITEM_CRAFT, "Stick");
269                 specs[2] = ItemSpec(ITEM_CRAFT, "steel_ingot");
270                 specs[3] = ItemSpec(ITEM_CRAFT, "steel_ingot");
271                 specs[4] = ItemSpec(ITEM_CRAFT, "Stick");
272                 specs[5] = ItemSpec(ITEM_CRAFT, "steel_ingot");
273                 specs[6] = ItemSpec(ITEM_CRAFT, "steel_ingot");
274                 specs[7] = ItemSpec(ITEM_CRAFT, "Stick");
275                 specs[8] = ItemSpec(ITEM_CRAFT, "steel_ingot");
276                 if(checkItemCombination(items, specs))
277                 {
278                         return new MaterialItem(CONTENT_RAIL, 15);
279                 }
280         }
281
282         // Chest
283         {
284                 ItemSpec specs[9];
285                 specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
286                 specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
287                 specs[2] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
288                 specs[3] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
289                 specs[5] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
290                 specs[6] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
291                 specs[7] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
292                 specs[8] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
293                 if(checkItemCombination(items, specs))
294                 {
295                         return new MaterialItem(CONTENT_CHEST, 1);
296                 }
297         }
298
299         // Furnace
300         {
301                 ItemSpec specs[9];
302                 specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
303                 specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
304                 specs[2] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
305                 specs[3] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
306                 specs[5] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
307                 specs[6] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
308                 specs[7] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
309                 specs[8] = ItemSpec(ITEM_MATERIAL, CONTENT_COBBLE);
310                 if(checkItemCombination(items, specs))
311                 {
312                         return new MaterialItem(CONTENT_FURNACE, 1);
313                 }
314         }
315
316         // Steel block
317         {
318                 ItemSpec specs[9];
319                 specs[0] = ItemSpec(ITEM_CRAFT, "steel_ingot");
320                 specs[1] = ItemSpec(ITEM_CRAFT, "steel_ingot");
321                 specs[2] = ItemSpec(ITEM_CRAFT, "steel_ingot");
322                 specs[3] = ItemSpec(ITEM_CRAFT, "steel_ingot");
323                 specs[4] = ItemSpec(ITEM_CRAFT, "steel_ingot");
324                 specs[5] = ItemSpec(ITEM_CRAFT, "steel_ingot");
325                 specs[6] = ItemSpec(ITEM_CRAFT, "steel_ingot");
326                 specs[7] = ItemSpec(ITEM_CRAFT, "steel_ingot");
327                 specs[8] = ItemSpec(ITEM_CRAFT, "steel_ingot");
328                 if(checkItemCombination(items, specs))
329                 {
330                         return new MaterialItem(CONTENT_STEEL, 1);
331                 }
332         }
333
334         // Sandstone
335         {
336                 ItemSpec specs[9];
337                 specs[3] = ItemSpec(ITEM_MATERIAL, CONTENT_SAND);
338                 specs[4] = ItemSpec(ITEM_MATERIAL, CONTENT_SAND);
339                 specs[6] = ItemSpec(ITEM_MATERIAL, CONTENT_SAND);
340                 specs[7] = ItemSpec(ITEM_MATERIAL, CONTENT_SAND);
341                 if(checkItemCombination(items, specs))
342                 {
343                         return new MaterialItem(CONTENT_SANDSTONE, 1);
344                 }
345         }
346
347         // Clay
348         {
349                 ItemSpec specs[9];
350                 specs[3] = ItemSpec(ITEM_CRAFT, "lump_of_clay");
351                 specs[4] = ItemSpec(ITEM_CRAFT, "lump_of_clay");
352                 specs[6] = ItemSpec(ITEM_CRAFT, "lump_of_clay");
353                 specs[7] = ItemSpec(ITEM_CRAFT, "lump_of_clay");
354                 if(checkItemCombination(items, specs))
355                 {
356                         return new MaterialItem(CONTENT_CLAY, 1);
357                 }
358         }
359
360         // Brick
361         {
362                 ItemSpec specs[9];
363                 specs[3] = ItemSpec(ITEM_CRAFT, "clay_brick");
364                 specs[4] = ItemSpec(ITEM_CRAFT, "clay_brick");
365                 specs[6] = ItemSpec(ITEM_CRAFT, "clay_brick");
366                 specs[7] = ItemSpec(ITEM_CRAFT, "clay_brick");
367                 if(checkItemCombination(items, specs))
368                 {
369                         return new MaterialItem(CONTENT_BRICK, 1);
370                 }
371         }
372
373         // Paper
374         {
375                 ItemSpec specs[9];
376                 specs[3] = ItemSpec(ITEM_MATERIAL, CONTENT_PAPYRUS);
377                 specs[4] = ItemSpec(ITEM_MATERIAL, CONTENT_PAPYRUS);
378                 specs[5] = ItemSpec(ITEM_MATERIAL, CONTENT_PAPYRUS);
379                 if(checkItemCombination(items, specs))
380                 {
381                         return new CraftItem("paper", 1);
382                 }
383         }
384
385         // Book
386         {
387                 ItemSpec specs[9];
388                 specs[1] = ItemSpec(ITEM_CRAFT, "paper");
389                 specs[4] = ItemSpec(ITEM_CRAFT, "paper");
390                 specs[7] = ItemSpec(ITEM_CRAFT, "paper");
391                 if(checkItemCombination(items, specs))
392                 {
393                         return new CraftItem("book", 1);
394                 }
395         }
396
397         // Book shelf
398         {
399                 ItemSpec specs[9];
400                 specs[0] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
401                 specs[1] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
402                 specs[2] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
403                 specs[3] = ItemSpec(ITEM_CRAFT, "book");
404                 specs[4] = ItemSpec(ITEM_CRAFT, "book");
405                 specs[5] = ItemSpec(ITEM_CRAFT, "book");
406                 specs[6] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
407                 specs[7] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
408                 specs[8] = ItemSpec(ITEM_MATERIAL, CONTENT_WOOD);
409                 if(checkItemCombination(items, specs))
410                 {
411                         return new MaterialItem(CONTENT_BOOKSHELF, 1);
412                 }
413         }
414
415         return NULL;
416 }
417
418 void craft_set_creative_inventory(Player *player)
419 {
420         player->resetInventory();
421         
422         // Give some good tools
423         {
424                 InventoryItem *item = new ToolItem("MesePick", 0);
425                 void* r = player->inventory.addItem("main", item);
426                 assert(r == NULL);
427         }
428         {
429                 InventoryItem *item = new ToolItem("SteelPick", 0);
430                 void* r = player->inventory.addItem("main", item);
431                 assert(r == NULL);
432         }
433         {
434                 InventoryItem *item = new ToolItem("SteelAxe", 0);
435                 void* r = player->inventory.addItem("main", item);
436                 assert(r == NULL);
437         }
438         {
439                 InventoryItem *item = new ToolItem("SteelShovel", 0);
440                 void* r = player->inventory.addItem("main", item);
441                 assert(r == NULL);
442         }
443
444         /*
445                 Give materials
446         */
447         
448         // CONTENT_IGNORE-terminated list
449         u8 material_items[] = {
450                 CONTENT_TORCH,
451                 CONTENT_COBBLE,
452                 CONTENT_MUD,
453                 CONTENT_STONE,
454                 CONTENT_SAND,
455                 CONTENT_SANDSTONE,
456                 CONTENT_CLAY,
457                 CONTENT_BRICK,
458                 CONTENT_TREE,
459                 CONTENT_LEAVES,
460                 CONTENT_CACTUS,
461                 CONTENT_PAPYRUS,
462                 CONTENT_BOOKSHELF,
463                 CONTENT_GLASS,
464                 CONTENT_FENCE,
465                 CONTENT_RAIL,
466                 CONTENT_MESE,
467                 CONTENT_WATERSOURCE,
468                 CONTENT_CLOUD,
469                 CONTENT_CHEST,
470                 CONTENT_FURNACE,
471                 CONTENT_SIGN_WALL,
472                 CONTENT_IGNORE
473         };
474         
475         u8 *mip = material_items;
476         for(u16 i=0; i<PLAYER_INVENTORY_SIZE; i++)
477         {
478                 if(*mip == CONTENT_IGNORE)
479                         break;
480
481                 InventoryItem *item = new MaterialItem(*mip, 1);
482                 player->inventory.addItem("main", item);
483
484                 mip++;
485         }
486
487 #if 0
488         assert(USEFUL_CONTENT_COUNT <= PLAYER_INVENTORY_SIZE);
489         
490         // add torch first
491         InventoryItem *item = new MaterialItem(CONTENT_TORCH, 1);
492         player->inventory.addItem("main", item);
493         
494         // Then others
495         for(u16 i=0; i<USEFUL_CONTENT_COUNT; i++)
496         {
497                 // Skip some materials
498                 if(i == CONTENT_WATER || i == CONTENT_TORCH
499                         || i == CONTENT_COALSTONE)
500                         continue;
501
502                 InventoryItem *item = new MaterialItem(i, 1);
503                 player->inventory.addItem("main", item);
504         }
505 #endif
506
507         /*// Sign
508         {
509                 InventoryItem *item = new MapBlockObjectItem("Sign Example text");
510                 void* r = player->inventory.addItem("main", item);
511                 assert(r == NULL);
512         }*/
513 }
514
515 void craft_give_initial_stuff(Player *player)
516 {
517         {
518                 InventoryItem *item = new ToolItem("SteelPick", 0);
519                 void* r = player->inventory.addItem("main", item);
520                 assert(r == NULL);
521         }
522         {
523                 InventoryItem *item = new MaterialItem(CONTENT_TORCH, 99);
524                 void* r = player->inventory.addItem("main", item);
525                 assert(r == NULL);
526         }
527         {
528                 InventoryItem *item = new ToolItem("SteelAxe", 0);
529                 void* r = player->inventory.addItem("main", item);
530                 assert(r == NULL);
531         }
532         {
533                 InventoryItem *item = new ToolItem("SteelShovel", 0);
534                 void* r = player->inventory.addItem("main", item);
535                 assert(r == NULL);
536         }
537         {
538                 InventoryItem *item = new MaterialItem(CONTENT_COBBLE, 99);
539                 void* r = player->inventory.addItem("main", item);
540                 assert(r == NULL);
541         }
542         /*{
543                 InventoryItem *item = new MaterialItem(CONTENT_MESE, 6);
544                 void* r = player->inventory.addItem("main", item);
545                 assert(r == NULL);
546         }
547         {
548                 InventoryItem *item = new MaterialItem(CONTENT_COALSTONE, 6);
549                 void* r = player->inventory.addItem("main", item);
550                 assert(r == NULL);
551         }
552         {
553                 InventoryItem *item = new MaterialItem(CONTENT_WOOD, 6);
554                 void* r = player->inventory.addItem("main", item);
555                 assert(r == NULL);
556         }
557         {
558                 InventoryItem *item = new CraftItem("Stick", 4);
559                 void* r = player->inventory.addItem("main", item);
560                 assert(r == NULL);
561         }
562         {
563                 InventoryItem *item = new ToolItem("WPick", 32000);
564                 void* r = player->inventory.addItem("main", item);
565                 assert(r == NULL);
566         }
567         {
568                 InventoryItem *item = new ToolItem("STPick", 32000);
569                 void* r = player->inventory.addItem("main", item);
570                 assert(r == NULL);
571         }*/
572         /*// and some signs
573         for(u16 i=0; i<4; i++)
574         {
575                 InventoryItem *item = new MapBlockObjectItem("Sign Example text");
576                 bool r = player->inventory.addItem("main", item);
577                 assert(r == true);
578         }*/
579         /*// Give some other stuff
580         {
581                 InventoryItem *item = new MaterialItem(CONTENT_TREE, 999);
582                 bool r = player->inventory.addItem("main", item);
583                 assert(r == true);
584         }*/
585 }
586