This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Help with Objects/Functions with arguments

3 posts · 3926 views

Hello! I am programming a Pokemon Stadium/Battler in CLisp, and I am having trouble making a damage function that takes values from a move and a pokemon, and doing math with it. In the example below, I am using the move "Blizzard" and the pokemon "Cloyster".
;;----------BLIZZARD--------;;
(defparameter *Blizzard* (make-instance 'Moves))
(setf (slot-value *Blizzard* 'NAME) "Blizzard")
(setf (slot-value *Blizzard* 'BASEDMG) 120)
(setf (slot-value *Blizzard* 'PP) 5)
(setf (slot-value *Blizzard* 'ACCURACY) 90)
(setf (slot-value *Blizzard* 'STATUS) nil)
(setf (slot-value *Blizzard* 'TYPING) "Ice")
(setf (slot-value *Blizzard* 'CATEGORY) "Special")


;;Cloyster
(defparameter *Cloyster* (make-instance 'Pokemon))
(setf (slot-value *Cloyster* 'HP) 156)
(setf (slot-value *Cloyster* 'ATK) 146)
(setf (slot-value *Cloyster* 'DEF) 231)
(setf (slot-value *Cloyster* 'SPECIAL) 136)
(setf (slot-value *Cloyster* 'SPEED) 121)
(setf (slot-value *Cloyster* 'TYPEONE) "Ice")
(setf (slot-value *Cloyster* 'TYPETWO) "Water")
(setf (slot-value *Cloyster* 'LEVEL) 50)
Here are the classes as well:
(defclass Pokemon ()
	(HP
		ATK 
		DEF 
		SPECIAL
		SPEED
		TYPEONE
		TYPETWO
		LEVEL
		ATTACKONE
		ATTACKTWO
		ATTACKTHREE
		ATTACKFOUR))

(defclass Moves ()
	(NAME
		BASEDMG
		PP
		ACCURACY
		STATUS
		TYPING
		CATEGORY))
The Damage Function:
(defun Damage (Moves Pokemon)
	(+ (/ (* (* (+ (/ 100 5) 2) BASEDMG) (/ ATK DEF)) 50) 2))
If you also find sloppy or redundant code, please help me out and make it shorter.
Thanks much! :D

Re: Help with Objects/Functions with arguments

with-slots is useful for accessing slots:
(defun Damage (Moves Pokemon)
  (with-slots (BASEDMG) Moves
    (with-slots (ATK DEF) Pokemon
      (+ (/ (* (* (+ (/ 100 5) 2) BASEDMG) (/ ATK DEF)) 50) 2))))
It's common in Lisp to use accessors and initargs rather than dealing with slots directly, depending on how "intimate" the code is perceived to be with the actual definition of the class.

Re: Help with Objects/Functions with arguments

Thank you for the help!