Feel free to join the next Helmholtz Hacky Hour #26 on Wednesday, April 21, 2021 from 2PM to 3PM!

Commit ebf9489f authored by Martin Lange's avatar Martin Lange

Merge branch 'feature/grassing' into 'master'

Simple grassing system with reproduction and grass/search behaviour

See merge request !6
parents 5c9f0fa6 c29cea4e
Pipeline #12198 passed with stage
in 12 seconds
This diff is collapsed.
......@@ -51,8 +51,8 @@ code = "code/"
docs = "docs/"
# The input source file(s) as glob pattern(s).
files = ["README.md"]
## File(s) to include in code output (unprocessed), as glob pattern(s).
# code_files = ["**/*.java"]
# File(s) to include in code output (unprocessed), as glob pattern(s).
code_files = ["gradle/**/*", "gradlew", "gradlew.bat"]
## File(s) to include in docs output (unprocessed), as glob pattern(s).
# doc_files = ["**/*.png", "**/*.jpg"]
## Entrypoint block name. Optional. If not supplied, unnamed code blocks are used.
......
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
#!/usr/bin/env sh
#
# Copyright 2015 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
##############################################################################
##
## Gradle start up script for UN*X
##
##############################################################################
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`"/$link"
fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >/dev/null
APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
warn () {
echo "$*"
}
die () {
echo
echo "$*"
echo
exit 1
}
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "`uname`" in
CYGWIN* )
cygwin=true
;;
Darwin* )
darwin=true
;;
MINGW* )
msys=true
;;
NONSTOP* )
nonstop=true
;;
esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD="$JAVA_HOME/bin/java"
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD="java"
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
# Increase the maximum file descriptors if we can.
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
MAX_FD_LIMIT=`ulimit -H -n`
if [ $? -eq 0 ] ; then
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
MAX_FD="$MAX_FD_LIMIT"
fi
ulimit -n $MAX_FD
if [ $? -ne 0 ] ; then
warn "Could not set maximum file descriptor limit: $MAX_FD"
fi
else
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
fi
fi
# For Darwin, add options to specify how the application appears in the dock
if $darwin; then
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi
# For Cygwin or MSYS, switch paths to Windows format before running java
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`
# We build the pattern for arguments to be converted via cygpath
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
SEP=""
for dir in $ROOTDIRSRAW ; do
ROOTDIRS="$ROOTDIRS$SEP$dir"
SEP="|"
done
OURCYGPATTERN="(^($ROOTDIRS))"
# Add a user-defined pattern to the cygpath arguments
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
fi
# Now convert the arguments - kludge to limit ourselves to /bin/sh
i=0
for arg in "$@" ; do
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
else
eval `echo args$i`="\"$arg\""
fi
i=`expr $i + 1`
done
case $i in
0) set -- ;;
1) set -- "$args0" ;;
2) set -- "$args0" "$args1" ;;
3) set -- "$args0" "$args1" "$args2" ;;
4) set -- "$args0" "$args1" "$args2" "$args3" ;;
5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
esac
fi
# Escape application args
save () {
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
echo " "
}
APP_ARGS=`save "$@"`
# Collect all arguments for the java command, following the shell quoting and substitution rules
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
exec "$JAVACMD" "$@"
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto init
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:init
@rem Get command-line arguments, handling Windows variants
if not "%OS%" == "Windows_NT" goto win9xME_args
:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2
:win9xME_args_slurp
if "x%~1" == "x" goto execute
set CMD_LINE_ARGS=%*
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega
# Appendix
This section describes code that is not vital for understanding the model or the ECS concept.
**Contents**
- [Graphics](#graphics)
- [Utilities](#utilities)
- [Gradle files](#gradle-files)
- [Git ignore file](#git-ignore-file)
## Graphics
The graphics system draws the amount of grass per cell by shades of green. Further, it draws searching grassers as orange triangles pointing in the direction of their heading, and grassing grassers as white rectangles.
```java
/// file:src/main/java/grassing/sys/Graphics.java
package grassing.sys;
import com.artemis.BaseEntitySystem;
import com.artemis.ComponentMapper;
import com.artemis.annotations.All;
import com.artemis.annotations.One;
import com.artemis.annotations.Wire;
import com.artemis.utils.IntBag;
import grassing.comp.*;
import grassing.res.Grass;
import grassing.util.FloatGrid;
import grassing.util.MathUtil;
import javax.swing.*;
import java.awt.*;
@All(Position.class)
public class Graphics extends BaseEntitySystem {
protected ComponentMapper<Position> position;
protected ComponentMapper<Movement> movement;
@Wire Grass grass;
private JPanel canvas;
final private int CELL_SIZE = 6;
public Graphics() {
super();
}
@Override
protected void initialize() {
var frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
canvas = new JPanel() {
@Override
public void paint(java.awt.Graphics g) {
Graphics.this.paint((Graphics2D)g);
}
};
var dim = new Dimension(grass.grass.width * CELL_SIZE, grass.grass.height * CELL_SIZE);
canvas.setPreferredSize( dim );
frame.add(canvas);
frame.pack();
frame.setVisible(true);
}
@Override
protected void processSystem() {
canvas.paintImmediately(0, 0, grass.grass.width * CELL_SIZE, grass.grass.height * CELL_SIZE);
}
void paint(Graphics2D g2d) {
FloatGrid grass = this.grass.grass;
int s = CELL_SIZE;
g2d.setBackground(Color.BLACK);
for(int y=0; y<grass.height; y++) {
for(int x=0; x<grass.width; x++) {
g2d.setPaint(new Color(0f, 0.7f * grass.get(x, y), 0f));
g2d.fillRect(x*s, y*s, s, s);
}
}
int[] x = new int[3];
int[] y = new int[3];
IntBag grassers = subscription.getEntities();
for (int i = 0; i < grassers.size(); i++) {
int entity = grassers.get(i);
var pos = position.get(entity);
var mov = movement.get(entity);
if(mov == null) {
g2d.setPaint(Color.WHITE);
g2d.fillRect((int) ((pos.x - 0.3f) * s), (int) ((pos.y - 0.3f) * s), (int) (0.6f * s), (int) (0.6f * s));
} else {
g2d.setPaint(Color.ORANGE);
var f = MathUtil.heading(mov.direction);
var r = MathUtil.heading(mov.direction + 0.5f * (float) Math.PI);
x[0] = (int) ((pos.x + f.x) * s);
y[0] = (int) ((pos.y + f.y) * s);
x[1] = (int) ((pos.x + 0.4f * r.x) * s);
y[1] = (int) ((pos.y + 0.4f * r.y) * s);
x[2] = (int) ((pos.x - 0.4f * r.x) * s);
y[2] = (int) ((pos.y - 0.4f * r.y) * s);
g2d.fillPolygon(x, y, 3);
}
}
}
}
```
## Utilities
The grid of `float` values for the grass.
```java
/// file:src/main/java/grassing/util/FloatGrid.java
package grassing.util;
public class FloatGrid {
final public int width;
final public int height;
final public int length;
protected final float[] data;
public FloatGrid(int width, int height) {
this.data = new float[width * height];
this.width = width;
this.height = height;
this.length = width * height;
}
public final int getIndex(int x, int y) {
return (y * width + x);
}
public final boolean contains(int x, int y) {
return x >= 0 && y >= 0 && x < width && y < height;
}
public final float get(int idx) {
return data[idx];
}
public final float get(int x, int y) {
return data[getIndex(x, y)];
}
public final void set(int idx, float value) {
data[idx] = value;
}
public final void set(int x, int y, float value) {
data[getIndex(x, y)] = value;
}
}
```
Utility functions to handle angles.
```java
/// file:src/main/java/grassing/util/MathUtil.java
package grassing.util;
public abstract class MathUtil {
public static Vector heading(float angle) {
return new Vector((float) Math.cos(angle), (float) Math.sin(angle));
}
public static float deg2rad(float deg) {
return (float) (deg * Math.PI / 180f);
}
}
```
Vector for heading calculated from angle.
```java
/// file:src/main/java/grassing/util/Vector.java
package grassing.util;
public class Vector {
public float x;
public float y;
public Vector(float x, float y) {
this.x = x;
this.y = y;
}
}
```
## Gradle files
[Gradle](https://gradle.org/) is used as build tool and for dependency management.
```groovy
/// file:build.gradle
apply plugin: 'application'
sourceCompatibility = 11
targetCompatibility = 11
repositories {
mavenCentral()
}
dependencies {
implementation group: 'net.onedaybeard.artemis', name: 'artemis-odb', version: '2.3.0'
}
application {
getMainClass().set('grassing.Main')
}
```
```groovy
/// file:settings.gradle
rootProject.name = 'Grassing'
```
## Git ignore file
Regarding VCS/Git, we ignore files generated by Gradle, as well as build output.
```
/// file:.gitignore
.gradle
build
```
# How to use this project
This projected is written via Literate Programming. Code is weaved into the Markdown documentation, and is extracted to a usable Java [Gradle](https://gradle.org/) project using the command line tool [Yarner](https://github.com/mlange-42/yarner).
There are several ways to use this projects or it's [downloadable outputs](https://git.ufz.de/oesa/ecs-tutorial/-/jobs/artifacts/master/download?job=build). We start from simply running downloaded executables, over building the downloaded Java project, to cloning the repository and running the complete chain from Yarner to execution.
After following any of the following sections, you should see a window with grassers bustling around.
**Contents**
* [Run from downloads](#run-from-downloads)
* [Browse the plain Java code](#browse-the-plain-java-code)
* [Build from downloads](#build-from-downloads)
* [Clone, extract, build](#clone-extract-build)
## Run from downloads
The [downloads](https://git.ufz.de/oesa/ecs-tutorial/-/jobs/artifacts/master/download?job=build) contain a file `Grassing.zip`. Extract it and navigate into folder `Grassing/bin`. Double-click or run file `Grassing` (or `Grassing.bat` if you are on Windows).
> This required [Java](https://www.java.com) 11 or newer to be installed on your machine. Simply try running the model, and install Java in case you get a respective error. Download Java [here](https://adoptopenjdk.net/).
## Browse the plain Java code
The [downloads](https://git.ufz.de/oesa/ecs-tutorial/-/jobs/artifacts/master/download?job=build) contain a folder `code`. This folder contains the project's Java code that you can browse following the structure of a "normal" Java project, rather than weaved into the Literate Programming document.
To work on the code, you can import the folder (potentially after renaming it) into your favourite Java-supporting IDE. If your IDE supports Gradle, you can import it as a Gradle project.
## Build from downloads
The [downloads](https://git.ufz.de/oesa/ecs-tutorial/-/jobs/artifacts/master/download?job=build) contain a folder `code`. Besides the project's Java code it contains a [Gradle](https://gradle.org/) project.
You can build and run the project with the following command executed inside folder `code`:
```
gradlew run
```
> This requires the Java JDK 11 or newer to be installed on your machine. Download Java [here](https://adoptopenjdk.net/).
## Clone, extract, build
Clone the repository using the following command:
```
git clone https://git.ufz.de/oesa/ecs-tutorial.git
```
> This required [Git](https://git-scm.com/) to be installed on your machine. Download Git [here](https://git-scm.com/downloads).
Navigate into the project and run [Yarner](https://github.com/mlange-42/yarner):
```
cd ecs-tutorial
yarner
```
> This requires [Yarner](https://github.com/mlange-42/yarner) to be installed on your machine. Download Yarner [here](https://github.com/mlange-42/yarner/releases/). Yarner needs to be on the PATH, or you need use the full path to Yarner:
>
> ```
> C:/path/to/yarner/yarner
> ```
This will generate Java sources and a [Gradle](https://gradle.org/) project in sub-folder `code`. Navigate into it and run the project:
```
cd code
gradlew run
```
> This requires the Java JDK 11 or newer to be installed on your machine. Download Java [here](https://adoptopenjdk.net/).
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment