]> git.lizzy.rs Git - dragonblocks.git/blob - engine/recipe.js
Reset file permissions to defaults
[dragonblocks.git] / engine / recipe.js
1 /*
2  * recipe.js
3  * 
4  * Copyright 2020 Elias Fleckenstein <eliasfleckenstein@web.de>
5  * 
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * 
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  * 
21  * 
22  */
23 dragonblocks.Recipe = class{
24         constructor(obj){
25                 if(! obj || ! obj.result || ! obj.recipe instanceof Array || ! obj.recipe[0] instanceof Array)
26                         return;
27                 this.recipe = obj.recipe;
28                 this.result = obj.result;
29                 this.height = this.recipe.length;
30                 this.width = this.recipe[0].length;
31                 dragonblocks.recipes.push(this);
32         }
33         match(craftfield){
34                 if(craftfield.width < this.width || craftfield.height < this.height)
35                         return false;
36                 for(let ydiff = 0; ydiff <= craftfield.height - this.height; ydiff++){
37                         for(let xdiff = 0; xdiff <= craftfield.width - this.width; xdiff++){
38                                 let found = true;
39                                 for(let y = 0; y < craftfield.height; y++){
40                                         for(let x = 0; x < craftfield.width; x++){
41                                                 if(! this.recipe[y - ydiff] || ! this.recipe[y - ydiff][x - xdiff]){
42                                                         if(craftfield.list[y * craftfield.width + x].item)
43                                                                 found = false;
44                                                 }
45                                                 else if(! dragonblocks.itemMatch(craftfield.list[y * craftfield.width + x].item, this.recipe[y - ydiff][x - xdiff]))
46                                                         found = false;
47                                         }
48                                 }
49                                 if(found)
50                                         return true;
51                         }
52                 }
53                 return false;
54         }
55 }
56 dragonblocks.recipes = [];
57 dragonblocks.registerRecipe = function(obj){
58         new dragonblocks.Recipe(obj);
59 }