Skip to content

Instantly share code, notes, and snippets.

@oxbowmantella
Forked from dribnet/.block
Last active June 8, 2017 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oxbowmantella/ad69650f3b3a0bc9e10b85d7c570bbd7 to your computer and use it in GitHub Desktop.
Save oxbowmantella/ad69650f3b3a0bc9e10b85d7c570bbd7 to your computer and use it in GitHub Desktop.
17.1.MDDN242 PS4
license: mit
// note: this file is poorly named - it can generally be ignored.
// helper functions below for supporting blocks/purview
function saveBlocksImages(doZoom) {
if(doZoom == null) {
doZoom = false;
}
// generate 960x500 preview.jpg of entire canvas
// TODO: should this be recycled?
var offscreenCanvas = document.createElement('canvas');
offscreenCanvas.width = 960;
offscreenCanvas.height = 500;
var context = offscreenCanvas.getContext('2d');
// background is flat white
context.fillStyle="#FFFFFF";
context.fillRect(0, 0, 960, 500);
context.drawImage(this.canvas, 0, 0, 960, 500);
// save to browser
var downloadMime = 'image/octet-stream';
var imageData = offscreenCanvas.toDataURL('image/jpeg');
imageData = imageData.replace('image/jpeg', downloadMime);
p5.prototype.downloadFile(imageData, 'preview.jpg', 'jpg');
// generate 230x120 thumbnail.png centered on mouse
offscreenCanvas.width = 230;
offscreenCanvas.height = 120;
// background is flat white
context = offscreenCanvas.getContext('2d');
context.fillStyle="#FFFFFF";
context.fillRect(0, 0, 230, 120);
if(doZoom) {
// pixelDensity does the right thing on retina displays
var pd = this._pixelDensity;
var sx = pd * mouseX - pd * 230/2;
var sy = pd * mouseY - pd * 120/2;
var sw = pd * 230;
var sh = pd * 120;
// bounds checking - just displace if necessary
if (sx < 0) {
sx = 0;
}
if (sx > this.canvas.width - sw) {
sx = this.canvas.width - sw;
}
if (sy < 0) {
sy = 0;
}
if (sy > this.canvas.height - sh) {
sy = this.canvas.height - sh;
}
// save to browser
context.drawImage(this.canvas, sx, sy, sw, sh, 0, 0, 230, 120);
}
else {
// now scaledown
var full_width = this.canvas.width;
var full_height = this.canvas.height;
context.drawImage(this.canvas, 0, 0, full_width, full_height, 0, 0, 230, 120);
}
imageData = offscreenCanvas.toDataURL('image/png');
imageData = imageData.replace('image/png', downloadMime);
p5.prototype.downloadFile(imageData, 'thumbnail.png', 'png');
}

PS4 MDDN 242 2017

Virus Journey 4 (final)

My intial concept was the make something that related to gaming, i.e. pcs VS console, however i ended up changing that, i wanted to display the spread of computer viruses and how they can spread around the world, this was because of the recent "Wanna Cry" virus leak that was encrypting PCs and locking people out.

the idea in my simlulation is that 2 viruses spawn in (in view size 32) and then move around the world, infecting pcs and servers, which, where some could be connected to each other or be solo pcs/server.

a pc can be recovered with a simple factory re-set, so that is why i made it so they are "repairable", however i didnt give this functionality to servers, since I figured that if the server was set up in some kind of data raid array, WITHOUT redudant backups, and were to get infected by a virus it would essentionally mean that the whole server would need to be reset, and since that isnt as easy and factory reseting a pc, i had them "fall of the grid".

the virus doesnt have a life span because during the "Wanna Cry" out break, pcs that werent up to date with updates were vulnerable to getting the virus, so not having a life span signifies that even though it can be contained, there will generally be cases where it still exists.

SIMULATION RULES (optimal running size:32; 64(no virus),16(laggy):
spawnned in agents:

  • all pcs are un-effected my the virus
  • all servers are un-effected my the virus
  • all near by servers and pcs are conneted (with a red line, as data connection)

once virus spawns in:

  • all pcs have a 90% chance on infection
  • servers have a 70% chance on infection
virus rules:
  • all viruses rome free on the feild
  • pc infections can be repaired(by click, to show repair)
  • servers can not be repaied and are disabled(to represent a data raid going corrupt)
  • if a pc is infected then all connecting pc links are broken to it

function Agent1() {
// any variables you add here are for your own internal state
this.is_alive = false;
this.was_alive = false;
// setup is run when the agent is reset
// value is a number between 0 and 100
this.setup = function(value) {
if(value > 70) {
this.is_alive = true;
}
else {
this.is_alive = false;
}
this.was_alive = this.is_alive;
}
// this happens generally on mouse over
this.activate = function() {
this.is_alive = true;
}
// decide on your next move based on neighbors (but don't do it yet)
this.step = function(neighbors) {
var num_neighbors_alive = 0;
for(var i=0; i<neighbors.length; i++){
if(neighbors[i].agent.is_alive){
num_neighbors_alive = num_neighbors_alive + 1;
}
}
//lonelyness
if(this.is_alive && num_neighbors_alive < 2){
this.next_alive = false;
}
//overpopulation
else if(this.is_alive && num_neighbors_alive > 8){
this.next_alive = false;
}
//reproduction
else if(this.is_alive == false && num_neighbors_alive == 3){
this.next_alive = true;
}
//statis
else{
this.next_alive = this.is_alive;
}
}
// execute the next move you decided on
this.update_state = function() {
this.was_alive = this.is_alive;
this.is_alive = this.next_alive;
}
this.draw = function(size) {
if(this.is_alive == true && this.was_alive == true) {
image(aliveImage, 0, 0, size, size);
}
//was alive now dead
else if(this.is_alive == true && this.was_alive == false) {
image(angryImage, 0, 0, size, size);
}
//was dead now alive
else if(this.is_alive == false && this.was_alive == true) {
image(sadImage, 0, 0, size, size);
}
else {
image(deadImage, 0, 0, size, size);
}
}
}
function Agent2() {
// any variables you add here are for your own internal state
this.power = 0.0;
this.next_power = 0.0;
// setup is run when the agent is reset
// value is a number between 0 and 100
this.setup = function(value, agent_type) {
this.power = value;
this.agent_type = agent_type;
this.next_power = this.power;
}
// this happens generally on mouse over
this.activate = function() {
this.power = 100.0;
}
// decide on your next move based on neighbors (but don't do it yet)
this.step = function(neighbors) {
var surrounding_power = 0;
var death_limit1 = 49.9999;
var death_limit2 = 50.0001;
for(var i=0; i<neighbors.length; i++) {
surrounding_power = surrounding_power + neighbors[i].agent.power;
}
var avg_power = surrounding_power / neighbors.length;
if(this.agent_type == 0) {
if(avg_power < death_limit1) {
this.next_power = 0;
}
else {
this.next_power = 100;
}
}
else {
if(avg_power < death_limit2) {
this.next_power = 0;
}
else {
this.next_power = 100;
}
}
if(this.next_power > 100) {
this.next_power = 100;
}
}
// execute the next move you decided on
this.update_state = function() {
this.power = this.next_power;
}
this.draw = function(size) {
var half_size = size/2;
var low = color(0, 0, 0);
var high = color(255, 255, 255);
var c = lerpColor(low, high, this.power / 100.0);
stroke(0);
fill(c);
ellipse(half_size, half_size, size, size);
}
}
var virus; //virus pic
var comp; //computer pic
var iComp; //infected computer pic
var server; //server pic
var iSever; //infected server pic
var ran;
function Agent3Preload() {
iComp = loadImage("infected_comp.png");
virus = loadImage("virus.png");
comp = loadImage("comp.png");
server = loadImage("server.png");
iServer = loadImage("iServer.png");
}
function Agent3() {
this.virus_infect = false;
// any variables you add here are for your own internal state
// setup is run when the agent is reset
// value is a number between 0 and 100
this.setup = function(value, agent_type) {
this.agent_type = agent_type;
this.neighbor_lines = []; //line drawing array for data line
if(this.agent_type == 4){
this.virus_infect =true;
}
}
// this happens generally on mouse over
this.activate = function(neighbors) {
if(this.virus_infect = true && this.agent_type == 3){
this.agent_type = 4;
this.virus_infect = true;
}
}
// decide on your next move based on neighbors (but don't do it yet)
this.step = function(neighbors, radius) {
var ran = random(100);
this.neighbor_lines = [];
if (this.agent_type == 4 || this.agent_type == 1){ //connect if the pc and server are !infected
for(var i=0; i<neighbors.length; i++) {
var pos = neighbors[i].pos;
if (neighbors[i].agent.agent_type>0){
this.neighbor_lines.push([pos.x, pos.y]); //array of positions to draw constellation lines to
}
}
}
this.close = false;
if(this.agent_type == 1) { //server infection code
for(var i=0; i<neighbors.length; i++) {
var neighbor_type = neighbors[i].agent.agent_type;
if(neighbors[i].agent.agent_type == 2){
if(ran <= 70){
this.agent_type = 0;
}
}
}
}
else if(this.agent_type == 0) { //infected server fall off
for(var i=0; i<neighbors.length; i++) {
var neighbor_type = neighbors[i].agent.agent_type;
if(neighbors[i].agent.agent_type == 0 && neighbors[i].agent.agent_type == 4){
this.agent_type = 3;
}
}
return createVector(0, radius);
}
else if(this.agent_type == 4) { //pc --> infected pc
if(ran <= 90){
for(var i=0; i<neighbors.length; i++) {
var neighbor_type = neighbors[i].agent.agent_type;
if(neighbors[i].agent.agent_type == 2){
this.agent_type = 3;
}
}
}
}
else if(this.agent_type == 2) { //virus
v = p5.Vector.random2D().mult(radius*4);
if(this.agent_type == 2) {
for(var i=0; i<neighbors.length; i++) {
var npos = neighbors[i].pos;
// var neighbor_type = neighbors[i].agent.agent_type;
var d = npos.mag();
if ((d > 0) && (d < radius + neighbors[i].radius)) {
this.close = true;
// Calculate vector pointing away from neighbor
var move_away = npos.mult(-1);
move_away.normalize();
move_away.div(d*0.1); // Weight by distance
v.add(move_away);
}
}
}
return v;
}
}
this.draw = function(radius) {
//print(ran);
stroke(189,27,42);
for(var i=0; i<this.neighbor_lines.length; i++) {
var dataline = this.neighbor_lines[i];
line(0, 0, dataline[0], dataline[1]);
}
stroke(0);
if(this.close) {
}
else if(this.agent_type == 0) { //nfected server
image(iServer, 0, 0, radius*4, radius*4);
}
else if(this.agent_type == 1) { // server
image(server, radius-(radius/2),radius-(radius/2), radius*4, radius*4);
}
else if(this.agent_type == 2) { //virus
image(virus, 0, 0, radius*3, radius*3);
}
else if(this.agent_type == 3) { //infected computer
image(iComp, 0, 0, radius*3, radius*3);
}
else if(this.agent_type == 4) { //pc
image(comp, 0, 0, radius*3, radius*3);
}
}
}
<head>
<script src="http://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.7/p5.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.7/addons/p5.dom.js"></script>
<script language="javascript" type="text/javascript" src=".purview_helper.js"></script>
<script language="javascript" type="text/javascript" src="agent3.js"></script>
<script language="javascript" type="text/javascript" src="sketch.js"></script>
<style>
body { padding: 0; margin: 0; }
.inner { position: absolute; }
#controls {
font: 300 12px "Helvetica Neue";
padding: 5;
margin: 5;
background: #f0f0f0;
opacity: 0.0;
-webkit-transition: opacity 0.2s ease;
-moz-transition: opacity 0.2s ease;
-o-transition: opacity 0.2s ease;
-ms-transition: opacity 0.2s ease;
}
#controls:hover { opacity: 0.9; }
</style>
</head>
<body style="background-color:white">
<div class="outer">
<div class="inner">
<div id="canvasContainer"></div>
</div>
<div class="inner" id="controls" height="500px">
<table>
<tr>
<td>World</td>
<td id="selector1Container"></td>
</tr>
<tr>
<td>Size</td>
<td id="selector2Container"></td>
</tr>
<tr>
<td>Speed</td>
<td id="selector3Container"></td>
</tr>
<tr>
<td></td>
<td id="playButtonContainer"></td>
</tr>
<tr>
<td></td>
<td id="clearButtonContainer"></td>
</tr>
<tr>
<td></td>
<td id="checkContainer"></td>
</tr>
</div>
</div>
</table>
</body>
{
"commits": [
{
"sha": "3ceebfe2025a77c67b9560f87d1ca19d6fb6a2b8",
"name": "Virus Journey FINAL"
},
{
"sha": "32ad6257573904fc60067440f1eb68e47682cff4",
"name": "Virus Journey 3"
},
{
"sha": "5ace4e67e300aec6fe7523ca1f036334e4a438dd",
"name": "Virus Journey 2"
},
{
"sha": "1ce0eb77a1f2a92d91de533fe8e2eee339e8db1c",
"name": "Virus Journey 1"
},
{
"sha": "4d0be253d7af2c4a5798f3faca6a724fbca154f6",
"name": "Direction and Cell Types"
},
{
"sha": "7ca949d1c08c2aaf94ca286431b3ab7a7bcbc36c",
"name": "Grid Simulation"
},
{
"sha": "cf91e338127af6251c6df1cf94848b4d6252b7cd",
"name": "Sketch"
}
]
}
View raw

(Sorry about that, but we can’t show files that are this big right now.)

View raw

(Sorry about that, but we can’t show files that are this big right now.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment