NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

// Name(s): Philip Plant
// ID(s): 07009860


// 159.240 - Assignment 3 skeleton code.

#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <stdio.h>

// The Ship struct
struct Ship {
float x,y; // Every ship has a position
float vx,vy; // and a velocity
int team;


// These three data members are for targeting.
// Ignore them
bool target_acquired;
bool destroyed;
Ship* target;
};

// Parameters for each of the rules
const float g_cohesion_desire = 0.0004f; // how much a ship wants to stick to its neighbours
const float g_cohesion_radius = 100.0f; // in what radius are ships considered neighbours for this rule

const float g_separation_radius = 5.0f; // in what radius are ships too close to each other.
const float g_separation_affinity = 0.1; // how much a ship wants to stay separated.

const float g_alignment_desire = 0.002f; // how much a ship wants to align to its neighbours
const float g_alignment_radius = 100.0f; // the radius in which ships are considered neighbours for this rule


// Some general parameters
const int g_windowWidth = 1280; // how wide is the screen
const int g_windowHeight = 800; // how high is the screen
const float g_global_speed = 0.7f; // bullets and ships are slowed by this coefficient
const float g_max_velocity = 2.5f; // no ships can go faster than this number
const float g_velocity_degrade = 1.0f; // for 1.0f, degrading the velocity (ie. friction) is disabled. Set to 0.8 to see
const bool g_friendly_fire = false; // whether or not bullets can destroy any ship


const int g_initial_agent_count = 80; // Number of ships to start with
const float g_initial_speed = 0.9f; // The initial speed of the ships
const int g_team_count = 2;

const float g_target_pursuit_coefficient = 0.05f; // how much ships try to pursue their targets

const float g_bullet_speed = 4.9f; // how fast bullets travel in one timestep
const float g_bullet_close_distance = 4.0f; // if a bullet is closer than this, then the ship explodes


// These are some global parameters that keep track of how many ships are created and destroyed
unsigned int g_ships_destroyed = 0;
unsigned int g_ships_created = g_initial_agent_count;


const float g_scale = 4.0; // The scale size of the simulation



int RandInt(int x,int y) {
return rand()%(y-x+1)+x;
}
double RandFloat() {
return (rand())/(RAND_MAX+1.0);
}


// Some Win32 code is hidden in here. Please don't change support.h
#include "support.h"


// This function just makes sure that a ship can't go faster than g_max_velocity.
// Sometimes they just disappear if we don't do this.
void ClampVelocity(Ship *s) {
if (s->vx > g_max_velocity) s->vx = g_max_velocity;
if (s->vy > g_max_velocity) s->vy = g_max_velocity;

if (s->vx < -g_max_velocity) s->vx = -g_max_velocity;
if (s->vy < -g_max_velocity) s->vy = -g_max_velocity;
}

// This function applies the velocity of a ship to its position to move it along.
void MoveShip(Ship *s) {
s->x += s->vx * g_global_speed;
s->y += s->vy * g_global_speed;
}

// This function gets the distance as a float from ship a to ship b.
float GetDistanceBetween(Ship* a, Ship* b) {
float tx = b->x - a->x;
float ty = b->y - a->y;

return sqrtf(tx*tx + ty*ty);
}

//---------------- COHESION RULE ----------------
// This function adds the cohesion rule contribution to the
// velocity of the Ship pointer passed to it.
void AddCohesionRule(Ship *a) {
float avg_x, avg_y, count;
float distance = 0;
avg_x = 0;
avg_y = 0;
count = 0;
for(unsigned int i=0; i < g_agents.size(); i++){
distance = GetDistanceBetween(g_agents[i], a);
if(g_agents[i] != a && distance < g_cohesion_radius && g_agents[i]->team == a->team){
avg_x += (g_agents[i] -> x) - (a -> x);
avg_y += (g_agents[i] -> y) - (a -> y);
count ++;
}
}
if(count <= 0){count = 0.001;}
a -> vx += (avg_x/count)* g_cohesion_desire;
a -> vy += (avg_y/count)* g_cohesion_desire;
}

//---------------- SEPARATION RULE ----------------
void AddSeparationRule(Ship *a) {
float avg_x, avg_y, count;
float distance = 0;
avg_x = 0;
avg_y = 0;
count = 0;
for(unsigned int i=0; i < g_agents.size(); i++){
distance = GetDistanceBetween(g_agents[i], a);
if(g_agents[i] != a && distance < g_separation_radius && g_agents[i]->team == a->team){
avg_x += (g_agents[i] -> x) - (a -> x);
avg_y += (g_agents[i] -> y) - (a -> y);
count ++;
}
}
if(count <= 0){count = 0.001;}
a -> vx -= (avg_x/count)* g_separation_affinity;
a -> vy -= (avg_y/count)* g_separation_affinity;
}

//---------------- ALIGNMENT RULE ----------------
void AddAlignmentRule(Ship *a) {
float avg_x, avg_y, count;
float distance = 0;
avg_x = 0;
avg_y = 0;
count = 0;
for(unsigned int i=0; i < g_agents.size(); i++){
distance = GetDistanceBetween(g_agents[i], a);
if(g_agents[i] != a && distance < g_alignment_radius && g_agents[i]->team == a->team){
avg_x += g_agents[i] -> vx;
avg_y += g_agents[i] -> vy;
count ++;
}
}
if(count <= 0){count = 0.001;}
a -> vx += (avg_x/count)* g_alignment_desire;
a -> vy += (avg_y/count)* g_alignment_desire;
}


void AddAttackRule(Ship *a) {
a->target_acquired = true;

// First we have to find the closest ship to the Ship a.

// To do this, we set a minimum distance to the distance between Ship a, and Ship 0.
float min = GetDistanceBetween(g_agents[0], a);
a->target = NULL;
Ship *target = g_agents[0];
//a->target = g_agents[0]; // Keep track of which ship is the closest.

// We now loop through all the ships, and check to see if we can find a smaller distance
for (unsigned int m=0; m < g_agents.size(); ++m) {

float potnewmin = GetDistanceBetween(g_agents[m], a);

// here we check if our potential new minimum distance is better, and reassign the target
// if it is.
if (potnewmin < min && g_agents[m]->team != a->team) {
min = potnewmin;
target = g_agents[m];
}
}

if (target != NULL && target->team != a->team) {

// If we get in here, it means that we have a target to attack.
// What we need to do, is obtain a relative vector to the target,
// and shoot in that direction.

a->target = target;
float vx = a->target->x - a->x;
float vy = a->target->y - a->y;
float mag = sqrtf(vx*vx + vy*vy);
vx /= mag+0.0001f;
vy /= mag+0.0001f;
if (a->vx * vx + a->vy * vy > 0.8f) {
if (RandFloat() < 0.01f) {
// This function will actually shoot a bullet in the direction
// we give it.
FireBullet(a->x, a->y, vx*g_bullet_speed,vy*g_bullet_speed, a);
}
}

a->vx += vx * g_target_pursuit_coefficient;
a->vy += vy * g_target_pursuit_coefficient;
}
}

void DegradeVelocity(Ship* a) {
a->vx = a->vx * g_velocity_degrade;
a->vy = a->vy * g_velocity_degrade;
}




bool UpdateAgents() {
for (unsigned int i=0; i < g_agents.size(); ++i) {

AddCohesionRule(g_agents[i]); // Cohesion rule applied to Ship i
AddSeparationRule(g_agents[i]); // Separation rule applied to Ship i
AddAlignmentRule(g_agents[i]); // Alignment rule applied to Ship i

AddAttackRule(g_agents[i]); // Special attack rule applied to Ship i


DegradeVelocity(g_agents[i]); // This function will add some friction to slow things down

ClampVelocity(g_agents[i]); // Check that velocities aren't too big.
MoveShip(g_agents[i]); // Finally, move the ship by its velocity.

}

return true; // return true, everything went ok.
}



     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.