Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate color from genome #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions src/Bot.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.util.Arrays;

public class Bot {

private static final int LV_ORGANIC_HOLD = 1; // органика
Expand Down Expand Up @@ -662,7 +664,7 @@ private void botDouble() {
int ma = (int) (rand() * MIND_SIZE); // 0..63
int mc = (int) (rand() * MIND_SIZE); // 0..63
newbot.mind[ma] = (byte) mc;
newbot.c_family = getNewColor(c_family); // цвет семьи вычисляем новый
newbot.c_family = getNewColor(this); // цвет семьи вычисляем новый
}

newbot.direction = (int) (rand() * 8); // направление, куда повернут новорожденный, генерируется случайно
Expand All @@ -675,15 +677,21 @@ private void botDouble() {
World.simulation.matrix[xt][yt] = newbot; // отмечаем нового бота в массиве matrix
}

private int getNewColor(int parentColor) {
int r, g, b;
r = getRed(parentColor);
g = getGreen(parentColor);
b = getBlue(parentColor);

double delta = ((200000 / (World.simulation.generation + 1000)) + 20);

return getIntColor(vc(r + (int) (rand() * delta - delta / 2)), vc(g + (int) (rand() * delta - delta / 2)), vc(b + (int) (rand() * delta - delta / 2)));
public int getNewColor(Bot parent) {
int[] array = new int[MIND_SIZE];
for (int i = 0; i < MIND_SIZE; i++) {
array[i] = (int) (parent.mind[i]);
}
int thirdPart = (int) ((float) MIND_SIZE / 3);
float converter = (float) 256 / thirdPart;
int[] rArray = Arrays.copyOfRange(array, 0, thirdPart);
int[] gArray = Arrays.copyOfRange(array, thirdPart, thirdPart * 2);
int[] bArray = Arrays.copyOfRange(array, thirdPart * 2, MIND_SIZE);
return getIntColor(
(int) ((float) Arrays.stream(rArray).sum() / rArray.length * converter),
(int) ((float) Arrays.stream(gArray).sum() / gArray.length * converter),
(int) ((float) Arrays.stream(bArray).sum() / bArray.length * converter)
);
}


Expand Down
39 changes: 39 additions & 0 deletions src/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class World extends JFrame {
private int zoom;
int sealevel;
private int drawstep;
private int limit = 40000;
int[][] map; //Карта мира
private int[] mapInGPU; //Карта для GPU
private Image mapbuffer = null;
Expand All @@ -44,6 +45,9 @@ public void paint(Graphics g) {
private JLabel generationLabel = new JLabel(" Generation: 0 ");
private JLabel populationLabel = new JLabel(" Population: 0 ");
private JLabel organicLabel = new JLabel(" Organic: 0 ");
private JLabel seaLabel = new JLabel(" Sea: 0 ");
private JLabel limitLabel = new JLabel(" Limit: " + limit + " ");
private JSlider limitSlider = new JSlider(JSlider.HORIZONTAL, 0, 200, 50);

private JSlider perlinSlider = new JSlider (JSlider.HORIZONTAL, 0, 480, 300);
private JButton mapButton = new JButton("Create Map");
Expand Down Expand Up @@ -98,6 +102,12 @@ public World() {
organicLabel.setPreferredSize(new Dimension(140, 18));
organicLabel.setBorder(BorderFactory.createLoweredBevelBorder());
statusPanel.add(organicLabel);
seaLabel.setPreferredSize(new Dimension(140, 18));
seaLabel.setBorder(BorderFactory.createLoweredBevelBorder());
statusPanel.add(seaLabel);
limitLabel.setPreferredSize(new Dimension(140, 18));
limitLabel.setBorder(BorderFactory.createLoweredBevelBorder());
statusPanel.add(limitLabel);


JToolBar toolbar = new JToolBar();
Expand Down Expand Up @@ -148,6 +158,16 @@ public World() {
drawstepSlider.setAlignmentX(JComponent.LEFT_ALIGNMENT);
toolbar.add(drawstepSlider);

JLabel slider4Label = new JLabel("Population limit");
toolbar.add(slider4Label);
limitSlider.addChangeListener(new limitSliderChange());
limitSlider.setMajorTickSpacing(50);
limitSlider.setPaintTicks(true);
limitSlider.setPaintLabels(true);
limitSlider.setPreferredSize(new Dimension(100, drawstepSlider.getPreferredSize().height));
limitSlider.setAlignmentX(JComponent.LEFT_ALIGNMENT);
toolbar.add(limitSlider);


ButtonGroup group = new ButtonGroup();
group.add(baseButton);
Expand Down Expand Up @@ -178,6 +198,13 @@ public void stateChanged(ChangeEvent event) {
}
}

class limitSliderChange implements ChangeListener {
public void stateChanged(ChangeEvent event) {
int ds = limitSlider.getValue();
if (ds == 0) ds = 1;
limit = ds * 1000;
}
}
class drawstepSliderChange implements ChangeListener {
public void stateChanged(ChangeEvent event) {
int ds = drawstepSlider.getValue();
Expand Down Expand Up @@ -311,6 +338,9 @@ public void paint1() {
generationLabel.setText(" Generation: " + String.valueOf(generation));
populationLabel.setText(" Population: " + String.valueOf(population));
organicLabel.setText(" Organic: " + String.valueOf(organic));
seaLabel.setText(" Sealevel: " + String.valueOf(sealevel));
limitLabel.setText(" Limit: " + String.valueOf(limit));
sealevelSlider.setValue(sealevel);

buffer = buf;
canvas.repaint();
Expand All @@ -334,6 +364,15 @@ public void run() {
paint1(); // отображаем текущее состояние симуляции на экран
}
long time3 = System.currentTimeMillis();
// контроль популяции
// времена года
if (generation % 50 == 0 && 80 < sealevel && sealevel < 256) {
boolean biggerEq500 = generation % 1000 >= 500;
if ((population > limit && sealevel < 256) || biggerEq500)
sealevel++;
else if ((population < 10000 && sealevel > 120) || !biggerEq500)
sealevel--;
}
// System.out.println("Paint: " + (time3-time2));
}
started = false; // Закончили работу
Expand Down