library(ggplot2)

library(ggthemes)

#SHIP

#dominator, hullhp, shieldregen, shieldmax, startingarmor, widthinpixels, armorcells

ship <- c(14000, 500, 10000, 1500, 180, 15)

#engagementrange

range <- 1000

#weaponaccuracy - this will be made a function of time and weapon later. the accuracy of a hellbore is 10

acc <- 10

#fudge factor

errorsd <- 0.00

#the fudge factor should be a function of range (more error in position at greater range), but not a function of weapon firing angle, and be expressed in terms of pixels

error <- errorsd*range

#where does one shot hit within the weapon's arc of fire, in pixel difference from the target? get a random angle in degrees according to a uniform distribution,

#then consider that the circumference is 2 pi * range pixels, so the hit coordinates in pixels are

shotangle <- function(acc) return(runif(1,-acc/2,acc/2)/360*2*pi*range)

#how much is the visual arc of the ship in rad?

shipangle <- ship[5]/(2* pi *range)

#how much is the visual arc of a single cell of armor in rad?

cellangle <- shipangle/ship[6]

#now assume the weapon is targeting the center of the ship's visual arc and that the ship is in the center of the weapon's firing arc

#which cell will the shot hit, or will it miss?

#call the cells (MISS, cell1, cell2, ... ,celli, MISS) and get a vector giving the (maximum for negative / minimum for positive) angles for hitting each

anglerangevector <- vector(mode="double", length = ship[6]+1)

anglerangevector[1] <- -shipangle/2

for (i in 1:(length(anglerangevector)-1)) anglerangevector[i+1] <- anglerangevector*+cellangle*

#now convert it to pixels

anglerangevector <- anglerangevector*2*pi*range

#this vector will store the hits

shipcellvector <- vector(mode="double", length = ship[6]+2)

#now add a random positional error to the coordinates of the hit

hitlocation <- function(acc){

location <- shotangle(acc)

location <- location + rnorm(1,0,error)

return(location)

}

#so which box was hit?

cellhit <- function(angle){

if(angle < anglerangevector[1]) return(1)

if(angle > anglerangevector[ship[6]+1]) return(ship[6]+2)

for (i in 1:length(anglerangevector)) {

if ((angle > anglerangevector*) & (angle <= anglerangevector[i+1])) return(i+1)*

}

}

# this function generates the shot distribution per 1 shot with 100000 samples

createdistribution <- function(acc){

distributionvector <- vector(mode="double", length = ship[6]+2)

for (i in 1:100000){

wherehit <- cellhit(hitlocation(acc))

distributionvector[wherehit] <- distributionvector[wherehit] +1

}

return(distributionvector/sum(distributionvector))

}

# this is the default distribution of damage to armor cells

b <- matrix(0,nrow=5,ncol=5)

b[1:5,2:4] <- 1/30

b[2:4,1:5] <- 1/30

b[2:4,2:4] <- 1/15

b[1,1] <- 0

b[1,5] <- 0

b[5,1] <- 0

b[5,5] <- 0

#this function generates a sum of matrices multiplied by the distribution

createhitmatrix <- function(acc){

hitmatrix <- matrix(0,5,ship[6]+4)

distributionvector <- createdistribution(acc)

for (i in 1:ship[6]){

hitmatrix[,i:(i+4)] <- hitmatrix[,i:(i+4)]+b*(distributionvector[i+1])

}

return(hitmatrix)

}

hitchance <- function(acc){

hitchance <- 0

distributionvector <- createdistribution(acc)

hitchance <- (1-(distributionvector[1]+distributionvector[ship[6]]))

return(hitchance)

}

#for weapons with damage changing over time we need a sequence of matrices

createhitmatrixsequence <- function(accvector){

hitmatrixsequence <- list()

for (i in 1:length(accvector)){

hitmatrixsequence[*] <- createhitmatrix(accvector**)*

}

return(hitmatrixsequence)

}

armordamage <- function(damage, armor, startingarmor) damage*(max(0.15,damage/(damage+max(0.05*startingarmor,armor))))

armordamageselectivereduction <- function(damage, armor,startingarmor) {

useminarmor <- 0

if(armor < 0.05*startingarmor / 15) useminarmor <- 1

if(useminarmor == 0){

if(armor == 0) {return (damage)}

return(damage*(max(0.15,damage/(damage+armor))))

}

else{

return(damage*(max(0.15,damage/(damage+0.05*startingarmor/15))))

}

}

#squall fires 2 missiles / sec for 10 secs, then recharges for 10 secs

squalltics <- c(2,2,2,2,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0,0)

#locust fires 10 missiles / sec for 4 secs, then recharges for 5 secs

locusttics <- c(10,10,10,10,0,0,0,0,0)

#hurricane fires 9 missiles every 15 seconds

hurricanetics <- c(9,0,0,0,0,0,0,0,0,0,0,0,0,0,0)

#harpoon pod fires 4 missiles in 1 second, then recharges for 8 seconds (in reality 8.25)

harpoontics <- c(4,0,0,0,0,0,0,0,0)

#sabot pod fires 2*5 missiles, then recharges for 8 seconds (in reality 8.75 seconds and firing time is .5 seconds)

sabottics <- c(10,0,0,0,0,0,0,0,0)

#gauss fires 1 shot, then charges for 1 second

gausstics <- c(1,0)

#hephaestus fires 4 shots / sec

hephaestustics <- c(4)

#mark ix fires 4 shots every 3 seconds

markixtics <- c(4,0,0)

#mjolnir fires 4 shots per 3 seconds. Since there is no neat way to handle this when time is discrete at 1 sec we'll handle as follows

mjolnirtics <- c(1,1,2,1,2,1,2,1,1)

#hellbore fires 1 shot per 4 seconds

hellboretics <- c(1,0,0,0)

#storm needler fires 10 shots per second

stormneedlertics <- c(10)

#WEAPON ACCURACY

#missiles do not have spread

squallacc <- c(0)

locustacc <- c(0)

hurricaneacc <- c(0)

harpoonacc <- c(0)

sabotacc <- c(0)

#gauss has a spread of 0 and no increase per shot

gaussacc <- c(0)

#hephaestus has a spread of 0 and it increases by 2 per shot to a max of 10

hephaestusacc <- c(seq(0,10,2))

#mark ix has a spread of 0 and it increases by 2 per shot to a max of 15

markixacc <- c(seq(0,15,2),15)

#mjolnir has a spread of 0 and it increases by 1 per shot to a max of 5

mjolniracc <- c(seq(1,5,1))

#hellbore has a spread of 10

hellboreacc <- c(10)

#storm needler has a spread of 10

stormneedleracc <- c(10)

#damage per shot, damage type (2=kinetic, 0.5=he, 0.25=frag, 1=energy), tics, weapon name, weapon accuracy over time, hit chance

squall <- list(250, 2, squalltics, "Squall", squallacc)

locust <- list(200, 0.25, locusttics, "Locust", locustacc)

hurricane <- list(500, 0.5, hurricanetics, "Hurricane", hurricaneacc)

harpoon <- list(750, 0.5, harpoontics, "Harpoon", harpoonacc)

sabot <- list(200, 2, sabottics, "Sabot", sabotacc)

gauss <- list(700, 2, gausstics, "Gauss", gaussacc)

hephaestus <- list(120, 0.5, hephaestustics, "Hephaestus", hephaestusacc)

markix <- list(200, 2, markixtics, "Mark IX", markixacc)

mjolnir <- list(400, 1, mjolnirtics, "Mjolnir", mjolniracc)

hellbore <- list(750, 0.5, hellboretics, "Hellbore", hellboreacc)

stormneedler <- list(50, 2, stormneedlertics, "Storm Needler", stormneedleracc)

dummy <- list(0,0,c(0),"",c(0),c(0))

weapon1 <- squall

weapon2 <- squall

weapon3 <- harpoon

weapon4 <- harpoon

weapon5 <- hephaestus

weapon6 <- gauss

#now create the sequences of hit matrices and hit chances for each weapon

if(weapon1[4] != ""){

hitchancevector <- vector(mode = "double", length = length(weapon1[[5]]))

for (i in 1:length(weapon1[[5]])){

hitchancevector* <- hitchance(weapon1[[5]]**)*

}

hitchancevector

weapon1[[6]] <- hitchancevector

weapon1[[7]] <- createhitmatrixsequence(weapon1[[5]])

}

if(weapon2[4] != ""){

hitchancevector <- vector(mode = "double", length = length(weapon2[[5]]))

for (i in 1:length(weapon2[[5]])){

hitchancevector* <- hitchance(weapon2[[5]]**)*

}

hitchancevector

weapon2[[6]] <- hitchancevector

weapon2[[7]] <- createhitmatrixsequence(weapon2[[5]])

}

if(weapon3[4] != ""){

hitchancevector <- vector(mode = "double", length = length(weapon3[[5]]))

for (i in 1:length(weapon3[[5]])){

hitchancevector* <- hitchance(weapon3[[5]]**)*

}

hitchancevector

weapon3[[6]] <- hitchancevector

weapon3[[7]] <- createhitmatrixsequence(weapon3[[5]])

}

if(weapon4[4] != ""){

hitchancevector <- vector(mode = "double", length = length(weapon4[[5]]))

for (i in 1:length(weapon4[[5]])){

hitchancevector* <- hitchance(weapon4[[5]]**)*

}

hitchancevector

weapon4[[6]] <- hitchancevector

weapon4[[7]] <- createhitmatrixsequence(weapon4[[5]])

}

if(weapon5[4] != ""){

hitchancevector <- vector(mode = "double", length = length(weapon5[[5]]))

for (i in 1:length(weapon5[[5]])){

hitchancevector* <- hitchance(weapon5[[5]]**)*

}

hitchancevector

weapon5[[6]] <- hitchancevector

weapon5[[7]] <- createhitmatrixsequence(weapon5[[5]])

}

if(weapon6[4] != ""){

hitchancevector <- vector(mode = "double", length = length(weapon6[[5]]))

for (i in 1:length(weapon6[[5]])){

hitchancevector* <- hitchance(weapon6[[5]]**)*

}

hitchancevector

weapon6[[6]] <- hitchancevector

weapon6[[7]] <- createhitmatrixsequence(weapon6[[5]])

}

shieldblock <- 0

shielddamageattimepoint <- function(weapon, timepoint){

nohits <- weapon[[3]][(timepoint %% (length(weapon[[3]])))+1]

if (nohits == 0) {return(0)} else {

return(weapon[[1]]*nohits)

}

}

damageattimepoint <- function(weapon, timepoint, armor, startingarmor, hitx, hity, shots){

# vectors in R are indexed starting from 1

hitmatrix <- weapon[[7]][[min(shots,length(weapon[[7]]))]]

nohits <- weapon[[3]][(timepoint %% (length(weapon[[3]])))+1]

if (nohits == 0) {return(0)} else {

damagesum <- 0

for (i in 1:nohits) {

# damagesum <- damagesum + as.double(weapon[[1]]*hitmatrix[hitx,hity])

damagesum <- damagesum + armordamageselectivereduction(as.double(weapon[[1]]*hitmatrix[hitx,hity]),armor,startingarmor)

shots <- shots + 1

hitmatrix <- weapon[[7]][[min(shots,length(weapon[[7]]))]]

}

return(damagesum)

}

}

weapon1shots <- 1

weapon2shots <- 1

weapon3shots <- 1

weapon4shots <- 1

weapon5shots <- 1

weapon6shots <- 1

armormatrix <- matrix(ship[4]/15,5,ship[6]+4)

timeseries <- function(timepoint, shieldhp, armorhp, hullhp, shieldregen, shieldmax, startingarmor,armormatrix){

weaponacc <- 0

#are we using shield to block?

shieldblock <- 0

hulldamage <- 0

if(hullhp > 0){} else {shieldhp <- 0}

#1. shields. if shieldhp is sufficient, use shield to block

if (weapon1[[4]] !=""){

weapon1mult <- unlist(weapon1[2])

if (shieldhp > shielddamageattimepoint(weapon1, timepoint)*weapon1mult){

shieldhp <- shieldhp - shielddamageattimepoint(weapon1, timepoint)*weapon1mult*weapon1[[6]][min(weapon1shots,length(weapon1[[6]]))]

shieldhp <- max(shieldhp, 0)

if(shielddamageattimepoint(weapon1,timepoint) > 0) {shieldblock <- 1}

} else {

#if you did not use shield to block, regenerate flux

#2. armor and hull

if(unlist(weapon1[2])==0.25){weapon1mult = 0.25} else {weapon1mult= 1 / unlist(weapon1[2])}

#2.1. damage armor and hull

hulldamage <- 0

#damageattimepoint <- function(weapon, timepoint, armor, startingarmor, hitx, hity, shots){

for (j in 2:9){

for (i in 2:4){

hulldamage <- hulldamage+max(0,weapon1mult*damageattimepoint(weapon1, timepoint,armormatrix[i,j],startingarmor,i,j,weapon1shots)-armormatrix[i,j])/weapon1mult

armormatrix[i,j] <- max(0,armormatrix[i,j]-weapon1mult*damageattimepoint(weapon1, timepoint,armormatrix[i,j],startingarmor,i,j,weapon1shots)*weapon1[[6]][min(weapon1shots,length(weapon1[[6]]))])

}}

for (j in 2:9){

for (i in c(1,5)){

hulldamage <- hulldamage+max(0,weapon1mult*damageattimepoint(weapon1, timepoint,armormatrix[i,j],startingarmor,i,j,weapon1shots)-armormatrix[i,j])/weapon1mult

armormatrix[i,j] <- max(0,armormatrix[i,j]-weapon1mult*damageattimepoint(weapon1, timepoint,armormatrix[i,j],startingarmor,i,j,weapon1shots)*weapon1[[6]][min(weapon1shots,length(weapon1[[6]]))])

}}

for (j in c(1,10)){

for (i in 2:4){

hulldamage <- hulldamage+max(0,weapon1mult*damageattimepoint(weapon1, timepoint,armormatrix[i,j],startingarmor,i,j,weapon1shots)-armormatrix[i,j])/weapon1mult

armormatrix[i,j] <- max(0,armormatrix[i,j]-weapon1mult*damageattimepoint(weapon1, timepoint,armormatrix[i,j],startingarmor,i,j,weapon1shots)*weapon1[[6]][min(weapon1shots,length(weapon1[[6]]))])

}}

hullhp <- hullhp - hulldamage*weapon1[[6]][min(weapon1shots,length(weapon1[[6]]))]

hullhp <- max(hullhp, 0)

armorhp <- sum(armormatrix)/(46/15)

if(hullhp==0) armorhp <- 0

}

}

weapon1shots <- weapon1shots + weapon1[[3]][(timepoint %% (length(weapon1[[3]])+1))]

#repeat for other weapons

if (weapon2[[4]] !=""){

weapon2mult <- unlist(weapon2[2])

if (shieldhp > shielddamageattimepoint(weapon2, timepoint)*weapon2mult){

shieldhp <- shieldhp - shielddamageattimepoint(weapon2, timepoint)*weapon2mult*weapon2[[6]][min(weapon2shots,length(weapon2[[6]]))]

shieldhp <- max(shieldhp, 0)

if(shielddamageattimepoint(weapon2,timepoint) > 0) {shieldblock <- 1}

} else {

#if you did not use shield to block, regenerate flux

#2. armor and hull

if(unlist(weapon2[2])==0.25){weapon2mult = 0.25} else {weapon2mult= 1 / unlist(weapon2[2])}

#2.1. damage armor and hull

hulldamage <- 0

#damageattimepoint <- function(weapon, timepoint, armor, startingarmor, hitx, hity, shots){

for (j in 2:9){

for (i in 2:4){

hulldamage <- hulldamage+max(0,weapon2mult*damageattimepoint(weapon2, timepoint,armormatrix[i,j],startingarmor,i,j,weapon2shots)-armormatrix[i,j])/weapon2mult

armormatrix[i,j] <- max(0,armormatrix[i,j]-weapon2mult*damageattimepoint(weapon2, timepoint,armormatrix[i,j],startingarmor,i,j,weapon2shots)*weapon2[[6]][min(weapon2shots,length(weapon2[[6]]))])

}}

for (j in 2:9){

for (i in c(1,5)){

hulldamage <- hulldamage+max(0,weapon2mult*damageattimepoint(weapon2, timepoint,armormatrix[i,j],startingarmor,i,j,weapon2shots)-armormatrix[i,j])/weapon2mult

armormatrix[i,j] <- max(0,armormatrix[i,j]-weapon2mult*damageattimepoint(weapon2, timepoint,armormatrix[i,j],startingarmor,i,j,weapon2shots)*weapon2[[6]][min(weapon2shots,length(weapon2[[6]]))])

}}

for (j in c(1,10)){

for (i in 2:4){

hulldamage <- hulldamage+max(0,weapon2mult*damageattimepoint(weapon2, timepoint,armormatrix[i,j],startingarmor,i,j,weapon2shots)-armormatrix[i,j])/weapon2mult

armormatrix[i,j] <- max(0,armormatrix[i,j]-weapon2mult*damageattimepoint(weapon2, timepoint,armormatrix[i,j],startingarmor,i,j,weapon2shots)*weapon2[[6]][min(weapon2shots,length(weapon2[[6]]))])

}}

hullhp <- hullhp - hulldamage*weapon2[[6]][min(weapon2shots,length(weapon2[[6]]))]

hullhp <- max(hullhp, 0)

armorhp <- sum(armormatrix)/(46/15)

if(hullhp==0) armorhp <- 0

}

}

weapon2shots <- weapon2shots + weapon2[[3]][(timepoint %% (length(weapon2[[3]])))+1]

if (weapon3[[4]] !=""){

weapon3mult <- unlist(weapon3[2])

if (shieldhp > shielddamageattimepoint(weapon3, timepoint)*weapon3mult){

shieldhp <- shieldhp - shielddamageattimepoint(weapon3, timepoint)*weapon3mult*weapon3[[6]][min(weapon3shots,length(weapon3[[6]]))]

shieldhp <- max(shieldhp, 0)

if(shielddamageattimepoint(weapon3,timepoint) > 0) {shieldblock <- 1}

} else {

#if you did not use shield to block, regenerate flux

#2. armor and hull

if(unlist(weapon3[2])==0.25){weapon3mult = 0.25} else {weapon3mult= 1 / unlist(weapon3[2])}

#2.1. damage armor and hull

hulldamage <- 0

#damageattimepoint <- function(weapon, timepoint, armor, startingarmor, hitx, hity, shots){

for (j in 2:9){

for (i in 2:4){

hulldamage <- hulldamage+max(0,weapon3mult*damageattimepoint(weapon3, timepoint,armormatrix[i,j],startingarmor,i,j,weapon3shots)-armormatrix[i,j])/weapon3mult

armormatrix[i,j] <- max(0,armormatrix[i,j]-weapon3mult*damageattimepoint(weapon3, timepoint,armormatrix[i,j],startingarmor,i,j,weapon3shots)*weapon3[[6]][min(weapon3shots,length(weapon3[[6]]))])

}}

for (j in 2:9){

for (i in c(1,5)){

hulldamage <- hulldamage+max(0,weapon3mult*damageattimepoint(weapon3, timepoint,armormatrix[i,j],startingarmor,i,j,weapon3shots)-armormatrix[i,j])/weapon3mult

armormatrix[i,j] <- max(0,armormatrix[i,j]-weapon3mult*damageattimepoint(weapon3, timepoint,armormatrix[i,j],startingarmor,i,j,weapon3shots)*weapon3[[6]][min(weapon3shots,length(weapon3[[6]]))])

}}

for (j in c(1,10)){

for (i in 2:4){

hulldamage <- hulldamage+max(0,weapon3mult*damageattimepoint(weapon3, timepoint,armormatrix[i,j],startingarmor,i,j,weapon3shots)-armormatrix[i,j])/weapon3mult

armormatrix[i,j] <- max(0,armormatrix[i,j]-weapon3mult*damageattimepoint(weapon3, timepoint,armormatrix[i,j],startingarmor,i,j,weapon3shots)*weapon3[[6]][min(weapon3shots,length(weapon3[[6]]))])

}}

hullhp <- hullhp - hulldamage*weapon3[[6]][min(weapon3shots,length(weapon3[[6]]))]

hullhp <- max(hullhp, 0)

armorhp <- sum(armormatrix)/(46/15)

if(hullhp==0) armorhp <- 0

}

}

weapon3shots <- weapon3shots + weapon3[[3]][(timepoint %% (length(weapon3[[3]])))+1]

if (weapon4[[4]] !=""){

weapon4mult <- unlist(weapon4[2])

if (shieldhp > shielddamageattimepoint(weapon4, timepoint)*weapon4mult){

shieldhp <- shieldhp - shielddamageattimepoint(weapon4, timepoint)*weapon4mult*weapon4[[6]][min(weapon4shots,length(weapon4[[6]]))]

shieldhp <- max(shieldhp, 0)

if(shielddamageattimepoint(weapon4,timepoint) > 0) {shieldblock <- 1}

} else {

#if you did not use shield to block, regenerate flux

#2. armor and hull

if(unlist(weapon4[2])==0.25){weapon4mult = 0.25} else {weapon4mult= 1 / unlist(weapon4[2])}

#2.1. damage armor and hull

hulldamage <- 0

#damageattimepoint <- function(weapon, timepoint, armor, startingarmor, hitx, hity, shots){

for (j in 2:9){

for (i in 2:4){

hulldamage <- hulldamage+max(0,weapon4mult*damageattimepoint(weapon4, timepoint,armormatrix[i,j],startingarmor,i,j,weapon4shots)-armormatrix[i,j])/weapon4mult

armormatrix[i,j] <- max(0,armormatrix[i,j]-weapon4mult*damageattimepoint(weapon4, timepoint,armormatrix[i,j],startingarmor,i,j,weapon4shots)*weapon4[[6]][min(weapon4shots,length(weapon4[[6]]))])

}}

for (j in 2:9){

for (i in c(1,5)){

hulldamage <- hulldamage+max(0,weapon4mult*damageattimepoint(weapon4, timepoint,armormatrix[i,j],startingarmor,i,j,weapon4shots)-armormatrix[i,j])/weapon4mult

armormatrix[i,j] <- max(0,armormatrix[i,j]-weapon4mult*damageattimepoint(weapon4, timepoint,armormatrix[i,j],startingarmor,i,j,weapon4shots)*weapon4[[6]][min(weapon4shots,length(weapon4[[6]]))])

}}

for (j in c(1,10)){

for (i in 2:4){

hulldamage <- hulldamage+max(0,weapon4mult*damageattimepoint(weapon4, timepoint,armormatrix[i,j],startingarmor,i,j,weapon4shots)-armormatrix[i,j])/weapon4mult

armormatrix[i,j] <- max(0,armormatrix[i,j]-weapon4mult*damageattimepoint(weapon4, timepoint,armormatrix[i,j],startingarmor,i,j,weapon4shots)*weapon4[[6]][min(weapon4shots,length(weapon4[[6]]))])

}}

hullhp <- hullhp - hulldamage*weapon4[[6]][min(weapon4shots,length(weapon4[[6]]))]

hullhp <- max(hullhp, 0)

armorhp <- sum(armormatrix)/(46/15)

if(hullhp==0) armorhp <- 0

}

}

weapon4shots <- weapon4shots + weapon4[[3]][(timepoint %% (length(weapon4[[3]])))+1]

if (weapon5[[4]] !=""){

weapon5mult <- unlist(weapon5[2])

if (shieldhp > shielddamageattimepoint(weapon5, timepoint)*weapon5mult){

shieldhp <- shieldhp - shielddamageattimepoint(weapon5, timepoint)*weapon5mult*weapon5[[6]][min(weapon5shots,length(weapon5[[6]]))]

shieldhp <- max(shieldhp, 0)

if(shielddamageattimepoint(weapon5,timepoint) > 0) {shieldblock <- 1}

} else {

#if you did not use shield to block, regenerate flux

#2. armor and hull

if(unlist(weapon5[2])==0.25){weapon5mult = 0.25} else {weapon5mult= 1 / unlist(weapon5[2])}

#2.1. damage armor and hull

hulldamage <- 0

#damageattimepoint <- function(weapon, timepoint, armor, startingarmor, hitx, hity, shots){

for (j in 2:9){

for (i in 2:4){

hulldamage <- hulldamage+max(0,weapon5mult*damageattimepoint(weapon5, timepoint,armormatrix[i,j],startingarmor,i,j,weapon5shots)-armormatrix[i,j])/weapon5mult

armormatrix[i,j] <- max(0,armormatrix[i,j]-weapon5mult*damageattimepoint(weapon5, timepoint,armormatrix[i,j],startingarmor,i,j,weapon5shots)*weapon5[[6]][min(weapon5shots,length(weapon5[[6]]))])

}}

for (j in 2:9){

for (i in c(1,5)){

hulldamage <- hulldamage+max(0,weapon5mult*damageattimepoint(weapon5, timepoint,armormatrix[i,j],startingarmor,i,j,weapon5shots)-armormatrix[i,j])/weapon5mult

armormatrix[i,j] <- max(0,armormatrix[i,j]-weapon5mult*damageattimepoint(weapon5, timepoint,armormatrix[i,j],startingarmor,i,j,weapon5shots)*weapon5[[6]][min(weapon5shots,length(weapon5[[6]]))])

}}

for (j in c(1,10)){

for (i in 2:4){

hulldamage <- hulldamage+max(0,weapon5mult*damageattimepoint(weapon5, timepoint,armormatrix[i,j],startingarmor,i,j,weapon5shots)-armormatrix[i,j])/weapon5mult

armormatrix[i,j] <- max(0,armormatrix[i,j]-weapon5mult*damageattimepoint(weapon5, timepoint,armormatrix[i,j],startingarmor,i,j,weapon5shots)*weapon5[[6]][min(weapon5shots,length(weapon5[[6]]))])

}}

hullhp <- hullhp - hulldamage*weapon5[[6]][min(weapon5shots,length(weapon5[[6]]))]

hullhp <- max(hullhp, 0)

armorhp <- sum(armormatrix)/(46/15)

if(hullhp==0) armorhp <- 0

}

}

weapon5shots <- weapon5shots + weapon5[[3]][(timepoint %% (length(weapon5[[3]])))+1]

if (weapon6[[4]] !=""){

weapon6mult <- unlist(weapon6[2])

if (shieldhp > shielddamageattimepoint(weapon6, timepoint)*weapon6mult){

shieldhp <- shieldhp - shielddamageattimepoint(weapon6, timepoint)*weapon6mult*weapon6[[6]][min(weapon6shots,length(weapon6[[6]]))]

shieldhp <- max(shieldhp, 0)

if(shielddamageattimepoint(weapon6,timepoint) > 0) {shieldblock <- 1}

} else {

#if you did not use shield to block, regenerate flux

#2. armor and hull

if(unlist(weapon6[2])==0.25){weapon6mult = 0.25} else {weapon6mult= 1 / unlist(weapon6[2])}

#2.1. damage armor and hull

hulldamage <- 0

#damageattimepoint <- function(weapon, timepoint, armor, startingarmor, hitx, hity, shots){

for (j in 2:9){

for (i in 2:4){

hulldamage <- hulldamage+max(0,weapon6mult*damageattimepoint(weapon6, timepoint,armormatrix[i,j],startingarmor,i,j,weapon6shots)-armormatrix[i,j])/weapon6mult

armormatrix[i,j] <- max(0,armormatrix[i,j]-weapon6mult*damageattimepoint(weapon6, timepoint,armormatrix[i,j],startingarmor,i,j,weapon6shots)*weapon6[[6]][min(weapon6shots,length(weapon6[[6]]))])

}}

for (j in 2:9){

for (i in c(1,5)){

hulldamage <- hulldamage+max(0,weapon6mult*damageattimepoint(weapon6, timepoint,armormatrix[i,j],startingarmor,i,j,weapon6shots)-armormatrix[i,j])/weapon6mult

armormatrix[i,j] <- max(0,armormatrix[i,j]-weapon6mult*damageattimepoint(weapon6, timepoint,armormatrix[i,j],startingarmor,i,j,weapon6shots)*weapon6[[6]][min(weapon6shots,length(weapon6[[6]]))])

}}

for (j in c(1,10)){

for (i in 2:4){

hulldamage <- hulldamage+max(0,weapon6mult*damageattimepoint(weapon6, timepoint,armormatrix[i,j],startingarmor,i,j,weapon6shots)-armormatrix[i,j])/weapon6mult

armormatrix[i,j] <- max(0,armormatrix[i,j]-weapon6mult*damageattimepoint(weapon6, timepoint,armormatrix[i,j],startingarmor,i,j,weapon6shots)*weapon6[[6]][min(weapon6shots,length(weapon6[[6]]))])

}}

hullhp <- hullhp - hulldamage*weapon6[[6]][min(weapon6shots,length(weapon6[[6]]))]

hullhp <- max(hullhp, 0)

armorhp <- sum(armormatrix)/(46/15)

if(hullhp==0) armorhp <- 0

}

}

weapon6shots <- weapon6shots + weapon6[[3]][(timepoint %% (length(weapon6[[3]])))+1]

hullhp <- hullhp - hulldamage

hullhp <- max(hullhp, 0)

armorhp <- sum(armormatrix)/(46/15)

if(hullhp==0) armorhp <- 0

if (shieldblock==0){shieldhp <- min(shieldmax,shieldhp+shieldregen)}

return(list(timepoint, shieldhp, armorhp, hullhp, shieldregen, shieldmax, startingarmor,armormatrix))

}

totaltime = 60

armorhp <- ship[4]

shieldhp <- ship[3]

hullhp <- ship[1]

shieldregen <- ship[2]

shieldmax <- ship[3]

armorhp <- ship[4]

startingarmor <- ship[4]

armormatrix <- matrix(ship[4]/15,nrow=5,ncol=10)

armormatrix[1,1] <-0

armormatrix[1,10] <-0

armormatrix[5,1] <-0

armormatrix[5,10] <- 0

timeseriesarray <- data.frame(matrix(ncol = 4,nrow=0))

for (t in 1:totaltime){

state <- timeseries(t,shieldhp,armorhp,hullhp,shieldregen,shieldmax,startingarmor,armormatrix)

shieldhp <- state[[2]]

armorhp <- state[[3]]

hullhp <- state[[4]]

flux <- shieldmax - shieldhp

armormatrix <- state[[8]]

if(hullhp == 0){flux <- 0}

timeseriesarray <- rbind(timeseriesarray , c(state[[1]], flux/shieldmax*100, state[[3]]/startingarmor*100, state[[4]]/ship[1]*100))

}

colnames(timeseriesarray) <- c("Time", "Flux", "Armor", "Hull")

weaponstitle <- paste(unlist(weapon1[4]),unlist(weapon2[4]),unlist(weapon3[4]),unlist(weapon4[4]),unlist(weapon5[4]),unlist(weapon6[4]))

ggplot(timeseriesarray, aes(x=Time)) +

geom_line(aes(y = Flux, color = "Flux")) +

geom_line(aes(y = Armor, color="Armor")) +

geom_line(aes(y = Hull, color="Hull")) +

scale_colour_manual("",

breaks = c("Flux", "Armor", "Hull"),

values = c("lightsteelblue", "red", "maroon")) +

ylab("% max") +

xlab("Time (s)") +

labs(title=weaponstitle)