Help with Objects/Functions with arguments

Discussion of Common Lisp
Post Reply
Ninjacop
Posts: 2
Joined: Wed Dec 27, 2017 8:01 pm

Help with Objects/Functions with arguments

Post by Ninjacop » Wed Dec 27, 2017 8:13 pm

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

David Mullen
Posts: 78
Joined: Mon Dec 01, 2014 12:29 pm
Contact:

Re: Help with Objects/Functions with arguments

Post by David Mullen » Sun Dec 31, 2017 5:21 pm

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.

Ninjacop
Posts: 2
Joined: Wed Dec 27, 2017 8:01 pm

Re: Help with Objects/Functions with arguments

Post by Ninjacop » Fri Jan 12, 2018 6:20 am

Thank you for the help!

Post Reply