Skip to content
Snippets Groups Projects
Commit cd0682f2 authored by julienpillis's avatar julienpillis
Browse files

amélioration flux entrée-sortie

parent da9764b6
No related branches found
No related tags found
1 merge request!14Merge v2
......@@ -117,7 +117,17 @@ func (agt *Agent) IsMovementSafe() (bool, int) {
if !(borneInfCol < 0 || borneInfRow < 0 || borneSupRow > 50 || borneSupCol > 50) {
for i := borneInfRow; i < borneSupRow; i++ {
for j := borneInfCol; j < borneSupCol; j++ {
if !(j >= infCol && j < supCol && i >= infRow && i < supRow) && (agt.env.station[i][j] != "B" && agt.env.station[i][j] != "_" && agt.env.station[i][j] != "W" && agt.env.station[i][j] != "S" && agt.env.station[i][j] != "O") {
if agt.env.station[i][j] == "O" {
// Vérification si porte de métro
metro := findMetro(ag.env, &alg.Coord{i, j})
if metro != nil && !metro.way.gatesClosed && alg.EqualCoord(&ag.destination, &alg.Coord{i, j}) {
// On s'assure que les portes ne sont pas fermées et que c'est la destination
return true, or
} else {
safe = false
}
}
if !(j >= infCol && j < supCol && i >= infRow && i < supRow) && (agt.env.station[i][j] != "B" && agt.env.station[i][j] != "_" && agt.env.station[i][j] != "W" && agt.env.station[i][j] != "S") {
// Si on n'est pas sur une case atteignable, en dehors de la zone qu'occupe l'agent avant déplacement, on est bloqué
safe = false
}
......@@ -216,8 +226,11 @@ func (ag *Agent) MoveAgent() {
if len(ag.path) == 0 || ag.isGoingToExitPath() {
start, end := ag.generatePathExtremities()
// Recherche d'un chemin si inexistant
ag.path = alg.FindPath(ag.env.station, start, end, *alg.NewNode(-1, -1, 0, 0, 0, 0), false, 2*time.Second)
if len(ag.path) > 0 {
ag.path = alg.FindPath(ag.env.station, start, end, ag.path[0], false, 2*time.Second)
} else {
ag.path = alg.FindPath(ag.env.station, start, end, *alg.NewNode(-1, -1, 0, 0, 0, 0), false, 2*time.Second)
}
}
// ================== Etude de faisabilité =======================
......@@ -333,7 +346,7 @@ func (ag *Agent) isGoingToExitPath() bool {
// Si la destination est une porte de métro, on va essayer de libérer le chemin des agents sortants
exit_path := metro.way.pathsToExit[gate_index]
for _, cell := range exit_path {
if alg.EqualCoord(&alg.Coord{cell.Row(), cell.Col()}, &ag.position) {
if alg.EqualCoord(&alg.Coord{cell.Row(), cell.Col()}, &alg.Coord{ag.path[0].Row(), ag.path[0].Col()}) {
return true
}
}
......@@ -343,3 +356,14 @@ func (ag *Agent) isGoingToExitPath() bool {
}
return false
}
func findMetro(env *Environment, gateToFind *alg.Coord) *Metro {
for _, metro := range env.metros {
for _, gate := range metro.way.gates {
if alg.EqualCoord(&gate, gateToFind) {
return &metro
}
}
}
return nil
}
......@@ -49,9 +49,9 @@ func (metro *Metro) Start() {
}
if refTime.Add(metro.frequency).Before(time.Now()) {
metro.dropUsers()
metro.way.openGates()
metro.openGates()
metro.pickUpUsers()
metro.way.closeGates()
metro.closeGates()
metro.removeMetro()
metro.freeSpace = rand.Intn(10)
refTime = time.Now()
......@@ -81,14 +81,11 @@ func (metro *Metro) pickUpGate(gate *alg.Coord, endTime time.Time) {
if !time.Now().Before(endTime) {
return
} else {
gate_cell := metro.way.env.station[gate[0]][gate[1]]
if len(gate_cell) > 1 {
agent := metro.findAgent(AgentID(gate_cell))
//fmt.Println("gate cell", gate[0],gate[1], "agent", agent)
if agent != nil && agent.width*agent.height <= metro.freeSpace && alg.EqualCoord(&agent.destination, gate) {
//fmt.Println("agent entering metro : ", agent.id)
fmt.Println("agent entering metro : ", agent.id, "at gate ", gate)
metro.way.env.agentsChan[agent.id] <- *req.NewRequest(metro.comChannel, EnterMetro)
<-metro.comChannel
metro.freeSpace = metro.freeSpace - agent.width*agent.height
......@@ -232,3 +229,24 @@ func (metro *Metro) removeMetro() {
}
}
func (metro *Metro) openGates() {
// Début d'autorisation d'entrer dans le métro
for _, gate := range metro.way.gates {
metro.way.env.station[gate[0]][gate[1]] = "O"
}
metro.way.gatesClosed = false
}
func (metro *Metro) closeGates() {
// Fin d'autorisation d'entrer dans le métro
metro.way.gatesClosed = true
for _, gate := range metro.way.gates {
if len(metro.way.env.station[gate[0]][gate[1]]) > 1 {
// On autorise les agents déjà sur la case à rentrer dans le métro
metro.pickUpGate(&gate, time.Now().Add(time.Duration(1*time.Second)))
}
metro.way.env.station[gate[0]][gate[1]] = "G"
}
}
......@@ -13,11 +13,12 @@ type Way struct {
id WayID
upLeftCoord alg.Coord // inclus
downRightCoord alg.Coord // inclus
goToLeft bool // si vrai, le métro se déplace de droite à gauche, si faux de gauche à droite
goToLeft bool // si vrai, le métro se déplace de droite à gauche, si faux de gauche à droite
horizontal bool
gates []alg.Coord //listes des portes associée à la voie
nearestExit []alg.Coord // Chemin vers la sortie la plus proche pour chaque porte (index vers pathsToExit)
pathsToExit [][]alg.Node
gatesClosed bool
env *Environment
}
......@@ -57,16 +58,3 @@ func NewWay(wayId WayID, upLeftCoord, downRightCoord alg.Coord, goToLeft bool, g
env: env}
}
func (way *Way) openGates() {
// Début d'autorisation d'entrer dans le métro
for _, gate := range way.gates {
way.env.station[gate[0]][gate[1]] = "O"
}
}
func (way *Way) closeGates() {
// Fin d'autorisation d'entrer dans le métro
for _, gate := range way.gates {
way.env.station[gate[0]][gate[1]] = "G"
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment