Skip to content

Commit

Permalink
Merge pull request #29 from inwc3/jassSynth
Browse files Browse the repository at this point in the history
Jass synth
  • Loading branch information
WaterKnight authored Feb 24, 2019
2 parents b47b5c5 + 9ccae0e commit 76b24e8
Show file tree
Hide file tree
Showing 68 changed files with 20,015 additions and 225 deletions.
703 changes: 516 additions & 187 deletions src/main/antlr/Jass.g4

Large diffs are not rendered by default.

419 changes: 419 additions & 0 deletions src/main/antlr/LightJass.g4

Large diffs are not rendered by default.

28 changes: 26 additions & 2 deletions src/main/java/net/moonlightflower/wc3libs/app/ObjMerger.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import net.moonlightflower.wc3libs.slk.app.doodads.DoodSLK;
import net.moonlightflower.wc3libs.slk.app.meta.*;
import net.moonlightflower.wc3libs.slk.app.objs.*;
import net.moonlightflower.wc3libs.txt.Jass;
import net.moonlightflower.wc3libs.txt.app.jass.Jass;
import net.moonlightflower.wc3libs.txt.Profile;
import net.moonlightflower.wc3libs.txt.TXTSectionId;
import net.moonlightflower.wc3libs.txt.WTS;
Expand Down Expand Up @@ -435,7 +435,7 @@ public void filter(@Nonnull Filter filter) throws IOException {

log.info("examine tokens");
for (Token token : j.getTokens()) {
if (token.getType() == JassLexer.INT_LITERAL) {
if (token.getType() == JassLexer.OCT_INT_LITERAL || token.getType() == JassLexer.DEC_INT_LITERAL || token.getType() == JassLexer.HEX_INT_LITERAL || token.getType() == JassLexer.ID_INT_LITERAL) {
String text = token.getText();

int val;
Expand All @@ -458,6 +458,30 @@ public void filter(@Nonnull Filter filter) throws IOException {
jRefedIds.add(id);
}
}

/*if (token.getType() == JassLexer.INT_LITERAL) {
String text = token.getText();
int val;
if (text.startsWith("0x") || text.startsWith("0X")) {
val = Math.decode(text.substring(2).toLowerCase(), Math.CODE_HEX);
} else if (text.startsWith("$")) {
val = Math.decode(text.substring(1), Math.CODE_HEX);
} else if (text.startsWith("0")) {
val = Math.decode(text.substring(1), Math.CODE_OCT);
} else if (text.startsWith("'")) {
val = Math.decode(text.substring(1, text.length() - 1), Math.CODE_ASCII);
} else {
val = Math.decode(text, Math.CODE_DEC);
}
Id id = Id.valueOf(Math.encode(val, Math.CODE_ASCII));
if (id.toString().length() == 4) {
jRefedIds.add(id);
}
}*/
}
}

Expand Down
229 changes: 229 additions & 0 deletions src/main/java/net/moonlightflower/wc3libs/bin/app/W3I.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import net.moonlightflower.wc3libs.port.JMpqPort;
import net.moonlightflower.wc3libs.port.MpqPort;
import net.moonlightflower.wc3libs.port.Orient;
import net.moonlightflower.wc3libs.txt.app.jass.FuncImpl;
import net.moonlightflower.wc3libs.txt.app.jass.FuncDecl;
import net.moonlightflower.wc3libs.txt.app.jass.JassScript;
import net.moonlightflower.wc3libs.txt.app.jass.statement.Statement;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -799,6 +803,42 @@ public void setAllyLowPrioFlag(int index, boolean val) {
_allyLowPrioFlags.setPos(index, val);
}

public Set<Integer> getAllyLowPrioPlayerNums() {
Set<Integer> ret = new LinkedHashSet<>();

int players = _allyLowPrioFlags.toInt();
int c = 0;

while (players != 0) {
if ((players & 1) == 1) ret.add(c);

c++;
players >>= 1;
}

return ret;
}

public void removeAllyLowPrioPlayers(int... players) {
int rem = 0;

for (int player : players) {
rem |= (1 << player);
}

_allyLowPrioFlags.setVal(_allyLowPrioFlags.toInt() & ~rem);
}

public void addAllyLowPrioPlayerNums(int... players) {
int add = 0;

for (int player : players) {
add |= (1 << player);
}

_allyLowPrioFlags.setVal(_allyLowPrioFlags.toInt() | add);
}

private FlagsInt _allyHighPrioFlags = AllyFlags.valueOf(0);

public int getAllyHighPrioFlags() {
Expand All @@ -813,6 +853,42 @@ public void setAllyHighPrioFlag(int index, boolean val) {
_allyHighPrioFlags.setPos(index, val);
}

public Set<Integer> getAllyHighPrioPlayerNums() {
Set<Integer> ret = new LinkedHashSet<>();

int players = _allyHighPrioFlags.toInt();
int c = 0;

while (players != 0) {
if ((players & 1) == 1) ret.add(c);

c++;
players >>= 1;
}

return ret;
}

public void removeAllyHighPrioPlayers(int... players) {
int rem = 0;

for (int player : players) {
rem |= (1 << player);
}

_allyHighPrioFlags.setVal(_allyHighPrioFlags.toInt() & ~rem);
}

public void addAllyHighPrioPlayerNums(int... players) {
int add = 0;

for (int player : players) {
add |= (1 << player);
}

_allyHighPrioFlags.setVal(_allyHighPrioFlags.toInt() | add);
}

@Override
public String toString() {
String allyLowPrioFlagsS = String.format("%12s", Integer.toBinaryString(getAllyLowPrioFlags()));
Expand Down Expand Up @@ -2241,6 +2317,159 @@ public static W3I ofMapFile(@Nonnull File mapFile) throws Exception {
w3i.read(inStream);

inStream.close();

return w3i;
}

public FuncImpl makeInitCustomPlayerSlots() {
FuncDecl funcDecl = new FuncDecl(false, FuncDecl.INIT_CUSTOM_PLAYER_SLOTS, new ArrayList<>(), null);

List<Statement> stmts = new ArrayList<>();

for (Player player : getPlayers()) {
stmts.add(Statement.create("call SetPlayerStartLocation(Player(" + player.getNum() + ")" + ", " + player.getNum() + ")"));
stmts.add(Statement.create("call SetPlayerColor(Player(" + player.getNum() + ")" + ", ConvertPlayerColor(" + player.getNum() + "))"));
stmts.add(Statement.create("call SetPlayerRacePreference(Player(" + player.getNum() + ")" + ", " + player.getRace() + ")"));
stmts.add(Statement.create("call SetPlayerRaceSelectable(Player(" + player.getNum() + ")" + ", false)"));
stmts.add(Statement.create("call SetPlayerController(Player(" + player.getNum() + ")" + ", " + player.getType() + ")"));
}

FuncImpl.Body body = new FuncImpl.Body(new ArrayList<>(), stmts);

FuncImpl funcImpl = new FuncImpl(funcDecl, body);

return funcImpl;
}

public FuncImpl makeInitCustomTeams() {
FuncDecl funcDecl = new FuncDecl(false, FuncDecl.INIT_CUSTOM_TEAMS, new ArrayList<>(), null);

List<Statement> stmts = new ArrayList<>();

Map<Integer, Player> numToPlayerMap = new LinkedHashMap<>();

for (Player player : getPlayers()) {
numToPlayerMap.put(player.getNum(), player);
}

for (Force force : getForces()) {
for (Integer playerNum : force.getPlayerNums()) {
Player player = numToPlayerMap.get(playerNum);

stmts.add(Statement.create("call SetPlayerTeam(Player(" + player.getNum() + ")" + ", " + getForces().indexOf(force) + ")"));

if (force.getFlag(Force.Flags.Flag.ALLIED)) {
for (Integer playerNum2 : force.getPlayerNums()) {
if (playerNum == playerNum2) continue;

stmts.add(Statement.create("call SetPlayerAllianceStateAllyBJ(Player(" + player.getNum() + ")" + ", " + playerNum2 + ", true)"));
}
}
if (force.getFlag(Force.Flags.Flag.SHARED_VISION)) {
for (Integer playerNum2 : force.getPlayerNums()) {
if (playerNum == playerNum2) continue;

stmts.add(Statement.create("call SetPlayerAllianceStateVisionBJ(Player(" + player.getNum() + ")" + ", " + playerNum2 + ", true)"));
}
}
}
}

FuncImpl.Body body = new FuncImpl.Body(new ArrayList<>(), stmts);

FuncImpl funcImpl = new FuncImpl(funcDecl, body);

return funcImpl;
}

public FuncImpl makeInitAllyPriorities() {
FuncDecl funcDecl = new FuncDecl(false, FuncDecl.INIT_ALLY_PRIORITIES, new ArrayList<>(), null);

List<Statement> stmts = new ArrayList<>();

for (Player player : getPlayers()) {
Set<Integer> lowNums = player.getAllyLowPrioPlayerNums();
Set<Integer> highNums = player.getAllyHighPrioPlayerNums();

int playerNum = player.getNum();

stmts.add(Statement.create(String.format("call SetStartLocPrioCount(%d, %d)", playerNum, lowNums.size() + highNums.size())));

int c = 0;

for (Integer lowNum : lowNums) {
stmts.add(Statement.create(String.format("call SetStartLocPrio(%d, %d, %d, %s)", playerNum, c, lowNum, "MAP_LOC_PRIO_LOW")));
c++;
}
for (Integer highNum : highNums) {
stmts.add(Statement.create(String.format("call SetStartLocPrio(%d, %d, %d, %s)", playerNum, c, highNum, "MAP_LOC_PRIO_HIGH")));
c++;
}
}

FuncImpl.Body body = new FuncImpl.Body(new ArrayList<>(), stmts);

FuncImpl funcImpl = new FuncImpl(funcDecl, body);

return funcImpl;
}

public FuncImpl makeConfig() {
FuncDecl funcDecl = new FuncDecl(false, FuncDecl.CONFIG_NAME, new ArrayList<>(), null);

List<Statement> stmts = new ArrayList<>();

stmts.add(Statement.create("call SetMapName(\"" + getMapName() + "\")"));
stmts.add(Statement.create("call SetMapDescription(\"" + getMapDescription() + "\")"));

stmts.add(Statement.create("call SetPlayers(" + getPlayers().size() + ")"));
stmts.add(Statement.create("call SetForces(" + getForces().size() + ")"));

stmts.add(Statement.create("call SetGamePlacement(MAP_PLACEMENT_TEAMS_TOGETHER)"));

for (Player player : getPlayers()) {
stmts.add(Statement.create("call DefineStartLocation(" + player.getNum() + ", " + player.getStartPos().getX() + ", " + player.getStartPos().getY() + ")"));
}

stmts.add(Statement.create("call " + FuncDecl.INIT_CUSTOM_PLAYER_SLOTS + "()"));
stmts.add(Statement.create("call " + FuncDecl.INIT_CUSTOM_TEAMS + "()"));
stmts.add(Statement.create("call " + FuncDecl.INIT_ALLY_PRIORITIES + "()"));

FuncImpl.Body body = new FuncImpl.Body(new ArrayList<>(), stmts);

FuncImpl funcImpl = new FuncImpl(funcDecl, body);

return funcImpl;
}

public void injectConfigsInJassScript(@Nonnull JassScript jassScript) {
List<String> funcNames = new ArrayList<>();

funcNames.add(FuncDecl.CONFIG_NAME);
funcNames.add(FuncDecl.INIT_CUSTOM_PLAYER_SLOTS);
funcNames.add(FuncDecl.INIT_CUSTOM_TEAMS);
funcNames.add(FuncDecl.INIT_ALLY_PRIORITIES);

List<FuncImpl> funcImplsToRemove = new ArrayList<>();

for (FuncImpl funcImpl : jassScript.getFuncImpls()) {
if (funcNames.contains(funcImpl.getDecl().getName())) {
funcImplsToRemove.add(funcImpl);
}
}

for (FuncImpl funcImpl : funcImplsToRemove) {
jassScript.removeFuncImpl(funcImpl);
}

FuncImpl initCustomPlayerSlots = makeInitCustomPlayerSlots();
FuncImpl initCustomTeams = makeInitCustomTeams();
FuncImpl initAllyPriorities = makeInitAllyPriorities();
FuncImpl config = makeConfig();

jassScript.addFuncImpl(initCustomPlayerSlots);
jassScript.addFuncImpl(initCustomTeams);
jassScript.addFuncImpl(initAllyPriorities);
jassScript.addFuncImpl(config);
}
}
4 changes: 2 additions & 2 deletions src/main/java/net/moonlightflower/wc3libs/bin/app/WTG.java
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,7 @@ private void read_0x4(@Nonnull Reader reader, boolean hasBranch) throws Exceptio
for (int i = 0; i < _func.getParams().size(); i++) {
Param sub = null;

/*Func subFunc = funcMap.get(func.getParam(i));
/*FuncImpl subFunc = funcMap.get(func.getParam(i));
switch (subFunc.getType()) {
case BOOLCALL:
Expand Down Expand Up @@ -1068,7 +1068,7 @@ private void read_0x7(@Nonnull Reader reader, boolean hasBranch) throws Exceptio
for (int i = 0; i < _func.getParams().size(); i++) {
Param sub = null;

/*Func subFunc = funcMap.get(func.getParam(i).toLowerCase());
/*FuncImpl subFunc = funcMap.get(func.getParam(i).toLowerCase());
switch (subFunc.getType()) {
case BOOLCALL:
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/net/moonlightflower/wc3libs/txt/PLD.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.moonlightflower.wc3libs.antlr.JassLexer;
import net.moonlightflower.wc3libs.antlr.JassParser;
import net.moonlightflower.wc3libs.txt.app.jass.FuncImpl;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.misc.Interval;

Expand All @@ -10,7 +11,7 @@
import java.util.Set;

public class PLD {
public JassParser.FuncContext toJassFunc() {
public FuncImpl toJassFunc() {
StringBuilder sb = new StringBuilder();

sb.append("function PreloadFiles takes nothing returns nothing");
Expand All @@ -37,9 +38,9 @@ public JassParser.FuncContext toJassFunc() {

JassParser parser = new JassParser(tokenStream);

JassParser.FuncContext func = parser.func();

return func;
return null;
//TODO: fix
//return FuncImpl.create(parser.func_impl());
}

private Set<String> _preloads = new LinkedHashSet<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package net.moonlightflower.wc3libs.txt.app.jass;

import net.moonlightflower.wc3libs.antlr.JassParser;
import net.moonlightflower.wc3libs.antlr.LightJassParser;
import net.moonlightflower.wc3libs.txt.app.jass.expr.bool.BoolExpr;

import javax.annotation.Nonnull;
import java.io.StringWriter;

public interface Condition {
/*static Condition create(@Nonnull JassParser.ConditionContext conditionContext) {
return BoolExpr.create(conditionContext.bool_expr());
}*/

static Condition create(@Nonnull LightJassParser.ConditionContext conditionContext) {
return BoolExpr.create(conditionContext.expr());
}

void write(@Nonnull StringWriter sw);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package net.moonlightflower.wc3libs.txt.app.jass;

public interface Decl {
}
Loading

0 comments on commit 76b24e8

Please sign in to comment.