import java.applet.*; import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.text.*; import java.util.*; import java.util.zip.*; public class zombies extends BApplet { // Zombie Infection Simulation // Kevan Davis, 16/8/03 int freeze; int num=1000; int numweps = 6; int speed=1; int panic=5; int human; int panichuman; int zombie; int wall; int armedhuman, corpse, chainsaw; Being[] beings = new Being[4000]; Weapon[] weapons = new Weapon[10]; void setup() { int x,y; size(250,250); human=color(200,0,200); panichuman=color(255,120,255); zombie=color(0,255,0); wall=color(50,50,60); chainsaw = color(0,0,255); armedhuman = color(255,255,255); corpse = color(100, 100, 100); noBackground(); fill(color(0,0,0)); rect(0,0,width,height); fill(wall); stroke(color(0,0,0)); for (int i=0; i<100; i++) { rect((int)random(width-1)+1, (int)random(height-1)+1, (int)random(60)+10, (int)random(60)+10); } fill(color(0,0,0)); for (int i=0; i<30; i++) { rect((int)random(width-1)+1, (int)random(height-1)+1, (int)random(20)+20,(int)random(20)+20); x=(int)random(width-1)+1; y=(int)random(height-1)+1; } for(int i=0; i<4000; i++) { beings[i] = new Being(); beings[i].position(); } for(int i = 0; i < numweps; i ++ ) { weapons[i] = new Weapon(); weapons[i].position(); } beings[1].infect(); freeze=0; } void loop() { if (freeze==0) { for(int i=0; iwidth-1 || vx<1 || vy>height-1 || vy<1) { seenWall = true; continue; } int seeing = getPixel(vx,vy); if(seeing == chainsaw) return 6; if(seeing == armedhuman) return 5; if(seeing == panichuman) return 4; if(seeing == human) return 2; if(seeing == zombie) return 1; if(seeing == wall) seenWall = true; } } return seenWall ? 3 : 0; } void keyPressed() { if(key == ' ') { for(int i=0; i4) { speed=1; } } if(key == 'p' || key == 'P') { panic = 5 - panic; } if(key == 'g' || key == 'G') { if (zombie==color(155,155,155)) { zombie=color(0,255,0); } else { zombie=color(155,155,155); } } if(key == '+' && num < 4000) { num += 100; key='z'; } if(key == '-' && num > 100) { num -= 100; key='z'; } if(key == 'z' || key == 'Z') { freeze=1; setup(); } if(key == 'b' || key == 'B') { for(int i = 0; i < num; i ++) { if(beings[i].type == 2) { beings[i].type = 5; } } } } class Being { int xpos, ypos, dir; int avoid; int type, active; int panicimmunity; Being() { dir = (int)random(4)+1; avoid = -1; type = 2; active = 0; } void position() { for (int ok=0; ok<100; ok++) { xpos = (int)random(width-1)+1; ypos = (int)random(height-1)+1; if (getPixel(xpos,ypos)==color(0,0,0)) break; } } boolean infect(int x, int y) { if (x==xpos && y==ypos) { type = 1; return true; } return false; } boolean attack(int x, int y, Being attacker) { if(type == 1) { /* // There's a 10% chance that the zombie will kill the attacker if(random(100) <= 10) { // BRAINS! attacker.infect(); } else { */ type = 7; // Corpse setPixel(xpos, ypos, corpse); return true; // } } return false; } boolean infect() { type = 1; setPixel(xpos, ypos, zombie); avoid = -1; return true; } void uninfect() { type = 2; } void move() { // Corpses don't move if(type == 7) { setPixel(xpos, ypos, corpse); return; } int r = (int)random(10); if (((type==2 || type == 5) && (active>0 || r>panic)) || r==1) { setPixel(xpos, ypos, color(0,0,0)); if (look(xpos,ypos,dir,1)==0) { if (dir==1) { ypos--; } if (dir==2) { xpos++; } if (dir==3) { ypos++; } if (dir==4) { xpos--; } } else // We're blocked, so change direction { do { dir = (int)random(4)+1; } while(dir == avoid); } if (type == 1) { setPixel(xpos, ypos, zombie); } else if (type == 2 && active > 0) { setPixel(xpos, ypos, panichuman); } else if (type == 5) { setPixel(xpos, ypos, armedhuman); } else { setPixel(xpos, ypos, human); } if (active>0) { active--; if(active == 0) { avoid = -1; } } } int target = look(xpos,ypos,dir,10); switch(type) { case 1: // Zombie if (target==2 || target==4 || target == 5) { active = 10; } if (active==0 && target!=1) { dir = (int)random(4)+1; } int victim = look(xpos,ypos,dir,1); if (victim == 2 || victim==4 || victim == 5) { int ix = xpos; int iy = ypos; if (dir==1) { iy--; } if (dir==2) { ix++; } if (dir==3) { iy++; } if (dir==4) { ix--; } for(int i=0; i4) { dir = dir - 4; } avoid = dir; panicimmunity = 0; } else if (target == 4) { panicimmunity += 10; if(panicimmunity > 1000) { // We've gotten used to panicking people and have calmed down again active = 0; } } else { panicimmunity -= 1; } if ((int)random(8)==1) { dir = (int)random(4)+1; } victim = look(xpos, ypos, dir, 1); if (victim == 6) { // Chainsaw! int ix = xpos, iy = ypos; switch(dir) { case 1: iy--; break; case 2: ix++; break; case 3: iy++; break; case 4: ix--; break; } for(int i = 0; i < numweps; i++) { if(weapons[i].pickup(ix, iy) == true) { type = 5; break; } } } break; case 5: // Armed human if (target == 1) { active = 10; } victim = look(xpos, ypos, dir, 1); if (victim == 1) { // Zombie sighted int ix = xpos, iy = ypos; switch(dir) { case 1: iy--; break; case 2: ix++; break; case 3: iy++; break; case 4: ix--; break; } for(int i = 0; i< num; i++) { if(beings[i].attack(ix, iy, this)) break; } } break; } } } public class Weapon extends Being { boolean pickup(int x, int y) { if(x == xpos && y == ypos) { setPixel(xpos, ypos, color(0,0,0)); position(); move(); return true; } return false; } Weapon() { dir = (int)random(4)+1; avoid = -1; type = 6; active = 0; } void move() { setPixel(xpos, ypos, chainsaw); } } }