Soccer Simulator
Model was written in NetLogo 5.0.3
•
Viewed 1439 times
•
Downloaded 94 times
•
Run 0 times
Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)
Comments and Questions
Click to Run Model
;;; DEFINING VARIABLES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; globals [ width ;; width of the pitch height ;; height of the pitch red-goal ;; patches that belongs to the red goal blue-goal ;; patches that belong to the blue goal the-ball ;; the ball score-red ;; amount of goals that the red team has scored since the setup score-blue ;; amount of goals that the blue team has scored since the setup red-team ;; players of the red team blue-team ;; players of the blue team seed ;; current seed number previous-seed ;; previous seed number pass-distance ;; the maximum distance a player can pass move-ball-distance ;; the distance the ball is moved forward when the player is moving trick-ball-distance ;; the distance the ball is moved forward when the player is doing a trick shoot-distance-selfish ;; the maximum distance a selfish player will shoot shoot-distance-teamplayer ;; the maximum distance a teamplayer will shoot speed ;; the speed of a player without the ball speed-receiver ;; the speed of the player receiving a pass speed-with-ball ;; the speed of the player with the ball speed-ball-pass ;; the speed of the ball when it is passed speed-ball-shot ;; the speed of the ball when it is shot speed-keeper ;; the movement speed of the keeper in vertical direction distance-ballpossession ;; the distance of the player to the ball for a player to recognize he is in ball possession locate-ball-distance ;; the distance from which a player locates the ball and recognize he is close to the ball locate-player-distance ;; the distance from which a player locates another player and recognize he is close to that player defender-distance ;; the maximum distance the defender wants to stand from the goal keeper-reach ;; the reach of the keeper ] breed [players player] ;; soccer players breed [keepers keeper] ;; keeper breed [balls ball] ;; the ball breed [referees referee] ;; referee patches-own[ available? ] players-own[ ball? ;; does the player have the ball? teamball? ;; does the team of the player have the ball? nearbygoal? ;; is the goal nearby? oppinfront? ;; is there an opponent in front? oppnearby? ;; is there an opponent nearby? nearbyteam? ;; is there a free teammate nearby? team ;; the team the player belongs to nearbyball? ;; is the ball nearby? inbetween? ;; is there an opponent between the player and the ball? (is the player standing free?) target ;; the target of the player mygoal ;; the goal where the player has to score teamplayer? ;; is the player a teamplayer? defensive? ;; is the player a defensive player? defending? ;; is the player currently in a defensive position? ] referees-own[ team ;; used when neither of the teams have the ball ] keepers-own[ team ] balls-own[ owner ;; the owner of the ball target ;; the target where the ball can be played to (either a player or a patch of the goal) closest-blue ;; the closest player to the ball of the blue team closest-red ;; the closest player to the ball of the red team closestfront-blue ;; the closest player of the blue team in defending position (between the ball and the goal of the defenders) closestfront-red ;; the closest player of the blue team in defending position (between the ball and the goal of the defenders) ] ;;; SETUP THE MODEL ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; to setup ;; setup the field en set values of the variables clear-all set width 25 set height 18 set score-red 0 set score-blue 0 set pass-distance 10 ;; default 10 set shoot-distance-selfish 13 ;; default 10 set shoot-distance-teamplayer 7.5 ;; default 5 set speed 0.5 ;; default 0.5 set speed-with-ball 0.45 ;; default 0.45 set move-ball-distance 0.75 ;; default 0.75 set trick-ball-distance 1.2 ;; default 1 set speed-ball-pass 1 ;; default 1 set speed-ball-shot 2 ;; default 2 set distance-ballpossession 0.8 ;; default 0.8 set locate-ball-distance 10 ;; default 10 set locate-player-distance 1.5 ;; default 1.5 set defender-distance 15 ;; default 15 set speed-keeper 0.1 ;; default 0.1 set keeper-reach 1.5 ;; default 1.5 set speed-receiver 0.1 ;; default 0.1 set-default-shape keepers "keeper" set-default-shape players "player" set-default-shape balls "football" set-default-shape referees "keeper" prepare-patch prepare-teams prepare-referee prepare-ball end to prepare-patch ;; set the colors of the field ask patches [ set pcolor green set available? 0 ] let land define-lines ask land [ set pcolor white set available? 2 ] set blue-goal create-blue-goal ask blue-goal [ set pcolor blue - 2 set available? 1 ] set red-goal create-red-goal ask red-goal [ set pcolor red - 2 set available? 1 ] end to-report define-lines report patches with [(pxcor = width OR pxcor = -1 * width OR pycor = height OR pycor = -1 * height) AND pxcor <= width AND pxcor >= -1 * width AND pycor <= height AND pycor >= -1 * height] end to-report create-red-goal report patches with [((pxcor <= -1 * width AND pxcor >= -1 * width - 2)) and (pycor >= -3 and pycor <= 3)] end to-report create-blue-goal report patches with [((pxcor >= width AND pxcor <= width + 2)) and (pycor >= -3 and pycor <= 3)] end to prepare-teams set previous-seed seed if not fixed-seed? [ set seed-number new-seed ] random-seed seed-number prepare-team1 prepare-team2 set seed seed-number end to prepare-team1 ;; prepare the first (blue) team create-players defense-team-blue [ set color blue set teamplayer? true set defensive? true ] create-players defense-selfish-blue [ set color blue set teamplayer? false set defensive? true ] create-players attack-team-blue [ set color blue set teamplayer? true set defensive? false ] create-players attack-selfish-blue [ set color blue set teamplayer? false set defensive? false ] ask players with [color = blue] [ set team "blue" set mygoal blue-goal set inbetween? true set size 2 setxy (-1 * random width) (random (2 * height - 1) - height + 1) set defending? true ] create-keepers 1 [ setxy (1 - width) 0 set size 2 set heading 90 set color blue set team "blue" ] set blue-team players with [color = blue] end to prepare-team2 ;; prepare the second (red) team create-players defense-team-red [ set color red set teamplayer? true set defensive? true ] create-players defense-selfish-red [ set color red set teamplayer? false set defensive? true ] create-players attack-team-red [ set color red set teamplayer? true set defensive? false ] create-players attack-selfish-red [ set color red set teamplayer? false set defensive? false ] ask players with [color = red] [ set team "red" set mygoal red-goal set inbetween? true set size 2 setxy ( random width) (random (2 * height - 1) - height + 1) set defending? true ] create-keepers 1 [ setxy (width - 1) 0 set size 2 set heading 270 set color red set team "red" ] set red-team players with [color = red] end to prepare-referee create-referees 1 [ setxy 0 (height + 1) set color yellow set heading 180 set size 2 ] create-referees 1 [ setxy 0 (0 - height - 1) set color yellow set heading 0 set size 2 ] end to prepare-ball create-balls 1 [ setxy 0 0 set owner one-of referees set the-ball self set closest-red (min-one-of (players with [color = red]) [distance myself] ) set closest-blue (min-one-of (players with [color = blue]) [distance myself] ) ] end ;;; GO PROCEDURE ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; to go ;; observer procedure information decision move-ball set-motion check-goal check-out end ;;; GATHERING INFORMATION ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; to information ;; gather information for all the players ask players[ ifelse (distance the-ball) < distance-ballpossession [ ;; do i have the ball? set ball? true ;; i have the ball ask one-of balls [set owner myself] set nearbyteam?(( other players in-radius pass-distance) with [team = [team] of myself] ) ;; how many team members are nearby? and free? set oppnearby? count ((players in-radius locate-player-distance) with [team != [team] of myself]) ;; how many opponents are nearby? ifelse any? patches in-radius shoot-distance-selfish with [pcolor = [color] of myself - 2] and not teamplayer? [ ;; is the goal nearby? set nearbygoal? true ] [ ifelse any? patches in-radius shoot-distance-teamplayer with [pcolor = [color] of myself - 2] and teamplayer? [ ;; set nearbygoal? true ] [ set nearbygoal? false ] ] ] [ set ball? false ;; i dont have the ball ifelse ([team] of ([owner] of the-ball) = [team] of self) and (self != ([target] of the-ball)) [ ;; does my team have the ball? set teamball? true ifelse distance (the-ball) < locate-ball-distance [ ;;am i close to the ball? set nearbyball? true set nearbyteam? count (( other players in-radius 2) with [team = [team] of myself]) ;; am i to close to someone else of my team? face the-ball ifelse (any? other players with [ball? = false] in-cone (distance the-ball) 20) or (any? other players with [ball? = false] in-radius locate-player-distance) ;; are there opponents in between me and the ball? [ set inbetween? true ] [ set inbetween? false ] ] [ set nearbyball? false ] ] [ set teamball? false ;; my team does not have the ball ifelse team = "blue" [ ifelse xcor < [xcor] of the-ball [ set defending? true ] [ set defending? false ] ] [ ifelse xcor > [xcor] of the-ball [ set defending? true ] [ set defending? false ] ] ifelse [closest-blue] of the-ball = self or [closest-red] of the-ball = self or [closestfront-blue] of the-ball = self or [closestfront-red] of the-ball = self ;; am i the closest to the ball? [ set nearbyball? true ] [ set nearbyball? false ] ] ] ] end ;;; DECISION TIME ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; to decision ask players [ ifelse ball? [ ;; do you have the ball? ifelse teamplayer? [ ;; are you a teamplayer? ihavetheball-teamplayer] [ ihavetheball-selfish ] ] [ ifelse teamball? [ ;; does your team have the ball? ifelse nearbyball? [nearby-teammate] ;; are you close by? [ ifelse defensive? ;; are you defensive? [ far-teammate-defense] [ far-teammate-attack] ] ] [ ifelse nearbyball? [ ;; are you close by? nearby-opponent ] [ ifelse defensive? ;; are you defensive? [ far-opponent-defense ] [ far-opponent-attack ] ] ] ] ] end to ihavetheball-selfish ;; player procedure - when the player is selfish and has the ball let target-goal mygoal face one-of mygoal ifelse nearbygoal? [ ;; is the goal nearby? print "shoot" shoot ] [ ifelse oppinfront? > 0 or oppnearby? > 0 [ ;; is there an opponent in front? ifelse count nearbyteam? with [not inbetween?] > 2 [ ;; are there multiple teammates nearby and free? print "pass" pass ] [ print "trick" trick ] ] [ print "move" i-move ] ] end to ihavetheball-teamplayer ;; player procedure let target-goal mygoal face one-of mygoal ifelse nearbygoal? [ ;; is the goal nearby? print "shoot" shoot ] [ ifelse oppinfront? > 0 or oppnearby? > 0 [ ;; is there an opponent in front? ifelse count nearbyteam? with [not inbetween?] > 0 [ ;; are there multiple teammates nearby and free? print "pass" pass ] [ print "trick" i-move ] ] [ print "move" i-move ] ] end ;;; ACTIONS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; to nearby-opponent ;; player procedure let my-team team set target one-of balls face target end to far-opponent-defense ;; player procedure ifelse team = "blue" [ ifelse (distance one-of red-goal < defender-distance) [ rt random 360 ] [ face one-of red-goal set heading heading - 90 + random 180 ] ] [ ifelse (distance one-of blue-goal > defender-distance ) [ face one-of blue-goal set heading heading - 90 + random 180 ] [ rt random 360 ] ] end to far-opponent-attack ;; player procedure ifelse count players with [(color = [color] of myself) and (defending?)] > 2 [ rt random 360] [ ifelse team = "blue" [ ifelse (distance one-of red-goal < defender-distance) [ rt random 360] [ face one-of red-goal set heading heading - 90 + random 180 ] ] [ ifelse (distance one-of blue-goal > defender-distance ) [ face one-of blue-goal set heading heading - 90 + random 180] [ rt random 360 ] ] ] end to far-teammate-attack ;; player procedure ifelse team = "blue" [ ifelse xcor < ([xcor] of the-ball) [ face one-of mygoal ] [ rt random 360 ] ] [ ifelse xcor > ([xcor] of the-ball) [ face one-of mygoal ] [ rt random 360 ] ] end to nearby-teammate ;; player procedure let my-team team ifelse nearbyteam? > 0 [ face one-of ((other players in-radius 2.5) with [team = my-team]) set heading heading + 180 ] [ ifelse team = "blue" [ face one-of blue-goal ifelse ([ycor] of the-ball) < ycor [ set heading heading - 30 ] [ set heading heading + 30 ] ] [ face one-of red-goal ifelse ([ycor] of the-ball) < ycor [ set heading heading + 30 ] [ set heading heading - 30 ] ] ] end to far-teammate-defense rt random 360 end to i-move ;; player procedure ask the-ball [ set heading ([heading] of owner) setxy ([xcor] of myself) ([ycor] of myself) fd move-ball-distance set target 0 ] end to trick ask the-ball[ set heading ([heading] of owner) setxy ([xcor] of myself) ([ycor] of myself) fd trick-ball-distance set target 0 set owner one-of referees ] end to shoot ;; player procedure let target-goal one-of mygoal ask the-ball [ set target target-goal face target ] end to pass ;; player procedure let my-team team let new-target one-of ((other players in-radius pass-distance) with [team = my-team and not inbetween?]) if new-target != nobody [ ask the-ball [ set target new-target face target ask target [ set teamball? false ] print target ] ] end ;;; MOVE THE PLAYERS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; to set-motion ;; observer procedure let the players walk ask players [ ifelse ball? [ ifelse ([available?] of patch-ahead 2) < 1 [ fd speed-with-ball ] [ set heading heading + 180 fd speed-with-ball ] ] [ ifelse self != [target] of the-ball [ ifelse ([available?] of patch-ahead 1) < 1 [ fd speed ] [ set heading heading + 180 fd speed ] ] [ fd speed-receiver ] ] ] ask keepers[ ifelse [ycor] of the-ball > ycor [ if [pycor] of (one-of blue-goal) > ycor[ setxy xcor (ycor + speed-keeper) ] ] [ if [pycor] of (one-of blue-goal) < ycor[ setxy xcor (ycor - speed-keeper) ] ] ] end ;;; MOVE THE BALL ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; to move-ball ;; movement of the ball ask the-ball[ set closestfront-red (min-one-of (players with [color = red and xcor > [xcor] of the-ball]) [distance myself] ) set closestfront-blue (min-one-of (players with [color = blue and xcor < [xcor] of the-ball]) [distance myself] ) set closest-red (min-one-of (players with [color = red]) [distance myself]) set closest-blue (min-one-of (players with [color = blue]) [distance myself]) if (([available?] of patch-ahead 1.2) < 2) and (([available?] of patch-ahead 0.6) < 2) [ if target != 0 [ if (distance target > shoot-distance-selfish + 1) and (distance target > shoot-distance-teamplayer + 1) and (distance target > pass-distance + 1) [ set target 0 set owner one-of referees ] ifelse is-patch? target [ if distance one-of keepers < keeper-reach [set heading heading + 90 + random 180] fd speed-ball-shot ] [ fd speed-ball-pass ] ] ] ] end ;;; CHECK GOAL OR OUT ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; to check-goal ;; check whether a goal is scored if [pcolor] of (patch ([xcor] of the-ball) ([ycor] of the-ball) ) = red - 2 [ set score-red score-red + 1 goal ] if [pcolor] of (patch ([xcor] of the-ball) ([ycor] of the-ball) ) = blue - 2 [ set score-blue score-blue + 1 goal ] end to goal clear-turtles prepare-teams prepare-referee prepare-ball end to check-out ;; check whether the ball is outside the field if [pcolor] of (patch ([xcor] of the-ball) ([ycor] of the-ball) ) = green [ if ([xcor] of the-ball > width + 1) or ([xcor] of the-ball < (-1 * (width + 1)))[ out ] ] if [pcolor] of (patch ([xcor] of the-ball) ([ycor] of the-ball) ) = white [ out ] end to out ask the-ball [ set xcor 0 set ycor 0 set target 0 ] end
There are 2 versions of this model.
Attached files
File | Type | Description | Last updated | |
---|---|---|---|---|
Soccer Simulator.png | preview | Preview for 'Soccer Simulator' | over 9 years ago, by Sander van Egmond | Download |
This model does not have any ancestors.
This model does not have any descendants.
Hopi Hopi
Explanation of the model
Can you explain to me the model? How agents interact each other ?
Posted almost 6 years ago