Page 1 of 1

Help with Objects/Functions with arguments

Posted: Wed Dec 27, 2017 8:13 pm
by Ninjacop
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".

Code: Select all


;;----------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:

Code: Select all

(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:

Code: Select all

(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

Posted: Sun Dec 31, 2017 5:21 pm
by David Mullen
with-slots is useful for accessing slots:

Code: Select all

(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

Posted: Fri Jan 12, 2018 6:20 am
by Ninjacop
Thank you for the help!