Update: Ferrety with artwork

I’ve added artwork to ferrety and cleaned up the ruby a bit since I’ve learned more about it lately:

#!/usr/bin/env ruby

# TODO these need to be made into command line parameters
$Debug = true
$Delay = 2

class Ferrety
	include Comparable
	attr_accessor :name, :mood, :posture, :face, :eyes, :started, :posturesArt, :facesArt, :eyesArt, :variability

	@@Left  = 0
	@@Right = 1

	def initialize(name, mood = :happy, variability = 20)
		@name, @mood, @started, @variability = name, mood, Time.now.to_i, variability

		@posturesArt = Array.new
		@posture     = rand(3)
		# TODO get these out of here - artwork doesn't belong in
		# source code files.

		# Artwork credits:
		# Amy (allynch@worldnet.att.net)
		# Targon
		# Targon
		@posturesArt.push(<<EOF)
                                             _     _
                                            (^)___(^)
                                            {  _  _ }
                                            |  *  * |
    __              ___...___            ===`.= x =,===
  ,# XX=     .---###        ""##---___   ___,/`_^_,
  | |      _/                         """  \#####"|
   # #--'##                         """"   |####"/
    #----##,-,,,_ _              """"      /###""/
                (# /-,#---,,,####,,,,,--##\.##/,#,
                `-_`-_`-_                   `-_`-_`_
                    \\\\\\\\\\\\                     \\\\\\\\\\\\
EOF

		@posturesArt.push(<<EOF)
                                       _,-/\^---,
               ;"~~~~~~~~";          _/;; ~~  {0 `---v
             ;" :::::   :: "\_     _/   ;;     ~ _../
           ;" ;;    ;;;       \___/::    ;;,'~~~~
         ;"  ;;;;.    ;;     ;;;    ::   ,/
        / ;;   ;;;______;;;;  ;;;    ::,/
       /;;V_;; _-~~~~~~~~~~;_  ;;;   ,/
      | :/ / ,/              \_  ~~)/
      |:| / /~~~=              \;; \~~=
      ;:;{::~~~~~~=              \__~~~=
   ;~~:;  ~~~~~~~~~               ~~~~~~
   \/~~
EOF

		@posturesArt.push(<<EOF)
                                 _,-/"---,
          ;"""""""""";         _/;; ""  <@`---v
        ; :::::  ::  "\      _/ ;;  "    _.../
       ;"     ;;  ;;;  \___/::    ;;,'""""
      ;"          ;;;;.  ;;  ;;;  ::/
     ,/ / ;;  ;;;______;;;  ;;; ::,/
     /;;V_;;   ;;;       \       /
     | :/ / ,/            \_ "")/
     | | / /"""=            \;;\""=
     ; ;{::""""""=            \"""=
  ;"""";
  \/"""
EOF

		@eyesArt = [ [ [ 4,48 ], [  4,51 ] ],   # Where the eyes are in the artwork
			     [ [ 2,48 ], [ -1,-1 ] ],
		             [ [ 2,43 ], [ -1,-1 ] ] ]

	end

	def to_s
		"Ferrety " + @name + $Debug ? ", started at #{@started}" : ""
	end

	def name_sort(ferrety)
		self.name <=> ferrety.name
	end

	def started_sort(ferrety)
		self.started <=> ferrety.started
	end

	# To change sort methods, uncomment the desired sort method line
	alias :<=> :started_sort # Sort by started timestamp
	#alias :<=> :name_sort # Sort by name

	def dump_data
		instance_variables.each do |variable|
			variable = variable.sub('@','')
			puts variable + ": " + send(variable).to_s + "\n"
		end
	end

	# Personality
	def think
		sleep($Delay)
		
		# Will the mood change?
		change = rand(100)
		puts "#{@variability}% variability and a d100 roll of #{change}\n" if $Debug 
		if change < @variability
			# mood changes
			# TODO change these to weighted probabilities

			case @mood
			when :content
				dest_moods = [ :happy, :sleepy, :affectionate, :mischeivous, :playful, :antsy ]
				@eyes      = '^^'
			when :happy
				dest_moods = [ :content, :lonely, :playful, :antsy, :scared, :hurt, :excited, :affectionate, :mischeivous, :tired ]
				@eyes      = '^^'
			when :lonely
				dest_moods = [ :happy, :sad, :antsy, :scared, :angry, :hurt, :affectionate, :sleepy ]
				@eyes      = 'nn'
			when :sad
				dest_moods = [ :happy, :lonely, :scared, :angry, :hurt, :affectionate, :sleepy ]
				@eyes      = ';;'
			when :scared
				dest_moods = [ :sad, :lonely, :antsy, :angry, :hurt, :affectionate, :mischievous, :sleepy ]
				@eyes      = 'OO'
			when :antsy
				dest_moods = [ :happy, :playful, :angry, :excited, :affectionate, :mischievous ]
				@eyes      = '>>'
			when :playful
				dest_moods = [ :happy, :content, :antsy, :scared, :angry, :excited, :affectionate, :mischievous, :tired, :sleepy ]
				@eyes      = 'oo'
			when :angry
				dest_moods = [ :sad, :lonely, :antsy, :hurt, :excited, :mischievous, :tired ]
				@eyes      = '**'
			when :hurt
				dest_moods = [ :lonely, :scared, :angry, :sleepy ]
				@eyes      = ';;'
			when :sleepy
				dest_moods = [ :happy, :content, :sad, :lonely, :scared, :affectionate ]
				@eyes      = 'nn'
			when :affectionate
				dest_moods = [ :happy, :content, :playful, :antsy, :excited, :mischievous, :tired, :sleepy ]
				@eyes      = 'nn'
			when :excited
				dest_moods = [ :happy, :playful, :antsy, :scared, :angry, :affectionate, :mischievous, :tired ]
				@eyes      = 'OO'
			when :mischievous
				dest_moods = [ :happy, :sad, :lonely, :playful, :antsy, :scared, :angry, :excited, :affectionate, :tired ]
				@eyes      = '>>'
			when :tired
				dest_moods = [ :happy, :content, :sad, :lonely, :scared, :hurt, :affectionate, :sleepy ]
				@eyes      = '\/'
			end
			@mood = dest_moods[rand(dest_moods.size)]
			puts "New mood is #{@mood}.\n" if $Debug
		end

		# There is a 1:1 correspondence of mood->face.
		# Posture will vary.
		@face          = @mood

		if (roll = rand(100)) < @variability
			@posture = rand(3)

			puts "Changing posture to #{@posture} with variability #{@variability} and roll of #{roll}.\n" if $Debug
		end
		

		# DONE copy over the artwork and sub in the eyes
		@faceArt = @posturesArt[@posture];

		# DONE methodize this procedure - left eye is
		# @eyesArt[@posture][0] and right eye is
		# @eyesArt[@posture][1]
		
		# eye replacement
		sub_eye(@@Left)
		sub_eye(@@Right)

		# TODO implement screen clearing parameter

		# render character to screen
		puts @faceArt

	end

	private
	def sub_eye(eye)
		if (eye == @@Left || eye == @@Right) &&
		    @eyesArt[@posture][eye][0] != -1 &&
		    @eyesArt[@posture][eye][1] != -1
		   	target_line  = @eyesArt[@posture][eye][0]
			current_line = 1
			line_pos     = 0

			until current_line == target_line
				line_pos = @faceArt.index("\n", line_pos)
				current_line += 1
			end

			puts "Found #{eye == @@Left ? "left" : "right"} eye at @eyesArt + 0x#{line_pos.to_s(16)}.\n" if $Debug

			line_pos += @eyesArt[@posture][eye][1]
			@faceArt[line_pos] = @eyes[eye]
		end
	end
end

# Test code
ferrety = Ferrety.new("Test1")
ferrety.dump_data

# do 10 loops of think
(1..10).each do
	ferrety.think
end
Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: