/*:
 * @plugindesc 为RPG Maker MV新增任务系统。
 * @author 小优【66RPG:rpg-sheep】【百度贴吧:优加星爱兔子】
 *
 * @param Default Volume
 * @desc This will be the volume of the BGM played.
 * @default 90
 *
 * @help 在脚本中输入以下内容来操作任务系统:
 * ======================================================================
 * 新增任务:
 * $gameParty.addmission(id,name,description,childs,reward,autocomplete);
 * id(string):可以任意填写,是你识别任务的唯一参数,注意最好不要重复。
 * name(string):可以任意填写,是显示的任务名称。
 * description(string):可以任意填写,是显示的任务介绍。
 * childs(array):任务要点列表,以[child,child,...]的格式填写,在下一条目有介绍。
 * reward(array):完成奖励,以[['EXP',数量],['MONEY',数量],[物品ID,数量],...]的格式填写。
 * autocomplete(boolean):是否自动完成任务,自动完成的意思就是达成条件后无需NPC触发。
 * ======================================================================
 * 为某个任务新增要点:
 * $gameParty.addmissionchild(id,child);
 * id(string):任务ID。
 * child(array):要点,以[id,name,maxnumber,readnumber,autocomplete,completed]的格式填写。
 *     id(string):可以任意填写,是你识别要点的唯一参数,同一任务中注意最好不要重复。
 *     name(string):可以任意填写,是显示的要点名称。
 *     maxnumber(int):大于0!达到这个数本要点将会被判定为达成。
 *     readnumber(int/变量指针):
 *         如果填int:需要你手动改变,可以做类似“摘三朵花”之类的任务。
 *         如果填变量指针:会自动监测那个变量,可以做类似“生命达到1000”之类的任务。
 *     autocomplete(boolean):是否自动完成本条件,自动完成的意思就是达成要点后无需NPC触发。
 *     completed(boolean):初始状态是否完成,一般填false。
 * ======================================================================
 * 为某个任务的某个要点中的readnumber+1:
 * $gameParty.upratemissionchild(id,childid);
 * id(string):任务ID。
 * childid(string):要点ID。
 * ======================================================================
 * 指定某个任务的某个要点中的readnumber:
 * $gameParty.setratemissionchild(id,childid,num);、
 * id(string):任务ID。
 * childid(string):要点ID。
 * num(int):值。
 * ======================================================================
 * 强制完成任务要点:
 * $gameParty.completemissionchild(id,childid);
 * id(string):任务ID。
 * childid(string):要点ID。
 * ======================================================================
 * 手动完成任务要点:
 * $gameParty.donemissionchild(id,childid);
 * id(string):任务ID。
 * childid(string):要点ID。
 * 这条与上面那个不一样之处在于:
 *     1、这个方法会检测是否满足达成要点的条件,并返回一个布尔值。
 *     2、如果不满足条件,还是不会完成。
 * 所以这个方法我的建议是放在条件分歧的脚本一栏里。
 * ======================================================================
 * 强制完成任务:
 * $gameParty.completemission(id);
 * id(string):任务ID。
 * ======================================================================
 * 手动完成任务:
 * $gameParty.donemission(id);
 * id(string):任务ID。
 * 与上一条的差别同前。
 * ======================================================================
 */
 
function Window_XY_Mission() {this.initialize.apply(this, arguments);}
 
Window_XY_Mission.prototype = Object.create(Window_Base.prototype);
Window_XY_Mission.prototype.constructor = Window_XY_Mission;
 
Window_XY_Mission.prototype.initialize = function() {
    Window_Base.prototype.initialize.call(this, Graphics.boxWidth - this.windowWidth(), Graphics.boxHeight/2, this.windowWidth(), this.windowHeight());
    this.opacity = 0;
        this.line = 0;
        this.padd = 1;
    this.refresh();
};
Window_XY_Mission.prototype.standardFontSize = function() {return 18;};
Window_XY_Mission.prototype.standardPadding = function() {return 0;};
Window_XY_Mission.prototype.textPadding = function() {return 10;};
Window_XY_Mission.prototype.windowWidth = function() {return 300;};
Window_XY_Mission.prototype.windowHeight = function() {return 300};
Window_XY_Mission.prototype.update = function() {
        Window_Base.prototype.update.call(this);
        $gameParty.testallmission();
        this.refresh();
};
Window_XY_Mission.prototype.refresh = function() {
    this.contents.clear();
    var width = this.contentsWidth();
        var height = this.standardFontSize()*$gameParty.getdrawline() + 2*this.textPadding();
        this.line = 0;
        this.padd = 0;
    var missionlist = $gameParty.getallmission();
        if(missionlist.length > 0){this.drawBackground(0, 0, width, height);}
        for(var i = 0;i < missionlist.length;i++){
                var childinfos = [];
                for(var b = 0;b < missionlist[i].childs.length;b++){
                    childinfos.push([missionlist[i].childs[b][1],missionlist[i].getchildratebyindex(b),missionlist[i].childs[b][6]]);        
            }
                this.drawMission(width,missionlist[i].name,missionlist[i].description,childinfos);
                this.padd ++;
        }
 
};
Window_XY_Mission.prototype.drawBackground = function(x, y, width, height) {
        this.contents.context.fillStyle = 'rgba(0, 0, 0, 0.4)';
    this.contents.context.fillRect(0, 0, this.windowWidth(), this.windowHeight());
};
Window_XY_Mission.prototype.drawMission = function(width,name,description,childinfos) {
    this.drawText(name, this.textPadding(), this.standardFontSize()*this.line + this.textPadding()*this.padd, width ,'left');
        this.line ++;
        if(description != ''){
            this.drawText(description, this.textPadding()*3, this.standardFontSize()*this.line + this.textPadding()*this.padd, width, 'left');
        this.line ++;
        }
        for(var i = 0;i < childinfos.length;i++){
                this.changeTextColor(childinfos[i][2] ? 'rgba(160, 160, 160, 0.4)' : 'rgba(255, 255, 255, 1)');
                this.drawText('▪' + childinfos[i][0], this.textPadding()*5, this.standardFontSize()*this.line + this.textPadding()*this.padd, width, 'left');
        this.drawText(childinfos[i][1], 0, this.standardFontSize()*this.line + this.textPadding()*this.padd, width, 'right');
        this.line ++;
        }
};/*
Window_XY_Mission.prototype.drawMissionChild = function(name,rate,completed) {
        this.changeTextColor(this.systemColor());
    var color1 = this.dimColor1();
    var color2 = this.dimColor2();
    this.contents.FillRect(0, 0, width, height, 'rgba(0, 0, 0, 0.3)');
    this.contents.gradientFillRect(x + width / 2, y, width / 2, height, color1, color2);
};
Window_XY_Mission.prototype.changeColorbycompleted = function(completed) {
        complete ? this.changeTextColor('rgba(0, 0, 0, 0.6)') : this.changeTextColor('rgba(0, 0, 0, 0.6)');
};*/
/*
Window_XY_Mission.prototype.isOpenAndActive = function() {
    return this.isOpen() && this.active;
};
 
Window_XY_Mission.prototype.processTouch = function() {
    if (this.isOpenAndActive()) {
        if (TouchInput.isTriggered() && this.isTouchedInsideFrame()) {
            this._touching = true;
            this.onTouch(true);
        } else if (TouchInput.isCancelled()) {
            if (this.isCancelEnabled()) {
                this.processCancel();
            }
        }
        if (this._touching) {
            if (TouchInput.isPressed()) {
                this.onTouch(false);
            } else {
                this._touching = false;
            }
        }
    } else {
        this._touching = false;
    }
};
 
Window_XY_Mission.prototype.isTouchedInsideFrame = function() {
    var x = this.canvasToLocalX(TouchInput.x);
    var y = this.canvasToLocalY(TouchInput.y);
    return ((x >= 0)&&(y >= 0)&&(x <= this.width)&&(y <= this.width));
};
Window_XY_Mission.prototype.onTouch = function(triggered) {
    var x = this.canvasToLocalX(TouchInput.x);
    var y = this.canvasToLocalY(TouchInput.y);
    if (this.isTouchedInsideFrame() {
        SoundManager.playCursor();
    }
        //SoundManager.playCursor();
};
*/
Scene_Map.prototype.old_createDisplayObjects = Scene_Map.prototype.createDisplayObjects;
Scene_Map.prototype.createDisplayObjects = function() {
        this.old_createDisplayObjects();
        this.XY_createMissionWindow();
};
Scene_Map.prototype.XY_createMissionWindow = function() {
    this._XY_MissionWindow = new Window_XY_Mission();
    this.addWindow(this._XY_MissionWindow);
};
alert(1);
/*XY_Mission*/
function XY_Mission(id,name,description,childs,reward,autocomplete){
        this.id = id;
    this.name = name;
    this.description = description;
        //Child[id,name,maxnumber,readnumber,autocomplete,completed]
    this.childs = childs;
    this.reward = reward;
    this.autocomplete = autocomplete;
        this.completed = false;
        //添加任务要点
    this.addchild = function(child){
        this.childs.push(child);
    }
        //根据id寻找任务要点
        this.findchildindexbyid = function(id){
        for(var i = 0;i < this.childs.length;i++){
            if(this.childs[i][0] === id){
                return i;
            }
        }
    }
        //获取任务要点完成度(仅绘图用)
        this.getchildratebyid = function(id){
        return this.getchildratebyindex(this.findchildindexbyid(id));
    }
        this.getchildratebyindex = function(index){
                return this.childs[index][3] + "/" + this.childs[index][2];
    }
        //增加一点任务要点完成度
        this.upchildratebyid = function(id){
        this.upchildratebyindex(this.findchildindexbyid(id));
    }
        this.upchildratebyindex = function(index){
                this.childs[index][3]++;
    }
        //设置任务要点完成度
        this.setchildratebyid = function(id,num){
        this.setchildratebyindex(this.findchildindexbyid(id),num);
    }
        this.setchildratebyindex = function(index,num){
                this.childs[index][3] = num;
    }
        //设置任务要点完成度
        this.removechildbyid = function(id){
        this.removechildbyindex(this.findchildindexbyid(id));
    }
        this.removechildbyindex = function(index){
                this.childs.splice(index,1);
    }
        //强制完成任务要点
        this.completechildbyid = function(id){
        this.completechildbyindex(this.findchildindexbyid(id));
    }
        this.completechildbyindex = function(index){
                this.childs[index][5] = true;
    }
        //获取任务要点是否达成
        this.ischildcompletebyid = function(id){
        return this.ischildcompletebyindex(this.findchildindexbyid(id));
    }
        this.ischildcompletebyindex = function(index){
                return (this.childs[index][3] >= this.childs[index][2]);
    }
        //获取任务要点是否完成
        this.ischildcompletedbyid = function(id){
        return this.ischildcompletedbyindex(this.findchildindexbyid(id));
    }
        this.ischildcompletedbyindex = function(index){
                return this.childs[index][5];
    }
        //手动完成任务要点
        this.donechildbyid = function(id){
        return this.donechildbyindex(this.findchildindexbyid(id));
    }
        this.donechildbyindex = function(index){
                if(this.ischildcompletebyindex(index)){this.completechildbyindex(index);}
                return this.ischildcompletedbyindex(index);
    }
        //自动完成任务要点
        this.testchildbyid = function(id){
        this.testchildbyindex(this.findchildindexbyid(id));
    }
        this.testchildbyindex = function(index){
                if(this.ischildcompletebyindex(index)&&this.childs[index][4]){this.completechildbyindex(index);}
    }
        //自动完成全部任务要点
        this.testallchild = function(){
                for(var i = 0;i < this.childs.length;i++){
                        this.testchildbyindex(i);
        }
    }
        //强制完成任务
        this.complete = function(){
                this.completed = true;
    }
        //获取任务是否达成
        this.iscomplete = function(){
        var isco = true;
        for(var i = 0;i < this.childs.length;i++){
            if(!this.ischildcompletedbyindex(i)){
                isco = false;
            }
        }
        return isco;
    }
        //获取任务是否完成
        this.iscompleted = function(){
                return this.completed;
    }
        //手动完成任务
        this.done = function(){
                if(this.iscomplete()){this.complete();}
                return this.iscompleted();
    }
        //自动完成任务
        this.test = function(){
                if(this.iscomplete()&&this.autocomplete){this.complete();}
    }
        //自动完成任务并且自动完成任务要点
        this.testall = function(){
                this.testallchild();
                this.test();
    }
}
/*Game_Party*/
Game_Party.prototype.XY_Mission_old_initialize = Game_Party.prototype.initialize;
Game_Party.prototype.initialize = function() {
    Game_Party.prototype.XY_Mission_old_initialize();
    this._missionlist = [];
};
//添加任务
Game_Party.prototype.addmission = function(id,name,description,childs,reward,autocomplete) {
    this._missionlist.push(new XY_Mission(id,name,description,childs,reward,autocomplete));
};
//添加要点
Game_Party.prototype.addmissionchild = function(id,child) {
    this._missionlist[this.findmissionindexbyid(id)].addchild(child);
};
//根据id寻找任务
Game_Party.prototype.findmissionindexbyid = function(id){
    for(var i = 0;i < this._missionlist.length;i++){
        if(this._missionlist[i].id === id){
            return i;
        }
    }
}
//刷新所有
Game_Party.prototype.testallmission = function(id){
    for(var i = 0;i < this._missionlist.length;i++){
        this._missionlist[i].testall();
    }
}
//增加一点任务要点完成度
Game_Party.prototype.upratemissionchild = function(id,childid){
    this._missionlist[this.findmissionindexbyid(id)].upchildratebyid(childid);
}
//设置任务要点完成度
Game_Party.prototype.setratemissionchild = function(id,childid,num){
    this._missionlist[this.findmissionindexbyid(id)].setchildratebyid(childid,num);
}
//强制完成任务要点
Game_Party.prototype.completemissionchild = function(id,childid){
    this._missionlist[this.findmissionindexbyid(id)].completechildbyid(childid);
}
//手动完成任务要点
Game_Party.prototype.donemissionchild = function(id,childid){
    return this._missionlist[this.findmissionindexbyid(id)].donechildbyid(childid);
}
//强制完成任务
Game_Party.prototype.completemission = function(id){
    this._missionlist[this.findmissionindexbyid(id)].complete();
}
//手动完成任务
Game_Party.prototype.donemission = function(id){
    return this._missionlist[this.findmissionindexbyid(id)].done();
}
//获取所有未完成任务
Game_Party.prototype.getallmission = function(){
        var allmission = [];
        for(var i = 0;i < this._missionlist.length;i++){
                if(!this._missionlist[i].iscompleted()){
                        allmission.push(this._missionlist[i]);
                }
        }
    return allmission;
}
//获取绘图行数
Game_Party.prototype.getdrawline = function(){
        var templine = 0;
        var tempmission = this.getallmission();
        templine += tempmission.length;
        for(var i = 0;i < tempmission.length;i++){
                if(tempmission[i].description != ''){
                        templine++;
                }
                templine += tempmission[i].childs.length;
        }
        return templine;
}