file_id
int64 1
215k
| content
stringlengths 7
454k
| repo
stringlengths 6
113
| path
stringlengths 6
251
|
---|---|---|---|
1 | // We define the operation of splitting
// a binary number n into two numbers
// a(n), b(n) as follows. Let 0 ≤ i1 < i2 <
// . . . < ik be the indices of the bits (with
// the least significant bit having index 0) in
// n that are 1. Then the indices of the bits
// of a(n) that are 1 are i1, i3, i5, . . . and the
// indices of the bits of b(n) that are 1 are
// i2, i4, i6, . . .
// For example, if n is 110110101 in binary
// then, again in binary, we have a =
// 010010001 and b = 100100100.
// Input
// Each test case consists of a single integer
// n between 1 and 231 − 1 written in standard decimal (base 10) format on a single line. Input is
// terminated by a line containing ‘0’ which should not be processed.
// Output
// The output for each test case consists of a single line, containing the integers a(n) and b(n) separated
// by a single space. Both a(n) and b(n) should be written in decimal format.
// Sample Input
// 6
// 7
// 13
// 0
// Sample Output
// 2 4
// 5 2
// 9 4
/**
* Created by kdn251 on 2/10/17.
*/
import java.util.*;
import java.io.*;
public class SplittingNumbers {
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
while((line = br.readLine()) != null) {
//read number
int number = Integer.parseInt(line);
//terminate if number is zero
if(number == 0) break;
//intialize variables
int count = 0;
int a = 0;
int b = 0;
while(number > 0) {
//get lowest set bit
int currentBit = number ^ (number & (number - 1));
//if count is even or a with current bit
if(count % 2 == 0) {
a |= currentBit;
}
//if count is odd or b with current bit
else {
b |= currentBit;
}
//increment count
count++;
//clear lowest set bit for next iteration
number &= (number - 1);
}
//print a and b
System.out.println(a + " " + b);
}
}
}
| kdn251/interviews | uva/SplittingNumbers.java |
2 | /*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you 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
*
* http://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.
*
* This project is based on a modification of https://github.com/uber/h3 which is licensed under the Apache 2.0 License.
*
* Copyright 2016-2018, 2020-2021 Uber Technologies, Inc.
*/
package org.elasticsearch.h3;
/**
* Mutable IJK hexagon coordinates
*
* Each axis is spaced 120 degrees apart.
*
* References two Vec2d cartesian coordinate systems:
*
* 1. gnomonic: face-centered polyhedral gnomonic projection space with
* traditional scaling and x-axes aligned with the face Class II
* i-axes.
*
* 2. hex2d: local face-centered coordinate system scaled a specific H3 grid
* resolution unit length and with x-axes aligned with the local
* i-axes
*/
final class CoordIJK {
/** CoordIJK unit vectors corresponding to the 7 H3 digits.
*/
private static final int[][] UNIT_VECS = {
{ 0, 0, 0 }, // direction 0
{ 0, 0, 1 }, // direction 1
{ 0, 1, 0 }, // direction 2
{ 0, 1, 1 }, // direction 3
{ 1, 0, 0 }, // direction 4
{ 1, 0, 1 }, // direction 5
{ 1, 1, 0 } // direction 6
};
/** H3 digit representing ijk+ axes direction.
* Values will be within the lowest 3 bits of an integer.
*/
public enum Direction {
CENTER_DIGIT(0),
K_AXES_DIGIT(1),
J_AXES_DIGIT(2),
JK_AXES_DIGIT(J_AXES_DIGIT.digit() | K_AXES_DIGIT.digit()),
I_AXES_DIGIT(4),
IK_AXES_DIGIT(I_AXES_DIGIT.digit() | K_AXES_DIGIT.digit()),
IJ_AXES_DIGIT(I_AXES_DIGIT.digit() | J_AXES_DIGIT.digit()),
INVALID_DIGIT(7),
NUM_DIGITS(INVALID_DIGIT.digit()),
PENTAGON_SKIPPED_DIGIT(K_AXES_DIGIT.digit());
Direction(int digit) {
this.digit = digit;
}
private final int digit;
public int digit() {
return digit;
}
}
int i; // i component
int j; // j component
int k; // k component
CoordIJK(int i, int j, int k) {
this.i = i;
this.j = j;
this.k = k;
}
/**
* Reset the value of the IJK coordinates to the provided ones.
*
* @param i the i coordinate
* @param j the j coordinate
* @param k the k coordinate
*/
void reset(int i, int j, int k) {
this.i = i;
this.j = j;
this.k = k;
}
/**
* Find the center point in 2D cartesian coordinates of a hex.
*/
public Vec2d ijkToHex2d() {
final int i = Math.subtractExact(this.i, this.k);
final int j = Math.subtractExact(this.j, this.k);
return new Vec2d(i - 0.5 * j, j * Constants.M_SQRT3_2);
}
/**
* Find the center point in spherical coordinates of a hex on a particular icosahedral face.
*/
public LatLng ijkToGeo(int face, int res, boolean substrate) {
final int i = Math.subtractExact(this.i, this.k);
final int j = Math.subtractExact(this.j, this.k);
return Vec2d.hex2dToGeo(i - 0.5 * j, j * Constants.M_SQRT3_2, face, res, substrate);
}
/**
* Add ijk coordinates.
*
* @param i the i coordinate
* @param j the j coordinate
* @param k the k coordinate
*/
public void ijkAdd(int i, int j, int k) {
this.i = Math.addExact(this.i, i);
this.j = Math.addExact(this.j, j);
this.k = Math.addExact(this.k, k);
}
/**
* Subtract ijk coordinates.
*
* @param i the i coordinate
* @param j the j coordinate
* @param k the k coordinate
*/
public void ijkSub(int i, int j, int k) {
this.i = Math.subtractExact(this.i, i);
this.j = Math.subtractExact(this.j, j);
this.k = Math.subtractExact(this.k, k);
}
/**
* Normalizes ijk coordinates by setting the ijk coordinates
* to the smallest possible positive values.
*/
public void ijkNormalize() {
final int min = Math.min(i, Math.min(j, k));
ijkSub(min, min, min);
}
/**
* Find the normalized ijk coordinates of the hex centered on the current
* hex at the next finer aperture 7 counter-clockwise resolution.
*/
public void downAp7() {
// res r unit vectors in res r+1
// iVec (3, 0, 1)
// jVec (1, 3, 0)
// kVec (0, 1, 3)
final int i = Math.addExact(Math.multiplyExact(this.i, 3), this.j);
final int j = Math.addExact(Math.multiplyExact(this.j, 3), this.k);
final int k = Math.addExact(Math.multiplyExact(this.k, 3), this.i);
this.i = i;
this.j = j;
this.k = k;
ijkNormalize();
}
/**
* Find the normalized ijk coordinates of the hex centered on the current
* hex at the next finer aperture 7 clockwise resolution.
*/
public void downAp7r() {
// iVec (3, 1, 0)
// jVec (0, 3, 1)
// kVec (1, 0, 3)
final int i = Math.addExact(Math.multiplyExact(this.i, 3), this.k);
final int j = Math.addExact(Math.multiplyExact(this.j, 3), this.i);
final int k = Math.addExact(Math.multiplyExact(this.k, 3), this.j);
this.i = i;
this.j = j;
this.k = k;
ijkNormalize();
}
/**
* Find the normalized ijk coordinates of the hex centered on the current
* hex at the next finer aperture 3 counter-clockwise resolution.
*/
public void downAp3() {
// res r unit vectors in res r+1
// iVec (2, 0, 1)
// jVec (1, 2, 0)
// kVec (0, 1, 2)
final int i = Math.addExact(Math.multiplyExact(this.i, 2), this.j);
final int j = Math.addExact(Math.multiplyExact(this.j, 2), this.k);
final int k = Math.addExact(Math.multiplyExact(this.k, 2), this.i);
this.i = i;
this.j = j;
this.k = k;
ijkNormalize();
}
/**
* Find the normalized ijk coordinates of the hex centered on the current
* hex at the next finer aperture 3 clockwise resolution.
*/
public void downAp3r() {
// res r unit vectors in res r+1
// iVec (2, 1, 0)
// jVec (0, 2, 1)
// kVec (1, 0, 2)
final int i = Math.addExact(Math.multiplyExact(this.i, 2), this.k);
final int j = Math.addExact(Math.multiplyExact(this.j, 2), this.i);
final int k = Math.addExact(Math.multiplyExact(this.k, 2), this.j);
this.i = i;
this.j = j;
this.k = k;
ijkNormalize();
}
/**
* Rotates ijk coordinates 60 degrees clockwise.
*
*/
public void ijkRotate60cw() {
// unit vector rotations
// iVec (1, 0, 1)
// jVec (1, 1, 0)
// kVec (0, 1, 1)
final int i = Math.addExact(this.i, this.j);
final int j = Math.addExact(this.j, this.k);
final int k = Math.addExact(this.i, this.k);
this.i = i;
this.j = j;
this.k = k;
ijkNormalize();
}
/**
* Rotates ijk coordinates 60 degrees counter-clockwise.
*/
public void ijkRotate60ccw() {
// unit vector rotations
// iVec (1, 1, 0)
// jVec (0, 1, 1)
// kVec (1, 0, 1)
final int i = Math.addExact(this.i, this.k);
final int j = Math.addExact(this.i, this.j);
final int k = Math.addExact(this.j, this.k);
this.i = i;
this.j = j;
this.k = k;
ijkNormalize();
}
/**
* Find the normalized ijk coordinates of the hex in the specified digit
* direction from the current ijk coordinates.
* @param digit The digit direction from the original ijk coordinates.
*/
public void neighbor(int digit) {
if (digit > Direction.CENTER_DIGIT.digit() && digit < Direction.NUM_DIGITS.digit()) {
ijkAdd(UNIT_VECS[digit][0], UNIT_VECS[digit][1], UNIT_VECS[digit][2]);
ijkNormalize();
}
}
/**
* Find the normalized ijk coordinates of the indexing parent of a cell in a
* clockwise aperture 7 grid.
*/
public void upAp7r() {
final int i = Math.subtractExact(this.i, this.k);
final int j = Math.subtractExact(this.j, this.k);
this.i = (int) Math.round((Math.addExact(Math.multiplyExact(2, i), j)) / 7.0);
this.j = (int) Math.round((Math.subtractExact(Math.multiplyExact(3, j), i)) / 7.0);
this.k = 0;
ijkNormalize();
}
/**
* Find the normalized ijk coordinates of the indexing parent of a cell in a
* counter-clockwise aperture 7 grid.
*
*/
public void upAp7() {
final int i = Math.subtractExact(this.i, this.k);
final int j = Math.subtractExact(this.j, this.k);
this.i = (int) Math.round((Math.subtractExact(Math.multiplyExact(3, i), j)) / 7.0);
this.j = (int) Math.round((Math.addExact(Math.multiplyExact(2, j), i)) / 7.0);
this.k = 0;
ijkNormalize();
}
/**
* Determines the H3 digit corresponding to a unit vector in ijk coordinates.
*
* @return The H3 digit (0-6) corresponding to the ijk unit vector, or
* INVALID_DIGIT on failure.
*/
public int unitIjkToDigit() {
// should be call on a normalized object
if (Math.min(i, Math.min(j, k)) < 0 || Math.max(i, Math.max(j, k)) > 1) {
return Direction.INVALID_DIGIT.digit();
}
return i << 2 | j << 1 | k;
}
/**
* Rotates indexing digit 60 degrees clockwise. Returns result.
*
* @param digit Indexing digit (between 1 and 6 inclusive)
*/
public static int rotate60cw(int digit) {
return switch (digit) {
case 1 -> // K_AXES_DIGIT
Direction.JK_AXES_DIGIT.digit();
case 3 -> // JK_AXES_DIGIT:
Direction.J_AXES_DIGIT.digit();
case 2 -> // J_AXES_DIGIT:
Direction.IJ_AXES_DIGIT.digit();
case 6 -> // IJ_AXES_DIGIT
Direction.I_AXES_DIGIT.digit();
case 4 -> // I_AXES_DIGIT
Direction.IK_AXES_DIGIT.digit();
case 5 -> // IK_AXES_DIGIT
Direction.K_AXES_DIGIT.digit();
default -> digit;
};
}
/**
* Rotates indexing digit 60 degrees counter-clockwise. Returns result.
*
* @param digit Indexing digit (between 1 and 6 inclusive)
*/
public static int rotate60ccw(int digit) {
return switch (digit) {
case 1 -> // K_AXES_DIGIT
Direction.IK_AXES_DIGIT.digit();
case 5 -> // IK_AXES_DIGIT
Direction.I_AXES_DIGIT.digit();
case 4 -> // I_AXES_DIGIT
Direction.IJ_AXES_DIGIT.digit();
case 6 -> // IJ_AXES_DIGIT
Direction.J_AXES_DIGIT.digit();
case 2 -> // J_AXES_DIGIT:
Direction.JK_AXES_DIGIT.digit();
case 3 -> // JK_AXES_DIGIT:
Direction.K_AXES_DIGIT.digit();
default -> digit;
};
}
}
| elastic/elasticsearch | libs/h3/src/main/java/org/elasticsearch/h3/CoordIJK.java |
3 | package com.macro.mall.search.domain;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.annotations.Setting;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
/**
* 搜索商品的信息
* Created by macro on 2018/6/19.
*/
@Data
@EqualsAndHashCode
@Document(indexName = "pms")
@Setting(shards = 1,replicas = 0)
public class EsProduct implements Serializable {
private static final long serialVersionUID = -1L;
@Id
private Long id;
@Field(type = FieldType.Keyword)
private String productSn;
private Long brandId;
@Field(type = FieldType.Keyword)
private String brandName;
private Long productCategoryId;
@Field(type = FieldType.Keyword)
private String productCategoryName;
private String pic;
@Field(analyzer = "ik_max_word",type = FieldType.Text)
private String name;
@Field(analyzer = "ik_max_word",type = FieldType.Text)
private String subTitle;
@Field(analyzer = "ik_max_word",type = FieldType.Text)
private String keywords;
private BigDecimal price;
private Integer sale;
private Integer newStatus;
private Integer recommandStatus;
private Integer stock;
private Integer promotionType;
private Integer sort;
@Field(type = FieldType.Nested, fielddata = true)
private List<EsProductAttributeValue> attrValueList;
}
| macrozheng/mall | mall-search/src/main/java/com/macro/mall/search/domain/EsProduct.java |
4 | /*
* Copyright (C) 2009 The Guava 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
*
* http://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.
*/
package com.google.common.base;
import com.google.common.annotations.GwtCompatible;
import java.lang.ref.WeakReference;
import java.util.Locale;
import java.util.ServiceConfigurationError;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import javax.annotation.CheckForNull;
/**
* Methods factored out so that they can be emulated differently in GWT.
*
* @author Jesse Wilson
*/
@GwtCompatible(emulated = true)
@ElementTypesAreNonnullByDefault
final class Platform {
private static final Logger logger = Logger.getLogger(Platform.class.getName());
private static final PatternCompiler patternCompiler = loadPatternCompiler();
private Platform() {}
static CharMatcher precomputeCharMatcher(CharMatcher matcher) {
return matcher.precomputedInternal();
}
static <T extends Enum<T>> Optional<T> getEnumIfPresent(Class<T> enumClass, String value) {
WeakReference<? extends Enum<?>> ref = Enums.getEnumConstants(enumClass).get(value);
/*
* We use `fromNullable` instead of `of` because `WeakReference.get()` has a nullable return
* type.
*
* In practice, we are very unlikely to see `null`: The `WeakReference` to the enum constant
* won't be cleared as long as the enum constant is referenced somewhere, and the enum constant
* is referenced somewhere for as long as the enum class is loaded. *Maybe in theory* the enum
* class could be unloaded after the above call to `getEnumConstants` but before we call
* `get()`, but that is vanishingly unlikely.
*/
return ref == null ? Optional.absent() : Optional.fromNullable(enumClass.cast(ref.get()));
}
static String formatCompact4Digits(double value) {
return String.format(Locale.ROOT, "%.4g", value);
}
static boolean stringIsNullOrEmpty(@CheckForNull String string) {
return string == null || string.isEmpty();
}
/**
* Returns the string if it is not null, or an empty string otherwise.
*
* @param string the string to test and possibly return
* @return {@code string} if it is not null; {@code ""} otherwise
*/
static String nullToEmpty(@CheckForNull String string) {
return (string == null) ? "" : string;
}
/**
* Returns the string if it is not empty, or a null string otherwise.
*
* @param string the string to test and possibly return
* @return {@code string} if it is not empty; {@code null} otherwise
*/
@CheckForNull
static String emptyToNull(@CheckForNull String string) {
return stringIsNullOrEmpty(string) ? null : string;
}
static CommonPattern compilePattern(String pattern) {
Preconditions.checkNotNull(pattern);
return patternCompiler.compile(pattern);
}
static boolean patternCompilerIsPcreLike() {
return patternCompiler.isPcreLike();
}
private static PatternCompiler loadPatternCompiler() {
return new JdkPatternCompiler();
}
private static void logPatternCompilerError(ServiceConfigurationError e) {
logger.log(Level.WARNING, "Error loading regex compiler, falling back to next option", e);
}
private static final class JdkPatternCompiler implements PatternCompiler {
@Override
public CommonPattern compile(String pattern) {
return new JdkPattern(Pattern.compile(pattern));
}
@Override
public boolean isPcreLike() {
return true;
}
}
}
| google/guava | guava/src/com/google/common/base/Platform.java |
5 | // Companion Code to the paper "Generative Forests" by R. Nock and M. Guillame-Bert.
import java.io.*;
import java.util.*;
/**************************************************************************************************************************************
* Class Algorithm
*****/
class SplitDetails implements Debuggable {
// records the split details of a split
// important for split_observations, since some observations can have unknown values and thus this
// needs to be computed & recorded ONCE
int index_feature;
int index_split;
int[][] split_observations;
SplitDetails(int index_f, int index_s, int[][] split_o) {
index_feature = index_f;
index_split = index_s;
split_observations = split_o;
}
}
public class Algorithm implements Debuggable {
public static String
LEAF_CHOICE_RANDOMIZED_WITH_NON_ZERO_EMPIRICAL_MEASURE =
"LEAF_CHOICE_RANDOMIZED_WITH_NON_ZERO_EMPIRICAL_MEASURE",
LEAF_CHOICE_HEAVIEST_LEAF_AMONG_TREES = "LEAF_CHOICE_HEAVIEST_LEAF_AMONG_TREES";
// different ways of picking a leaf
public static String[] LEAF_CHOICE = {
LEAF_CHOICE_RANDOMIZED_WITH_NON_ZERO_EMPIRICAL_MEASURE, LEAF_CHOICE_HEAVIEST_LEAF_AMONG_TREES
};
public static double MINIMUM_EMPIRICAL_CARD_AT_NEW_LEAF_FOR_BOOSTING = 1.0;
// boosting can "peel off" support with zero empirical card: this requires minimum local card at
// new leaf
public static double MINIMUM_P_R_FOR_BOOSTING = EPS2;
// minimum estimated weight of empirical measure to allow a split for EOGT
public static int MAXIMAL_NUMBER_OF_SPLIT_TESTS_TRIES_PER_BOOSTING_ITERATION = 1000;
// if the total number of tests to be done is larger than this, does random picking of tests for
// this number of iterations (else is exhaustive)
public static int STEPS_TO_COMPUTE_DENSITY_ESTIMATION = 100;
// interval to save density estimation values
public static void CHECK_LEAF_CHOICE_VALID(String s) {
int i = 0;
do {
if (LEAF_CHOICE[i].equals(s)) return;
else i++;
} while (i < LEAF_CHOICE.length);
Dataset.perror("Algorithm.class :: no such choice as " + s + " for leaf choice");
}
Dataset myDS;
GenerativeModelBasedOnEnsembleOfTrees geot;
// parameters for learning
int nb_iterations, initial_number_of_trees;
boolean nb_trees_increase_allowed;
String how_to_choose_leaf, splitting_method;
public static String[] DENSITY_ESTIMATION_OUTPUT_TYPE = {"LIKELIHOODS", "LOG_LIKELIHOODS"};
public static int INDEX_DENSITY_ESTIMATION_OUTPUT_TYPE = 0;
// type of output for density estimation
Vector<Vector<Double>> density_estimation_outputs_likelihoods;
Vector<Vector<Double>> density_estimation_outputs_log_likelihoods;
Algorithm(
Dataset ds,
int name,
int nb_iterations,
int initial_number_of_trees,
String split_m,
boolean generative_forest) {
this(ds, name);
if (generative_forest) geot.generative_forest();
else geot.ensemble_of_generative_trees();
this.nb_iterations = nb_iterations;
this.initial_number_of_trees = initial_number_of_trees;
this.how_to_choose_leaf = Algorithm.LEAF_CHOICE_HEAVIEST_LEAF_AMONG_TREES;
splitting_method = split_m;
nb_trees_increase_allowed = false;
}
Algorithm(Dataset ds, int n) {
myDS = ds;
geot = new GenerativeModelBasedOnEnsembleOfTrees(ds, n);
}
public GenerativeModelBasedOnEnsembleOfTrees learn_geot() {
if (nb_trees_increase_allowed) Dataset.perror("NOT YET");
if (geot.generative_forest) System.out.print(" [GF] ");
else if (geot.ensemble_of_generative_trees) System.out.print("[EOGT] ");
geot.init(initial_number_of_trees);
HashSet<MeasuredSupportAtTupleOfNodes> tuples_of_leaves_with_positive_measure_geot =
new HashSet<>();
if (splitting_method.equals(Wrapper.BOOSTING))
tuples_of_leaves_with_positive_measure_geot.add(
new MeasuredSupportAtTupleOfNodes(geot, false, true, -1));
int i;
Node leaf;
boolean ok;
Vector<Double> current_likelihood;
Vector<Double> current_log_likelihood;
double[][] avs = new double[2][]; // [0][] = likelihood; [1][] = loglikelihood
avs[0] = new double[2];
avs[1] = new double[2];
if ((geot.generative_forest) && (myDS.myDomain.myW.density_estimation)) {
density_estimation_outputs_likelihoods = new Vector<>();
density_estimation_outputs_log_likelihoods = new Vector<>();
}
for (i = 0; i < nb_iterations; i++) {
leaf = null;
ok = true;
leaf = choose_leaf(how_to_choose_leaf);
if (leaf == null) {
Dataset.warning(" no splittable leaf found ");
ok = false;
}
if (ok) {
if (geot.generative_forest)
ok = split_generative_forest(leaf, tuples_of_leaves_with_positive_measure_geot);
else if (geot.ensemble_of_generative_trees)
ok =
split_ensemble_of_generative_trees(leaf, tuples_of_leaves_with_positive_measure_geot);
else Dataset.perror("Algorithm.class :: no such option available yet");
}
if (!ok) {
System.out.print("X ");
break;
} else System.out.print(".");
if (i % 10 == 0) System.out.print(myDS.myDomain.memString());
leaf.myTree.checkConsistency();
if ((i % Algorithm.STEPS_TO_COMPUTE_DENSITY_ESTIMATION == 0)
&& (geot.generative_forest)
&& (myDS.myDomain.myW.density_estimation)) {
current_likelihood = new Vector<>();
current_log_likelihood = new Vector<>();
avs = geot.expected_density_estimation_output();
current_likelihood.addElement(new Double(i));
current_likelihood.addElement(new Double(avs[0][0]));
current_likelihood.addElement(new Double(avs[0][1]));
density_estimation_outputs_likelihoods.addElement(current_likelihood);
current_log_likelihood.addElement(new Double(i));
current_log_likelihood.addElement(new Double(avs[1][0]));
current_log_likelihood.addElement(new Double(avs[1][1]));
density_estimation_outputs_log_likelihoods.addElement(current_log_likelihood);
}
}
System.out.print(" [soft_probs] ");
geot.compute_probabilities_soft();
return geot;
}
Node choose_leaf(String how_to_choose_leaf) {
Algorithm.CHECK_LEAF_CHOICE_VALID(how_to_choose_leaf);
// a leaf has a handle to its tree so can pick a leaf regardless of the tree
if (geot.trees == null) Dataset.perror("Algorithm.class :: no tree to choose a leaf from");
int i, j, meas = -1;
Node n, ret = null;
boolean firstleaf = true;
Iterator it;
if (how_to_choose_leaf.equals(Algorithm.LEAF_CHOICE_HEAVIEST_LEAF_AMONG_TREES)) {
for (i = 0; i < geot.trees.size(); i++) {
it = geot.trees.elementAt(i).leaves.iterator();
while (it.hasNext()) {
n = (Node) it.next();
if ((n.observations_in_node)
&& (n.has_feature_tests())
&& ((firstleaf) || (n.observation_indexes_in_node.length > meas))) {
firstleaf = false;
ret = n;
meas = n.observation_indexes_in_node.length;
}
}
}
}
return ret;
}
public boolean split_generative_forest(Node leaf, HashSet<MeasuredSupportAtTupleOfNodes> tol) {
if (!leaf.is_leaf) Dataset.perror("Algorithm.class :: " + leaf + " is not a leaf");
FeatureTest[][] all_feature_tests = new FeatureTest[leaf.node_support.dim()][];
// x = feature index, y = split index
boolean[] splittable_feature = new boolean[leaf.node_support.dim()];
boolean at_least_one_splittable_feature = false;
SplitDetails sd = null;
int i, nb_total_splits = 0, j, index_f = -1, index_s = -1;
Vector<FeatureTest> dumft;
for (i = 0; i < leaf.node_support.dim(); i++) {
dumft = FeatureTest.ALL_FEATURE_TESTS(leaf.node_support.feature(i), myDS);
if (dumft != null) {
at_least_one_splittable_feature = true;
splittable_feature[i] = true;
all_feature_tests[i] = new FeatureTest[dumft.size()];
for (j = 0; j < dumft.size(); j++) all_feature_tests[i][j] = dumft.elementAt(j);
nb_total_splits += all_feature_tests[i].length;
} else {
splittable_feature[i] = false;
all_feature_tests[i] = new FeatureTest[0];
}
}
if (!at_least_one_splittable_feature)
Dataset.perror("Algorithm.class :: no splittable feature for node " + leaf);
if (splitting_method.equals(Wrapper.BOOSTING)) {
if (Wrapper.FAST_SPLITTING)
sd =
split_top_down_boosting_generative_forest_fast(
leaf, splittable_feature, all_feature_tests, tol);
else
sd =
split_top_down_boosting_generative_forest(
leaf, splittable_feature, all_feature_tests, tol);
} else Dataset.perror("Algorithm.class :: no such split choice as " + splitting_method);
if (sd == null) return false;
index_f = sd.index_feature;
index_s = sd.index_split;
int[][] new_split_observations = sd.split_observations;
// feature found at index index_f, split_index to be used at all_feature_tests[index_f][index_s]
// node update
if ((leaf.is_leaf)
|| (leaf.node_feature_test != all_feature_tests[index_f][index_s])
|| (leaf.node_feature_split_index != index_f))
Dataset.perror(
"Algorithm.class :: leaf should have been updated ("
+ leaf.is_leaf
+ ") ("
+ leaf.node_feature_test
+ " vs "
+ all_feature_tests[index_f][index_s]
+ ") ("
+ leaf.node_feature_split_index
+ " vs "
+ index_s
+ ")");
// create two new nodes
Feature[] new_features =
leaf.node_feature_test.split_feature(
myDS, leaf.node_support.feature(leaf.node_feature_split_index), true, true);
Support[] new_supports =
leaf.node_support.split_support(leaf.node_feature_split_index, new_features);
Node new_left_leaf =
new Node(
leaf.myTree,
leaf.myTree.number_nodes,
leaf.depth + 1,
new_split_observations[0],
new_supports[0]);
Node new_right_leaf =
new Node(
leaf.myTree,
leaf.myTree.number_nodes + 1,
leaf.depth + 1,
new_split_observations[1],
new_supports[1]);
if (new_split_observations[0].length + new_split_observations[1].length
!= leaf.observation_indexes_in_node.length)
Dataset.perror("Algorithm.class :: error in splitting supports, mismatch in #examples");
leaf.p_left =
(double) new_split_observations[0].length
/ ((double) leaf.observation_indexes_in_node.length);
leaf.p_right =
(double) new_split_observations[1].length
/ ((double) leaf.observation_indexes_in_node.length);
if (splitting_method.equals(Wrapper.BOOSTING)) {
// unmarking the elements in tol
Iterator it = tol.iterator();
MeasuredSupportAtTupleOfNodes ms;
while (it.hasNext()) {
ms = (MeasuredSupportAtTupleOfNodes) it.next();
if (ms.marked_for_update) ms.unmark_for_update(new_left_leaf, new_right_leaf);
}
}
// update tree's statistics
leaf.myTree.statistics_number_of_nodes_for_each_feature[index_f]++;
// link nodes
leaf.left_child = new_left_leaf;
leaf.right_child = new_right_leaf;
leaf.left_child.p_reach = leaf.p_reach * leaf.p_left;
leaf.right_child.p_reach = leaf.p_reach * leaf.p_right;
// update tree leaves & size
leaf.myTree.number_nodes += 2;
boolean test_remove =
leaf.myTree.remove_leaf_from_tree_structure(leaf); // leaf.myTree.leaves.remove(leaf);
if (!test_remove)
Dataset.perror("Algorithm.class :: Leaf " + leaf.name + " not found in tree's leaves");
if (leaf.depth > leaf.myTree.depth)
Dataset.perror(
"Algorithm.class :: Leaf "
+ leaf.name
+ " at depth "
+ leaf.depth
+ " > leaf.myTree depth = "
+ leaf.myTree.depth);
if (leaf.depth == leaf.myTree.depth) leaf.myTree.depth++;
leaf.myTree.add_leaf_to_tree_structure(new_left_leaf);
leaf.myTree.add_leaf_to_tree_structure(new_right_leaf);
return true;
}
public boolean split_ensemble_of_generative_trees(
Node leaf, HashSet<MeasuredSupportAtTupleOfNodes> tol) {
if (!leaf.is_leaf) Dataset.perror("Algorithm.class :: " + leaf + " is not a leaf");
FeatureTest[][] all_feature_tests = new FeatureTest[leaf.node_support.dim()][];
// x = feature index, y = split index
boolean[] splittable_feature = new boolean[leaf.node_support.dim()];
boolean at_least_one_splittable_feature = false;
SplitDetails sd = null;
int i, nb_total_splits = 0, j, index_f = -1, index_s = -1;
Vector<FeatureTest> dumft;
for (i = 0; i < leaf.node_support.dim(); i++) {
dumft = FeatureTest.ALL_FEATURE_TESTS(leaf.node_support.feature(i), myDS);
if (dumft != null) {
at_least_one_splittable_feature = true;
splittable_feature[i] = true;
all_feature_tests[i] = new FeatureTest[dumft.size()];
for (j = 0; j < dumft.size(); j++) all_feature_tests[i][j] = dumft.elementAt(j);
nb_total_splits += all_feature_tests[i].length;
} else {
splittable_feature[i] = false;
all_feature_tests[i] = new FeatureTest[0];
}
}
if (!at_least_one_splittable_feature)
Dataset.perror("Algorithm.class :: no splittable feature for node " + leaf);
if (splitting_method.equals(Wrapper.BOOSTING)) {
sd =
split_top_down_boosting_ensemble_of_generative_trees_fast(
leaf, splittable_feature, all_feature_tests, tol);
} else Dataset.perror("Algorithm.class :: no such split choice as " + splitting_method);
if (sd == null) return false;
index_f = sd.index_feature;
index_s = sd.index_split;
int[][] new_split_observations = sd.split_observations;
// feature found at index index_f, split_index to be used at all_feature_tests[index_f][index_s]
// node update
if ((leaf.is_leaf)
|| (leaf.node_feature_test != all_feature_tests[index_f][index_s])
|| (leaf.node_feature_split_index != index_f))
Dataset.perror(
"Algorithm.class :: leaf should have been updated ("
+ leaf.is_leaf
+ ") ("
+ leaf.node_feature_test
+ " vs "
+ all_feature_tests[index_f][index_s]
+ ") ("
+ leaf.node_feature_split_index
+ " vs "
+ index_s
+ ")");
// create two new nodes
Feature[] new_features =
leaf.node_feature_test.split_feature(
myDS, leaf.node_support.feature(leaf.node_feature_split_index), true, true);
Support[] new_supports =
leaf.node_support.split_support(leaf.node_feature_split_index, new_features);
Node new_left_leaf =
new Node(
leaf.myTree,
leaf.myTree.number_nodes,
leaf.depth + 1,
new_split_observations[0],
new_supports[0]);
Node new_right_leaf =
new Node(
leaf.myTree,
leaf.myTree.number_nodes + 1,
leaf.depth + 1,
new_split_observations[1],
new_supports[1]);
if (new_split_observations[0].length + new_split_observations[1].length
!= leaf.observation_indexes_in_node.length)
Dataset.perror("Algorithm.class :: error in splitting supports, mismatch in #examples");
leaf.p_left =
(double) new_split_observations[0].length
/ ((double) leaf.observation_indexes_in_node.length);
leaf.p_right =
(double) new_split_observations[1].length
/ ((double) leaf.observation_indexes_in_node.length);
if (splitting_method.equals(Wrapper.BOOSTING)) {
// unmarking the elements in tol
Iterator it = tol.iterator();
MeasuredSupportAtTupleOfNodes ms;
while (it.hasNext()) {
ms = (MeasuredSupportAtTupleOfNodes) it.next();
if (ms.marked_for_update) ms.unmark_for_update(new_left_leaf, new_right_leaf);
}
}
// update tree's statistics
leaf.myTree.statistics_number_of_nodes_for_each_feature[index_f]++;
// link nodes
leaf.left_child = new_left_leaf;
leaf.right_child = new_right_leaf;
leaf.left_child.p_reach = leaf.p_reach * leaf.p_left;
leaf.right_child.p_reach = leaf.p_reach * leaf.p_right;
// update tree leaves & size
leaf.myTree.number_nodes += 2;
boolean test_remove =
leaf.myTree.remove_leaf_from_tree_structure(leaf); // leaf.myTree.leaves.remove(leaf);
if (!test_remove)
Dataset.perror("Algorithm.class :: Leaf " + leaf.name + " not found in tree's leaves");
if (leaf.depth > leaf.myTree.depth)
Dataset.perror(
"Algorithm.class :: Leaf "
+ leaf.name
+ " at depth "
+ leaf.depth
+ " > leaf.myTree depth = "
+ leaf.myTree.depth);
if (leaf.depth == leaf.myTree.depth) leaf.myTree.depth++;
leaf.myTree.add_leaf_to_tree_structure(new_left_leaf);
leaf.myTree.add_leaf_to_tree_structure(new_right_leaf);
return true;
}
public HashSet<String> filter_for_leaf(
Hashtable<String, MeasuredSupportAtTupleOfNodes> all_measured_supports_at_intersection,
Node leaf) {
// returns a subset of keys of all_measured_supports_at_intersection having leaf in them
HashSet<String> ret = new HashSet<>();
Vector<Node> all_nodes;
boolean tree_done;
int i;
for (i = 0; i < geot.trees.size(); i++) {
all_nodes = new Vector<>();
tree_done = false;
}
return ret;
}
public SplitDetails split_top_down_boosting_generative_forest(
Node leaf,
boolean[] splittable_feature,
FeatureTest[][] all_feature_tests,
HashSet<MeasuredSupportAtTupleOfNodes> tol) {
HashSet<MeasuredSupportAtTupleOfNodes> subset_with_leaf = new HashSet<>();
HashSet<MeasuredSupportAtTupleOfNodes> new_candidates_after_split;
HashSet<MeasuredSupportAtTupleOfNodes> best_candidates_for_left_after_split = null;
HashSet<MeasuredSupportAtTupleOfNodes> best_candidates_for_right_after_split = null;
Iterator it = tol.iterator();
MeasuredSupportAtTupleOfNodes ms;
int number_obs_tot = 0,
i,
j,
k,
index_f = -1,
index_s = -1,
number_left_obs,
number_right_obs,
iter = 0,
index_try;
double cur_Bayes, p_ccstar, opt_Bayes = -1.0;
boolean found, at_least_one;
while (it.hasNext()) {
ms = (MeasuredSupportAtTupleOfNodes) it.next();
if (ms.tree_nodes_support_contains_node(leaf)) {
subset_with_leaf.add(ms);
number_obs_tot += ms.generative_support.local_empirical_measure.observations_indexes.length;
}
}
if (number_obs_tot != leaf.observation_indexes_in_node.length)
Dataset.perror(
"Algorithm.class :: mismatch between the total #observations ("
+ number_obs_tot
+ ") and leaf weight ("
+ leaf.observation_indexes_in_node.length
+ ")");
if (subset_with_leaf.size() == 0)
Dataset.perror(
"Algorithm.class :: no subsets of MeasuredSupportAtTupleOfNodes with leaf " + leaf);
// speed up
Vector<Integer[]> couple_feature_index_feature_test_index = new Vector<>();
for (i = 0; i < splittable_feature.length; i++)
if (splittable_feature[i])
for (j = 0; j < all_feature_tests[i].length; j++)
couple_feature_index_feature_test_index.addElement(
new Integer[] {new Integer(i), new Integer(j)});
List<Integer> shuffled_indexes = new ArrayList<>();
for (i = 0; i < couple_feature_index_feature_test_index.size(); i++) shuffled_indexes.add(i);
Collections.shuffle(shuffled_indexes);
int
max_number_tries =
(couple_feature_index_feature_test_index.size()
> Algorithm.MAXIMAL_NUMBER_OF_SPLIT_TESTS_TRIES_PER_BOOSTING_ITERATION)
? Algorithm.MAXIMAL_NUMBER_OF_SPLIT_TESTS_TRIES_PER_BOOSTING_ITERATION
: couple_feature_index_feature_test_index.size(),
dumi;
// works regardless of the option chosen
found = false;
for (k = 0; k < max_number_tries; k++) {
index_try = shuffled_indexes.get(shuffled_indexes.size() - 1).intValue();
i = couple_feature_index_feature_test_index.elementAt(index_try)[0].intValue();
j = couple_feature_index_feature_test_index.elementAt(index_try)[1].intValue();
dumi = shuffled_indexes.remove(shuffled_indexes.size() - 1);
iter++;
new_candidates_after_split = new HashSet<>();
cur_Bayes = 0.0;
// computes all split_top_down_boosting_statistics & current Bayes entropy (HL in (7))
number_left_obs = number_right_obs = 0;
it = subset_with_leaf.iterator();
at_least_one = false;
while (it.hasNext()) {
at_least_one = true;
ms = (MeasuredSupportAtTupleOfNodes) it.next();
ms.split_top_down_boosting_statistics =
ms.split_for_boosting_computations(all_feature_tests[i][j], i);
if ((ms.split_top_down_boosting_statistics[0] != null)
&& (ms.split_top_down_boosting_statistics[0]
.generative_support
.local_empirical_measure
.observations_indexes
!= null))
number_left_obs +=
ms.split_top_down_boosting_statistics[0]
.generative_support
.local_empirical_measure
.observations_indexes
.length;
if ((ms.split_top_down_boosting_statistics[1] != null)
&& (ms.split_top_down_boosting_statistics[1]
.generative_support
.local_empirical_measure
.observations_indexes
!= null))
number_right_obs +=
ms.split_top_down_boosting_statistics[1]
.generative_support
.local_empirical_measure
.observations_indexes
.length;
p_ccstar =
(Statistics.PRIOR
* ((double)
ms.generative_support.local_empirical_measure.observations_indexes.length)
/ ((double) myDS.observations_from_file.size()))
+ ((1.0 - Statistics.PRIOR)
* ms.generative_support.support.weight_uniform_distribution);
cur_Bayes +=
(p_ccstar
* Statistics.MU_BAYES_GENERATIVE_FOREST(
ms.split_top_down_boosting_statistics, 1.0));
}
// Bayes entropy improvement ?
if ((at_least_one)
&& (number_left_obs >= Algorithm.MINIMUM_EMPIRICAL_CARD_AT_NEW_LEAF_FOR_BOOSTING)
&& (number_right_obs >= Algorithm.MINIMUM_EMPIRICAL_CARD_AT_NEW_LEAF_FOR_BOOSTING)
&& ((!found) || (cur_Bayes < opt_Bayes))) {
index_f = i;
index_s = j;
opt_Bayes = cur_Bayes;
found = true;
best_candidates_for_left_after_split = new HashSet<>();
best_candidates_for_right_after_split = new HashSet<>();
it = subset_with_leaf.iterator();
while (it.hasNext()) {
ms = (MeasuredSupportAtTupleOfNodes) it.next();
if (ms.split_top_down_boosting_statistics[0] != null)
best_candidates_for_left_after_split.add(ms.split_top_down_boosting_statistics[0]);
if (ms.split_top_down_boosting_statistics[1] != null)
best_candidates_for_right_after_split.add(ms.split_top_down_boosting_statistics[1]);
}
}
}
// update leaf
if ((index_f == -1) || (index_s == -1)) return null;
leaf.node_feature_test = all_feature_tests[index_f][index_s];
leaf.node_feature_split_index = index_f;
leaf.is_leaf = false;
// splits observations using split details
// collects all observations already assigned from subset
Vector<Integer> left_obs = new Vector<>();
Vector<Integer> right_obs = new Vector<>();
if (best_candidates_for_left_after_split.size() > 0) {
it = best_candidates_for_left_after_split.iterator();
while (it.hasNext()) {
ms = (MeasuredSupportAtTupleOfNodes) it.next();
if ((ms.generative_support.local_empirical_measure.observations_indexes != null)
&& (ms.generative_support.local_empirical_measure.observations_indexes.length > 0))
for (i = 0;
i < ms.generative_support.local_empirical_measure.observations_indexes.length;
i++)
left_obs.addElement(
new Integer(ms.generative_support.local_empirical_measure.observations_indexes[i]));
}
}
if (best_candidates_for_right_after_split.size() > 0) {
it = best_candidates_for_right_after_split.iterator();
while (it.hasNext()) {
ms = (MeasuredSupportAtTupleOfNodes) it.next();
if ((ms.generative_support.local_empirical_measure.observations_indexes != null)
&& (ms.generative_support.local_empirical_measure.observations_indexes.length > 0))
for (i = 0;
i < ms.generative_support.local_empirical_measure.observations_indexes.length;
i++)
right_obs.addElement(
new Integer(ms.generative_support.local_empirical_measure.observations_indexes[i]));
}
}
Vector<Integer> all_leaf_obs = new Vector<>();
int[] alo;
for (i = 0; i < leaf.observation_indexes_in_node.length; i++)
all_leaf_obs.addElement(new Integer(leaf.observation_indexes_in_node[i]));
int[][] new_split_observations = new int[2][];
new_split_observations[0] = new int[left_obs.size()];
new_split_observations[1] = new int[right_obs.size()];
for (i = 0; i < left_obs.size(); i++)
new_split_observations[0][i] = left_obs.elementAt(i).intValue();
for (i = 0; i < right_obs.size(); i++)
new_split_observations[1][i] = right_obs.elementAt(i).intValue();
// remove subset_with_leaf from tol
it = subset_with_leaf.iterator();
while (it.hasNext()) {
ms = (MeasuredSupportAtTupleOfNodes) it.next();
found = tol.remove(ms);
if (!found)
Dataset.perror(
"Algorithm.class :: subset of support " + ms + " should be in the set of all");
}
// update tol: add all elements in best_candidates_for_left_after_split &
// best_candidates_for_right_after_split WITH >0 MEASURE, remove from tol all elements in
// subset;
if (best_candidates_for_left_after_split.size() > 0) {
it = best_candidates_for_left_after_split.iterator();
while (it.hasNext()) {
ms = (MeasuredSupportAtTupleOfNodes) it.next();
if ((ms.generative_support.local_empirical_measure.observations_indexes != null)
&& (ms.generative_support.local_empirical_measure.observations_indexes.length > 0)) {
ms.marked_for_update = true;
ms.marked_for_update_index_tree = leaf.myTree.name;
ms.marked_for_update_which_child = MeasuredSupportAtTupleOfNodes.LEFT_CHILD;
tol.add(ms);
}
}
}
if (best_candidates_for_right_after_split.size() > 0) {
it = best_candidates_for_right_after_split.iterator();
while (it.hasNext()) {
ms = (MeasuredSupportAtTupleOfNodes) it.next();
if ((ms.generative_support.local_empirical_measure.observations_indexes != null)
&& (ms.generative_support.local_empirical_measure.observations_indexes.length > 0)) {
ms.marked_for_update = true;
ms.marked_for_update_index_tree = leaf.myTree.name;
ms.marked_for_update_which_child = MeasuredSupportAtTupleOfNodes.RIGHT_CHILD;
tol.add(ms);
}
}
}
return new SplitDetails(index_f, index_s, new_split_observations);
}
public SplitDetails split_top_down_boosting_generative_forest_fast(
Node leaf,
boolean[] splittable_feature,
FeatureTest[][] all_feature_tests,
HashSet<MeasuredSupportAtTupleOfNodes> tol) {
// tol contains all current tuple of leaves whose intersection support has >0 empirical measure
// fast method which does not record where observations with missing values go (to match the
// argument used to compute Bayes risk during testing all splits and that for the split
// selected)
HashSet<MeasuredSupportAtTupleOfNodes> subset_with_leaf = new HashSet<>();
HashSet<MeasuredSupportAtTupleOfNodes> new_candidates_after_split;
HashSet<MeasuredSupportAtTupleOfNodes> best_candidates_for_left_after_split = null;
HashSet<MeasuredSupportAtTupleOfNodes> best_candidates_for_right_after_split = null;
Iterator it = tol.iterator();
MeasuredSupportAtTupleOfNodes ms;
int number_obs_tot = 0,
i,
j,
k,
index_f = -1,
index_s = -1,
number_left_obs,
number_right_obs,
iter = 0,
index_try;
double cur_Bayes, p_ccstar, opt_Bayes = -1.0;
boolean found, at_least_one;
double[] rapid_split_stats = new double[4];
while (it.hasNext()) {
ms = (MeasuredSupportAtTupleOfNodes) it.next();
if (ms.tree_nodes_support_contains_node(leaf)) {
subset_with_leaf.add(ms);
number_obs_tot += ms.generative_support.local_empirical_measure.observations_indexes.length;
}
}
if (number_obs_tot != leaf.observation_indexes_in_node.length)
Dataset.perror(
"Algorithm.class :: mismatch between the total #observations ("
+ number_obs_tot
+ ") and leaf weight ("
+ leaf.observation_indexes_in_node.length
+ ")");
if (subset_with_leaf.size() == 0)
Dataset.perror(
"Algorithm.class :: no subsets of MeasuredSupportAtTupleOfNodes with leaf " + leaf);
// speed up
Vector<Integer[]> couple_feature_index_feature_test_index = new Vector<>();
for (i = 0; i < splittable_feature.length; i++)
if (splittable_feature[i])
for (j = 0; j < all_feature_tests[i].length; j++)
couple_feature_index_feature_test_index.addElement(
new Integer[] {new Integer(i), new Integer(j)});
List<Integer> shuffled_indexes = new ArrayList<>();
for (i = 0; i < couple_feature_index_feature_test_index.size(); i++) shuffled_indexes.add(i);
Collections.shuffle(shuffled_indexes);
int
max_number_tries =
(couple_feature_index_feature_test_index.size()
> Algorithm.MAXIMAL_NUMBER_OF_SPLIT_TESTS_TRIES_PER_BOOSTING_ITERATION)
? Algorithm.MAXIMAL_NUMBER_OF_SPLIT_TESTS_TRIES_PER_BOOSTING_ITERATION
: couple_feature_index_feature_test_index.size(),
dumi;
// works regardless of the option chosen
found = false;
for (k = 0; k < max_number_tries; k++) {
index_try = shuffled_indexes.get(shuffled_indexes.size() - 1).intValue();
i = couple_feature_index_feature_test_index.elementAt(index_try)[0].intValue();
j = couple_feature_index_feature_test_index.elementAt(index_try)[1].intValue();
dumi = shuffled_indexes.remove(shuffled_indexes.size() - 1);
iter++;
new_candidates_after_split = new HashSet<>();
cur_Bayes = 0.0;
// computes all split_top_down_boosting_statistics & current Bayes entropy (HL in (7))
number_left_obs = number_right_obs = 0;
it = subset_with_leaf.iterator();
at_least_one = false;
while (it.hasNext()) {
at_least_one = true;
ms = (MeasuredSupportAtTupleOfNodes) it.next();
rapid_split_stats = ms.rapid_split_statistics(all_feature_tests[i][j], i);
number_left_obs += rapid_split_stats[0];
number_right_obs += rapid_split_stats[1];
p_ccstar =
(Statistics.PRIOR
* ((double)
ms.generative_support.local_empirical_measure.observations_indexes.length)
/ ((double) myDS.observations_from_file.size()))
+ ((1.0 - Statistics.PRIOR)
* ms.generative_support.support.weight_uniform_distribution);
cur_Bayes +=
(p_ccstar
* Statistics.MU_BAYES_GENERATIVE_FOREST_SIMPLE(
rapid_split_stats[0],
rapid_split_stats[1],
rapid_split_stats[2],
rapid_split_stats[3],
myDS.observations_from_file.size(),
1.0));
}
// Bayes entropy improvement ?
if ((at_least_one)
&& (number_left_obs >= Algorithm.MINIMUM_EMPIRICAL_CARD_AT_NEW_LEAF_FOR_BOOSTING)
&& (number_right_obs >= Algorithm.MINIMUM_EMPIRICAL_CARD_AT_NEW_LEAF_FOR_BOOSTING)
&& ((!found) || (cur_Bayes < opt_Bayes))) { //
index_f = i;
index_s = j;
opt_Bayes = cur_Bayes;
found = true;
}
}
// update leaf
if ((index_f == -1) || (index_s == -1)) return null;
// compute best_candidates_for_left_after_split, best_candidates_for_right_after_split : SPEED
// UP
it = subset_with_leaf.iterator();
best_candidates_for_left_after_split = new HashSet<>();
best_candidates_for_right_after_split = new HashSet<>();
while (it.hasNext()) {
ms = (MeasuredSupportAtTupleOfNodes) it.next();
ms.split_top_down_boosting_statistics =
ms.split_for_boosting_computations(all_feature_tests[index_f][index_s], index_f);
if (ms.split_top_down_boosting_statistics[0] != null)
best_candidates_for_left_after_split.add(ms.split_top_down_boosting_statistics[0]);
if (ms.split_top_down_boosting_statistics[1] != null)
best_candidates_for_right_after_split.add(ms.split_top_down_boosting_statistics[1]);
}
// end of SPEED UP
leaf.node_feature_test = all_feature_tests[index_f][index_s];
leaf.node_feature_split_index = index_f;
leaf.is_leaf = false;
// splits observations using split details
// collects all observations already assigned from subset
Vector<Integer> left_obs = new Vector<>();
Vector<Integer> right_obs = new Vector<>();
if (best_candidates_for_left_after_split.size() > 0) {
it = best_candidates_for_left_after_split.iterator();
while (it.hasNext()) {
ms = (MeasuredSupportAtTupleOfNodes) it.next();
if ((ms.generative_support.local_empirical_measure.observations_indexes != null)
&& (ms.generative_support.local_empirical_measure.observations_indexes.length > 0))
for (i = 0;
i < ms.generative_support.local_empirical_measure.observations_indexes.length;
i++)
left_obs.addElement(
new Integer(ms.generative_support.local_empirical_measure.observations_indexes[i]));
}
}
if (best_candidates_for_right_after_split.size() > 0) {
it = best_candidates_for_right_after_split.iterator();
while (it.hasNext()) {
ms = (MeasuredSupportAtTupleOfNodes) it.next();
if ((ms.generative_support.local_empirical_measure.observations_indexes != null)
&& (ms.generative_support.local_empirical_measure.observations_indexes.length > 0))
for (i = 0;
i < ms.generative_support.local_empirical_measure.observations_indexes.length;
i++)
right_obs.addElement(
new Integer(ms.generative_support.local_empirical_measure.observations_indexes[i]));
}
}
Vector<Integer> all_leaf_obs = new Vector<>();
int[] alo;
for (i = 0; i < leaf.observation_indexes_in_node.length; i++)
all_leaf_obs.addElement(new Integer(leaf.observation_indexes_in_node[i]));
int[][] new_split_observations = new int[2][];
new_split_observations[0] = new int[left_obs.size()];
new_split_observations[1] = new int[right_obs.size()];
for (i = 0; i < left_obs.size(); i++)
new_split_observations[0][i] = left_obs.elementAt(i).intValue();
for (i = 0; i < right_obs.size(); i++)
new_split_observations[1][i] = right_obs.elementAt(i).intValue();
// remove subset_with_leaf from tol
it = subset_with_leaf.iterator();
while (it.hasNext()) {
ms = (MeasuredSupportAtTupleOfNodes) it.next();
found = tol.remove(ms);
if (!found)
Dataset.perror(
"Algorithm.class :: subset of support " + ms + " should be in the set of all");
}
// update tol: add all elements in best_candidates_for_left_after_split &
// best_candidates_for_right_after_split WITH >0 MEASURE, remove from tol all elements in
// subset;
if (best_candidates_for_left_after_split.size() > 0) {
it = best_candidates_for_left_after_split.iterator();
while (it.hasNext()) {
ms = (MeasuredSupportAtTupleOfNodes) it.next();
if ((ms.generative_support.local_empirical_measure.observations_indexes != null)
&& (ms.generative_support.local_empirical_measure.observations_indexes.length > 0)) {
ms.marked_for_update = true;
ms.marked_for_update_index_tree = leaf.myTree.name;
ms.marked_for_update_which_child = MeasuredSupportAtTupleOfNodes.LEFT_CHILD;
tol.add(ms);
}
}
}
if (best_candidates_for_right_after_split.size() > 0) {
it = best_candidates_for_right_after_split.iterator();
while (it.hasNext()) {
ms = (MeasuredSupportAtTupleOfNodes) it.next();
if ((ms.generative_support.local_empirical_measure.observations_indexes != null)
&& (ms.generative_support.local_empirical_measure.observations_indexes.length > 0)) {
ms.marked_for_update = true;
ms.marked_for_update_index_tree = leaf.myTree.name;
ms.marked_for_update_which_child = MeasuredSupportAtTupleOfNodes.RIGHT_CHILD;
tol.add(ms);
}
}
}
return new SplitDetails(index_f, index_s, new_split_observations);
}
public SplitDetails split_top_down_boosting_ensemble_of_generative_trees_fast(
Node leaf,
boolean[] splittable_feature,
FeatureTest[][] all_feature_tests,
HashSet<MeasuredSupportAtTupleOfNodes> tol) {
// tol contains all current tuple of leaves whose intersection support has >0 empirical measure
HashSet<MeasuredSupportAtTupleOfNodes> subset_with_leaf = new HashSet<>();
HashSet<MeasuredSupportAtTupleOfNodes> new_candidates_after_split;
HashSet<MeasuredSupportAtTupleOfNodes> best_candidates_for_left_after_split = null;
HashSet<MeasuredSupportAtTupleOfNodes> best_candidates_for_right_after_split = null;
Iterator it = tol.iterator();
MeasuredSupportAtTupleOfNodes ms;
int number_obs_tot = 0,
i,
j,
k,
l,
index_f = -1,
number_left_obs,
number_right_obs,
index_s = -1,
iter = 0,
index_try;
double cur_Bayes,
p_ccstar,
opt_Bayes = -1.0,
p_R,
p_R_left,
p_R_right,
vol0,
vol1,
p0,
p1,
u_left,
u_right;
boolean found, at_least_one;
Support[] split_support;
double[] rapid_split_stats = new double[4];
while (it.hasNext()) {
ms = (MeasuredSupportAtTupleOfNodes) it.next();
if (ms.tree_nodes_support_contains_node(leaf)) {
subset_with_leaf.add(ms);
number_obs_tot += ms.generative_support.local_empirical_measure.observations_indexes.length;
}
}
if (number_obs_tot != leaf.observation_indexes_in_node.length)
Dataset.perror(
"Algorithm.class :: mismatch between the total #observations ("
+ number_obs_tot
+ ") and leaf weight ("
+ leaf.observation_indexes_in_node.length
+ ")");
if (subset_with_leaf.size() == 0)
Dataset.perror(
"Algorithm.class :: no subsets of MeasuredSupportAtTupleOfNodes with leaf " + leaf);
// speed up
Vector<Integer[]> couple_feature_index_feature_test_index = new Vector<>();
for (i = 0; i < splittable_feature.length; i++)
if (splittable_feature[i])
for (j = 0; j < all_feature_tests[i].length; j++)
couple_feature_index_feature_test_index.addElement(
new Integer[] {new Integer(i), new Integer(j)});
List<Integer> shuffled_indexes = new ArrayList<>();
for (i = 0; i < couple_feature_index_feature_test_index.size(); i++) shuffled_indexes.add(i);
Collections.shuffle(shuffled_indexes);
int
max_number_tries =
(couple_feature_index_feature_test_index.size()
> Algorithm.MAXIMAL_NUMBER_OF_SPLIT_TESTS_TRIES_PER_BOOSTING_ITERATION)
? Algorithm.MAXIMAL_NUMBER_OF_SPLIT_TESTS_TRIES_PER_BOOSTING_ITERATION
: couple_feature_index_feature_test_index.size(),
dumi;
// works regardless of the option chosen
found = false;
for (k = 0; k < max_number_tries; k++) {
index_try = shuffled_indexes.get(shuffled_indexes.size() - 1).intValue();
i = couple_feature_index_feature_test_index.elementAt(index_try)[0].intValue();
j = couple_feature_index_feature_test_index.elementAt(index_try)[1].intValue();
dumi = shuffled_indexes.remove(shuffled_indexes.size() - 1);
iter++;
new_candidates_after_split = new HashSet<>();
cur_Bayes = 0.0;
// computes all split_top_down_boosting_statistics & current Bayes entropy (HL in (7))
number_left_obs = number_right_obs = 0;
it = subset_with_leaf.iterator();
at_least_one = false;
while (it.hasNext()) {
at_least_one = true;
ms = (MeasuredSupportAtTupleOfNodes) it.next();
rapid_split_stats = ms.rapid_split_statistics(all_feature_tests[i][j], i);
number_left_obs += rapid_split_stats[0];
number_right_obs +=
rapid_split_stats[
1]; // "cheating" on cardinals just to make sure supports not empty (not used in
// ranking splits)
l =
Statistics.R.nextInt(
ms.tree_nodes_support
.size()); // very fast approx. should take into account all trees BUT this eases
// distributed training
p_R =
(ms.tree_nodes_support.elementAt(l).p_reach
/ ms.tree_nodes_support.elementAt(l).node_support.volume)
* ms.generative_support.support.volume;
split_support =
Support.SPLIT_SUPPORT(myDS, ms.generative_support.support, all_feature_tests[i][j], i);
if (split_support[0] != null) vol0 = (double) split_support[0].volume;
else vol0 = 0.0;
if (split_support[1] != null) vol1 = (double) split_support[1].volume;
else vol1 = 0.0;
p0 = vol0 / (vol0 + vol1);
p1 = vol1 / (vol0 + vol1);
p_R_left = p_R * p0;
p_R_right = p_R * p1;
u_left = ms.generative_support.support.weight_uniform_distribution * p0;
u_right = ms.generative_support.support.weight_uniform_distribution * p1;
p_ccstar =
(Statistics.PRIOR * p_R)
+ ((1.0 - Statistics.PRIOR)
* ms.generative_support.support.weight_uniform_distribution);
cur_Bayes +=
(p_ccstar
* Statistics.MU_BAYES_ENSEMBLE_OF_GENERATIVE_TREES(
myDS, p_R, p_R_left, p_R_right, u_left, u_right, 1.0));
}
// Bayes entropy improvement ?
if ((at_least_one)
&& (number_left_obs >= Algorithm.MINIMUM_EMPIRICAL_CARD_AT_NEW_LEAF_FOR_BOOSTING)
&& (number_right_obs >= Algorithm.MINIMUM_EMPIRICAL_CARD_AT_NEW_LEAF_FOR_BOOSTING)
&& ((!found) || (cur_Bayes < opt_Bayes))) {
index_f = i;
index_s = j;
opt_Bayes = cur_Bayes;
found = true;
}
}
// update leaf
if ((index_f == -1) || (index_s == -1)) return null;
// compute best_candidates_for_left_after_split, best_candidates_for_right_after_split : SPEED
// UP
it = subset_with_leaf.iterator();
best_candidates_for_left_after_split = new HashSet<>();
best_candidates_for_right_after_split = new HashSet<>();
while (it.hasNext()) {
ms = (MeasuredSupportAtTupleOfNodes) it.next();
ms.split_top_down_boosting_statistics =
ms.split_for_boosting_computations(all_feature_tests[index_f][index_s], index_f);
if (ms.split_top_down_boosting_statistics[0] != null)
best_candidates_for_left_after_split.add(ms.split_top_down_boosting_statistics[0]);
if (ms.split_top_down_boosting_statistics[1] != null)
best_candidates_for_right_after_split.add(ms.split_top_down_boosting_statistics[1]);
}
// end of SPEED UP
leaf.node_feature_test = all_feature_tests[index_f][index_s];
leaf.node_feature_split_index = index_f;
leaf.is_leaf = false;
// splits observations using split details
// collects all observations already assigned from subset
Vector<Integer> left_obs = new Vector<>();
Vector<Integer> right_obs = new Vector<>();
if (best_candidates_for_left_after_split.size() > 0) {
it = best_candidates_for_left_after_split.iterator();
while (it.hasNext()) {
ms = (MeasuredSupportAtTupleOfNodes) it.next();
if ((ms.generative_support.local_empirical_measure.observations_indexes != null)
&& (ms.generative_support.local_empirical_measure.observations_indexes.length > 0))
for (i = 0;
i < ms.generative_support.local_empirical_measure.observations_indexes.length;
i++)
left_obs.addElement(
new Integer(ms.generative_support.local_empirical_measure.observations_indexes[i]));
}
}
if (best_candidates_for_right_after_split.size() > 0) {
it = best_candidates_for_right_after_split.iterator();
while (it.hasNext()) {
ms = (MeasuredSupportAtTupleOfNodes) it.next();
if ((ms.generative_support.local_empirical_measure.observations_indexes != null)
&& (ms.generative_support.local_empirical_measure.observations_indexes.length > 0))
for (i = 0;
i < ms.generative_support.local_empirical_measure.observations_indexes.length;
i++)
right_obs.addElement(
new Integer(ms.generative_support.local_empirical_measure.observations_indexes[i]));
}
}
Vector<Integer> all_leaf_obs = new Vector<>();
int[] alo;
for (i = 0; i < leaf.observation_indexes_in_node.length; i++)
all_leaf_obs.addElement(new Integer(leaf.observation_indexes_in_node[i]));
int[][] new_split_observations = new int[2][];
new_split_observations[0] = new int[left_obs.size()];
new_split_observations[1] = new int[right_obs.size()];
for (i = 0; i < left_obs.size(); i++)
new_split_observations[0][i] = left_obs.elementAt(i).intValue();
for (i = 0; i < right_obs.size(); i++)
new_split_observations[1][i] = right_obs.elementAt(i).intValue();
// remove subset_with_leaf from tol
it = subset_with_leaf.iterator();
while (it.hasNext()) {
ms = (MeasuredSupportAtTupleOfNodes) it.next();
found = tol.remove(ms);
if (!found)
Dataset.perror(
"Algorithm.class :: subset of support " + ms + " should be in the set of all");
}
// update tol: add all elements in best_candidates_for_left_after_split &
// best_candidates_for_right_after_split WITH >0 MEASURE, remove from tol all elements in
// subset;
if (best_candidates_for_left_after_split.size() > 0) {
it = best_candidates_for_left_after_split.iterator();
while (it.hasNext()) {
ms = (MeasuredSupportAtTupleOfNodes) it.next();
if ((ms.generative_support.local_empirical_measure.observations_indexes != null)
&& (ms.generative_support.local_empirical_measure.observations_indexes.length > 0)) {
ms.marked_for_update = true;
ms.marked_for_update_index_tree = leaf.myTree.name;
ms.marked_for_update_which_child = MeasuredSupportAtTupleOfNodes.LEFT_CHILD;
tol.add(ms);
}
}
}
if (best_candidates_for_right_after_split.size() > 0) {
it = best_candidates_for_right_after_split.iterator();
while (it.hasNext()) {
ms = (MeasuredSupportAtTupleOfNodes) it.next();
if ((ms.generative_support.local_empirical_measure.observations_indexes != null)
&& (ms.generative_support.local_empirical_measure.observations_indexes.length > 0)) {
ms.marked_for_update = true;
ms.marked_for_update_index_tree = leaf.myTree.name;
ms.marked_for_update_which_child = MeasuredSupportAtTupleOfNodes.RIGHT_CHILD;
tol.add(ms);
}
}
}
return new SplitDetails(index_f, index_s, new_split_observations);
}
}
| google-research/google-research | generative_forests/src/Algorithm.java |
6 | package mindustry.content;
import arc.*;
import arc.graphics.*;
import arc.graphics.g2d.*;
import arc.math.*;
import arc.math.geom.*;
import arc.struct.*;
import arc.util.*;
import mindustry.entities.*;
import mindustry.entities.abilities.*;
import mindustry.gen.*;
import mindustry.graphics.*;
import mindustry.type.*;
import mindustry.world.*;
import mindustry.world.blocks.units.UnitAssembler.*;
import static arc.graphics.g2d.Draw.rect;
import static arc.graphics.g2d.Draw.*;
import static arc.graphics.g2d.Lines.*;
import static arc.math.Angles.*;
import static mindustry.Vars.*;
public class Fx{
public static final Rand rand = new Rand();
public static final Vec2 v = new Vec2();
public static final Effect
none = new Effect(0, 0f, e -> {}),
blockCrash = new Effect(90f, e -> {
if(!(e.data instanceof Block block)) return;
alpha(e.fin() + 0.5f);
float offset = Mathf.lerp(0f, 180f, e.fout());
color(0f, 0f, 0f, 0.44f);
rect(block.fullIcon, e.x - offset * 4f, e.y, (float)block.size * 8f, (float)block.size * 8f);
color(Color.white);
rect(block.fullIcon, e.x + offset, e.y + offset * 5f, (float)block.size * 8f, (float)block.size * 8f);
}),
trailFade = new Effect(400f, e -> {
if(!(e.data instanceof Trail trail)) return;
//lifetime is how many frames it takes to fade out the trail
e.lifetime = trail.length * 1.4f;
if(!state.isPaused()){
trail.shorten();
}
trail.drawCap(e.color, e.rotation);
trail.draw(e.color, e.rotation);
}),
unitSpawn = new Effect(30f, e -> {
if(!(e.data instanceof UnitType unit)) return;
TextureRegion region = unit.fullIcon;
float scl = (1f + e.fout() * 2f) * region.scl();
alpha(e.fout());
mixcol(Color.white, e.fin());
rect(region, e.x, e.y, 180f);
reset();
alpha(e.fin());
rect(region, e.x, e.y, region.width * scl, region.height * scl, e.rotation - 90);
}),
unitCapKill = new Effect(80f, e -> {
color(Color.scarlet);
alpha(e.fout(Interp.pow4Out));
float size = 10f + e.fout(Interp.pow10In) * 25f;
Draw.rect(Icon.warning.getRegion(), e.x, e.y, size, size);
}),
unitEnvKill = new Effect(80f, e -> {
color(Color.scarlet);
alpha(e.fout(Interp.pow4Out));
float size = 10f + e.fout(Interp.pow10In) * 25f;
Draw.rect(Icon.cancel.getRegion(), e.x, e.y, size, size);
}),
unitControl = new Effect(30f, e -> {
if(!(e.data instanceof Unit select)) return;
boolean block = select instanceof BlockUnitc;
mixcol(Pal.accent, 1f);
alpha(e.fout());
rect(block ? ((BlockUnitc)select).tile().block.fullIcon : select.type.fullIcon, select.x, select.y, block ? 0f : select.rotation - 90f);
alpha(1f);
Lines.stroke(e.fslope());
Lines.square(select.x, select.y, e.fout() * select.hitSize * 2f, 45);
Lines.stroke(e.fslope() * 2f);
Lines.square(select.x, select.y, e.fout() * select.hitSize * 3f, 45f);
reset();
}),
unitDespawn = new Effect(100f, e -> {
if(!(e.data instanceof Unit select) || select.type == null) return;
float scl = e.fout(Interp.pow2Out);
float p = Draw.scl;
Draw.scl *= scl;
mixcol(Pal.accent, 1f);
rect(select.type.fullIcon, select.x, select.y, select.rotation - 90f);
reset();
Draw.scl = p;
}),
unitSpirit = new Effect(17f, e -> {
if(!(e.data instanceof Position to)) return;
color(Pal.accent);
Tmp.v1.set(e.x, e.y).interpolate(Tmp.v2.set(to), e.fin(), Interp.pow2In);
float x = Tmp.v1.x, y = Tmp.v1.y;
float size = 2.5f * e.fin();
Fill.square(x, y, 1.5f * size, 45f);
Tmp.v1.set(e.x, e.y).interpolate(Tmp.v2.set(to), e.fin(), Interp.pow5In);
x = Tmp.v1.x;
y = Tmp.v1.y;
Fill.square(x, y, size, 45f);
}),
itemTransfer = new Effect(12f, e -> {
if(!(e.data instanceof Position to)) return;
Tmp.v1.set(e.x, e.y).interpolate(Tmp.v2.set(to), e.fin(), Interp.pow3)
.add(Tmp.v2.sub(e.x, e.y).nor().rotate90(1).scl(Mathf.randomSeedRange(e.id, 1f) * e.fslope() * 10f));
float x = Tmp.v1.x, y = Tmp.v1.y;
float size = 1f;
color(Pal.accent);
Fill.circle(x, y, e.fslope() * 3f * size);
color(e.color);
Fill.circle(x, y, e.fslope() * 1.5f * size);
}),
pointBeam = new Effect(25f, 300f, e -> {
if(!(e.data instanceof Position pos)) return;
Draw.color(e.color, e.fout());
Lines.stroke(1.5f);
Lines.line(e.x, e.y, pos.getX(), pos.getY());
Drawf.light(e.x, e.y, pos.getX(), pos.getY(), 20f, e.color, 0.6f * e.fout());
}),
pointHit = new Effect(8f, e -> {
color(Color.white, e.color, e.fin());
stroke(e.fout() + 0.2f);
Lines.circle(e.x, e.y, e.fin() * 6f);
}),
lightning = new Effect(10f, 500f, e -> {
if(!(e.data instanceof Seq)) return;
Seq<Vec2> lines = e.data();
stroke(3f * e.fout());
color(e.color, Color.white, e.fin());
for(int i = 0; i < lines.size - 1; i++){
Vec2 cur = lines.get(i);
Vec2 next = lines.get(i + 1);
Lines.line(cur.x, cur.y, next.x, next.y, false);
}
for(Vec2 p : lines){
Fill.circle(p.x, p.y, Lines.getStroke() / 2f);
}
}),
coreBuildShockwave = new Effect(120, 500f, e -> {
e.lifetime = e.rotation;
color(Pal.command);
stroke(e.fout(Interp.pow5Out) * 4f);
Lines.circle(e.x, e.y, e.fin() * e.rotation * 2f);
}),
coreBuildBlock = new Effect(80f, e -> {
if(!(e.data instanceof Block block)) return;
mixcol(Pal.accent, 1f);
alpha(e.fout());
rect(block.fullIcon, e.x, e.y);
}).layer(Layer.turret - 5f),
pointShockwave = new Effect(20, e -> {
color(e.color);
stroke(e.fout() * 2f);
Lines.circle(e.x, e.y, e.finpow() * e.rotation);
randLenVectors(e.id + 1, 8, 1f + 23f * e.finpow(), (x, y) ->
lineAngle(e.x + x, e.y + y, Mathf.angle(x, y), 1f + e.fout() * 3f));
}),
moveCommand = new Effect(20, e -> {
color(Pal.command);
stroke(e.fout() * 5f);
Lines.circle(e.x, e.y, 6f + e.fin() * 2f);
}).layer(Layer.overlayUI),
attackCommand = new Effect(20, e -> {
color(Pal.remove);
stroke(e.fout() * 5f);
poly(e.x, e.y, 4, 7f + e.fin() * 2f);
}).layer(Layer.overlayUI),
commandSend = new Effect(28, e -> {
color(Pal.command);
stroke(e.fout() * 2f);
Lines.circle(e.x, e.y, 4f + e.finpow() * e.rotation);
}),
upgradeCore = new Effect(120f, e -> {
if(!(e.data instanceof Block block)) return;
mixcol(Tmp.c1.set(Color.white).lerp(Pal.accent, e.fin()), 1f);
alpha(e.fout());
rect(block.fullIcon, e.x, e.y);
}).layer(Layer.turret - 5f),
upgradeCoreBloom = new Effect(80f, e -> {
color(Pal.accent);
stroke(4f * e.fout());
Lines.square(e.x, e.y, tilesize / 2f * e.rotation + 2f);
}),
placeBlock = new Effect(16, e -> {
color(Pal.accent);
stroke(3f - e.fin() * 2f);
Lines.square(e.x, e.y, tilesize / 2f * e.rotation + e.fin() * 3f);
}),
coreLaunchConstruct = new Effect(35, e -> {
color(Pal.accent);
stroke(4f - e.fin() * 3f);
Lines.square(e.x, e.y, tilesize / 2f * e.rotation * 1.2f + e.fin() * 5f);
randLenVectors(e.id, 5 + (int)(e.rotation * 5), e.rotation * 3f + (tilesize * e.rotation) * e.finpow() * 1.5f, (x, y) -> {
Lines.lineAngle(e.x + x, e.y + y, Mathf.angle(x, y), 1f + e.fout() * (4f + e.rotation));
});
}),
tapBlock = new Effect(12, e -> {
color(Pal.accent);
stroke(3f - e.fin() * 2f);
Lines.circle(e.x, e.y, 4f + (tilesize / 1.5f * e.rotation) * e.fin());
}),
breakBlock = new Effect(12, e -> {
color(Pal.remove);
stroke(3f - e.fin() * 2f);
Lines.square(e.x, e.y, tilesize / 2f * e.rotation + e.fin() * 3f);
randLenVectors(e.id, 3 + (int)(e.rotation * 3), e.rotation * 2f + (tilesize * e.rotation) * e.finpow(), (x, y) -> {
Fill.square(e.x + x, e.y + y, 1f + e.fout() * (3f + e.rotation));
});
}),
payloadDeposit = new Effect(30f, e -> {
if(!(e.data instanceof YeetData data)) return;
Tmp.v1.set(e.x, e.y).lerp(data.target, e.finpow());
float x = Tmp.v1.x, y = Tmp.v1.y;
scl(e.fout(Interp.pow3Out) * 1.05f);
if(data.item instanceof Block block){
Drawf.squareShadow(x, y, block.size * tilesize * 1.85f, 1f);
}else if(data.item instanceof UnitType unit){
unit.drawSoftShadow(e.x, e.y, e.rotation, 1f);
}
mixcol(Pal.accent, e.fin());
rect(data.item.fullIcon, x, y, data.item instanceof Block ? 0f : e.rotation - 90f);
}).layer(Layer.flyingUnitLow - 5f),
select = new Effect(23, e -> {
color(Pal.accent);
stroke(e.fout() * 3f);
Lines.circle(e.x, e.y, 3f + e.fin() * 14f);
}),
smoke = new Effect(100, e -> {
color(Color.gray, Pal.darkishGray, e.fin());
Fill.circle(e.x, e.y, (7f - e.fin() * 7f)/2f);
}),
fallSmoke = new Effect(110, e -> {
color(Color.gray, Color.darkGray, e.rotation);
Fill.circle(e.x, e.y, e.fout() * 3.5f);
}),
unitWreck = new Effect(200f, e -> {
if(!(e.data instanceof TextureRegion reg)) return;
Draw.mixcol(Pal.rubble, 1f);
float vel = e.fin(Interp.pow5Out) * 2f * Mathf.randomSeed(e.id, 1f);
float totalRot = Mathf.randomSeed(e.id + 1, 10f);
Tmp.v1.trns(Mathf.randomSeed(e.id + 2, 360f), vel);
Draw.z(Mathf.lerp(Layer.flyingUnitLow, Layer.debris, e.fin()));
Draw.alpha(e.fout(Interp.pow5Out));
Draw.rect(reg, e.x + Tmp.v1.x, e.y + Tmp.v1.y, e.rotation - 90 + totalRot * e.fin(Interp.pow5Out));
}),
rocketSmoke = new Effect(120, e -> {
color(Color.gray);
alpha(Mathf.clamp(e.fout()*1.6f - Interp.pow3In.apply(e.rotation)*1.2f));
Fill.circle(e.x, e.y, (1f + 6f * e.rotation) - e.fin()*2f);
}),
rocketSmokeLarge = new Effect(220, e -> {
color(Color.gray);
alpha(Mathf.clamp(e.fout()*1.6f - Interp.pow3In.apply(e.rotation)*1.2f));
Fill.circle(e.x, e.y, (1f + 6f * e.rotation * 1.3f) - e.fin()*2f);
}),
magmasmoke = new Effect(110, e -> {
color(Color.gray);
Fill.circle(e.x, e.y, e.fslope() * 6f);
}),
spawn = new Effect(30, e -> {
stroke(2f * e.fout());
color(Pal.accent);
Lines.poly(e.x, e.y, 4, 5f + e.fin() * 12f);
}),
unitAssemble = new Effect(70, e -> {
if(!(e.data instanceof UnitType type)) return;
alpha(e.fout());
mixcol(Pal.accent, e.fout());
rect(type.fullIcon, e.x, e.y, e.rotation);
}).layer(Layer.flyingUnit + 5f),
padlaunch = new Effect(10, e -> {
stroke(4f * e.fout());
color(Pal.accent);
Lines.poly(e.x, e.y, 4, 5f + e.fin() * 60f);
}),
breakProp = new Effect(23, e -> {
float scl = Math.max(e.rotation, 1);
color(Tmp.c1.set(e.color).mul(1.1f));
randLenVectors(e.id, 6, 19f * e.finpow() * scl, (x, y) -> {
Fill.circle(e.x + x, e.y + y, e.fout() * 3.5f * scl + 0.3f);
});
}).layer(Layer.debris),
unitDrop = new Effect(30, e -> {
color(Pal.lightishGray);
randLenVectors(e.id, 9, 3 + 20f * e.finpow(), (x, y) -> {
Fill.circle(e.x + x, e.y + y, e.fout() * 4f + 0.4f);
});
}).layer(Layer.debris),
unitLand = new Effect(30, e -> {
color(Tmp.c1.set(e.color).mul(1.1f));
//TODO doesn't respect rotation / size
randLenVectors(e.id, 6, 17f * e.finpow(), (x, y) -> {
Fill.circle(e.x + x, e.y + y, e.fout() * 4f + 0.3f);
});
}).layer(Layer.debris),
unitDust = new Effect(30, e -> {
color(Tmp.c1.set(e.color).mul(1.3f));
randLenVectors(e.id, 3, 8f * e.finpow(), e.rotation, 30f, (x, y) -> {
Fill.circle(e.x + x, e.y + y, e.fout() * 3f + 0.3f);
});
}).layer(Layer.debris),
unitLandSmall = new Effect(30, e -> {
color(Tmp.c1.set(e.color).mul(1.1f));
randLenVectors(e.id, (int)(6 * e.rotation), 12f * e.finpow() * e.rotation, (x, y) -> {
Fill.circle(e.x + x, e.y + y, e.fout() * 3f + 0.1f);
});
}).layer(Layer.debris),
unitPickup = new Effect(18, e -> {
color(Pal.lightishGray);
stroke(e.fin() * 2f);
Lines.poly(e.x, e.y, 4, 13f * e.fout());
}).layer(Layer.debris),
crawlDust = new Effect(35, e -> {
color(Tmp.c1.set(e.color).mul(1.6f));
randLenVectors(e.id, 2, 10f * e.finpow(), (x, y) -> {
Fill.circle(e.x + x, e.y + y, e.fslope() * 4f + 0.3f);
});
}).layer(Layer.debris),
landShock = new Effect(12, e -> {
color(Pal.lancerLaser);
stroke(e.fout() * 3f);
Lines.poly(e.x, e.y, 12, 20f * e.fout());
}).layer(Layer.debris),
pickup = new Effect(18, e -> {
color(Pal.lightishGray);
stroke(e.fout() * 2f);
Lines.spikes(e.x, e.y, 1f + e.fin() * 6f, e.fout() * 4f, 6);
}),
sparkExplosion = new Effect(30f, 160f, e -> {
color(e.color);
stroke(e.fout() * 3f);
float circleRad = 6f + e.finpow() * e.rotation;
Lines.circle(e.x, e.y, circleRad);
rand.setSeed(e.id);
for(int i = 0; i < 16; i++){
float angle = rand.random(360f);
float lenRand = rand.random(0.5f, 1f);
Lines.lineAngle(e.x, e.y, angle, e.foutpow() * e.rotation * 0.8f * rand.random(1f, 0.6f) + 2f, e.finpow() * e.rotation * 1.2f * lenRand + 6f);
}
}),
titanExplosion = new Effect(30f, 160f, e -> {
color(e.color);
stroke(e.fout() * 3f);
float circleRad = 6f + e.finpow() * 60f;
Lines.circle(e.x, e.y, circleRad);
rand.setSeed(e.id);
for(int i = 0; i < 16; i++){
float angle = rand.random(360f);
float lenRand = rand.random(0.5f, 1f);
Lines.lineAngle(e.x, e.y, angle, e.foutpow() * 50f * rand.random(1f, 0.6f) + 2f, e.finpow() * 70f * lenRand + 6f);
}
}),
titanSmoke = new Effect(300f, 300f, b -> {
float intensity = 3f;
color(b.color, 0.7f);
for(int i = 0; i < 4; i++){
rand.setSeed(b.id*2 + i);
float lenScl = rand.random(0.5f, 1f);
int fi = i;
b.scaled(b.lifetime * lenScl, e -> {
randLenVectors(e.id + fi - 1, e.fin(Interp.pow10Out), (int)(2.9f * intensity), 22f * intensity, (x, y, in, out) -> {
float fout = e.fout(Interp.pow5Out) * rand.random(0.5f, 1f);
float rad = fout * ((2f + intensity) * 2.35f);
Fill.circle(e.x + x, e.y + y, rad);
Drawf.light(e.x + x, e.y + y, rad * 2.5f, b.color, 0.5f);
});
});
}
}),
missileTrailSmoke = new Effect(180f, 300f, b -> {
float intensity = 2f;
color(b.color, 0.7f);
for(int i = 0; i < 4; i++){
rand.setSeed(b.id*2 + i);
float lenScl = rand.random(0.5f, 1f);
int fi = i;
b.scaled(b.lifetime * lenScl, e -> {
randLenVectors(e.id + fi - 1, e.fin(Interp.pow10Out), (int)(2.9f * intensity), 13f * intensity, (x, y, in, out) -> {
float fout = e.fout(Interp.pow5Out) * rand.random(0.5f, 1f);
float rad = fout * ((2f + intensity) * 2.35f);
Fill.circle(e.x + x, e.y + y, rad);
Drawf.light(e.x + x, e.y + y, rad * 2.5f, b.color, 0.5f);
});
});
}
}).layer(Layer.bullet - 1f),
neoplasmSplat = new Effect(400f, 300f, b -> {
float intensity = 3f;
color(Pal.neoplasm1);
for(int i = 0; i < 4; i++){
rand.setSeed(b.id*2 + i);
float lenScl = rand.random(0.5f, 1f);
int fi = i;
b.scaled(b.lifetime * lenScl, e -> {
randLenVectors(e.id + fi - 1, e.fin(Interp.pow10Out), (int)(5f * intensity), 22f * intensity, (x, y, in, out) -> {
float fout = e.fout(Interp.pow5Out) * rand.random(0.5f, 1f);
float rad = fout * ((2f + intensity) * 1.35f);
Fill.circle(e.x + x, e.y + y, rad);
Drawf.light(e.x + x, e.y + y, rad * 2.5f, b.color, 0.5f);
});
});
}
}).layer(Layer.bullet - 2f),
scatheExplosion = new Effect(60f, 160f, e -> {
color(e.color);
stroke(e.fout() * 5f);
float circleRad = 6f + e.finpow() * 60f;
Lines.circle(e.x, e.y, circleRad);
rand.setSeed(e.id);
for(int i = 0; i < 16; i++){
float angle = rand.random(360f);
float lenRand = rand.random(0.5f, 1f);
Tmp.v1.trns(angle, circleRad);
for(int s : Mathf.signs){
Drawf.tri(e.x + Tmp.v1.x, e.y + Tmp.v1.y, e.foutpow() * 40f, e.fout() * 30f * lenRand + 6f, angle + 90f + s * 90f);
}
}
}),
scatheLight = new Effect(60f, 160f, e -> {
float circleRad = 6f + e.finpow() * 60f;
color(e.color, e.foutpow());
Fill.circle(e.x, e.y, circleRad);
}).layer(Layer.bullet + 2f),
scatheSlash = new Effect(40f, 160f, e -> {
Draw.color(e.color);
for(int s : Mathf.signs){
Drawf.tri(e.x, e.y, e.fout() * 25f, e.foutpow() * 66f + 6f, e.rotation + s * 90f);
}
}),
dynamicSpikes = new Effect(40f, 100f, e -> {
color(e.color);
stroke(e.fout() * 2f);
float circleRad = 4f + e.finpow() * e.rotation;
Lines.circle(e.x, e.y, circleRad);
for(int i = 0; i < 4; i++){
Drawf.tri(e.x, e.y, 6f, e.rotation * 1.5f * e.fout(), i*90);
}
color();
for(int i = 0; i < 4; i++){
Drawf.tri(e.x, e.y, 3f, e.rotation * 1.45f / 3f * e.fout(), i*90);
}
Drawf.light(e.x, e.y, circleRad * 1.6f, Pal.heal, e.fout());
}),
greenBomb = new Effect(40f, 100f, e -> {
color(Pal.heal);
stroke(e.fout() * 2f);
float circleRad = 4f + e.finpow() * 65f;
Lines.circle(e.x, e.y, circleRad);
color(Pal.heal);
for(int i = 0; i < 4; i++){
Drawf.tri(e.x, e.y, 6f, 100f * e.fout(), i*90);
}
color();
for(int i = 0; i < 4; i++){
Drawf.tri(e.x, e.y, 3f, 35f * e.fout(), i*90);
}
Drawf.light(e.x, e.y, circleRad * 1.6f, Pal.heal, e.fout());
}),
greenLaserCharge = new Effect(80f, 100f, e -> {
color(Pal.heal);
stroke(e.fin() * 2f);
Lines.circle(e.x, e.y, 4f + e.fout() * 100f);
Fill.circle(e.x, e.y, e.fin() * 20);
randLenVectors(e.id, 20, 40f * e.fout(), (x, y) -> {
Fill.circle(e.x + x, e.y + y, e.fin() * 5f);
Drawf.light(e.x + x, e.y + y, e.fin() * 15f, Pal.heal, 0.7f);
});
color();
Fill.circle(e.x, e.y, e.fin() * 10);
Drawf.light(e.x, e.y, e.fin() * 20f, Pal.heal, 0.7f);
}).followParent(true).rotWithParent(true),
greenLaserChargeSmall = new Effect(40f, 100f, e -> {
color(Pal.heal);
stroke(e.fin() * 2f);
Lines.circle(e.x, e.y, e.fout() * 50f);
}).followParent(true).rotWithParent(true),
greenCloud = new Effect(80f, e -> {
color(Pal.heal);
randLenVectors(e.id, e.fin(), 7, 9f, (x, y, fin, fout) -> {
Fill.circle(e.x + x, e.y + y, 5f * fout);
});
}),
healWaveDynamic = new Effect(22, e -> {
color(Pal.heal);
stroke(e.fout() * 2f);
Lines.circle(e.x, e.y, 4f + e.finpow() * e.rotation);
}),
healWave = new Effect(22, e -> {
color(Pal.heal);
stroke(e.fout() * 2f);
Lines.circle(e.x, e.y, 4f + e.finpow() * 60f);
}),
heal = new Effect(11, e -> {
color(Pal.heal);
stroke(e.fout() * 2f);
Lines.circle(e.x, e.y, 2f + e.finpow() * 7f);
}),
dynamicWave = new Effect(22, e -> {
color(e.color, 0.7f);
stroke(e.fout() * 2f);
Lines.circle(e.x, e.y, 4f + e.finpow() * e.rotation);
}),
shieldWave = new Effect(22, e -> {
color(e.color, 0.7f);
stroke(e.fout() * 2f);
Lines.circle(e.x, e.y, 4f + e.finpow() * 60f);
}),
shieldApply = new Effect(11, e -> {
color(e.color, 0.7f);
stroke(e.fout() * 2f);
Lines.circle(e.x, e.y, 2f + e.finpow() * 7f);
}),
disperseTrail = new Effect(13, e -> {
color(Color.white, e.color, e.fin());
stroke(0.6f + e.fout() * 1.7f);
rand.setSeed(e.id);
for(int i = 0; i < 2; i++){
float rot = e.rotation + rand.range(15f) + 180f;
v.trns(rot, rand.random(e.fin() * 27f));
lineAngle(e.x + v.x, e.y + v.y, rot, e.fout() * rand.random(2f, 7f) + 1.5f);
}
}),
hitBulletSmall = new Effect(14, e -> {
color(Color.white, Pal.lightOrange, e.fin());
e.scaled(7f, s -> {
stroke(0.5f + s.fout());
Lines.circle(e.x, e.y, s.fin() * 5f);
});
stroke(0.5f + e.fout());
randLenVectors(e.id, 5, e.fin() * 15f, (x, y) -> {
float ang = Mathf.angle(x, y);
lineAngle(e.x + x, e.y + y, ang, e.fout() * 3 + 1f);
});
Drawf.light(e.x, e.y, 20f, Pal.lightOrange, 0.6f * e.fout());
}),
hitBulletColor = new Effect(14, e -> {
color(Color.white, e.color, e.fin());
e.scaled(7f, s -> {
stroke(0.5f + s.fout());
Lines.circle(e.x, e.y, s.fin() * 5f);
});
stroke(0.5f + e.fout());
randLenVectors(e.id, 5, e.fin() * 15f, (x, y) -> {
float ang = Mathf.angle(x, y);
lineAngle(e.x + x, e.y + y, ang, e.fout() * 3 + 1f);
});
Drawf.light(e.x, e.y, 20f, e.color, 0.6f * e.fout());
}),
hitSquaresColor = new Effect(14, e -> {
color(Color.white, e.color, e.fin());
e.scaled(7f, s -> {
stroke(0.5f + s.fout());
Lines.circle(e.x, e.y, s.fin() * 5f);
});
stroke(0.5f + e.fout());
randLenVectors(e.id, 5, e.fin() * 17f, (x, y) -> {
float ang = Mathf.angle(x, y);
Fill.square(e.x + x, e.y + y, e.fout() * 3.2f, ang);
});
Drawf.light(e.x, e.y, 20f, e.color, 0.6f * e.fout());
}),
hitFuse = new Effect(14, e -> {
color(Color.white, Pal.surge, e.fin());
e.scaled(7f, s -> {
stroke(0.5f + s.fout());
Lines.circle(e.x, e.y, s.fin() * 7f);
});
stroke(0.5f + e.fout());
randLenVectors(e.id, 6, e.fin() * 15f, (x, y) -> {
float ang = Mathf.angle(x, y);
lineAngle(e.x + x, e.y + y, ang, e.fout() * 3 + 1f);
});
}),
hitBulletBig = new Effect(13, e -> {
color(Color.white, Pal.lightOrange, e.fin());
stroke(0.5f + e.fout() * 1.5f);
randLenVectors(e.id, 8, e.finpow() * 30f, e.rotation, 50f, (x, y) -> {
float ang = Mathf.angle(x, y);
lineAngle(e.x + x, e.y + y, ang, e.fout() * 4 + 1.5f);
});
}),
hitFlameSmall = new Effect(14, e -> {
color(Pal.lightFlame, Pal.darkFlame, e.fin());
stroke(0.5f + e.fout());
randLenVectors(e.id, 2, 1f + e.fin() * 15f, e.rotation, 50f, (x, y) -> {
float ang = Mathf.angle(x, y);
lineAngle(e.x + x, e.y + y, ang, e.fout() * 3 + 1f);
});
}),
hitFlamePlasma = new Effect(14, e -> {
color(Color.white, Pal.heal, e.fin());
stroke(0.5f + e.fout());
randLenVectors(e.id, 2, 1f + e.fin() * 15f, e.rotation, 50f, (x, y) -> {
float ang = Mathf.angle(x, y);
lineAngle(e.x + x, e.y + y, ang, e.fout() * 3 + 1f);
});
}),
hitLiquid = new Effect(16, e -> {
color(e.color);
randLenVectors(e.id, 5, 1f + e.fin() * 15f, e.rotation, 60f, (x, y) -> {
Fill.circle(e.x + x, e.y + y, e.fout() * 2f);
});
}),
hitLaserBlast = new Effect(12, e -> {
color(e.color);
stroke(e.fout() * 1.5f);
randLenVectors(e.id, 8, e.finpow() * 17f, (x, y) -> {
float ang = Mathf.angle(x, y);
lineAngle(e.x + x, e.y + y, ang, e.fout() * 4 + 1f);
});
}),
hitEmpSpark = new Effect(40, e -> {
color(Pal.heal);
stroke(e.fout() * 1.6f);
randLenVectors(e.id, 18, e.finpow() * 27f, e.rotation, 360f, (x, y) -> {
float ang = Mathf.angle(x, y);
lineAngle(e.x + x, e.y + y, ang, e.fout() * 6 + 1f);
});
}),
hitLancer = new Effect(12, e -> {
color(Color.white);
stroke(e.fout() * 1.5f);
randLenVectors(e.id, 8, e.finpow() * 17f, (x, y) -> {
float ang = Mathf.angle(x, y);
lineAngle(e.x + x, e.y + y, ang, e.fout() * 4 + 1f);
});
}),
hitBeam = new Effect(12, e -> {
color(e.color);
stroke(e.fout() * 2f);
randLenVectors(e.id, 6, e.finpow() * 18f, (x, y) -> {
float ang = Mathf.angle(x, y);
lineAngle(e.x + x, e.y + y, ang, e.fout() * 4 + 1f);
});
}),
hitFlameBeam = new Effect(19, e -> {
color(e.color);
randLenVectors(e.id, 7, e.finpow() * 11f, (x, y) -> {
Fill.circle(e.x + x, e.y + y, e.fout() * 2 + 0.5f);
});
}),
hitMeltdown = new Effect(12, e -> {
color(Pal.meltdownHit);
stroke(e.fout() * 2f);
randLenVectors(e.id, 6, e.finpow() * 18f, (x, y) -> {
float ang = Mathf.angle(x, y);
lineAngle(e.x + x, e.y + y, ang, e.fout() * 4 + 1f);
});
}),
hitMeltHeal = new Effect(12, e -> {
color(Pal.heal);
stroke(e.fout() * 2f);
randLenVectors(e.id, 6, e.finpow() * 18f, (x, y) -> {
float ang = Mathf.angle(x, y);
lineAngle(e.x + x, e.y + y, ang, e.fout() * 4 + 1f);
});
}),
instBomb = new Effect(15f, 100f, e -> {
color(Pal.bulletYellowBack);
stroke(e.fout() * 4f);
Lines.circle(e.x, e.y, 4f + e.finpow() * 20f);
for(int i = 0; i < 4; i++){
Drawf.tri(e.x, e.y, 6f, 80f * e.fout(), i*90 + 45);
}
color();
for(int i = 0; i < 4; i++){
Drawf.tri(e.x, e.y, 3f, 30f * e.fout(), i*90 + 45);
}
Drawf.light(e.x, e.y, 150f, Pal.bulletYellowBack, 0.9f * e.fout());
}),
instTrail = new Effect(30, e -> {
for(int i = 0; i < 2; i++){
color(i == 0 ? Pal.bulletYellowBack : Pal.bulletYellow);
float m = i == 0 ? 1f : 0.5f;
float rot = e.rotation + 180f;
float w = 15f * e.fout() * m;
Drawf.tri(e.x, e.y, w, (30f + Mathf.randomSeedRange(e.id, 15f)) * m, rot);
Drawf.tri(e.x, e.y, w, 10f * m, rot + 180f);
}
Drawf.light(e.x, e.y, 60f, Pal.bulletYellowBack, 0.6f * e.fout());
}),
instShoot = new Effect(24f, e -> {
e.scaled(10f, b -> {
color(Color.white, Pal.bulletYellowBack, b.fin());
stroke(b.fout() * 3f + 0.2f);
Lines.circle(b.x, b.y, b.fin() * 50f);
});
color(Pal.bulletYellowBack);
for(int i : Mathf.signs){
Drawf.tri(e.x, e.y, 13f * e.fout(), 85f, e.rotation + 90f * i);
Drawf.tri(e.x, e.y, 13f * e.fout(), 50f, e.rotation + 20f * i);
}
Drawf.light(e.x, e.y, 180f, Pal.bulletYellowBack, 0.9f * e.fout());
}),
instHit = new Effect(20f, 200f, e -> {
color(Pal.bulletYellowBack);
for(int i = 0; i < 2; i++){
color(i == 0 ? Pal.bulletYellowBack : Pal.bulletYellow);
float m = i == 0 ? 1f : 0.5f;
for(int j = 0; j < 5; j++){
float rot = e.rotation + Mathf.randomSeedRange(e.id + j, 50f);
float w = 23f * e.fout() * m;
Drawf.tri(e.x, e.y, w, (80f + Mathf.randomSeedRange(e.id + j, 40f)) * m, rot);
Drawf.tri(e.x, e.y, w, 20f * m, rot + 180f);
}
}
e.scaled(10f, c -> {
color(Pal.bulletYellow);
stroke(c.fout() * 2f + 0.2f);
Lines.circle(e.x, e.y, c.fin() * 30f);
});
e.scaled(12f, c -> {
color(Pal.bulletYellowBack);
randLenVectors(e.id, 25, 5f + e.fin() * 80f, e.rotation, 60f, (x, y) -> {
Fill.square(e.x + x, e.y + y, c.fout() * 3f, 45f);
});
});
}),
hitLaser = new Effect(8, e -> {
color(Color.white, Pal.heal, e.fin());
stroke(0.5f + e.fout());
Lines.circle(e.x, e.y, e.fin() * 5f);
Drawf.light(e.x, e.y, 23f, Pal.heal, e.fout() * 0.7f);
}),
hitLaserColor = new Effect(8, e -> {
color(Color.white, e.color, e.fin());
stroke(0.5f + e.fout());
Lines.circle(e.x, e.y, e.fin() * 5f);
Drawf.light(e.x, e.y, 23f, e.color, e.fout() * 0.7f);
}),
despawn = new Effect(12, e -> {
color(Pal.lighterOrange, Color.gray, e.fin());
stroke(e.fout());
randLenVectors(e.id, 7, e.fin() * 7f, e.rotation, 40f, (x, y) -> {
float ang = Mathf.angle(x, y);
lineAngle(e.x + x, e.y + y, ang, e.fout() * 2 + 1f);
});
}),
airBubble = new Effect(100f, e -> {
randLenVectors(e.id, 1, e.fin() * 12f, (x, y) -> {
rect(renderer.bubbles[Math.min((int)(renderer.bubbles.length * Mathf.curveMargin(e.fin(), 0.11f, 0.06f)), renderer.bubbles.length - 1)], e.x + x, e.y + y);
});
}).layer(Layer.flyingUnitLow + 1),
flakExplosion = new Effect(20, e -> {
color(Pal.bulletYellow);
e.scaled(6, i -> {
stroke(3f * i.fout());
Lines.circle(e.x, e.y, 3f + i.fin() * 10f);
});
color(Color.gray);
randLenVectors(e.id, 5, 2f + 23f * e.finpow(), (x, y) -> {
Fill.circle(e.x + x, e.y + y, e.fout() * 3f + 0.5f);
});
color(Pal.lighterOrange);
stroke(e.fout());
randLenVectors(e.id + 1, 4, 1f + 23f * e.finpow(), (x, y) -> {
lineAngle(e.x + x, e.y + y, Mathf.angle(x, y), 1f + e.fout() * 3f);
});
Drawf.light(e.x, e.y, 50f, Pal.lighterOrange, 0.8f * e.fout());
}),
plasticExplosion = new Effect(24, e -> {
color(Pal.plastaniumFront);
e.scaled(7, i -> {
stroke(3f * i.fout());
Lines.circle(e.x, e.y, 3f + i.fin() * 24f);
});
color(Color.gray);
randLenVectors(e.id, 7, 2f + 28f * e.finpow(), (x, y) -> {
Fill.circle(e.x + x, e.y + y, e.fout() * 4f + 0.5f);
});
color(Pal.plastaniumBack);
stroke(e.fout());
randLenVectors(e.id + 1, 4, 1f + 25f * e.finpow(), (x, y) -> {
lineAngle(e.x + x, e.y + y, Mathf.angle(x, y), 1f + e.fout() * 3f);
});
Drawf.light(e.x, e.y, 50f, Pal.plastaniumBack, 0.8f * e.fout());
}),
plasticExplosionFlak = new Effect(28, e -> {
color(Pal.plastaniumFront);
e.scaled(7, i -> {
stroke(3f * i.fout());
Lines.circle(e.x, e.y, 3f + i.fin() * 34f);
});
color(Color.gray);
randLenVectors(e.id, 7, 2f + 30f * e.finpow(), (x, y) -> {
Fill.circle(e.x + x, e.y + y, e.fout() * 4f + 0.5f);
});
color(Pal.plastaniumBack);
stroke(e.fout());
randLenVectors(e.id + 1, 4, 1f + 30f * e.finpow(), (x, y) -> {
lineAngle(e.x + x, e.y + y, Mathf.angle(x, y), 1f + e.fout() * 3f);
});
}),
blastExplosion = new Effect(22, e -> {
color(Pal.missileYellow);
e.scaled(6, i -> {
stroke(3f * i.fout());
Lines.circle(e.x, e.y, 3f + i.fin() * 15f);
});
color(Color.gray);
randLenVectors(e.id, 5, 2f + 23f * e.finpow(), (x, y) -> {
Fill.circle(e.x + x, e.y + y, e.fout() * 4f + 0.5f);
});
color(Pal.missileYellowBack);
stroke(e.fout());
randLenVectors(e.id + 1, 4, 1f + 23f * e.finpow(), (x, y) -> {
lineAngle(e.x + x, e.y + y, Mathf.angle(x, y), 1f + e.fout() * 3f);
});
Drawf.light(e.x, e.y, 45f, Pal.missileYellowBack, 0.8f * e.fout());
}),
sapExplosion = new Effect(25, e -> {
color(Pal.sapBullet);
e.scaled(6, i -> {
stroke(3f * i.fout());
Lines.circle(e.x, e.y, 3f + i.fin() * 80f);
});
color(Color.gray);
randLenVectors(e.id, 9, 2f + 70 * e.finpow(), (x, y) -> {
Fill.circle(e.x + x, e.y + y, e.fout() * 4f + 0.5f);
});
color(Pal.sapBulletBack);
stroke(e.fout());
randLenVectors(e.id + 1, 8, 1f + 60f * e.finpow(), (x, y) -> {
lineAngle(e.x + x, e.y + y, Mathf.angle(x, y), 1f + e.fout() * 3f);
});
Drawf.light(e.x, e.y, 90f, Pal.sapBulletBack, 0.8f * e.fout());
}),
massiveExplosion = new Effect(30, e -> {
color(Pal.missileYellow);
e.scaled(7, i -> {
stroke(3f * i.fout());
Lines.circle(e.x, e.y, 4f + i.fin() * 30f);
});
color(Color.gray);
randLenVectors(e.id, 8, 2f + 30f * e.finpow(), (x, y) -> {
Fill.circle(e.x + x, e.y + y, e.fout() * 4f + 0.5f);
});
color(Pal.missileYellowBack);
stroke(e.fout());
randLenVectors(e.id + 1, 6, 1f + 29f * e.finpow(), (x, y) -> {
lineAngle(e.x + x, e.y + y, Mathf.angle(x, y), 1f + e.fout() * 4f);
});
Drawf.light(e.x, e.y, 50f, Pal.missileYellowBack, 0.8f * e.fout());
}),
artilleryTrail = new Effect(50, e -> {
color(e.color);
Fill.circle(e.x, e.y, e.rotation * e.fout());
}),
incendTrail = new Effect(50, e -> {
color(Pal.lightOrange);
Fill.circle(e.x, e.y, e.rotation * e.fout());
}),
missileTrail = new Effect(50, e -> {
color(e.color);
Fill.circle(e.x, e.y, e.rotation * e.fout());
}).layer(Layer.bullet - 0.001f), //below bullets
missileTrailShort = new Effect(22, e -> {
color(e.color);
Fill.circle(e.x, e.y, e.rotation * e.fout());
}).layer(Layer.bullet - 0.001f),
colorTrail = new Effect(50, e -> {
color(e.color);
Fill.circle(e.x, e.y, e.rotation * e.fout());
}),
absorb = new Effect(12, e -> {
color(Pal.accent);
stroke(2f * e.fout());
Lines.circle(e.x, e.y, 5f * e.fout());
}),
forceShrink = new Effect(20, e -> {
color(e.color, e.fout());
if(renderer.animateShields){
Fill.poly(e.x, e.y, 6, e.rotation * e.fout());
}else{
stroke(1.5f);
Draw.alpha(0.09f);
Fill.poly(e.x, e.y, 6, e.rotation * e.fout());
Draw.alpha(1f);
Lines.poly(e.x, e.y, 6, e.rotation * e.fout());
}
}).layer(Layer.shields),
flakExplosionBig = new Effect(30, e -> {
color(Pal.bulletYellowBack);
e.scaled(6, i -> {
stroke(3f * i.fout());
Lines.circle(e.x, e.y, 3f + i.fin() * 25f);
});
color(Color.gray);
randLenVectors(e.id, 6, 2f + 23f * e.finpow(), (x, y) -> {
Fill.circle(e.x + x, e.y + y, e.fout() * 4f + 0.5f);
});
color(Pal.bulletYellow);
stroke(e.fout());
randLenVectors(e.id + 1, 4, 1f + 23f * e.finpow(), (x, y) -> {
lineAngle(e.x + x, e.y + y, Mathf.angle(x, y), 1f + e.fout() * 3f);
});
Drawf.light(e.x, e.y, 60f, Pal.bulletYellowBack, 0.7f * e.fout());
}),
burning = new Effect(35f, e -> {
color(Pal.lightFlame, Pal.darkFlame, e.fin());
randLenVectors(e.id, 3, 2f + e.fin() * 7f, (x, y) -> {
Fill.circle(e.x + x, e.y + y, 0.1f + e.fout() * 1.4f);
});
}),
fireRemove = new Effect(70f, e -> {
if(Fire.regions[0] == null) return;
alpha(e.fout());
rect(Fire.regions[((int)(e.rotation + e.fin() * Fire.frames)) % Fire.frames], e.x + Mathf.randomSeedRange((int)e.y, 2), e.y + Mathf.randomSeedRange((int)e.x, 2));
Drawf.light(e.x, e.y, 50f + Mathf.absin(5f, 5f), Pal.lightFlame, 0.6f * e.fout());
}),
fire = new Effect(50f, e -> {
color(Pal.lightFlame, Pal.darkFlame, e.fin());
randLenVectors(e.id, 2, 2f + e.fin() * 9f, (x, y) -> {
Fill.circle(e.x + x, e.y + y, 0.2f + e.fslope() * 1.5f);
});
color();
Drawf.light(e.x, e.y, 20f * e.fslope(), Pal.lightFlame, 0.5f);
}),
fireHit = new Effect(35f, e -> {
color(Pal.lightFlame, Pal.darkFlame, e.fin());
randLenVectors(e.id, 3, 2f + e.fin() * 10f, (x, y) -> {
Fill.circle(e.x + x, e.y + y, 0.2f + e.fout() * 1.6f);
});
color();
}),
fireSmoke = new Effect(35f, e -> {
color(Color.gray);
randLenVectors(e.id, 1, 2f + e.fin() * 7f, (x, y) -> {
Fill.circle(e.x + x, e.y + y, 0.2f + e.fslope() * 1.5f);
});
}),
//TODO needs a lot of work
neoplasmHeal = new Effect(120f, e -> {
color(Pal.neoplasm1, Pal.neoplasm2, e.fin());
randLenVectors(e.id, 1, e.fin() * 3f, (x, y) -> {
Fill.circle(e.x + x, e.y + y, 0.2f + e.fslope() * 2f);
});
}).followParent(true).rotWithParent(true).layer(Layer.bullet - 2),
steam = new Effect(35f, e -> {
color(Color.lightGray);
randLenVectors(e.id, 2, 2f + e.fin() * 7f, (x, y) -> {
Fill.circle(e.x + x, e.y + y, 0.2f + e.fslope() * 1.5f);
});
}),
ventSteam = new Effect(140f, e -> {
color(e.color, Pal.vent2, e.fin());
alpha(e.fslope() * 0.78f);
float length = 3f + e.finpow() * 10f;
rand.setSeed(e.id);
for(int i = 0; i < rand.random(3, 5); i++){
v.trns(rand.random(360f), rand.random(length));
Fill.circle(e.x + v.x, e.y + v.y, rand.random(1.2f, 3.5f) + e.fslope() * 1.1f);
}
}).layer(Layer.darkness - 1),
drillSteam = new Effect(220f, e -> {
float length = 3f + e.finpow() * 20f;
rand.setSeed(e.id);
for(int i = 0; i < 13; i++){
v.trns(rand.random(360f), rand.random(length));
float sizer = rand.random(1.3f, 3.7f);
e.scaled(e.lifetime * rand.random(0.5f, 1f), b -> {
color(Color.gray, b.fslope() * 0.93f);
Fill.circle(e.x + v.x, e.y + v.y, sizer + b.fslope() * 1.2f);
});
}
}).startDelay(30f),
fluxVapor = new Effect(140f, e -> {
color(e.color);
alpha(e.fout() * 0.7f);
randLenVectors(e.id, 2, 3f + e.finpow() * 10f, (x, y) -> {
Fill.circle(e.x + x, e.y + y, 0.6f + e.fin() * 5f);
});
}).layer(Layer.bullet - 1f),
vapor = new Effect(110f, e -> {
color(e.color);
alpha(e.fout());
randLenVectors(e.id, 3, 2f + e.finpow() * 11f, (x, y) -> {
Fill.circle(e.x + x, e.y + y, 0.6f + e.fin() * 5f);
});
}),
vaporSmall = new Effect(50f, e -> {
color(e.color);
alpha(e.fout());
randLenVectors(e.id, 4, 2f + e.finpow() * 5f, (x, y) -> {
Fill.circle(e.x + x, e.y + y, 1f + e.fin() * 4f);
});
}),
fireballsmoke = new Effect(25f, e -> {
color(Color.gray);
randLenVectors(e.id, 1, 2f + e.fin() * 7f, (x, y) -> {
Fill.circle(e.x + x, e.y + y, 0.2f + e.fout() * 1.5f);
});
}),
ballfire = new Effect(25f, e -> {
color(Pal.lightFlame, Pal.darkFlame, e.fin());
randLenVectors(e.id, 2, 2f + e.fin() * 7f, (x, y) -> {
Fill.circle(e.x + x, e.y + y, 0.2f + e.fout() * 1.5f);
});
}),
freezing = new Effect(40f, e -> {
color(Liquids.cryofluid.color);
randLenVectors(e.id, 2, 1f + e.fin() * 2f, (x, y) -> {
Fill.circle(e.x + x, e.y + y, e.fout() * 1.2f);
});
}),
melting = new Effect(40f, e -> {
color(Liquids.slag.color, Color.white, e.fout() / 5f + Mathf.randomSeedRange(e.id, 0.12f));
randLenVectors(e.id, 2, 1f + e.fin() * 3f, (x, y) -> {
Fill.circle(e.x + x, e.y + y, .2f + e.fout() * 1.2f);
});
}),
wet = new Effect(80f, e -> {
color(Liquids.water.color);
alpha(Mathf.clamp(e.fin() * 2f));
Fill.circle(e.x, e.y, e.fout());
}),
muddy = new Effect(80f, e -> {
color(Pal.muddy);
alpha(Mathf.clamp(e.fin() * 2f));
Fill.circle(e.x, e.y, e.fout());
}),
sapped = new Effect(40f, e -> {
color(Pal.sap);
randLenVectors(e.id, 2, 1f + e.fin() * 2f, (x, y) -> {
Fill.square(e.x + x, e.y + y, e.fslope() * 1.1f, 45f);
});
}),
electrified = new Effect(40f, e -> {
color(Pal.heal);
randLenVectors(e.id, 2, 1f + e.fin() * 2f, (x, y) -> {
Fill.square(e.x + x, e.y + y, e.fslope() * 1.1f, 45f);
});
}),
sporeSlowed = new Effect(40f, e -> {
color(Pal.spore);
Fill.circle(e.x, e.y, e.fslope() * 1.1f);
}),
oily = new Effect(42f, e -> {
color(Liquids.oil.color);
randLenVectors(e.id, 2, 1f + e.fin() * 2f, (x, y) -> {
Fill.circle(e.x + x, e.y + y, e.fout());
});
}),
overdriven = new Effect(20f, e -> {
color(e.color);
randLenVectors(e.id, 2, 1f + e.fin() * 2f, (x, y) -> {
Fill.square(e.x + x, e.y + y, e.fout() * 2.3f + 0.5f);
});
}),
overclocked = new Effect(50f, e -> {
color(e.color);
Fill.square(e.x, e.y, e.fslope() * 2f, 45f);
}),
dropItem = new Effect(20f, e -> {
float length = 20f * e.finpow();
float size = 7f * e.fout();
if(!(e.data instanceof Item item)) return;
rect(item.fullIcon, e.x + trnsx(e.rotation, length), e.y + trnsy(e.rotation, length), size, size);
}),
shockwave = new Effect(10f, 80f, e -> {
color(Color.white, Color.lightGray, e.fin());
stroke(e.fout() * 2f + 0.2f);
Lines.circle(e.x, e.y, e.fin() * 28f);
}),
bigShockwave = new Effect(10f, 80f, e -> {
color(Color.white, Color.lightGray, e.fin());
stroke(e.fout() * 3f);
Lines.circle(e.x, e.y, e.fin() * 50f);
}),
spawnShockwave = new Effect(20f, 400f, e -> {
color(Color.white, Color.lightGray, e.fin());
stroke(e.fout() * 3f + 0.5f);
Lines.circle(e.x, e.y, e.fin() * (e.rotation + 50f));
}),
explosion = new Effect(30, e -> {
e.scaled(7, i -> {
stroke(3f * i.fout());
Lines.circle(e.x, e.y, 3f + i.fin() * 10f);
});
color(Color.gray);
randLenVectors(e.id, 6, 2f + 19f * e.finpow(), (x, y) -> {
Fill.circle(e.x + x, e.y + y, e.fout() * 3f + 0.5f);
Fill.circle(e.x + x / 2f, e.y + y / 2f, e.fout());
});
color(Pal.lighterOrange, Pal.lightOrange, Color.gray, e.fin());
stroke(1.5f * e.fout());
randLenVectors(e.id + 1, 8, 1f + 23f * e.finpow(), (x, y) -> {
lineAngle(e.x + x, e.y + y, Mathf.angle(x, y), 1f + e.fout() * 3f);
});
}),
dynamicExplosion = new Effect(30, 500f, b -> {
float intensity = b.rotation;
float baseLifetime = 26f + intensity * 15f;
b.lifetime = 43f + intensity * 35f;
color(Color.gray);
//TODO awful borders with linear filtering here
alpha(0.9f);
for(int i = 0; i < 4; i++){
rand.setSeed(b.id*2 + i);
float lenScl = rand.random(0.4f, 1f);
int fi = i;
b.scaled(b.lifetime * lenScl, e -> {
randLenVectors(e.id + fi - 1, e.fin(Interp.pow10Out), (int)(3f * intensity), 14f * intensity, (x, y, in, out) -> {
float fout = e.fout(Interp.pow5Out) * rand.random(0.5f, 1f);
Fill.circle(e.x + x, e.y + y, fout * ((2f + intensity) * 1.8f));
});
});
}
b.scaled(baseLifetime, e -> {
e.scaled(5 + intensity * 2.5f, i -> {
stroke((3.1f + intensity/5f) * i.fout());
Lines.circle(e.x, e.y, (3f + i.fin() * 14f) * intensity);
Drawf.light(e.x, e.y, i.fin() * 14f * 2f * intensity, Color.white, 0.9f * e.fout());
});
color(Pal.lighterOrange, Pal.lightOrange, Color.gray, e.fin());
stroke((1.7f * e.fout()) * (1f + (intensity - 1f) / 2f));
Draw.z(Layer.effect + 0.001f);
randLenVectors(e.id + 1, e.finpow() + 0.001f, (int)(9 * intensity), 40f * intensity, (x, y, in, out) -> {
lineAngle(e.x + x, e.y + y, Mathf.angle(x, y), 1f + out * 4 * (3f + intensity));
Drawf.light(e.x + x, e.y + y, (out * 4 * (3f + intensity)) * 3.5f, Draw.getColor(), 0.8f);
});
});
}),
reactorExplosion = new Effect(30, 500f, b -> {
float intensity = 6.8f;
float baseLifetime = 25f + intensity * 11f;
b.lifetime = 50f + intensity * 65f;
color(Pal.reactorPurple2);
alpha(0.7f);
for(int i = 0; i < 4; i++){
rand.setSeed(b.id*2 + i);
float lenScl = rand.random(0.4f, 1f);
int fi = i;
b.scaled(b.lifetime * lenScl, e -> {
randLenVectors(e.id + fi - 1, e.fin(Interp.pow10Out), (int)(2.9f * intensity), 22f * intensity, (x, y, in, out) -> {
float fout = e.fout(Interp.pow5Out) * rand.random(0.5f, 1f);
float rad = fout * ((2f + intensity) * 2.35f);
Fill.circle(e.x + x, e.y + y, rad);
Drawf.light(e.x + x, e.y + y, rad * 2.5f, Pal.reactorPurple, 0.5f);
});
});
}
b.scaled(baseLifetime, e -> {
Draw.color();
e.scaled(5 + intensity * 2f, i -> {
stroke((3.1f + intensity/5f) * i.fout());
Lines.circle(e.x, e.y, (3f + i.fin() * 14f) * intensity);
Drawf.light(e.x, e.y, i.fin() * 14f * 2f * intensity, Color.white, 0.9f * e.fout());
});
color(Pal.lighterOrange, Pal.reactorPurple, e.fin());
stroke((2f * e.fout()));
Draw.z(Layer.effect + 0.001f);
randLenVectors(e.id + 1, e.finpow() + 0.001f, (int)(8 * intensity), 28f * intensity, (x, y, in, out) -> {
lineAngle(e.x + x, e.y + y, Mathf.angle(x, y), 1f + out * 4 * (4f + intensity));
Drawf.light(e.x + x, e.y + y, (out * 4 * (3f + intensity)) * 3.5f, Draw.getColor(), 0.8f);
});
});
}),
impactReactorExplosion = new Effect(30, 500f, b -> {
float intensity = 8f;
float baseLifetime = 25f + intensity * 15f;
b.lifetime = 50f + intensity * 64f;
color(Pal.lighterOrange);
alpha(0.8f);
for(int i = 0; i < 5; i++){
rand.setSeed(b.id*2 + i);
float lenScl = rand.random(0.25f, 1f);
int fi = i;
b.scaled(b.lifetime * lenScl, e -> {
randLenVectors(e.id + fi - 1, e.fin(Interp.pow10Out), (int)(2.8f * intensity), 25f * intensity, (x, y, in, out) -> {
float fout = e.fout(Interp.pow5Out) * rand.random(0.5f, 1f);
float rad = fout * ((2f + intensity) * 2.35f);
Fill.circle(e.x + x, e.y + y, rad);
Drawf.light(e.x + x, e.y + y, rad * 2.6f, Pal.lighterOrange, 0.7f);
});
});
}
b.scaled(baseLifetime, e -> {
Draw.color();
e.scaled(5 + intensity * 2f, i -> {
stroke((3.1f + intensity/5f) * i.fout());
Lines.circle(e.x, e.y, (3f + i.fin() * 14f) * intensity);
Drawf.light(e.x, e.y, i.fin() * 14f * 2f * intensity, Color.white, 0.9f * e.fout());
});
color(Color.white, Pal.lighterOrange, e.fin());
stroke((2f * e.fout()));
Draw.z(Layer.effect + 0.001f);
randLenVectors(e.id + 1, e.finpow() + 0.001f, (int)(8 * intensity), 30f * intensity, (x, y, in, out) -> {
lineAngle(e.x + x, e.y + y, Mathf.angle(x, y), 1f + out * 4 * (4f + intensity));
Drawf.light(e.x + x, e.y + y, (out * 4 * (3f + intensity)) * 3.5f, Draw.getColor(), 0.8f);
});
});
}),
blockExplosionSmoke = new Effect(30, e -> {
color(Color.gray);
randLenVectors(e.id, 6, 4f + 30f * e.finpow(), (x, y) -> {
Fill.circle(e.x + x, e.y + y, e.fout() * 3f);
Fill.circle(e.x + x / 2f, e.y + y / 2f, e.fout());
});
}),
smokePuff = new Effect(30, e -> {
color(e.color);
randLenVectors(e.id, 6, 4f + 30f * e.finpow(), (x, y) -> {
Fill.circle(e.x + x, e.y + y, e.fout() * 3f);
Fill.circle(e.x + x / 2f, e.y + y / 2f, e.fout());
});
}),
shootSmall = new Effect(8, e -> {
color(Pal.lighterOrange, Pal.lightOrange, e.fin());
float w = 1f + 5 * e.fout();
Drawf.tri(e.x, e.y, w, 15f * e.fout(), e.rotation);
Drawf.tri(e.x, e.y, w, 3f * e.fout(), e.rotation + 180f);
}),
shootSmallColor = new Effect(8, e -> {
color(e.color, Color.gray, e.fin());
float w = 1f + 5 * e.fout();
Drawf.tri(e.x, e.y, w, 15f * e.fout(), e.rotation);
Drawf.tri(e.x, e.y, w, 3f * e.fout(), e.rotation + 180f);
}),
shootHeal = new Effect(8, e -> {
color(Pal.heal);
float w = 1f + 5 * e.fout();
Drawf.tri(e.x, e.y, w, 17f * e.fout(), e.rotation);
Drawf.tri(e.x, e.y, w, 4f * e.fout(), e.rotation + 180f);
}),
shootHealYellow = new Effect(8, e -> {
color(Pal.lightTrail);
float w = 1f + 5 * e.fout();
Drawf.tri(e.x, e.y, w, 17f * e.fout(), e.rotation);
Drawf.tri(e.x, e.y, w, 4f * e.fout(), e.rotation + 180f);
}),
shootSmallSmoke = new Effect(20f, e -> {
color(Pal.lighterOrange, Color.lightGray, Color.gray, e.fin());
randLenVectors(e.id, 5, e.finpow() * 6f, e.rotation, 20f, (x, y) -> {
Fill.circle(e.x + x, e.y + y, e.fout() * 1.5f);
});
}),
shootBig = new Effect(9, e -> {
color(Pal.lighterOrange, Pal.lightOrange, e.fin());
float w = 1.2f + 7 * e.fout();
Drawf.tri(e.x, e.y, w, 25f * e.fout(), e.rotation);
Drawf.tri(e.x, e.y, w, 4f * e.fout(), e.rotation + 180f);
}),
shootBig2 = new Effect(10, e -> {
color(Pal.lightOrange, Color.gray, e.fin());
float w = 1.2f + 8 * e.fout();
Drawf.tri(e.x, e.y, w, 29f * e.fout(), e.rotation);
Drawf.tri(e.x, e.y, w, 5f * e.fout(), e.rotation + 180f);
}),
shootBigColor = new Effect(11, e -> {
color(e.color, Color.gray, e.fin());
float w = 1.2f +9 * e.fout();
Drawf.tri(e.x, e.y, w, 32f * e.fout(), e.rotation);
Drawf.tri(e.x, e.y, w, 3f * e.fout(), e.rotation + 180f);
}),
shootTitan = new Effect(10, e -> {
color(Pal.lightOrange, e.color, e.fin());
float w = 1.3f + 10 * e.fout();
Drawf.tri(e.x, e.y, w, 35f * e.fout(), e.rotation);
Drawf.tri(e.x, e.y, w, 6f * e.fout(), e.rotation + 180f);
}),
shootBigSmoke = new Effect(17f, e -> {
color(Pal.lighterOrange, Color.lightGray, Color.gray, e.fin());
randLenVectors(e.id, 8, e.finpow() * 19f, e.rotation, 10f, (x, y) -> {
Fill.circle(e.x + x, e.y + y, e.fout() * 2f + 0.2f);
});
}),
shootBigSmoke2 = new Effect(18f, e -> {
color(Pal.lightOrange, Color.lightGray, Color.gray, e.fin());
randLenVectors(e.id, 9, e.finpow() * 23f, e.rotation, 20f, (x, y) -> {
Fill.circle(e.x + x, e.y + y, e.fout() * 2.4f + 0.2f);
});
}),
shootSmokeDisperse = new Effect(25f, e -> {
color(Pal.lightOrange, Color.white, Color.gray, e.fin());
randLenVectors(e.id, 9, e.finpow() * 29f, e.rotation, 18f, (x, y) -> {
Fill.circle(e.x + x, e.y + y, e.fout() * 2.2f + 0.1f);
});
}),
shootSmokeSquare = new Effect(20f, e -> {
color(Color.white, e.color, e.fin());
rand.setSeed(e.id);
for(int i = 0; i < 6; i++){
float rot = e.rotation + rand.range(22f);
v.trns(rot, rand.random(e.finpow() * 21f));
Fill.poly(e.x + v.x, e.y + v.y, 4, e.fout() * 2f + 0.2f, rand.random(360f));
}
}),
shootSmokeSquareSparse = new Effect(30f, e -> {
color(Color.white, e.color, e.fin());
rand.setSeed(e.id);
for(int i = 0; i < 2; i++){
float rot = e.rotation + rand.range(30f);
v.trns(rot, rand.random(e.finpow() * 27f));
Fill.poly(e.x + v.x, e.y + v.y, 4, e.fout() * 3.8f + 0.2f, rand.random(360f));
}
}),
shootSmokeSquareBig = new Effect(32f, e -> {
color(Color.white, e.color, e.fin());
rand.setSeed(e.id);
for(int i = 0; i < 13; i++){
float rot = e.rotation + rand.range(26f);
v.trns(rot, rand.random(e.finpow() * 30f));
Fill.poly(e.x + v.x, e.y + v.y, 4, e.fout() * 4f + 0.2f, rand.random(360f));
}
}),
shootSmokeTitan = new Effect(70f, e -> {
rand.setSeed(e.id);
for(int i = 0; i < 13; i++){
v.trns(e.rotation + rand.range(30f), rand.random(e.finpow() * 40f));
e.scaled(e.lifetime * rand.random(0.3f, 1f), b -> {
color(e.color, Pal.lightishGray, b.fin());
Fill.circle(e.x + v.x, e.y + v.y, b.fout() * 3.4f + 0.3f);
});
}
}),
shootSmokeSmite = new Effect(70f, e -> {
rand.setSeed(e.id);
for(int i = 0; i < 13; i++){
float a = e.rotation + rand.range(30f);
v.trns(a, rand.random(e.finpow() * 50f));
e.scaled(e.lifetime * rand.random(0.3f, 1f), b -> {
color(e.color);
Lines.stroke(b.fout() * 3f + 0.5f);
Lines.lineAngle(e.x + v.x, e.y + v.y, a, b.fout() * 8f + 0.4f);
});
}
}),
shootSmokeMissile = new Effect(130f, 300f, e -> {
color(Pal.redLight);
alpha(0.5f);
rand.setSeed(e.id);
for(int i = 0; i < 35; i++){
v.trns(e.rotation + 180f + rand.range(21f), rand.random(e.finpow() * 90f)).add(rand.range(3f), rand.range(3f));
e.scaled(e.lifetime * rand.random(0.2f, 1f), b -> {
Fill.circle(e.x + v.x, e.y + v.y, b.fout() * 9f + 0.3f);
});
}
}),
regenParticle = new Effect(100f, e -> {
color(Pal.regen);
Fill.square(e.x, e.y, e.fslope() * 1.5f + 0.14f, 45f);
}),
regenSuppressParticle = new Effect(35f, e -> {
color(e.color, Color.white, e.fin());
stroke(e.fout() * 1.4f + 0.5f);
randLenVectors(e.id, 4, 17f * e.fin(), (x, y) -> {
lineAngle(e.x + x, e.y + y, Mathf.angle(x, y), e.fslope() * 3f + 0.5f);
});
}),
regenSuppressSeek = new Effect(140f, e -> {
e.lifetime = Mathf.randomSeed(e.id, 120f, 200f);
if(!(e.data instanceof Position to)) return;
Tmp.v2.set(to).sub(e.x, e.y).nor().rotate90(1).scl(Mathf.randomSeedRange(e.id, 1f) * 50f);
Tmp.bz2.set(Tmp.v1.set(e.x, e.y), Tmp.v2.add(e.x, e.y), Tmp.v3.set(to));
Tmp.bz2.valueAt(Tmp.v4, e.fout());
color(e.color);
Fill.circle(Tmp.v4.x, Tmp.v4.y, e.fslope() * 2f + 0.1f);
}).followParent(false).rotWithParent(false),
surgeCruciSmoke = new Effect(160f, e -> {
color(Pal.slagOrange);
alpha(0.6f);
rand.setSeed(e.id);
for(int i = 0; i < 3; i++){
float len = rand.random(6f), rot = rand.range(40f) + e.rotation;
e.scaled(e.lifetime * rand.random(0.3f, 1f), b -> {
v.trns(rot, len * b.finpow());
Fill.circle(e.x + v.x, e.y + v.y, 2f * b.fslope() + 0.2f);
});
}
}),
neoplasiaSmoke = new Effect(280f, e -> {
color(Pal.neoplasmMid);
alpha(0.6f);
rand.setSeed(e.id);
for(int i = 0; i < 6; i++){
float len = rand.random(10f), rot = rand.range(120f) + e.rotation;
e.scaled(e.lifetime * rand.random(0.3f, 1f), b -> {
v.trns(rot, len * b.finpow());
Fill.circle(e.x + v.x, e.y + v.y, 3.3f * b.fslope() + 0.2f);
});
}
}),
heatReactorSmoke = new Effect(180f, e -> {
color(Color.gray);
rand.setSeed(e.id);
for(int i = 0; i < 5; i++){
float len = rand.random(6f), rot = rand.range(50f) + e.rotation;
e.scaled(e.lifetime * rand.random(0.3f, 1f), b -> {
alpha(0.9f * b.fout());
v.trns(rot, len * b.finpow());
Fill.circle(e.x + v.x, e.y + v.y, 2.4f * b.fin() + 0.6f);
});
}
}),
circleColorSpark = new Effect(21f, e -> {
color(Color.white, e.color, e.fin());
stroke(e.fout() * 1.1f + 0.5f);
randLenVectors(e.id, 9, 27f * e.fin(), 9f, (x, y) -> {
lineAngle(e.x + x, e.y + y, Mathf.angle(x, y), e.fslope() * 5f + 0.5f);
});
}),
colorSpark = new Effect(21f, e -> {
color(Color.white, e.color, e.fin());
stroke(e.fout() * 1.1f + 0.5f);
randLenVectors(e.id, 5, 27f * e.fin(), e.rotation, 9f, (x, y) -> {
lineAngle(e.x + x, e.y + y, Mathf.angle(x, y), e.fslope() * 5f + 0.5f);
});
}),
colorSparkBig = new Effect(25f, e -> {
color(Color.white, e.color, e.fin());
stroke(e.fout() * 1.3f + 0.7f);
randLenVectors(e.id, 8, 41f * e.fin(), e.rotation, 10f, (x, y) -> {
lineAngle(e.x + x, e.y + y, Mathf.angle(x, y), e.fslope() * 6f + 0.5f);
});
}),
randLifeSpark = new Effect(24f, e -> {
color(Color.white, e.color, e.fin());
stroke(e.fout() * 1.5f + 0.5f);
rand.setSeed(e.id);
for(int i = 0; i < 15; i++){
float ang = e.rotation + rand.range(9f), len = rand.random(90f * e.finpow());
e.scaled(e.lifetime * rand.random(0.5f, 1f), p -> {
v.trns(ang, len);
lineAngle(e.x + v.x, e.y + v.y, ang, p.fout() * 7f + 0.5f);
});
}
}),
shootPayloadDriver = new Effect(30f, e -> {
color(Pal.accent);
Lines.stroke(0.5f + 0.5f*e.fout());
float spread = 9f;
rand.setSeed(e.id);
for(int i = 0; i < 20; i++){
float ang = e.rotation + rand.range(17f);
v.trns(ang, rand.random(e.fin() * 55f));
Lines.lineAngle(e.x + v.x + rand.range(spread), e.y + v.y + rand.range(spread), ang, e.fout() * 5f * rand.random(1f) + 1f);
}
}),
shootSmallFlame = new Effect(32f, 80f, e -> {
color(Pal.lightFlame, Pal.darkFlame, Color.gray, e.fin());
randLenVectors(e.id, 8, e.finpow() * 60f, e.rotation, 10f, (x, y) -> {
Fill.circle(e.x + x, e.y + y, 0.65f + e.fout() * 1.5f);
});
}),
shootPyraFlame = new Effect(33f, 80f, e -> {
color(Pal.lightPyraFlame, Pal.darkPyraFlame, Color.gray, e.fin());
randLenVectors(e.id, 10, e.finpow() * 70f, e.rotation, 10f, (x, y) -> {
Fill.circle(e.x + x, e.y + y, 0.65f + e.fout() * 1.6f);
});
}),
shootLiquid = new Effect(15f, 80f, e -> {
color(e.color);
randLenVectors(e.id, 2, e.finpow() * 15f, e.rotation, 11f, (x, y) -> {
Fill.circle(e.x + x, e.y + y, 0.5f + e.fout() * 2.5f);
});
}),
casing1 = new Effect(30f, e -> {
color(Pal.lightOrange, Color.lightGray, Pal.lightishGray, e.fin());
alpha(e.fout(0.3f));
float rot = Math.abs(e.rotation) + 90f;
int i = -Mathf.sign(e.rotation);
float len = (2f + e.finpow() * 6f) * i;
float lr = rot + e.fin() * 30f * i;
Fill.rect(
e.x + trnsx(lr, len) + Mathf.randomSeedRange(e.id + i + 7, 3f * e.fin()),
e.y + trnsy(lr, len) + Mathf.randomSeedRange(e.id + i + 8, 3f * e.fin()),
1f, 2f, rot + e.fin() * 50f * i
);
}).layer(Layer.bullet),
casing2 = new Effect(34f, e -> {
color(Pal.lightOrange, Color.lightGray, Pal.lightishGray, e.fin());
alpha(e.fout(0.5f));
float rot = Math.abs(e.rotation) + 90f;
int i = -Mathf.sign(e.rotation);
float len = (2f + e.finpow() * 10f) * i;
float lr = rot + e.fin() * 20f * i;
rect(Core.atlas.find("casing"),
e.x + trnsx(lr, len) + Mathf.randomSeedRange(e.id + i + 7, 3f * e.fin()),
e.y + trnsy(lr, len) + Mathf.randomSeedRange(e.id + i + 8, 3f * e.fin()),
2f, 3f, rot + e.fin() * 50f * i
);
}).layer(Layer.bullet),
casing3 = new Effect(40f, e -> {
color(Pal.lightOrange, Pal.lightishGray, Pal.lightishGray, e.fin());
alpha(e.fout(0.5f));
float rot = Math.abs(e.rotation) + 90f;
int i = -Mathf.sign(e.rotation);
float len = (4f + e.finpow() * 9f) * i;
float lr = rot + Mathf.randomSeedRange(e.id + i + 6, 20f * e.fin()) * i;
rect(Core.atlas.find("casing"),
e.x + trnsx(lr, len) + Mathf.randomSeedRange(e.id + i + 7, 3f * e.fin()),
e.y + trnsy(lr, len) + Mathf.randomSeedRange(e.id + i + 8, 3f * e.fin()),
2.5f, 4f,
rot + e.fin() * 50f * i
);
}).layer(Layer.bullet),
casing4 = new Effect(45f, e -> {
color(Pal.lightOrange, Pal.lightishGray, Pal.lightishGray, e.fin());
alpha(e.fout(0.5f));
float rot = Math.abs(e.rotation) + 90f;
int i = -Mathf.sign(e.rotation);
float len = (4f + e.finpow() * 9f) * i;
float lr = rot + Mathf.randomSeedRange(e.id + i + 6, 20f * e.fin()) * i;
rect(Core.atlas.find("casing"),
e.x + trnsx(lr, len) + Mathf.randomSeedRange(e.id + i + 7, 3f * e.fin()),
e.y + trnsy(lr, len) + Mathf.randomSeedRange(e.id + i + 8, 3f * e.fin()),
3f, 6f,
rot + e.fin() * 50f * i
);
}).layer(Layer.bullet),
casing2Double = new Effect(34f, e -> {
color(Pal.lightOrange, Color.lightGray, Pal.lightishGray, e.fin());
alpha(e.fout(0.5f));
float rot = Math.abs(e.rotation) + 90f;
for(int i : Mathf.signs){
float len = (2f + e.finpow() * 10f) * i;
float lr = rot + e.fin() * 20f * i;
rect(Core.atlas.find("casing"),
e.x + trnsx(lr, len) + Mathf.randomSeedRange(e.id + i + 7, 3f * e.fin()),
e.y + trnsy(lr, len) + Mathf.randomSeedRange(e.id + i + 8, 3f * e.fin()),
2f, 3f, rot + e.fin() * 50f * i
);
}
}).layer(Layer.bullet),
casing3Double = new Effect(40f, e -> {
color(Pal.lightOrange, Pal.lightishGray, Pal.lightishGray, e.fin());
alpha(e.fout(0.5f));
float rot = Math.abs(e.rotation) + 90f;
for(int i : Mathf.signs){
float len = (4f + e.finpow() * 9f) * i;
float lr = rot + Mathf.randomSeedRange(e.id + i + 6, 20f * e.fin()) * i;
rect(Core.atlas.find("casing"),
e.x + trnsx(lr, len) + Mathf.randomSeedRange(e.id + i + 7, 3f * e.fin()),
e.y + trnsy(lr, len) + Mathf.randomSeedRange(e.id + i + 8, 3f * e.fin()),
2.5f, 4f,
rot + e.fin() * 50f * i
);
}
}).layer(Layer.bullet),
railShoot = new Effect(24f, e -> {
e.scaled(10f, b -> {
color(Color.white, Color.lightGray, b.fin());
stroke(b.fout() * 3f + 0.2f);
Lines.circle(b.x, b.y, b.fin() * 50f);
});
color(Pal.orangeSpark);
for(int i : Mathf.signs){
Drawf.tri(e.x, e.y, 13f * e.fout(), 85f, e.rotation + 90f * i);
}
}),
railTrail = new Effect(16f, e -> {
color(Pal.orangeSpark);
for(int i : Mathf.signs){
Drawf.tri(e.x, e.y, 10f * e.fout(), 24f, e.rotation + 90 + 90f * i);
}
Drawf.light(e.x, e.y, 60f * e.fout(), Pal.orangeSpark, 0.5f);
}),
railHit = new Effect(18f, 200f, e -> {
color(Pal.orangeSpark);
for(int i : Mathf.signs){
Drawf.tri(e.x, e.y, 10f * e.fout(), 60f, e.rotation + 140f * i);
}
}),
lancerLaserShoot = new Effect(21f, e -> {
color(Pal.lancerLaser);
for(int i : Mathf.signs){
Drawf.tri(e.x, e.y, 4f * e.fout(), 29f, e.rotation + 90f * i);
}
}),
lancerLaserShootSmoke = new Effect(26f, e -> {
color(Color.white);
float length = !(e.data instanceof Float) ? 70f : (Float)e.data;
randLenVectors(e.id, 7, length, e.rotation, 0f, (x, y) -> {
lineAngle(e.x + x, e.y + y, Mathf.angle(x, y), e.fout() * 9f);
});
}),
lancerLaserCharge = new Effect(38f, e -> {
color(Pal.lancerLaser);
randLenVectors(e.id, 14, 1f + 20f * e.fout(), e.rotation, 120f, (x, y) -> {
lineAngle(e.x + x, e.y + y, Mathf.angle(x, y), e.fslope() * 3f + 1f);
});
}),
lancerLaserChargeBegin = new Effect(60f, e -> {
float margin = 1f - Mathf.curve(e.fin(), 0.9f);
float fin = Math.min(margin, e.fin());
color(Pal.lancerLaser);
Fill.circle(e.x, e.y, fin * 3f);
color();
Fill.circle(e.x, e.y, fin * 2f);
}),
lightningCharge = new Effect(38f, e -> {
color(Pal.lancerLaser);
randLenVectors(e.id, 2, 1f + 20f * e.fout(), e.rotation, 120f, (x, y) -> {
Drawf.tri(e.x + x, e.y + y, e.fslope() * 3f + 1, e.fslope() * 3f + 1, Mathf.angle(x, y));
});
}),
sparkShoot = new Effect(12f, e -> {
color(Color.white, e.color, e.fin());
stroke(e.fout() * 1.2f + 0.6f);
randLenVectors(e.id, 7, 25f * e.finpow(), e.rotation, 3f, (x, y) -> {
lineAngle(e.x + x, e.y + y, Mathf.angle(x, y), e.fslope() * 5f + 0.5f);
});
}),
lightningShoot = new Effect(12f, e -> {
color(Color.white, Pal.lancerLaser, e.fin());
stroke(e.fout() * 1.2f + 0.5f);
randLenVectors(e.id, 7, 25f * e.finpow(), e.rotation, 50f, (x, y) -> {
lineAngle(e.x + x, e.y + y, Mathf.angle(x, y), e.fin() * 5f + 2f);
});
}),
thoriumShoot = new Effect(12f, e -> {
color(Color.white, Pal.thoriumPink, e.fin());
stroke(e.fout() * 1.2f + 0.5f);
randLenVectors(e.id, 7, 25f * e.finpow(), e.rotation, 50f, (x, y) -> {
lineAngle(e.x + x, e.y + y, Mathf.angle(x, y), e.fin() * 5f + 2f);
});
}),
reactorsmoke = new Effect(17, e -> {
randLenVectors(e.id, 4, e.fin() * 8f, (x, y) -> {
float size = 1f + e.fout() * 5f;
color(Color.lightGray, Color.gray, e.fin());
Fill.circle(e.x + x, e.y + y, size/2f);
});
}),
redgeneratespark = new Effect(90, e -> {
color(Pal.redSpark);
alpha(e.fslope());
rand.setSeed(e.id);
for(int i = 0; i < 2; i++){
v.trns(rand.random(360f), rand.random(e.finpow() * 9f)).add(e.x, e.y);
Fill.circle(v.x, v.y, rand.random(1.4f, 2.4f));
}
}).layer(Layer.bullet - 1f),
turbinegenerate = new Effect(100, e -> {
color(Pal.vent);
alpha(e.fslope() * 0.8f);
rand.setSeed(e.id);
for(int i = 0; i < 3; i++){
v.trns(rand.random(360f), rand.random(e.finpow() * 14f)).add(e.x, e.y);
Fill.circle(v.x, v.y, rand.random(1.4f, 3.4f));
}
}).layer(Layer.bullet - 1f),
generatespark = new Effect(18, e -> {
randLenVectors(e.id, 5, e.fin() * 8f, (x, y) -> {
color(Pal.orangeSpark, Color.gray, e.fin());
Fill.circle(e.x + x, e.y + y, e.fout() * 4f /2f);
});
}),
fuelburn = new Effect(23, e -> {
randLenVectors(e.id, 5, e.fin() * 9f, (x, y) -> {
color(Color.lightGray, Color.gray, e.fin());
Fill.circle(e.x + x, e.y + y, e.fout() * 2f);
});
}),
incinerateSlag = new Effect(34, e -> {
randLenVectors(e.id, 4, e.finpow() * 5f, (x, y) -> {
color(Pal.slagOrange, Color.gray, e.fin());
Fill.circle(e.x + x, e.y + y, e.fout() * 1.7f);
});
}),
coreBurn = new Effect(23, e -> {
randLenVectors(e.id, 5, e.fin() * 9f, (x, y) -> {
float len = e.fout() * 4f;
color(Pal.accent, Color.gray, e.fin());
Fill.circle(e.x + x, e.y + y, len/2f);
});
}),
plasticburn = new Effect(40, e -> {
randLenVectors(e.id, 5, 3f + e.fin() * 5f, (x, y) -> {
color(Pal.plasticBurn, Color.gray, e.fin());
Fill.circle(e.x + x, e.y + y, e.fout());
});
}),
conveyorPoof = new Effect(35, e -> {
color(Pal.plasticBurn, Color.gray, e.fin());
randLenVectors(e.id, 4, 3f + e.fin() * 4f, (x, y) -> {
Fill.circle(e.x + x, e.y + y, e.fout() * 1.11f);
});
}),
pulverize = new Effect(40, e -> {
randLenVectors(e.id, 5, 3f + e.fin() * 8f, (x, y) -> {
color(Pal.stoneGray);
Fill.square(e.x + x, e.y + y, e.fout() * 2f + 0.5f, 45);
});
}),
pulverizeRed = new Effect(40, e -> {
randLenVectors(e.id, 5, 3f + e.fin() * 8f, (x, y) -> {
color(Pal.redDust, Pal.stoneGray, e.fin());
Fill.square(e.x + x, e.y + y, e.fout() * 2f + 0.5f, 45);
});
}),
pulverizeSmall = new Effect(30, e -> {
randLenVectors(e.id, 3, e.fin() * 5f, (x, y) -> {
color(Pal.stoneGray);
Fill.square(e.x + x, e.y + y, e.fout() + 0.5f, 45);
});
}),
pulverizeMedium = new Effect(30, e -> {
randLenVectors(e.id, 5, 3f + e.fin() * 8f, (x, y) -> {
color(Pal.stoneGray);
Fill.square(e.x + x, e.y + y, e.fout() + 0.5f, 45);
});
}),
producesmoke = new Effect(12, e -> {
randLenVectors(e.id, 8, 4f + e.fin() * 18f, (x, y) -> {
color(Color.white, Pal.accent, e.fin());
Fill.square(e.x + x, e.y + y, 1f + e.fout() * 3f, 45);
});
}),
artilleryTrailSmoke = new Effect(50, e -> {
color(e.color);
rand.setSeed(e.id);
for(int i = 0; i < 13; i++){
float fin = e.fin() / rand.random(0.5f, 1f), fout = 1f - fin, angle = rand.random(360f), len = rand.random(0.5f, 1f);
if(fin <= 1f){
Tmp.v1.trns(angle, fin * 24f * len);
alpha((0.5f - Math.abs(fin - 0.5f)) * 2f);
Fill.circle(e.x + Tmp.v1.x, e.y + Tmp.v1.y, 0.5f + fout * 4f);
}
}
}),
smokeCloud = new Effect(70, e -> {
randLenVectors(e.id, e.fin(), 30, 30f, (x, y, fin, fout) -> {
color(Color.gray);
alpha((0.5f - Math.abs(fin - 0.5f)) * 2f);
Fill.circle(e.x + x, e.y + y, 0.5f + fout * 4f);
});
}),
smeltsmoke = new Effect(15, e -> {
randLenVectors(e.id, 6, 4f + e.fin() * 5f, (x, y) -> {
color(Color.white, e.color, e.fin());
Fill.square(e.x + x, e.y + y, 0.5f + e.fout() * 2f, 45);
});
}),
coalSmeltsmoke = new Effect(40f, e -> {
randLenVectors(e.id, 0.2f + e.fin(), 4, 6.3f, (x, y, fin, out) -> {
color(Color.darkGray, Pal.coalBlack, e.finpowdown());
Fill.circle(e.x + x, e.y + y, out * 2f + 0.35f);
});
}),
formsmoke = new Effect(40, e -> {
randLenVectors(e.id, 6, 5f + e.fin() * 8f, (x, y) -> {
color(Pal.plasticSmoke, Color.lightGray, e.fin());
Fill.square(e.x + x, e.y + y, 0.2f + e.fout() * 2f, 45);
});
}),
blastsmoke = new Effect(26, e -> {
randLenVectors(e.id, 12, 1f + e.fin() * 23f, (x, y) -> {
float size = 2f + e.fout() * 6f;
color(Color.lightGray, Color.darkGray, e.fin());
Fill.circle(e.x + x, e.y + y, size/2f);
});
}),
lava = new Effect(18, e -> {
randLenVectors(e.id, 3, 1f + e.fin() * 10f, (x, y) -> {
float size = e.fslope() * 4f;
color(Color.orange, Color.gray, e.fin());
Fill.circle(e.x + x, e.y + y, size/2f);
});
}),
dooropen = new Effect(10, e -> {
stroke(e.fout() * 1.6f);
Lines.square(e.x, e.y, e.rotation * tilesize / 2f + e.fin() * 2f);
}),
doorclose = new Effect(10, e -> {
stroke(e.fout() * 1.6f);
Lines.square(e.x, e.y, e.rotation * tilesize / 2f + e.fout() * 2f);
}),
dooropenlarge = new Effect(10, e -> {
stroke(e.fout() * 1.6f);
Lines.square(e.x, e.y, tilesize + e.fin() * 2f);
}),
doorcloselarge = new Effect(10, e -> {
stroke(e.fout() * 1.6f);
Lines.square(e.x, e.y, tilesize + e.fout() * 2f);
}),
generate = new Effect(11, e -> {
color(Color.orange, Color.yellow, e.fin());
stroke(1f);
Lines.spikes(e.x, e.y, e.fin() * 5f, 2, 8);
}),
mineWallSmall = new Effect(50, e -> {
color(e.color, Color.darkGray, e.fin());
randLenVectors(e.id, 2, e.fin() * 6f, (x, y) -> {
Fill.circle(e.x + x, e.y + y, e.fout() + 0.5f);
});
}),
mineSmall = new Effect(30, e -> {
color(e.color, Color.lightGray, e.fin());
randLenVectors(e.id, 3, e.fin() * 5f, (x, y) -> {
Fill.square(e.x + x, e.y + y, e.fout() + 0.5f, 45);
});
}),
mine = new Effect(20, e -> {
color(e.color, Color.lightGray, e.fin());
randLenVectors(e.id, 6, 3f + e.fin() * 6f, (x, y) -> {
Fill.square(e.x + x, e.y + y, e.fout() * 2f, 45);
});
}),
mineBig = new Effect(30, e -> {
color(e.color, Color.lightGray, e.fin());
randLenVectors(e.id, 6, 4f + e.fin() * 8f, (x, y) -> {
Fill.square(e.x + x, e.y + y, e.fout() * 2f + 0.2f, 45);
});
}),
mineHuge = new Effect(40, e -> {
color(e.color, Color.lightGray, e.fin());
randLenVectors(e.id, 8, 5f + e.fin() * 10f, (x, y) -> {
Fill.square(e.x + x, e.y + y, e.fout() * 2f + 0.5f, 45);
});
}),
mineImpact = new Effect(90, e -> {
color(e.color, Color.lightGray, e.fin());
randLenVectors(e.id, 12, 5f + e.finpow() * 22f, (x, y) -> {
Fill.square(e.x + x, e.y + y, e.fout() * 2.5f + 0.5f, 45);
});
}),
mineImpactWave = new Effect(50f, e -> {
color(e.color);
stroke(e.fout() * 1.5f);
randLenVectors(e.id, 12, 4f + e.finpow() * e.rotation, (x, y) -> {
lineAngle(e.x + x, e.y + y, Mathf.angle(x, y), e.fout() * 5 + 1f);
});
e.scaled(30f, b -> {
Lines.stroke(5f * b.fout());
Lines.circle(e.x, e.y, b.finpow() * 28f);
});
}),
payloadReceive = new Effect(30, e -> {
color(Color.white, Pal.accent, e.fin());
randLenVectors(e.id, 12, 7f + e.fin() * 13f, (x, y) -> {
Fill.square(e.x + x, e.y + y, e.fout() * 2.1f + 0.5f, 45);
});
}),
teleportActivate = new Effect(50, e -> {
color(e.color);
e.scaled(8f, e2 -> {
stroke(e2.fout() * 4f);
Lines.circle(e2.x, e2.y, 4f + e2.fin() * 27f);
});
stroke(e.fout() * 2f);
randLenVectors(e.id, 30, 4f + 40f * e.fin(), (x, y) -> {
lineAngle(e.x + x, e.y + y, Mathf.angle(x, y), e.fin() * 4f + 1f);
});
}),
teleport = new Effect(60, e -> {
color(e.color);
stroke(e.fin() * 2f);
Lines.circle(e.x, e.y, 7f + e.fout() * 8f);
randLenVectors(e.id, 20, 6f + 20f * e.fout(), (x, y) -> {
lineAngle(e.x + x, e.y + y, Mathf.angle(x, y), e.fin() * 4f + 1f);
});
}),
teleportOut = new Effect(20, e -> {
color(e.color);
stroke(e.fout() * 2f);
Lines.circle(e.x, e.y, 7f + e.fin() * 8f);
randLenVectors(e.id, 20, 4f + 20f * e.fin(), (x, y) -> {
lineAngle(e.x + x, e.y + y, Mathf.angle(x, y), e.fslope() * 4f + 1f);
});
}),
ripple = new Effect(30, e -> {
e.lifetime = 30f*e.rotation;
color(Tmp.c1.set(e.color).mul(1.5f));
stroke(e.fout() * 1.4f);
Lines.circle(e.x, e.y, (2f + e.fin() * 4f) * e.rotation);
}).layer(Layer.debris),
bubble = new Effect(20, e -> {
color(Tmp.c1.set(e.color).shiftValue(0.1f));
stroke(e.fout() + 0.2f);
randLenVectors(e.id, 2, e.rotation * 0.9f, (x, y) -> {
Lines.circle(e.x + x, e.y + y, 1f + e.fin() * 3f);
});
}),
launch = new Effect(28, e -> {
color(Pal.command);
stroke(e.fout() * 2f);
Lines.circle(e.x, e.y, 4f + e.finpow() * 120f);
}),
launchPod = new Effect(50, e -> {
color(Pal.engine);
e.scaled(25f, f -> {
stroke(f.fout() * 2f);
Lines.circle(e.x, e.y, 4f + f.finpow() * 30f);
});
stroke(e.fout() * 2f);
randLenVectors(e.id, 24, e.finpow() * 50f, (x, y) -> {
float ang = Mathf.angle(x, y);
lineAngle(e.x + x, e.y + y, ang, e.fout() * 4 + 1f);
});
}),
healWaveMend = new Effect(40, e -> {
color(e.color);
stroke(e.fout() * 2f);
Lines.circle(e.x, e.y, e.finpow() * e.rotation);
}),
overdriveWave = new Effect(50, e -> {
color(e.color);
stroke(e.fout());
Lines.circle(e.x, e.y, e.finpow() * e.rotation);
}),
healBlock = new Effect(20, e -> {
color(Pal.heal);
stroke(2f * e.fout() + 0.5f);
Lines.square(e.x, e.y, 1f + (e.fin() * e.rotation * tilesize / 2f - 1f));
}),
healBlockFull = new Effect(20, e -> {
if(!(e.data instanceof Block block)) return;
mixcol(e.color, 1f);
alpha(e.fout());
Draw.rect(block.fullIcon, e.x, e.y);
}),
rotateBlock = new Effect(30, e -> {
color(Pal.accent);
alpha(e.fout() * 1);
Fill.square(e.x, e.y, e.rotation * tilesize / 2f);
}),
lightBlock = new Effect(60, e -> {
color(e.color);
alpha(e.fout() * 1);
Fill.square(e.x, e.y, e.rotation * tilesize / 2f);
}),
overdriveBlockFull = new Effect(60, e -> {
color(e.color);
alpha(e.fslope() * 0.4f);
Fill.square(e.x, e.y, e.rotation * tilesize);
}),
shieldBreak = new Effect(40, e -> {
color(e.color);
stroke(3f * e.fout());
if(e.data instanceof ForceFieldAbility ab){
Lines.poly(e.x, e.y, ab.sides, e.rotation + e.fin(), ab.rotation);
return;
}
Lines.poly(e.x, e.y, 6, e.rotation + e.fin());
}).followParent(true),
arcShieldBreak = new Effect(40, e -> {
Lines.stroke(3 * e.fout(), e.color);
if(e.data instanceof Unit u){
ShieldArcAbility ab = (ShieldArcAbility) Structs.find(u.abilities, a -> a instanceof ShieldArcAbility);
if(ab != null){
Vec2 pos = Tmp.v1.set(ab.x, ab.y).rotate(u.rotation - 90f).add(u);
Lines.arc(pos.x, pos.y, ab.radius + ab.width/2, ab.angle / 360f, u.rotation + ab.angleOffset - ab.angle / 2f);
Lines.arc(pos.x, pos.y, ab.radius - ab.width/2, ab.angle / 360f, u.rotation + ab.angleOffset - ab.angle / 2f);
for(int i : Mathf.signs){
float
px = pos.x + Angles.trnsx(u.rotation + ab.angleOffset - ab.angle / 2f * i, ab.radius + ab.width / 2),
py = pos.y + Angles.trnsy(u.rotation + ab.angleOffset - ab.angle / 2f * i, ab.radius + ab.width / 2),
px1 = pos.x + Angles.trnsx(u.rotation + ab.angleOffset - ab.angle / 2f * i, ab.radius - ab.width / 2),
py1 = pos.y + Angles.trnsy(u.rotation + ab.angleOffset - ab.angle / 2f * i, ab.radius - ab.width / 2);
Lines.line(px, py, px1, py1);
}
}
}
}).followParent(true),
coreLandDust = new Effect(100f, e -> {
color(e.color, e.fout(0.1f));
rand.setSeed(e.id);
Tmp.v1.trns(e.rotation, e.finpow() * 90f * rand.random(0.2f, 1f));
Fill.circle(e.x + Tmp.v1.x, e.y + Tmp.v1.y, 8f * rand.random(0.6f, 1f) * e.fout(0.2f));
}).layer(Layer.groundUnit + 1f),
unitShieldBreak = new Effect(35, e -> {
if(!(e.data instanceof Unit unit)) return;
float radius = unit.hitSize() * 1.3f;
e.scaled(16f, c -> {
color(e.color, 0.9f);
stroke(c.fout() * 2f + 0.1f);
randLenVectors(e.id, (int)(radius * 1.2f), radius/2f + c.finpow() * radius*1.25f, (x, y) -> {
lineAngle(c.x + x, c.y + y, Mathf.angle(x, y), c.fout() * 5 + 1f);
});
});
color(e.color, e.fout() * 0.9f);
stroke(e.fout());
Lines.circle(e.x, e.y, radius);
}),
chainLightning = new Effect(20f, 300f, e -> {
if(!(e.data instanceof Position p)) return;
float tx = p.getX(), ty = p.getY(), dst = Mathf.dst(e.x, e.y, tx, ty);
Tmp.v1.set(p).sub(e.x, e.y).nor();
float normx = Tmp.v1.x, normy = Tmp.v1.y;
float range = 6f;
int links = Mathf.ceil(dst / range);
float spacing = dst / links;
Lines.stroke(2.5f * e.fout());
Draw.color(Color.white, e.color, e.fin());
Lines.beginLine();
Lines.linePoint(e.x, e.y);
rand.setSeed(e.id);
for(int i = 0; i < links; i++){
float nx, ny;
if(i == links - 1){
nx = tx;
ny = ty;
}else{
float len = (i + 1) * spacing;
Tmp.v1.setToRandomDirection(rand).scl(range/2f);
nx = e.x + normx * len + Tmp.v1.x;
ny = e.y + normy * len + Tmp.v1.y;
}
Lines.linePoint(nx, ny);
}
Lines.endLine();
}).followParent(false).rotWithParent(false),
chainEmp = new Effect(30f, 300f, e -> {
if(!(e.data instanceof Position p)) return;
float tx = p.getX(), ty = p.getY(), dst = Mathf.dst(e.x, e.y, tx, ty);
Tmp.v1.set(p).sub(e.x, e.y).nor();
float normx = Tmp.v1.x, normy = Tmp.v1.y;
float range = 6f;
int links = Mathf.ceil(dst / range);
float spacing = dst / links;
Lines.stroke(4f * e.fout());
Draw.color(Color.white, e.color, e.fin());
Lines.beginLine();
Lines.linePoint(e.x, e.y);
rand.setSeed(e.id);
for(int i = 0; i < links; i++){
float nx, ny;
if(i == links - 1){
nx = tx;
ny = ty;
}else{
float len = (i + 1) * spacing;
Tmp.v1.setToRandomDirection(rand).scl(range/2f);
nx = e.x + normx * len + Tmp.v1.x;
ny = e.y + normy * len + Tmp.v1.y;
}
Lines.linePoint(nx, ny);
}
Lines.endLine();
}).followParent(false).rotWithParent(false),
legDestroy = new Effect(90f, 100f, e -> {
if(!(e.data instanceof LegDestroyData data)) return;
rand.setSeed(e.id);
e.lifetime = rand.random(70f, 130f);
Tmp.v1.trns(rand.random(360f), rand.random(data.region.width / 8f) * e.finpow());
float ox = Tmp.v1.x, oy = Tmp.v1.y;
alpha(e.foutpowdown());
stroke(data.region.height * scl);
line(data.region, data.a.x + ox, data.a.y + oy, data.b.x + ox, data.b.y + oy, false);
}).layer(Layer.groundUnit + 5f),
debugLine = new Effect(90f, 1000000000000f, e -> {
if(!(e.data instanceof Vec2[] vec)) return;
Draw.color(e.color);
Lines.stroke(2f);
if(vec.length == 2){
Lines.line(vec[0].x, vec[0].y, vec[1].x, vec[1].y);
}else{
Lines.beginLine();
for(Vec2 v : vec)
Lines.linePoint(v.x, v.y);
Lines.endLine();
}
Draw.reset();
}),
debugRect = new Effect(90f, 1000000000000f, e -> {
if(!(e.data instanceof Rect rect)) return;
Draw.color(e.color);
Lines.stroke(2f);
Lines.rect(rect);
Draw.reset();
});
}
| Anuken/Mindustry | core/src/mindustry/content/Fx.java |
7 | /*
* Copyright (C) 2013 Square, Inc.
*
* 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
*
* http://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.
*/
package retrofit2;
import static android.os.Build.VERSION.SDK_INT;
import java.util.concurrent.Executor;
import javax.annotation.Nullable;
final class Platform {
static final @Nullable Executor callbackExecutor;
static final Reflection reflection;
static final BuiltInFactories builtInFactories;
static {
switch (System.getProperty("java.vm.name")) {
case "Dalvik":
callbackExecutor = new AndroidMainExecutor();
if (SDK_INT >= 24) {
reflection = new Reflection.Android24();
builtInFactories = new BuiltInFactories.Java8();
} else {
reflection = new Reflection();
builtInFactories = new BuiltInFactories();
}
break;
case "RoboVM":
callbackExecutor = null;
reflection = new Reflection();
builtInFactories = new BuiltInFactories();
break;
default:
callbackExecutor = null;
reflection = new Reflection.Java8();
builtInFactories = new BuiltInFactories.Java8();
break;
}
}
private Platform() {}
}
| square/retrofit | retrofit/src/main/java/retrofit2/Platform.java |
8 | /*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import org.slf4j.LoggerFactory;
/**
* Created by Alexis on 28-Apr-17. With Marker interface idea is to make empty interface and extend
* it. Basically it is just to identify the special objects from normal objects. Like in case of
* serialization , objects that need to be serialized must implement serializable interface (it is
* empty interface) and down the line writeObject() method must be checking if it is an instance of
* serializable or not.
*
* <p>Marker interface vs annotation Marker interfaces and marker annotations both have their uses,
* neither of them is obsolete or always better than the other one. If you want to define a type
* that does not have any new methods associated with it, a marker interface is the way to go. If
* you want to mark program elements other than classes and interfaces, to allow for the possibility
* of adding more information to the marker in the future, or to fit the marker into a framework
* that already makes heavy use of annotation types, then a marker annotation is the correct choice
*/
public class App {
/**
* Program entry point.
*
* @param args command line args
*/
public static void main(String[] args) {
final var logger = LoggerFactory.getLogger(App.class);
var guard = new Guard();
var thief = new Thief();
//noinspection ConstantConditions
if (guard instanceof Permission) {
guard.enter();
} else {
logger.info("You have no permission to enter, please leave this area");
}
//noinspection ConstantConditions
if (thief instanceof Permission) {
thief.steal();
} else {
thief.doNothing();
}
}
}
| iluwatar/java-design-patterns | marker/src/main/java/App.java |
9 | /* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
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
http://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.
==============================================================================*/
package org.tensorflow;
import java.util.ArrayList;
import java.util.List;
/**
* Driver for {@link Graph} execution.
*
* <p>A {@code Session} instance encapsulates the environment in which {@link Operation}s in a
* {@link Graph} are executed to compute {@link Tensor Tensors}. For example:
*
* <pre>{@code
* // Let's say graph is an instance of the Graph class
* // for the computation y = 3 * x
*
* try (Session s = new Session(graph)) {
* try (Tensor x = Tensor.create(2.0f);
* Tensor y = s.runner().feed("x", x).fetch("y").run().get(0)) {
* System.out.println(y.floatValue()); // Will print 6.0f
* }
* try (Tensor x = Tensor.create(1.1f);
* Tensor y = s.runner().feed("x", x).fetch("y").run().get(0)) {
* System.out.println(y.floatValue()); // Will print 3.3f
* }
* }
* }</pre>
*
* <p><b>WARNING:</b>A {@code Session} owns resources that <b>must</b> be explicitly freed by
* invoking {@link #close()}.
*
* <p>Instances of a Session are thread-safe.
*/
public final class Session implements AutoCloseable {
/** Construct a new session with the associated {@link Graph}. */
public Session(Graph g) {
this(g, null);
}
/**
* Construct a new session with the associated {@link Graph} and configuration options.
*
* @param g The {@link Graph} the created Session will operate on.
* @param config Configuration parameters for the session specified as a serialized <a
* href="https://www.tensorflow.org/code/tensorflow/core/protobuf/config.proto">ConfigProto</a>
* protocol buffer.
* @throws IllegalArgumentException if the config is not a valid serialization of the ConfigProto
* protocol buffer.
*/
public Session(Graph g, byte[] config) {
graph = g;
Graph.Reference r = g.ref();
try {
nativeHandle =
(config == null) ? allocate(r.nativeHandle()) : allocate2(r.nativeHandle(), null, config);
graphRef = g.ref();
} finally {
r.close();
}
}
/** Wrap an existing session with the associated {@link Graph}. */
Session(Graph g, long nativeHandle) {
graph = g;
this.nativeHandle = nativeHandle;
graphRef = g.ref();
}
/**
* Release resources associated with the Session.
*
* <p>Blocks until there are no active executions ({@link Session.Runner#run()} calls). A Session
* is not usable after close returns.
*/
@Override
public void close() {
graphRef.close();
synchronized (nativeHandleLock) {
if (nativeHandle == 0) {
return;
}
while (numActiveRuns > 0) {
try {
nativeHandleLock.wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
// Possible leak of the Session and Graph in this case?
return;
}
}
delete(nativeHandle);
nativeHandle = 0;
}
}
/**
* Run {@link Operation}s and evaluate {@link Tensor Tensors}.
*
* <p>A Runner runs the necessary graph fragments to execute every {@link Operation} required to
* evaluate the {@link Tensor Tensors} to fetch. The {@link #feed(String,int,Tensor)} call allows
* callers to override the value of {@link Tensor Tensors} in the graph by substituting the
* provided {@link Tensor Tensors} for the outputs of the operations provided to {@link
* #feed(String,int,Tensor)}.
*/
public final class Runner {
/**
* Avoid evaluating {@code operation} and substitute {@code t} for the value it produces.
*
* @param operation Is either the string name of the operation, in which case this method is a
* shorthand for {@code feed(operation, 0)}, or it is a string of the form
* <tt>operation_name:output_index</tt> , in which case this method acts like {@code
* feed(operation_name, output_index)}. These colon-separated names are commonly used in the
* {@code SignatureDef} protocol buffer messages that are included in {@link
* SavedModelBundle#metaGraphDef()}.
*/
public Runner feed(String operation, Tensor<?> t) {
return feed(parseOutput(operation), t);
}
/**
* Avoid evaluating the {@code index}-th output of {@code operation} by substituting {@code t}
* for the value it produces.
*
* <p>Operations in a {@link Graph} can have multiple outputs, {@code index} identifies which
* one {@code t} is being provided for.
*/
public Runner feed(String operation, int index, Tensor<?> t) {
Operation op = operationByName(operation);
if (op != null) {
inputs.add(op.output(index));
inputTensors.add(t);
}
return this;
}
/**
* Use {@code t} instead of the Tensor referred to by executing the operation referred to by
* {@code operand}.
*/
public Runner feed(Operand<?> operand, Tensor<?> t) {
inputs.add(operand.asOutput());
inputTensors.add(t);
return this;
}
/**
* Make {@link #run()} return the output of {@code operation}.
*
* @param operation Is either the string name of the operation, in which case this method is a
* shorthand for {@code fetch(operation, 0)}, or it is a string of the form
* <tt>operation_name:output_index</tt> , in which case this method acts like {@code
* fetch(operation_name, output_index)}. These colon-separated names are commonly used in
* the {@code SignatureDef} protocol buffer messages that are included in {@link
* SavedModelBundle#metaGraphDef()}.
*/
public Runner fetch(String operation) {
return fetch(parseOutput(operation));
}
/**
* Make {@link #run()} return the {@code index}-th output of {@code operation}.
*
* <p>Operations in a {@link Graph} can have multiple outputs, {@code index} identifies which
* one to return.
*/
public Runner fetch(String operation, int index) {
Operation op = operationByName(operation);
if (op != null) {
outputs.add(op.output(index));
}
return this;
}
/**
* Makes {@link #run()} return the Tensor referred to by {@code output}.
*/
public Runner fetch(Output<?> output) {
outputs.add(output);
return this;
}
/**
* Makes {@link #run()} return the Tensor referred to by the output of {@code operand}.
*/
public Runner fetch(Operand<?> operand) {
return fetch(operand.asOutput());
}
/**
* Make {@link #run()} execute {@code operation}, but not return any evaluated {@link Tensor
* Tensors}.
*/
public Runner addTarget(String operation) {
GraphOperation op = operationByName(operation);
if (op != null) {
targets.add(op);
}
return this;
}
/**
* Make {@link #run()} execute {@code operation}, but not return any evaluated {@link Tensor
* Tensors}.
*
* @throws IllegalArgumentException if the operation is not a {@link GraphOperation}
*/
public Runner addTarget(Operation operation) {
if (!(operation instanceof GraphOperation)) {
throw new IllegalArgumentException(
"Operation of type "
+ operation.getClass().getName()
+ " is not supported in graph sessions");
}
targets.add((GraphOperation) operation);
return this;
}
/**
* Make {@link #run} execute {@code operand}, but not return any evaluated {@link Tensor
* Tensors}.
*/
public Runner addTarget(Operand<?> operand) {
return addTarget(operand.asOutput().op());
}
/**
* (Experimental method): set options (typically for debugging) for this run.
*
* <p>The options are presented as a serialized <a
* href="https://www.tensorflow.org/code/tensorflow/core/protobuf/config.proto">RunOptions
* protocol buffer</a>.
*
* <p>The org.tensorflow package is free of any protocol buffer dependencies in order to remain
* friendly to resource constrained systems (where something like <a
* href="https://github.com/google/protobuf/tree/master/javanano#nano-version">nanoproto</a> may
* be more appropriate). A cost of that is this lack of type-safety in this API function. This
* choice is under review and this function may be replaced by more type-safe equivalents at any
* time.
*/
public Runner setOptions(byte[] options) {
this.runOptions = options;
return this;
}
/**
* Execute the graph fragments necessary to compute all requested fetches.
*
* <p><b>WARNING:</b> The caller assumes ownership of all returned {@link Tensor Tensors}, i.e.,
* the caller must call {@link Tensor#close} on all elements of the returned list to free up
* resources.
*
* <p>TODO(ashankar): Reconsider the return type here. Two things in particular: (a) Make it
* easier for the caller to cleanup (perhaps returning something like AutoCloseableList in
* SessionTest.java), and (b) Evaluate whether the return value should be a list, or maybe a
* {@code Map<Output, Tensor>}?
*
* <p>TODO(andrewmyers): It would also be good if whatever is returned here made it easier to
* extract output tensors in a type-safe way.
*/
public List<Tensor<?>> run() {
return runHelper(false).outputs;
}
/**
* Execute graph fragments to compute requested fetches and return metadata about the run.
*
* <p>This is exactly like {@link #run()}, but in addition to the requested Tensors, also
* returns metadata about the graph execution in the form of a serialized <a
* href="https://www.tensorflow.org/code/tensorflow/core/protobuf/config.proto">RunMetadata
* protocol buffer</a>.
*/
public Run runAndFetchMetadata() {
return runHelper(true);
}
private Run runHelper(boolean wantMetadata) {
long[] inputTensorHandles = new long[inputTensors.size()];
long[] inputOpHandles = new long[inputs.size()];
int[] inputOpIndices = new int[inputs.size()];
long[] outputOpHandles = new long[outputs.size()];
int[] outputOpIndices = new int[outputs.size()];
long[] targetOpHandles = new long[targets.size()];
long[] outputTensorHandles = new long[outputs.size()];
// It's okay to use Operation.getUnsafeNativeHandle() here since the safety depends on the
// validity of the Graph and graphRef ensures that.
int idx = 0;
for (Tensor<?> t : inputTensors) {
inputTensorHandles[idx++] = t.getNativeHandle();
}
idx = 0;
for (Output<?> o : inputs) {
inputOpHandles[idx] = o.getUnsafeNativeHandle();
inputOpIndices[idx] = o.index();
idx++;
}
idx = 0;
for (Output<?> o : outputs) {
outputOpHandles[idx] = o.getUnsafeNativeHandle();
outputOpIndices[idx] = o.index();
idx++;
}
idx = 0;
for (GraphOperation op : targets) {
targetOpHandles[idx++] = op.getUnsafeNativeHandle();
}
Reference runRef = new Reference();
byte[] metadata = null;
try {
metadata =
Session.run(
nativeHandle,
runOptions,
inputTensorHandles,
inputOpHandles,
inputOpIndices,
outputOpHandles,
outputOpIndices,
targetOpHandles,
wantMetadata,
outputTensorHandles);
} finally {
runRef.close();
}
List<Tensor<?>> outputs = new ArrayList<Tensor<?>>();
for (long h : outputTensorHandles) {
try {
outputs.add(Tensor.fromHandle(h));
} catch (Exception e) {
for (Tensor<?> t : outputs) {
t.close();
}
outputs.clear();
throw e;
}
}
Run ret = new Run();
ret.outputs = outputs;
ret.metadata = metadata;
return ret;
}
private class Reference implements AutoCloseable {
public Reference() {
synchronized (nativeHandleLock) {
if (nativeHandle == 0) {
throw new IllegalStateException("run() cannot be called on the Session after close()");
}
++numActiveRuns;
}
}
@Override
public void close() {
synchronized (nativeHandleLock) {
if (nativeHandle == 0) {
return;
}
if (--numActiveRuns == 0) {
nativeHandleLock.notifyAll();
}
}
}
}
private GraphOperation operationByName(String opName) {
GraphOperation op = graph.operation(opName);
if (op == null) {
throw new IllegalArgumentException("No Operation named [" + opName + "] in the Graph");
}
return op;
}
@SuppressWarnings("rawtypes")
private Output<?> parseOutput(String opName) {
int colon = opName.lastIndexOf(':');
if (colon == -1 || colon == opName.length() - 1) {
return new Output(operationByName(opName), 0);
}
try {
String op = opName.substring(0, colon);
int index = Integer.parseInt(opName.substring(colon + 1));
return new Output(operationByName(op), index);
} catch (NumberFormatException e) {
return new Output(operationByName(opName), 0);
}
}
private ArrayList<Output<?>> inputs = new ArrayList<Output<?>>();
private ArrayList<Tensor<?>> inputTensors = new ArrayList<Tensor<?>>();
private ArrayList<Output<?>> outputs = new ArrayList<Output<?>>();
private ArrayList<GraphOperation> targets = new ArrayList<GraphOperation>();
private byte[] runOptions = null;
}
/** Create a Runner to execute graph operations and evaluate Tensors. */
public Runner runner() {
return new Runner();
}
/**
* Output tensors and metadata obtained when executing a session.
*
* <p>See {@link Runner#runAndFetchMetadata()}
*/
public static final class Run {
/** Tensors from requested fetches. */
public List<Tensor<?>> outputs;
/**
* (Experimental): Metadata about the run.
*
* <p>A serialized <a
* href="https://www.tensorflow.org/code/tensorflow/core/protobuf/config.proto">RunMetadata
* protocol buffer</a>. The org.tensorflow package is free of any protocol buffer dependencies
* in order to remain friendly to resource constrained systems (where something like <a
* href="https://github.com/google/protobuf/tree/master/javanano#nano-version">nanoproto</a> may
* be more appropriate). A cost of that is this opaque blob. This choice is under review and
* this field may be replaced by more type-safe equivalents at any time.
*/
public byte[] metadata;
}
private final Graph graph;
private final Graph.Reference graphRef;
private final Object nativeHandleLock = new Object();
private long nativeHandle;
private int numActiveRuns;
// TODO(ashankar): Remove after TensorFlow 1.2 has been released with allocate2().
private static native long allocate(long graphHandle);
private static native long allocate2(long graphHandle, String target, byte[] config);
private static native void delete(long handle);
/**
* Execute a session.
*
* <p>The author apologizes for the ugliness of the long argument list of this method. However,
* take solace in the fact that this is a private method meant to cross the JNI boundary.
*
* @param handle to the C API TF_Session object (Session.nativeHandle)
* @param runOptions serialized representation of a RunOptions protocol buffer, or null
* @param inputOpHandles (see inputOpIndices)
* @param inputOpIndices (see inputTensorHandles)
* @param inputTensorHandles together with inputOpHandles and inputOpIndices specifies the values
* that are being "fed" (do not need to be computed) during graph execution.
* inputTensorHandles[i] (which corresponds to a Tensor.nativeHandle) is considered to be the
* inputOpIndices[i]-th output of the Operation inputOpHandles[i]. Thus, it is required that
* inputOpHandles.length == inputOpIndices.length == inputTensorHandles.length.
* @param outputOpHandles (see outputOpIndices)
* @param outputOpIndices together with outputOpHandles identifies the set of values that should
* be computed. The outputOpIndices[i]-th output of the Operation outputOpHandles[i], It is
* required that outputOpHandles.length == outputOpIndices.length.
* @param targetOpHandles is the set of Operations in the graph that are to be executed but whose
* output will not be returned
* @param wantRunMetadata indicates whether metadata about this execution should be returned.
* @param outputTensorHandles will be filled in with handles to the outputs requested. It is
* required that outputTensorHandles.length == outputOpHandles.length.
* @return if wantRunMetadata is true, serialized representation of the RunMetadata protocol
* buffer, false otherwise.
*/
private static native byte[] run(
long handle,
byte[] runOptions,
long[] inputTensorHandles,
long[] inputOpHandles,
int[] inputOpIndices,
long[] outputOpHandles,
int[] outputOpIndices,
long[] targetOpHandles,
boolean wantRunMetadata,
long[] outputTensorHandles);
}
| tensorflow/tensorflow | tensorflow/java/src/main/java/org/tensorflow/Session.java |
10 | "/*\n * Licensed to Elasticsearch B.V. under one or more contributor\n * license agreements. See the(...TRUNCATED) | elastic/elasticsearch | libs/h3/src/main/java/org/elasticsearch/h3/HexRing.java |
End of preview. Expand
in Dataset Viewer.
README.md exists but content is empty.
Use the Edit dataset card button to edit it.
- Downloads last month
- 45