aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/questing/QuestLine.java
blob: 4e541583bd9b8cd40767c01e7fea1e5b2ec2e4ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package io.github.moulberry.notenoughupdates.questing;

import com.google.gson.JsonObject;
import io.github.moulberry.notenoughupdates.NEUResourceManager;
import io.github.moulberry.notenoughupdates.NotEnoughUpdates;
import io.github.moulberry.notenoughupdates.cosmetics.CapeNode;
import io.github.moulberry.notenoughupdates.questing.requirements.*;
import io.github.moulberry.notenoughupdates.util.Utils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.WorldRenderer;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.client.shader.Framebuffer;
import net.minecraft.client.shader.Shader;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Matrix4f;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL14;
import org.lwjgl.util.vector.Vector3f;

import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static io.github.moulberry.notenoughupdates.GuiTextures.item_mask;

public class QuestLine {

    private List<Quest> quests = new ArrayList<>();
    private Framebuffer framebuffer = null;
    private Framebuffer framebufferGrayscale = null;
    private Shader grayscaleShader = null;

    public class Quest {
        //requirements
        //rewards
        //display icon
        //x,y offset
        //prerequisite quests id:0,6

        private Requirement requirement;

        private int x;
        private int y;
        private ItemStack display;
        private String[] tooltip = new String[0];
        private int[] prerequisites;
        private boolean completed = false;

        public Quest(ItemStack display, Requirement requirement, int x, int y, int... prerequisites) {
            this.requirement = requirement;
            this.display = display;
            this.x = x;
            this.y = y;
            this.prerequisites = prerequisites;
        }

        public String[] render(int mouseX, int mouseY, boolean hasCompleted) {
            if(hasCompleted != completed) return null;

            Minecraft.getMinecraft().getTextureManager().bindTexture(item_mask);
            if(completed) {
                GlStateManager.color(200/255f, 150/255f, 50/255f, 255/255f);
            } else {
                GlStateManager.color(100/255f, 100/255f, 100/255f, 255/255f);
            }
            Utils.drawTexturedRect(x-9, y-9, 18, 18, GL11.GL_NEAREST);

            Utils.drawItemStack(display, x-8, y-8);

            GlStateManager.color(100/255f, 100/255f, 100/255f, 255/255f);
            GlStateManager.disableTexture2D();
            for(int prereq : prerequisites) {
                Quest other = quests.get(prereq);

                Tessellator tessellator = Tessellator.getInstance();
                WorldRenderer worldrenderer = tessellator.getWorldRenderer();
                worldrenderer.begin(GL11.GL_LINES, DefaultVertexFormats.POSITION);
                worldrenderer.pos(x, y, 0).endVertex();
                worldrenderer.pos(other.x, other.y, 0).endVertex();
                tessellator.draw();
            }

            if(mouseX > x-9 && mouseX < x+9) {
                if(mouseY > y-9 && mouseY < y+9) {
                    return tooltip;
                }
            }
            return null;
        }

        public void tick() {
            if(completed) return;
            for(int prereq : prerequisites) {
                Quest other = quests.get(prereq);
                if(!other.completed) return;
            }
            requirement.updateRequirement();
            if(requirement.getCompleted()) {
                completed = true;
            }
        }
    }

    private Framebuffer checkFramebufferSizes(Framebuffer framebuffer, int width, int height, int scaleFactor) {
        int sw = width*scaleFactor;
        int sh = height*scaleFactor;

        if(framebuffer == null || framebuffer.framebufferWidth != sw || framebuffer.framebufferHeight != sh) {
            if(framebuffer == null) {
                framebuffer = new Framebuffer(sw, sh, true);
            } else {
                framebuffer.createBindFramebuffer(sw, sh);
            }
            framebuffer.setFramebufferFilter(GL11.GL_NEAREST);
        }
        return framebuffer;
    }

    private Matrix4f createProjectionMatrix(int width, int height) {
        Matrix4f projMatrix  = new Matrix4f();
        projMatrix.setIdentity();
        projMatrix.m00 = 2.0F / (float)width;
        projMatrix.m11 = 2.0F / (float)(-height);
        projMatrix.m22 = -0.0020001999F;
        projMatrix.m33 = 1.0F;
        projMatrix.m03 = -1.0F;
        projMatrix.m13 = 1.0F;
        projMatrix.m23 = -1.0001999F;
        return projMatrix;
    }

    public void render(int width, int height, int mouseX, int mouseY) {
        ScaledResolution scaledresolution = new ScaledResolution(Minecraft.getMinecraft());

        GL11.glTranslatef(width/2f, height/2f, 0);
        String[] tooltipDisplay = null;
        for(Quest quest : quests) {
            String[] tooltip = quest.render(mouseX-width/2, mouseY-height/2, true);
            if(tooltip != null && tooltip.length > 0) tooltipDisplay = tooltip;
        }
        GL11.glTranslatef(-width/2f, -height/2f, 0);

        //FBO/Shader setup
        if(framebuffer != null && grayscaleShader != null && (framebuffer.framebufferWidth != width || framebuffer.framebufferHeight != height)) {
            grayscaleShader.setProjectionMatrix(createProjectionMatrix(
                    width*scaledresolution.getScaleFactor(), height*scaledresolution.getScaleFactor()));
        }
        framebuffer = checkFramebufferSizes(framebuffer, width, height,
                scaledresolution.getScaleFactor());
        framebufferGrayscale = checkFramebufferSizes(framebufferGrayscale, width, height,
                scaledresolution.getScaleFactor());
        if(grayscaleShader == null) {
            try {
                grayscaleShader = new Shader(new NEUResourceManager(Minecraft.getMinecraft().getResourceManager()),
                        "grayscale",
                        framebuffer, framebufferGrayscale);
                grayscaleShader.setProjectionMatrix(createProjectionMatrix(
                        width*scaledresolution.getScaleFactor(), height*scaledresolution.getScaleFactor()));
            } catch(Exception e) {
                return;
            }
        }

        //Render contents of framebuffer to screen
        framebufferGrayscale.bindFramebufferTexture();
        GlStateManager.color(1f, 1f, 1f, 1f);
        Utils.drawTexturedRect(0, 0, width, height, 0, 1, 1, 0);
        framebufferGrayscale.unbindFramebufferTexture();

        //Render uncompleted quests to framebuffer
        GL11.glPushMatrix();
        framebuffer.framebufferClear();
        framebuffer.bindFramebuffer(false);
        GlStateManager.color(1f, 1f, 1f, 1f);
        GL11.glTranslatef(width/2f, height/2f, 0);
        for(Quest quest : quests) {
            String[] tooltip = quest.render(mouseX-width/2, mouseY-height/2, false);
            if(tooltip != null && tooltip.length > 0) tooltipDisplay = tooltip;
        }
        GL11.glTranslatef(-width/2f, -height/2f, 0);
        framebuffer.unbindFramebuffer();
        GL11.glPopMatrix();

        //Execute shader
        GL11.glPushMatrix();
        grayscaleShader.loadShader(0);
        Minecraft.getMinecraft().getFramebuffer().bindFramebuffer(true);
        GL11.glPopMatrix();

        if(tooltipDisplay != null) {
            List<String> tooltip = new ArrayList<>();
            tooltip.addAll(Arrays.asList(tooltipDisplay));

            Utils.drawHoveringText(tooltip, mouseX, mouseY, width, height, -1, Minecraft.getMinecraft().fontRendererObj, false);
        }
    }

    public void tick() {
        for(Quest quest : quests) {
            quest.tick();
        }
    }

    {
        //Requirement hubRequirement = new RequirementIslandType("Village");
        //Requirement locationRequirement = new RequirementLocation(-29, 79, -108, 2);
        //Requirement locationRequirement2 = new RequirementLocation(-29, 79, -108, 2, hubRequirement);
        Requirement req1 = new RequirementApi("coin_purse>20000");
        Requirement req2 = new RequirementIslandType("The End");
        Requirement req3 = new RequirementGuiOpen("Auctions Browser");

        Quest quest1 = new Quest(new ItemStack(Items.rotten_flesh), req1, 0, 0);
        Quest quest2 = new Quest(new ItemStack(Items.cooked_porkchop), req2, 0, 30);
        Quest quest3 = new Quest(new ItemStack(Items.diamond_axe), req3, 30, 30, 0, 1);
        quest1.tooltip = new String[]{"Line1", "Line2", "Line3"};
        quests.add(quest1);
        quests.add(quest2);
        quests.add(quest3);
    }

}