Skip to content

Commit

Permalink
[easyNPC] Added spawn consequence
Browse files Browse the repository at this point in the history
  • Loading branch information
mkaring committed Sep 23, 2014
1 parent 081b1cf commit 0178586
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
13 changes: 13 additions & 0 deletions illaeasynpc/src/main/antlr4/EasyNpc.g4
Expand Up @@ -80,6 +80,7 @@ consequence
| 'repair'
| 'rune' '(' magictypeWithRunes ',' INT ')'
| 'skill' '(' skill ')' set advancedNumber
| 'spawn' '(' monsterId ',' monsterCount ',' radius ',' location ')'
| 'state' set advancedNumber
| talkstateSet
| 'town' EQ town
Expand Down Expand Up @@ -334,6 +335,10 @@ questId
: INT
;

radius
: INT
;

magictype
: magictypeWithRunes | 'nomagic'
;
Expand All @@ -342,6 +347,14 @@ magictypeWithRunes
: 'bard' | 'druid' | 'mage' | 'priest'
;

monsterId
: INT
;

monsterCount
: INT
;

compare
: EQ | LT | GT | LET | GET | NEQ
;
Expand Down
@@ -0,0 +1,74 @@
/*
* This file is part of the Illarion project.
*
* Copyright © 2014 - Illarion e.V.
*
* Illarion is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Illarion is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
package illarion.easynpc.parsed.talk.consequences;

import illarion.common.types.Location;
import illarion.easynpc.parsed.talk.TalkConsequence;
import illarion.easynpc.writer.LuaRequireTable;
import illarion.easynpc.writer.LuaWriter;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.Writer;

/**
* This class is used to store all required values for the spawn consequence.
*
* @author Martin Karing <nitram@illarion.org>
*/
public final class ConsequenceSpawn implements TalkConsequence {
/**
* The LUA code needed to be included for a skill consequence.
*/
private static final String LUA_CODE =
"talkEntry:addConsequence(%1$s(%2$s, %3$s, %4$s, %5$s, %6$s, %7$s))" + LuaWriter.NL;

/**
* The LUA module needed for this consequence to work.
*/
private static final String LUA_MODULE = BASE_LUA_MODULE + "spawn";

private final int monsterId;
private final int count;
private final int radius;
@Nonnull
private final Location loc;

public ConsequenceSpawn(int monsterId, int count, int radius, @Nonnull Location loc) {
this.monsterId = monsterId;
this.count = count;
this.radius = radius;
this.loc = loc;
}

/**
* Get the module that is needed for this consequence to work.
*/
@Nonnull
@Override
public String getLuaModule() {
return LUA_MODULE;
}

/**
* Write the LUA code of this consequence.
*/
@Override
public void writeLua(@Nonnull Writer target, @Nonnull LuaRequireTable requires) throws IOException {
target.write(String.format(LUA_CODE, requires.getStorage(LUA_MODULE), monsterId, count, radius, loc.getScX(),
loc.getScY(), loc.getScZ()));
}
}
Expand Up @@ -543,6 +543,11 @@ public ParsedNpcVisitor visitConsequence(@NotNull ConsequenceContext ctx) {
currentTalkingLine.addConsequence(new ConsequenceSkill(getSkill(ctx.skill()), getOperator(ctx.set()),
getAdvancedNumber(ctx.advancedNumber())));
break;
case "spawn":
currentTalkingLine.addConsequence(
new ConsequenceSpawn(getMonsterId(ctx.monsterId()), getMonsterCount(ctx.monsterCount()),
getRadius(ctx.radius()), getLocation(ctx.location())));
break;
case "state":
currentTalkingLine.addConsequence(
new ConsequenceState(getOperator(ctx.set()), getAdvancedNumber(ctx.advancedNumber())));
Expand Down
27 changes: 27 additions & 0 deletions illaeasynpc/src/main/java/illarion/easynpc/parser/Utils.java
Expand Up @@ -338,6 +338,33 @@ static int getQuestId(@Nullable EasyNpcParser.QuestIdContext node) {
return getInteger(node.INT());
}

static int getMonsterId(@Nullable EasyNpcParser.MonsterIdContext node) {
if (node == null) {
LOGGER.warn("Expected node for item id not found.");
return 0;
}

return getInteger(node.INT());
}

static int getMonsterCount(@Nullable EasyNpcParser.MonsterCountContext node) {
if (node == null) {
LOGGER.warn("Expected node for item id not found.");
return 0;
}

return getInteger(node.INT());
}

static int getRadius(@Nullable EasyNpcParser.RadiusContext node) {
if (node == null) {
LOGGER.warn("Expected node for item id not found.");
return 0;
}

return getInteger(node.INT());
}

@Nullable
static Skill getSkill(@Nullable EasyNpcParser.SkillContext node) {
if (node == null) {
Expand Down

0 comments on commit 0178586

Please sign in to comment.